@@ -23,10 +23,7 @@ import {
2323 ANNOTATION_STORAGE_KEYS ,
2424 CHART_ANNOTATION_STYLES ,
2525} from "@/lib/annotation-constants" ;
26- import {
27- getChartDisplayDate ,
28- isSingleDayAnnotation ,
29- } from "@/lib/annotation-utils" ;
26+ import { isSingleDayAnnotation } from "@/lib/annotation-utils" ;
3027import { cn } from "@/lib/utils" ;
3128import {
3229 metricVisibilityAtom ,
@@ -466,7 +463,7 @@ export function MetricsChart({
466463 < div className = "flex items-center justify-center p-8" >
467464 < div className = "flex flex-col items-center py-12 text-center" >
468465 < div className = "relative flex size-12 items-center justify-center rounded bg-accent" >
469- < ChartLineIcon className = "size-6 text-muted- foreground" />
466+ < ChartLineIcon className = "size-6 text-foreground" />
470467 </ div >
471468 < p className = "mt-6 font-medium text-foreground text-lg" >
472469 No data available
@@ -489,14 +486,14 @@ export function MetricsChart({
489486 { annotations . length > 0 && (
490487 < div className = "flex items-center justify-between border-b bg-accent px-4 py-2" >
491488 < div className = "flex items-center gap-3" >
492- < span className = "text-muted- foreground text-sm" >
489+ < span className = "text-foreground text-sm" >
493490 { annotations . length } annotation
494491 { annotations . length !== 1 ? "s" : "" } on this chart
495492 </ span >
496493 { onToggleAnnotations !== undefined && (
497494 < div className = "flex items-center gap-2" >
498495 < Label
499- className = "text-muted- foreground text-xs"
496+ className = "text-foreground text-xs"
500497 htmlFor = "show-annotations"
501498 >
502499 Show annotations
@@ -639,20 +636,69 @@ export function MetricsChart({
639636
640637 { showAnnotations === true &&
641638 annotations . map ( ( annotation , index ) => {
642- const startDate = getChartDisplayDate (
643- annotation . xValue ,
644- granularity
645- ) ;
639+ if ( ! chartData . length ) {
640+ return null ;
641+ }
642+
643+ const chartFirst = chartData [ 0 ] ;
644+ const chartLast = chartData . at ( - 1 ) ;
645+ if ( ! ( chartFirst && chartLast ) ) {
646+ return null ;
647+ }
648+
649+ const annotationStart = dayjs ( annotation . xValue ) . toDate ( ) ;
650+ const annotationEnd = annotation . xEndValue
651+ ? dayjs ( annotation . xEndValue ) . toDate ( )
652+ : annotationStart ;
653+
654+ const chartStart = dayjs (
655+ ( chartFirst as ChartDataRow & { rawDate ?: string } )
656+ . rawDate || chartFirst . date
657+ ) . toDate ( ) ;
658+ const chartEnd = dayjs (
659+ ( chartLast as ChartDataRow & { rawDate ?: string } )
660+ . rawDate || chartLast . date
661+ ) . toDate ( ) ;
662+
663+ if (
664+ annotationEnd < chartStart ||
665+ annotationStart > chartEnd
666+ ) {
667+ return null ;
668+ }
669+
670+ let clampedStart = chartFirst . date ;
671+ for ( const point of chartData ) {
672+ const pointDate = dayjs (
673+ ( point as ChartDataRow & { rawDate ?: string } ) . rawDate ||
674+ point . date
675+ ) . toDate ( ) ;
676+ if ( pointDate >= annotationStart ) {
677+ clampedStart = point . date ;
678+ break ;
679+ }
680+ }
681+
682+ let clampedEnd = chartLast . date ;
683+ for ( let i = chartData . length - 1 ; i >= 0 ; i -- ) {
684+ const point = chartData [ i ] ;
685+ if ( ! point ) {
686+ continue ;
687+ }
688+ const pointDate = dayjs (
689+ ( point as ChartDataRow & { rawDate ?: string } ) . rawDate ||
690+ point . date
691+ ) . toDate ( ) ;
692+ if ( pointDate <= annotationEnd ) {
693+ clampedEnd = point . date ;
694+ break ;
695+ }
696+ }
646697
647698 if (
648699 annotation . annotationType === "range" &&
649700 annotation . xEndValue
650701 ) {
651- const endDate = getChartDisplayDate (
652- annotation . xEndValue ,
653- granularity
654- ) ;
655-
656702 const isSingleDay = isSingleDayAnnotation ( annotation ) ;
657703
658704 if ( isSingleDay ) {
@@ -672,7 +718,7 @@ export function MetricsChart({
672718 CHART_ANNOTATION_STYLES . strokeDasharray
673719 }
674720 strokeWidth = { CHART_ANNOTATION_STYLES . strokeWidth }
675- x = { startDate }
721+ x = { clampedStart }
676722 />
677723 ) ;
678724 }
@@ -694,13 +740,12 @@ export function MetricsChart({
694740 strokeDasharray = "3 3"
695741 strokeOpacity = { CHART_ANNOTATION_STYLES . strokeOpacity }
696742 strokeWidth = { 2 }
697- x1 = { startDate }
698- x2 = { endDate }
743+ x1 = { clampedStart }
744+ x2 = { clampedEnd }
699745 />
700746 ) ;
701747 }
702748
703- // Point or line annotations
704749 return (
705750 < ReferenceLine
706751 key = { annotation . id }
@@ -715,7 +760,7 @@ export function MetricsChart({
715760 stroke = { annotation . color }
716761 strokeDasharray = { CHART_ANNOTATION_STYLES . strokeDasharray }
717762 strokeWidth = { CHART_ANNOTATION_STYLES . strokeWidth }
718- x = { startDate }
763+ x = { clampedStart }
719764 />
720765 ) ;
721766 } ) }
0 commit comments