Skip to content

Commit c6c8704

Browse files
committed
fix: partial annotation view
1 parent 277d7af commit c6c8704

File tree

3 files changed

+72
-29
lines changed

3 files changed

+72
-29
lines changed

apps/dashboard/components/charts/annotations-panel.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function AnnotationsPanel({
5454
<>
5555
<Sheet onOpenChange={setIsOpen} open={isOpen}>
5656
<SheetTrigger asChild>
57-
<Button className="gap-2" size="sm" variant="outline">
57+
<Button className="gap-2" size="sm" variant="secondary">
5858
<NoteIcon className="size-4" weight="duotone" />
5959
Annotations ({annotations.length})
6060
</Button>
@@ -230,7 +230,7 @@ export function AnnotationsPanel({
230230
onConfirm={handleDelete}
231231
title="Delete Annotation"
232232
>
233-
{annotationToDelete && (
233+
{annotationToDelete ? (
234234
<div className="rounded border bg-card p-3">
235235
<div className="flex items-start gap-3">
236236
<div
@@ -253,7 +253,7 @@ export function AnnotationsPanel({
253253
</div>
254254
</div>
255255
</div>
256-
)}
256+
) : null}
257257
</DeleteDialog>
258258
</>
259259
);

apps/dashboard/components/charts/metrics-chart-with-annotations.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,9 @@ export function MetricsChartWithAnnotations({
114114
? new Date(annotation.xEndValue)
115115
: annotationStart;
116116

117-
return (
118-
(annotationStart >= startDate && annotationStart <= endDate) ||
119-
(annotationEnd >= startDate && annotationEnd <= endDate) ||
120-
(annotationStart <= startDate && annotationEnd >= endDate)
121-
);
117+
// Check if annotation range overlaps with visible chart range
118+
// Two ranges overlap if: annotationStart <= endDate && annotationEnd >= startDate
119+
return annotationStart <= endDate && annotationEnd >= startDate;
122120
});
123121
}, [allAnnotations, dateRange]);
124122

apps/dashboard/components/charts/metrics-chart.tsx

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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";
3027
import { cn } from "@/lib/utils";
3128
import {
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

Comments
 (0)