forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotatedChart.tsx
More file actions
325 lines (301 loc) · 9.05 KB
/
Copy pathAnnotatedChart.tsx
File metadata and controls
325 lines (301 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
'use client';
import { useCallback, useEffect, useRef, useState } from 'react';
import {
deleteAnnotation,
getAnnotations,
saveAnnotation,
updateAnnotation,
type Annotation,
} from '@/lib/annotations';
interface AnnotationMarkerProps {
annotation: Annotation;
/** 0–1 position along the chart width */
xFraction: number;
onEdit: (annotation: Annotation) => void;
onDelete: (id: string) => void;
}
function AnnotationMarker({ annotation, xFraction, onEdit, onDelete }: AnnotationMarkerProps) {
const [hovered, setHovered] = useState(false);
return (
<div
style={{
position: 'absolute',
left: `${xFraction * 100}%`,
top: 0,
bottom: 0,
pointerEvents: 'none',
zIndex: 10,
}}
>
{/* Vertical line */}
<div
style={{
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
width: 2,
background: '#6366f1',
opacity: 0.7,
}}
/>
{/* Dot + tooltip trigger */}
<div
style={{
position: 'absolute',
top: 8,
left: -7,
width: 14,
height: 14,
borderRadius: '50%',
background: '#6366f1',
border: '2px solid #ffffff',
boxShadow: '0 2px 6px rgba(99,102,241,0.4)',
cursor: 'pointer',
pointerEvents: 'all',
zIndex: 11,
}}
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
>
{hovered && (
<div
style={{
position: 'absolute',
top: 20,
left: '50%',
transform: 'translateX(-50%)',
background: '#1e293b',
color: '#f8fafc',
borderRadius: 10,
padding: '8px 12px',
fontSize: 12,
whiteSpace: 'nowrap',
boxShadow: '0 8px 20px rgba(0,0,0,0.2)',
zIndex: 20,
minWidth: 160,
}}
>
<div style={{ fontWeight: 700, marginBottom: 4 }}>{annotation.date}</div>
<div style={{ color: '#cbd5e1', marginBottom: 8 }}>{annotation.text}</div>
<div style={{ display: 'flex', gap: 8 }}>
<button
type="button"
onClick={() => onEdit(annotation)}
style={{
background: '#6366f1',
border: 'none',
borderRadius: 6,
color: '#fff',
padding: '3px 8px',
fontSize: 11,
cursor: 'pointer',
fontWeight: 700,
}}
>
Edit
</button>
<button
type="button"
onClick={() => onDelete(annotation.id)}
style={{
background: '#ef4444',
border: 'none',
borderRadius: 6,
color: '#fff',
padding: '3px 8px',
fontSize: 11,
cursor: 'pointer',
fontWeight: 700,
}}
>
Delete
</button>
</div>
</div>
)}
</div>
</div>
);
}
interface AnnotationModalProps {
initial?: Annotation;
defaultDate?: string;
onSave: (text: string, date: string) => void;
onClose: () => void;
}
function AnnotationModal({ initial, defaultDate = '', onSave, onClose }: AnnotationModalProps) {
const [text, setText] = useState(initial?.text ?? '');
const [date, setDate] = useState(initial?.date ?? defaultDate);
return (
<div
style={{
position: 'fixed',
inset: 0,
background: 'rgba(15,23,42,0.5)',
display: 'grid',
placeItems: 'center',
zIndex: 70,
padding: 16,
}}
onClick={onClose}
>
<div
style={{
background: '#ffffff',
borderRadius: 20,
padding: '24px',
width: 'min(400px, 100%)',
boxShadow: '0 24px 60px rgba(15,23,42,0.2)',
}}
onClick={(e) => e.stopPropagation()}
>
<h2 style={{ margin: '0 0 18px', fontSize: 20 }}>
{initial ? 'Edit annotation' : 'Add annotation'}
</h2>
<label style={{ display: 'grid', gap: 6, marginBottom: 14 }}>
<span style={{ fontSize: 13, fontWeight: 700 }}>Date</span>
<input
type="date"
value={date}
onChange={(e) => setDate(e.target.value)}
style={{ borderRadius: 10, border: '1px solid #cbd5e1', padding: '10px 12px', fontSize: 14 }}
/>
</label>
<label style={{ display: 'grid', gap: 6, marginBottom: 20 }}>
<span style={{ fontSize: 13, fontWeight: 700 }}>Note</span>
<textarea
value={text}
onChange={(e) => setText(e.target.value)}
rows={3}
placeholder="Describe this event…"
style={{
borderRadius: 10,
border: '1px solid #cbd5e1',
padding: '10px 12px',
fontSize: 14,
resize: 'vertical',
fontFamily: 'inherit',
}}
/>
</label>
<div style={{ display: 'flex', gap: 10, justifyContent: 'flex-end' }}>
<button
type="button"
onClick={onClose}
style={{
border: '1px solid #e2e8f0',
borderRadius: 10,
background: '#f8fafc',
padding: '10px 16px',
cursor: 'pointer',
fontWeight: 700,
fontSize: 13,
}}
>
Cancel
</button>
<button
type="button"
onClick={() => { if (text.trim() && date) onSave(text.trim(), date); }}
style={{
border: 'none',
borderRadius: 10,
background: '#6366f1',
color: '#ffffff',
padding: '10px 16px',
cursor: 'pointer',
fontWeight: 700,
fontSize: 13,
}}
>
Save
</button>
</div>
</div>
</div>
);
}
interface AnnotatedChartProps {
chartId: string;
/** The x-axis labels (used to position markers) */
labels: string[];
children: React.ReactNode;
}
/**
* Wraps a chart with annotation support.
* Click anywhere on the chart area to add an annotation.
*/
export default function AnnotatedChart({ chartId, labels, children }: AnnotatedChartProps) {
const containerRef = useRef<HTMLDivElement>(null);
const [annotations, setAnnotations] = useState<Annotation[]>([]);
const [modal, setModal] = useState<{ editing?: Annotation; clickX?: number } | null>(null);
const refresh = useCallback(() => setAnnotations(getAnnotations(chartId)), [chartId]);
useEffect(() => {
refresh();
}, [refresh]);
const handleChartClick = (e: React.MouseEvent<HTMLDivElement>) => {
if (!containerRef.current) return;
const rect = containerRef.current.getBoundingClientRect();
const xFraction = (e.clientX - rect.left) / rect.width;
setModal({ clickX: xFraction });
};
const handleSave = (text: string, date: string) => {
if (modal?.editing) {
updateAnnotation(modal.editing.id, { text, date });
} else {
saveAnnotation({ chartId, date, text });
}
refresh();
setModal(null);
};
const handleDelete = (id: string) => {
deleteAnnotation(id);
refresh();
};
// Map annotation date to x-fraction based on label index
const getXFraction = (annotation: Annotation): number => {
const idx = labels.findIndex((l) => l.includes(annotation.date) || annotation.date.includes(l));
if (idx === -1) return 0.5;
return idx / Math.max(labels.length - 1, 1);
};
// Derive a default date from click position
const getDefaultDate = (xFraction?: number): string => {
if (xFraction === undefined || labels.length === 0) return '';
const idx = Math.round(xFraction * (labels.length - 1));
return labels[idx] ?? '';
};
return (
<>
<div style={{ position: 'relative' }}>
{/* Clickable overlay */}
<div
ref={containerRef}
style={{ position: 'absolute', inset: 0, zIndex: 5, cursor: 'crosshair' }}
onClick={handleChartClick}
title="Click to add annotation"
aria-label="Click to add annotation"
/>
{/* Annotation markers */}
{annotations.map((annotation) => (
<AnnotationMarker
key={annotation.id}
annotation={annotation}
xFraction={getXFraction(annotation)}
onEdit={(a) => setModal({ editing: a })}
onDelete={handleDelete}
/>
))}
{children}
</div>
{modal && (
<AnnotationModal
initial={modal.editing}
defaultDate={getDefaultDate(modal.clickX)}
onSave={handleSave}
onClose={() => setModal(null)}
/>
)}
</>
);
}