-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.umd.js
More file actions
2915 lines (2693 loc) · 86.2 KB
/
Copy pathindex.umd.js
File metadata and controls
2915 lines (2693 loc) · 86.2 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ============================================================
// TemporalDataExplorer — JS Panel for @mgustineli/temporal-detection
//
// SVG line chart of per-frame temporal data with bidirectional
// video sync:
// - Video → Chart: vertical blue line tracks current frame
// - Chart → Video: click/drag on chart seeks the video
//
// Supports multiple charts with add/remove/reorder and
// per-dataset localStorage persistence.
//
// Hand-written UMD (no build step). Uses FiftyOne globals:
// __fos__ (state), __foo__ (operators), __fop__ (plugins),
// __mui__ (MUI), React, recoil
// ============================================================
(function () {
"use strict";
// --- Globals ---
var React = window.React;
var h = React.createElement;
var useState = React.useState;
var useEffect = React.useEffect;
var useRef = React.useRef;
var useCallback = React.useCallback;
var memo = React.memo;
var useRecoilValue = window.recoil.useRecoilValue;
var useSetRecoilState = window.recoil.useSetRecoilState;
var fos = window.__fos__;
var foo = window.__foo__;
var fop = window.__fop__;
var mui = window.__mui__;
var Box = mui.Box;
var Typography = mui.Typography;
var CircularProgress = mui.CircularProgress;
var LOG_PREFIX = "[TemporalDataExplorer]";
// Stable selector reference for "Use frame number" app config
// Created once at module level so Recoil can properly subscribe to changes
var useFrameNumberSelector = (fos && fos.appConfigOption)
? fos.appConfigOption({ modal: true, key: "useFrameNumber" })
: null;
// --- Constants ---
var CHART_HEIGHT = 350;
var MARGIN = { top: 35, right: 30, bottom: 50, left: 65 };
// --- Label Timeline Constants ---
var LT_MARGIN = { top: 35, right: 30, bottom: 30, left: 140 };
var LT_ROW_HEIGHT = 14;
var LT_ROW_GAP = 1;
var LT_DEFAULT_MAX_LABELS = 15;
// --- Event chart (boolean) constants ---
var EVT_MARGIN = { top: 20, right: 30, bottom: 30, left: 65 };
var EVT_TRACK_HEIGHT = 28;
var EVT_TOTAL_HEIGHT = EVT_MARGIN.top + EVT_TRACK_HEIGHT + EVT_MARGIN.bottom;
var EVT_COLOR = "#FF6D04";
// --- Caption ribbon (string) constants ---
var CAP_MARGIN = { top: 20, right: 30, bottom: 30, left: 65 };
var CAP_RIBBON_HEIGHT = 22;
var CAP_TEXT_STRIP_HEIGHT = 36;
var CAP_TOTAL_HEIGHT =
CAP_MARGIN.top +
CAP_RIBBON_HEIGHT +
CAP_TEXT_STRIP_HEIGHT +
CAP_MARGIN.bottom;
var LABEL_COLORS = [
"#FF6D04", "#4FC3F7", "#81C784", "#FFB74D", "#BA68C8",
"#4DD0E1", "#AED581", "#FF8A65", "#9575CD", "#FFD54F",
"#F06292", "#26A69A", "#DCE775", "#7986CB", "#A1887F",
"#90A4AE", "#CE93D8", "#80CBC4", "#FFAB91", "#80DEEA",
];
function hashString(str) {
var hash = 5381;
for (var i = 0; i < str.length; i++) {
hash = ((hash << 5) + hash) + str.charCodeAt(i);
hash = hash & hash;
}
return Math.abs(hash);
}
function labelColor(label) {
return LABEL_COLORS[hashString(label) % LABEL_COLORS.length];
}
function chartKey(field, type) {
return field + ":" + (type || "count");
}
// Format frame number as timestamp (M:SS.s or H:MM:SS.s)
function formatTimestamp(frame, fps) {
if (!fps || fps <= 0) return String(frame);
var seconds = (frame - 1) / fps;
var h = Math.floor(seconds / 3600);
var m = Math.floor((seconds % 3600) / 60);
var s = seconds % 60;
if (h > 0) {
return h + ":" + (m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s.toFixed(1);
}
return m + ":" + (s < 10 ? "0" : "") + s.toFixed(1);
}
// Format for tick labels (less precision)
function formatTimestampTick(frame, fps) {
if (!fps || fps <= 0) return String(frame);
var seconds = (frame - 1) / fps;
var h = Math.floor(seconds / 3600);
var m = Math.floor((seconds % 3600) / 60);
var s = Math.floor(seconds % 60);
if (h > 0) {
return h + ":" + (m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s;
}
return m + ":" + (s < 10 ? "0" : "") + s;
}
// ==========================================================
// Hook: useVideoState
// Reads video playback state from the modal looker.
// Handles both regular video and ImaVid modes.
// Pattern from cariad-imavid-state plugin.
// ==========================================================
function useVideoState() {
var isImaVid = useRecoilValue(fos.shouldRenderImaVidLooker(true));
var imaVidFrameNumber = useRecoilValue(
fos.imaVidLookerState("currentFrameNumber"),
);
var imaVidPlaying = useRecoilValue(fos.imaVidLookerState("playing"));
var modalLooker = useRecoilValue(fos.modalLooker);
// Dynamic group state — controls which sample is displayed
var isDynamicGroup = useRecoilValue(fos.isDynamicGroup);
var dynamicGroupIndex = useRecoilValue(fos.dynamicGroupCurrentElementIndex);
var setDynamicGroupIndex = useSetRecoilState(
fos.dynamicGroupCurrentElementIndex,
);
// Detect carousel vs pagination mode in dynamic groups
var dynamicGroupsViewMode = useRecoilValue(
fos.dynamicGroupsViewMode(true),
);
var isCarousel = isDynamicGroup && dynamicGroupsViewMode === "carousel";
// ImaVid frame setter — directly controls the ImaVid player's current frame
var setImaVidFrameNumber = useSetRecoilState(
fos.imaVidLookerState("currentFrameNumber"),
);
var stateRef = useRef({ playing: false, frameNumber: 1 });
var _s = useState(0);
var forceUpdate = _s[1];
useEffect(
function () {
// Path 1: Dynamic group in VIDEO mode — uses ImaVid looker
if (isDynamicGroup && isImaVid) {
stateRef.current = {
playing: imaVidPlaying,
frameNumber: imaVidFrameNumber,
};
forceUpdate(function (n) {
return n + 1;
});
return;
}
// Path 2: Dynamic group in PAGINATION mode (carousel handled by panel)
if (isDynamicGroup && !isCarousel) {
var dgFrame = (dynamicGroupIndex || 0) + 1;
stateRef.current = {
playing: false,
frameNumber: dgFrame,
};
forceUpdate(function (n) {
return n + 1;
});
return;
}
// Path 3: ImaVid mode (non-dynamic-group, if it ever applies)
if (isImaVid) {
stateRef.current = {
playing: imaVidPlaying,
frameNumber: imaVidFrameNumber,
};
forceUpdate(function (n) {
return n + 1;
});
return;
}
// Path 4: Regular video — subscribe to looker state
if (modalLooker && typeof modalLooker.subscribeToState === "function") {
stateRef.current = {
playing: modalLooker.state.playing,
frameNumber: modalLooker.state.frameNumber,
};
forceUpdate(function (n) {
return n + 1;
});
var unsub1 = modalLooker.subscribeToState("playing", function (v) {
stateRef.current = {
playing: v,
frameNumber: stateRef.current.frameNumber,
};
forceUpdate(function (n) {
return n + 1;
});
});
var unsub2 = modalLooker.subscribeToState(
"frameNumber",
function (v) {
stateRef.current = {
playing: stateRef.current.playing,
frameNumber: v,
};
forceUpdate(function (n) {
return n + 1;
});
},
);
return function () {
unsub1();
unsub2();
};
}
},
[isDynamicGroup, isCarousel, dynamicGroupIndex, isImaVid, imaVidFrameNumber, imaVidPlaying, modalLooker],
);
return {
playing: stateRef.current.playing,
frameNumber: stateRef.current.frameNumber,
modalLooker: modalLooker,
isImaVid: isImaVid,
isDynamicGroup: isDynamicGroup,
isCarousel: isCarousel,
setDynamicGroupIndex: setDynamicGroupIndex,
setImaVidFrameNumber: setImaVidFrameNumber,
};
}
// ==========================================================
// Utility: seekVideoToFrame
// Uses modalLooker.getVideo() to seek the <video> element
// (not in regular DOM, only accessible via the looker) and
// modalLooker.updater() to sync internal looker state.
// ==========================================================
function seekVideoToFrame(frameNumber, modalLooker, fps) {
if (!modalLooker) return;
// Seek the actual video element (native video only)
if (typeof modalLooker.getVideo === "function") {
var video = modalLooker.getVideo();
if (video && video.currentTime !== undefined) {
video.currentTime = (frameNumber - 1) / fps;
}
}
// Sync the looker's internal state
if (typeof modalLooker.updater === "function") {
modalLooker.updater({ frameNumber: frameNumber });
}
// Pause if playing (stay on the seeked frame)
if (typeof modalLooker.pause === "function") {
modalLooker.pause();
}
}
// ==========================================================
// Utility: seekImaVidToFrame
// Dispatches a timeline CustomEvent to seek the ImaVid player.
// This is the same mechanism FiftyOne's built-in set_frame_number
// operator and seek bar use. It updates:
// - The canvas (via renderFrame → drawFrameNoAnimation)
// - The Jotai frame number atom (resume point for playback)
// - The status indicator ("30/38" counter)
// - The seek bar position
// ==========================================================
function seekImaVidToFrame(frameNumber) {
var params = new URLSearchParams(window.location.search);
var sampleId = params.get("id");
var groupId = params.get("groupId");
if (!sampleId && !groupId) return;
var timelineName = groupId
? "timeline-" + groupId
: "timeline-" + sampleId;
window.dispatchEvent(
new CustomEvent("set-frame-number-" + timelineName, {
detail: { frameNumber: Math.max(frameNumber, 1) },
}),
);
}
// ==========================================================
// localStorage helpers for chart persistence
// ==========================================================
var LS_PREFIX = "@mgustineli/temporal-detection:fields:";
function saveChartFields(datasetName, charts) {
if (!datasetName) return;
try {
var entries = charts.map(function (c) {
return { field: c.field, type: c.type || "count" };
});
localStorage.setItem(LS_PREFIX + datasetName, JSON.stringify(entries));
} catch (e) { /* quota errors etc */ }
}
function loadChartFields(datasetName) {
if (!datasetName) return null;
try {
var raw = localStorage.getItem(LS_PREFIX + datasetName);
if (!raw) return null;
var parsed = JSON.parse(raw);
if (!parsed || !parsed.length) return null;
// Migration: old format was string array of field paths
if (typeof parsed[0] === "string") {
return parsed.map(function (f) {
return { field: f, type: "count" };
});
}
return parsed;
} catch (e) { return null; }
}
// ==========================================================
// Component: SVGChart
// Pure SVG line chart with current-frame indicator and
// click/drag-to-seek.
// ==========================================================
function SVGChart(props) {
var frames = props.frames;
var counts = props.counts;
var currentFrame = props.currentFrame;
var totalFrames = props.totalFrames;
var onFrameSeek = props.onFrameSeek;
var width = props.width;
var yAxisTitle = props.yAxisTitle || "Detection Count";
var useFrameNumber = props.useFrameNumber !== false;
var fps = props.fps || 30;
var plotWidth = width - MARGIN.left - MARGIN.right;
var plotHeight = CHART_HEIGHT - MARGIN.top - MARGIN.bottom;
if (plotWidth <= 0 || plotHeight <= 0) return null;
var maxCount = 1;
var isIntegerData = true;
for (var i = 0; i < counts.length; i++) {
var cv = counts[i];
if (cv > maxCount) maxCount = cv;
if (cv !== null && cv !== undefined && !Number.isInteger(cv)) {
isIntegerData = false;
}
}
// Add 10% headroom. For integer data, ceil to a clean integer so y-ticks
// land on round counts. For floats, keep precision so sub-1 ranges aren't
// collapsed (e.g. salt_and_pepper values in [0, 1]).
var yMax = isIntegerData
? Math.max(Math.ceil(maxCount * 1.1), 1)
: maxCount * 1.1;
var formatChartValue = function (v) {
if (v === null || v === undefined) return "";
if (typeof v !== "number") return String(v);
if (Number.isInteger(v)) return String(v);
return v.toFixed(2);
};
// --- Scale functions ---
var xScale = function (frame) {
return (
MARGIN.left + ((frame - 1) / Math.max(totalFrames - 1, 1)) * plotWidth
);
};
var yScale = function (count) {
return MARGIN.top + plotHeight - (count / yMax) * plotHeight;
};
// --- Frame seek from mouse position ---
var frameFromMouseX = function (clientX, svgEl) {
var rect = svgEl.getBoundingClientRect();
var clickX = clientX - rect.left;
if (clickX < MARGIN.left) clickX = MARGIN.left;
if (clickX > width - MARGIN.right) clickX = width - MARGIN.right;
var fraction = (clickX - MARGIN.left) / plotWidth;
var frame = Math.round(fraction * (totalFrames - 1)) + 1;
return Math.max(1, Math.min(totalFrames, frame));
};
// --- Mouse handlers for click + drag seeking ---
var svgRef = useRef(null);
var handleMouseDown = useCallback(
function (e) {
if (!svgRef.current) return;
e.preventDefault();
var frame = frameFromMouseX(e.clientX, svgRef.current);
onFrameSeek(frame);
var svg = svgRef.current;
var handleMouseMove = function (ev) {
var f = frameFromMouseX(ev.clientX, svg);
onFrameSeek(f);
};
var handleMouseUp = function () {
document.removeEventListener("mousemove", handleMouseMove);
document.removeEventListener("mouseup", handleMouseUp);
};
document.addEventListener("mousemove", handleMouseMove);
document.addEventListener("mouseup", handleMouseUp);
},
[onFrameSeek, totalFrames, width],
);
// --- Build SVG children ---
var children = [];
// Background
children.push(
h("rect", {
key: "bg",
width: width,
height: CHART_HEIGHT,
fill: "#18191A",
rx: 6,
}),
);
// Y axis gridlines + labels
var yTickCount = 5;
for (var t = 0; t <= yTickCount; t++) {
var rawTickVal = (yMax / yTickCount) * t;
var tickVal = isIntegerData ? Math.round(rawTickVal) : rawTickVal;
var y = yScale(tickVal);
children.push(
h("line", {
key: "yg-" + t,
x1: MARGIN.left,
y1: y,
x2: width - MARGIN.right,
y2: y,
stroke: "#1E1F20",
strokeWidth: 1,
strokeDasharray: "4,4",
}),
);
children.push(
h(
"text",
{
key: "yl-" + t,
x: MARGIN.left - 10,
y: y + 4,
fill: "#8F8D8B",
fontSize: 12,
textAnchor: "end",
fontFamily: "monospace",
},
formatChartValue(tickVal),
),
);
}
// X axis tick labels
var xTickStep = Math.max(1, Math.floor(totalFrames / 8));
var xTicks = [];
for (var f = 1; f <= totalFrames; f += xTickStep) {
xTicks.push(f);
}
if (xTicks[xTicks.length - 1] !== totalFrames) {
xTicks.push(totalFrames);
}
for (var xi = 0; xi < xTicks.length; xi++) {
var xv = xTicks[xi];
children.push(
h(
"text",
{
key: "xl-" + xi,
x: xScale(xv),
y: CHART_HEIGHT - MARGIN.bottom + 20,
fill: "#8F8D8B",
fontSize: 12,
textAnchor: "middle",
fontFamily: "monospace",
},
useFrameNumber ? xv : formatTimestampTick(xv, fps),
),
);
// Small tick mark
children.push(
h("line", {
key: "xt-" + xi,
x1: xScale(xv),
y1: MARGIN.top + plotHeight,
x2: xScale(xv),
y2: MARGIN.top + plotHeight + 5,
stroke: "#404040",
strokeWidth: 1,
}),
);
}
// Axis lines
children.push(
h("line", {
key: "xaxis",
x1: MARGIN.left,
y1: MARGIN.top + plotHeight,
x2: width - MARGIN.right,
y2: MARGIN.top + plotHeight,
stroke: "#404040",
strokeWidth: 1,
}),
);
children.push(
h("line", {
key: "yaxis",
x1: MARGIN.left,
y1: MARGIN.top,
x2: MARGIN.left,
y2: MARGIN.top + plotHeight,
stroke: "#404040",
strokeWidth: 1,
}),
);
// Area fill under line
if (frames.length > 1) {
var areaD = "M " + xScale(frames[0]) + "," + (MARGIN.top + plotHeight);
for (var ai = 0; ai < frames.length; ai++) {
areaD += " L " + xScale(frames[ai]) + "," + yScale(counts[ai]);
}
areaD +=
" L " +
xScale(frames[frames.length - 1]) +
"," +
(MARGIN.top + plotHeight) +
" Z";
children.push(
h("path", {
key: "area",
d: areaD,
fill: "rgba(255, 109, 4, 0.10)",
}),
);
}
// Data line
if (frames.length > 0) {
var pts = "";
for (var li = 0; li < frames.length; li++) {
if (li > 0) pts += " ";
pts += xScale(frames[li]) + "," + yScale(counts[li]);
}
children.push(
h("polyline", {
key: "line",
points: pts,
fill: "none",
stroke: "#FF6D04",
strokeWidth: 1.5,
strokeLinejoin: "round",
strokeLinecap: "round",
}),
);
}
// Current frame indicator
if (currentFrame >= 1 && currentFrame <= totalFrames) {
var cx = xScale(currentFrame);
// Vertical line
children.push(
h("line", {
key: "vline",
x1: cx,
y1: MARGIN.top,
x2: cx,
y2: MARGIN.top + plotHeight,
stroke: "#86B5F6",
strokeWidth: 2,
opacity: 0.85,
}),
);
// Frame label above chart
children.push(
h(
"text",
{
key: "vlabel",
x: cx,
y: MARGIN.top - 10,
fill: "#86B5F6",
fontSize: 14,
fontWeight: "bold",
textAnchor: "middle",
fontFamily: "monospace",
},
useFrameNumber ? "Frame " + currentFrame : formatTimestamp(currentFrame, fps),
),
);
// Dot at data point (if frame exists in data)
var dataIdx = -1;
for (var di = 0; di < frames.length; di++) {
if (frames[di] === currentFrame) {
dataIdx = di;
break;
}
}
if (dataIdx >= 0) {
children.push(
h("circle", {
key: "vdot",
cx: cx,
cy: yScale(counts[dataIdx]),
r: 5,
fill: "#86B5F6",
stroke: "#FFF9F5",
strokeWidth: 2,
}),
);
// Count label next to dot
children.push(
h(
"text",
{
key: "vcount",
x: cx + 10,
y: yScale(counts[dataIdx]) - 8,
fill: "#86B5F6",
fontSize: 12,
fontWeight: "bold",
fontFamily: "monospace",
},
formatChartValue(counts[dataIdx]),
),
);
}
}
// Y axis title
children.push(
h(
"text",
{
key: "ytitle",
x: 16,
y: CHART_HEIGHT / 2,
fill: "#6E6C6A",
fontSize: 14,
textAnchor: "middle",
transform: "rotate(-90, 16, " + CHART_HEIGHT / 2 + ")",
fontFamily: "sans-serif",
},
yAxisTitle,
),
);
// X axis title
children.push(
h(
"text",
{
key: "xtitle",
x: width / 2,
y: CHART_HEIGHT - 5,
fill: "#6E6C6A",
fontSize: 14,
textAnchor: "middle",
fontFamily: "sans-serif",
},
useFrameNumber ? "Frame Number" : "Time",
),
);
// Transparent overlay for click/drag — on top of everything
children.push(
h("rect", {
key: "overlay",
x: MARGIN.left,
y: MARGIN.top,
width: plotWidth,
height: plotHeight,
fill: "transparent",
cursor: "crosshair",
onMouseDown: handleMouseDown,
}),
);
return h(
"svg",
{
ref: svgRef,
width: width,
height: CHART_HEIGHT,
style: { display: "block", userSelect: "none" },
},
children,
);
}
// ==========================================================
// Component: LabelTimelineChart
// Swim lane heatmap showing per-label detection counts.
// ==========================================================
function LabelTimelineChart(props) {
var frames = props.frames;
var labels = props.labels;
var timeline = props.timeline;
var colorKeyMap = props.colorKeyMap || null; // maps label → color key (for instance tracks)
var currentFrame = props.currentFrame;
var totalFrames = props.totalFrames;
var onFrameSeek = props.onFrameSeek;
var width = props.width;
var useFrameNumber = props.useFrameNumber !== false;
var fps = props.fps || 30;
var _expanded = useState(false);
var expanded = _expanded[0];
var setExpanded = _expanded[1];
var _hoverInfo = useState(null);
var hoverInfo = _hoverInfo[0];
var setHoverInfo = _hoverInfo[1];
// Label filter: null = show all, array = selected labels
var _labelFilter = useState(null);
var labelFilter = _labelFilter[0];
var setLabelFilter = _labelFilter[1];
// Filter dropdown open state
var _filterOpen = useState(false);
var filterOpen = _filterOpen[0];
var setFilterOpen = _filterOpen[1];
var svgRef = useRef(null);
var wrapRef = useRef(null);
var filterBtnRef = useRef(null);
if (!labels || labels.length === 0) {
return h(
Box,
{
sx: {
display: "flex",
alignItems: "center",
justifyContent: "center",
height: 200,
bgcolor: "#18191A",
},
},
h(Typography, { sx: { color: "#8F8D8B" } }, "No label data available"),
);
}
// Toggle a label in the filter
var handleFilterToggle = function (label) {
setLabelFilter(function (prev) {
if (!prev) {
// Currently showing all → show all except this one
return labels.filter(function (l) { return l !== label; });
}
var idx = prev.indexOf(label);
if (idx >= 0) {
var next = prev.filter(function (l) { return l !== label; });
return next.length === 0 || next.length === labels.length ? null : next;
}
return prev.concat([label]);
});
};
var filteredLabels = labelFilter
? labels.filter(function (l) { return labelFilter.indexOf(l) >= 0; })
: labels;
var visibleLabels = expanded ? filteredLabels : filteredLabels.slice(0, LT_DEFAULT_MAX_LABELS);
var hiddenCount = filteredLabels.length - LT_DEFAULT_MAX_LABELS;
var showExpander = !expanded && hiddenCount > 0;
var showCollapser = expanded && filteredLabels.length > LT_DEFAULT_MAX_LABELS;
var plotWidth = width - LT_MARGIN.left - LT_MARGIN.right;
var heatmapHeight = visibleLabels.length * (LT_ROW_HEIGHT + LT_ROW_GAP);
var expanderHeight = (showExpander || showCollapser) ? 24 : 0;
var chartHeight = LT_MARGIN.top + heatmapHeight + expanderHeight + LT_MARGIN.bottom;
if (plotWidth <= 0) return null;
// Find per-label max count for opacity scaling
// Each label is scaled against its own peak, so every row
// independently shows where that label is most/least active
var labelMax = {};
for (var li = 0; li < visibleLabels.length; li++) {
var lm = 1;
var arr = timeline[visibleLabels[li]];
if (arr) {
for (var fi = 0; fi < arr.length; fi++) {
if (arr[fi] > lm) lm = arr[fi];
}
}
labelMax[visibleLabels[li]] = lm;
}
// Frame binning: when plotWidth / totalFrames < 2px
var binSize = 1;
var binnedFrameCount = frames.length;
if (frames.length > 0 && plotWidth / frames.length < 2) {
binSize = Math.ceil(frames.length / (plotWidth / 2));
binnedFrameCount = Math.ceil(frames.length / binSize);
}
// Bin timeline data if needed
var displayTimeline = timeline;
if (binSize > 1) {
displayTimeline = {};
for (var bi = 0; bi < visibleLabels.length; bi++) {
var lbl = visibleLabels[bi];
var src = timeline[lbl] || [];
var binned = [];
for (var bj = 0; bj < src.length; bj += binSize) {
var maxVal = 0;
for (var bk = bj; bk < Math.min(bj + binSize, src.length); bk++) {
if (src[bk] > maxVal) maxVal = src[bk];
}
binned.push(maxVal);
}
displayTimeline[lbl] = binned;
}
}
var cellWidth = plotWidth / binnedFrameCount;
// Scale function
var xScale = function (frame) {
return LT_MARGIN.left + ((frame - 1) / Math.max(totalFrames - 1, 1)) * plotWidth;
};
var frameFromMouseX = function (clientX, svgEl) {
var rect = svgEl.getBoundingClientRect();
var clickX = clientX - rect.left;
if (clickX < LT_MARGIN.left) clickX = LT_MARGIN.left;
if (clickX > width - LT_MARGIN.right) clickX = width - LT_MARGIN.right;
var fraction = (clickX - LT_MARGIN.left) / plotWidth;
var frame = Math.round(fraction * (totalFrames - 1)) + 1;
return Math.max(1, Math.min(totalFrames, frame));
};
// Mouse handlers for click + drag seeking
var handleMouseDown = useCallback(
function (e) {
if (!svgRef.current) return;
e.preventDefault();
var frame = frameFromMouseX(e.clientX, svgRef.current);
onFrameSeek(frame);
var svg = svgRef.current;
var onMove = function (ev) {
onFrameSeek(frameFromMouseX(ev.clientX, svg));
};
var onUp = function () {
document.removeEventListener("mousemove", onMove);
document.removeEventListener("mouseup", onUp);
};
document.addEventListener("mousemove", onMove);
document.addEventListener("mouseup", onUp);
},
[onFrameSeek, totalFrames, width],
);
// Hover handler
var handleHover = function (e) {
if (!svgRef.current || !wrapRef.current) return;
var wrapRect = wrapRef.current.getBoundingClientRect();
var svgRect = svgRef.current.getBoundingClientRect();
var mouseX = e.clientX - svgRect.left;
var binIdx = Math.max(
0,
Math.min(binnedFrameCount - 1, Math.floor((mouseX - LT_MARGIN.left) / cellWidth)),
);
var entries = [];
for (var ti = 0; ti < visibleLabels.length; ti++) {
var hl = visibleLabels[ti];
var val = displayTimeline[hl] ? (displayTimeline[hl][binIdx] || 0) : 0;
if (val > 0) entries.push({ label: hl, count: val, color: labelColor(colorKeyMap ? colorKeyMap[hl] || hl : hl) });
}
if (entries.length === 0) {
setHoverInfo(null);
return;
}
var frameStart = binIdx * binSize + 1;
var frameEnd = Math.min((binIdx + 1) * binSize, totalFrames);
var hoverFrame = frameFromMouseX(e.clientX, svgRef.current);
var frameLabel;
if (useFrameNumber) {
frameLabel = binSize > 1
? "Frames " + frameStart + "\u2013" + frameEnd
: "Frame " + hoverFrame;
} else {
frameLabel = binSize > 1
? formatTimestampTick(frameStart, fps) + "\u2013" + formatTimestampTick(frameEnd, fps)
: formatTimestamp(hoverFrame, fps);
}
setHoverInfo({
x: e.clientX,
y: e.clientY,
frameLabel: frameLabel,
entries: entries,
});
};
var handleMouseLeave = function () {
setHoverInfo(null);
};
// Build SVG children
var children = [];
// Background
children.push(
h("rect", {
key: "bg",
width: width,
height: chartHeight,
fill: "#18191A",
rx: 6,
}),
);
// Heatmap cells
for (var ri = 0; ri < visibleLabels.length; ri++) {
var lbl = visibleLabels[ri];
var color = labelColor(colorKeyMap ? colorKeyMap[lbl] || lbl : lbl);
var rowY = LT_MARGIN.top + ri * (LT_ROW_HEIGHT + LT_ROW_GAP);
var vals = displayTimeline[lbl] || [];
for (var ci = 0; ci < vals.length; ci++) {
if (vals[ci] === 0) continue;
var opacity = 0.2 + 0.8 * (vals[ci] / labelMax[lbl]);
children.push(
h("rect", {
key: "c-" + ri + "-" + ci,
x: LT_MARGIN.left + ci * cellWidth,
y: rowY,
width: Math.max(cellWidth - 0.5, 1),
height: LT_ROW_HEIGHT,
fill: color,
opacity: opacity,
}),
);
}
// Color swatch
children.push(
h("rect", {
key: "sw-" + ri,
x: LT_MARGIN.left - 18,
y: rowY + 3,
width: 8,
height: 8,
fill: color,
rx: 1,
}),
);
// Label name (truncated with SVG title tooltip)
var displayName = lbl.length > 16 ? lbl.substring(0, 15) + "\u2026" : lbl;
children.push(
h(
"text",
{
key: "ln-" + ri,
x: LT_MARGIN.left - 22,
y: rowY + LT_ROW_HEIGHT / 2 + 4,
fill: "#C1BFBD",
fontSize: 11,