-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
732 lines (610 loc) · 20 KB
/
graph.js
File metadata and controls
732 lines (610 loc) · 20 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
// SPDX-License-Identifier: MIT
// Utilities
const SVG_NS = "http://www.w3.org/2000/svg";
const KEY_BAR_WIDTH = 8;
const KEY_BAR_RPAD = 8;
const KEY_BAR_HEIGHT = 3;
const SMOLGRAPH = "smolgraph";
const LINE_WIDTH = 1;
// Minification-aware renaming
const CLICK = "click";
const GESTURE = "gesture";
const TOUCH = "touch";
const VISIBILITY = "visibility";
const VISIBLE = "visible";
const MIDDLE = "middle";
const CLIENT_X = "clientX";
const CLIENT_Y = "clientY";
const TRANSFORM = "transform";
const HIDDEN = "hidden";
const { abs, min, max, pow, floor} = Math;
const len = a => a.length;
const flatmap = (arr, f) => arr.flatMap(f);
const map = (arr, f) => arr.map(f);
const push = (arr, el) => arr.push(el);
const isInt = n => Number.isInteger(n);
// Global mutable state. Sue me.
// We need to differentiate these IDs between calls.
let clipPathCounter = 0;
const zip = (...args) =>
map(args[0], (_, i) =>
map(args, arg => arg[i])
);
const unzip = arrs => zip(...arrs);
const isStr = a => typeof a === "string";
const formatTickValue = value => isStr(value) ? value :
(isStr(value) || isInt(value) ? `${value}` : `${Number(value.toFixed(3))}`);
const formatTrackerLabel = (x, y) =>
`(${formatTickValue(x)}, ${formatTickValue(y)})`;
const twoargs = f => (a, b) => f(a, b);
const maximum = arr => arr.reduce(twoargs(max));
const bounds = arr => [arr.reduce(twoargs(min)), maximum(arr)];
const calculateNiceScale = (values, maxTicks = 10) => {
if (len(values) === 0) {
return { min: 0, max: 0, tickStep: 1, ticks: [0] };
}
const [dataMin, dataMax] = bounds(values);
const range = dataMax - dataMin;
const roughStep = range / maxTicks;
const exponent = floor(Math.log10(roughStep));
const fraction = roughStep / pow(10, exponent);
const niceFraction = fraction <= 1 ?
1 :
fraction <= 2 ?
2 :
fraction <= 5 ?
5 :
10;
const niceStep = niceFraction * pow(10, exponent);
if (niceStep === 0) {
return;
}
const niceMin = floor(dataMin / niceStep) * niceStep;
const niceMax = Math.ceil(dataMax / niceStep) * niceStep;
const ticks = [];
for (let t = niceMin; t <= niceMax + 1e-9; t += niceStep) {
push(ticks, parseFloat(t.toFixed(12)));
}
return { min: niceMin, max: niceMax, tickStep: niceStep, ticks };
};
const setAttr = (el, k, v) => {
el.setAttribute(k, v);
};
const setAttrs = (el, attrs) => {
for (const [k, v] of Object.entries(attrs)) {
setAttr(el, k, v);
}
};
const addChild = (el, child) => {
el.appendChild(child);
};
const addChildren = (el, children) => {
for (const child of children) {
addChild(el, child);
}
};
const el = (tag, attrs = {}, children = []) => {
const elem = document.createElementNS(SVG_NS, tag);
setAttrs(elem, attrs);
addChildren(elem, children);
return elem;
};
const text = (content, attrs = {}) => {
const res = el("text", attrs);
res.textContent = content;
return res;
};
const createScale = (domainMin, domainMax, rangeMin, rangeMax) => value =>
rangeMin + ((value - domainMin) / (domainMax - domainMin)) * (rangeMax - rangeMin);
const mkTickLine = (x1, y1, x2, y2) =>
el("line", {
class: "tick",
x1, y1, x2, y2,
});
const mkTickLabel = (textValue, x, y, anchor) =>
text(textValue, {
class: "tick-label",
x, y,
"text-anchor": anchor,
});
const genColors = data => map(data, (_, n) => `hsl(${n * 360 / len(data) + 80},40%,60%)`);
const rect = (x, y, width, height, rest = {}) =>
el("rect", {x, y, width, height, ...rest});
const boundData = (origData, minX, maxX, xIsStringy) =>
map(structuredClone(origData), ({data,label}) => ({
label,
data: xIsStringy ?
data.slice(minX, maxX) :
data.filter(([x]) => x >= minX && x <= maxX)
}));
const addEv = (elem, name, handler) => {
elem.addEventListener(name, e => {
handler(e);
e.preventDefault();
e.stopPropagation();
}, { passive: false });
};
const debounce = (f, timeout) => {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
f.apply(this, args);
}, timeout);
};
};
const justClass = className => ({"class": className});
const hide = elem => {
setAttr(elem, VISIBILITY, HIDDEN);
};
const binarySearch = (values, f) => {
const l = len(values);
let pos = 0;
let step = l;
while (step > 0) {
while (pos + step < l && f(values[pos + step]) < 0) {
pos += step;
}
step = floor(step / 2);
}
return pos;
};
export const drawGraph = config => {
const { data: allData } = config;
const {
width = 800,
height = 500,
lineColors = genColors(allData),
maxTicks = {x: 15, y: 10},
loadData,
axisLabels = {x: "X", y: "Y"},
onClick,
} = config;
const svg = el("svg", {
xmlns: SVG_NS,
viewBox: `0 0 ${width} ${height}`,
"class": SMOLGRAPH
});
// So we can use getBBox
addChild(document.body, svg);
const testText = text("test");
addChild(svg, testText);
const {width: testTextWidth, height: CHAR_HEIGHT } = testText.getBBox();
const CHAR_WIDTH = testTextWidth/4;
const KEY_PAD = CHAR_HEIGHT / 2;
const TEXT_CENTER_OFFSET = CHAR_HEIGHT * 0.3;
// Estimate of the height of capital letters...
const TEXT_TOP_OFFSET = CHAR_HEIGHT * 0.6;
const dataStack = [allData];
const tickWidth = tick => CHAR_WIDTH * len(formatTickValue(tick));
const projectLink = el("a", {
href: `https://github.com/414owen/${SMOLGRAPH}`
}, [
text(SMOLGRAPH, {
x: CHAR_WIDTH,
y: height - CHAR_WIDTH
})
]
);
const zoomOutButton = text("reset zoom", justClass("zoom-out"));
// Subsequent data loads shouldn't overwrite each other.
// We make sure the last request sent out for new data is the only
// one that actually gets rendered, by incrementing this before sending a request.
// And making sure it's the right value before rendering.
let dataLoadSentinel = 0;
const drawGraphData = (data = dataStack.at(-1)) => {
const dataSeries = map(data, d => d.data);
const lineLabels = map(data, d => d.label);
const xIsStringy = isStr(dataSeries[0][0][0]);
let xValues;
if (xIsStringy) {
xValues = map(dataSeries[0], (_, i) => i);
xValues.sort((a, b) => a - b);
} else {
xValues = [...new Set(flatmap(dataSeries, d => map(d, a => a[0])))];
}
// Zoomed in/out too far, can't guarantee correctness.
if (abs(xValues[0] - xValues.at(-1)) < 1e-10 ||
abs(xValues[0] - xValues.at(-1)) > 1e12) {
return;
}
const firstSeries = dataSeries[0];
const xLabel = xValue => xIsStringy ?
(xValue < len(firstSeries) && isInt(xValue) ?
firstSeries[xValue][0] :
"") :
xValue;
const ySeries = map(dataSeries, d => map(d, a => a[1]));
const yValues = ySeries.flat();
svg.innerHTML = "";
// Calculate scales
const xScaleData = calculateNiceScale(xValues, maxTicks.x);
if (!xScaleData) {return;}
if (xIsStringy) {
xScaleData.ticks = map(xScaleData.ticks, xLabel);
}
const yScaleData = calculateNiceScale(bounds(yValues), maxTicks.y);
const marginLeft = CHAR_HEIGHT + CHAR_WIDTH * 3 +
maximum(map(yScaleData.ticks, tickWidth));
const marginRight = tickWidth(xLabel(xScaleData.max)) / 2 + CHAR_WIDTH;
const marginTop = CHAR_HEIGHT/2 + CHAR_WIDTH;
const marginBottom = CHAR_HEIGHT * 2 + CHAR_WIDTH * 3;
const innerWidth = width - marginLeft - marginRight;
const innerHeight = height - marginTop - marginBottom;
const chartRight = marginLeft + innerWidth;
const chartBottom = marginTop + innerHeight;
const scaleXNum = createScale(xScaleData.min, xScaleData.max, marginLeft, chartRight);
const scaleX = xIsStringy ? ((x, i) => scaleXNum(i)) : scaleXNum;
const scaleY = createScale(yScaleData.min, yScaleData.max, chartBottom, marginTop);
setAttrs(zoomOutButton, {
x: chartRight - CHAR_WIDTH,
y: marginTop + CHAR_HEIGHT
});
const trackerLayer = el("g", justClass("tracker"));
const trackerEls = map(data, () => {
const line = el("line");
const dot = el("circle", {
r: 4,
});
addChildren(trackerLayer, [line, dot]);
return {line, dot};
});
const mkTicks = (
ticks,
scaleFn,
lineCoordsFn,
labelPosFn,
anchor
) =>
unzip(map(ticks, (tick, i) => {
const pos = scaleFn(tick, i);
const line = mkTickLine(...lineCoordsFn(pos));
const label = mkTickLabel(
formatTickValue(tick),
...labelPosFn(pos),
anchor
);
return [line, label];
}));
{
const TICK_Y = chartBottom + TEXT_TOP_OFFSET + CHAR_WIDTH;
const [hlines, hlabels] = mkTicks(
yScaleData.ticks,
tick => scaleY(tick),
y => [marginLeft, y, chartRight, y],
y => [marginLeft - CHAR_WIDTH, y + TEXT_CENTER_OFFSET],
"end"
);
const [vlines, vlabels] = mkTicks(
xScaleData.ticks,
(tick, i) => scaleX(tick, i * xScaleData.tickStep),
x => [x, marginTop, x, chartBottom],
x => [x, TICK_Y],
MIDDLE
);
addChildren(svg, vlines);
addChildren(svg, hlines);
addChildren(svg, vlabels);
addChildren(svg, hlabels);
// Draw axis labels
{
addChild(svg, text(axisLabels.x, {
x: marginLeft + innerWidth / 2,
y: TICK_Y + CHAR_WIDTH + CHAR_HEIGHT,
"text-anchor": MIDDLE,
}));
{
const y = marginTop + innerHeight / 2;
addChild(svg, text(axisLabels.y, {
"text-anchor": MIDDLE,
[TRANSFORM]: `translate(${CHAR_HEIGHT},${y}) rotate(-90)`
}));
}
}
}
// Draw data lines
const pathGroup = el("g", justClass("paths"), map(data, ({data: points}, idx) => {
const [firstX, firstY] = points[0];
const initial = `M${scaleX(firstX, 0)},${scaleY(firstY)}`;
const rest = map(points.slice(1), ([x, y], i) => `L${scaleX(x, i + 1)},${scaleY(y)}`);
const linePath = initial + rest.join("");
return el("path", {
d: linePath,
fill: "none",
stroke: lineColors[idx % len(lineColors)],
"stroke-width": LINE_WIDTH
});
}));
addChildren(svg, [
el("defs", {}, [
el("clipPath", {id: `${SMOLGRAPH}-chart-clip-${clipPathCounter}`}, [
el("path", {
d: `M${marginLeft},${marginTop}h${innerWidth}v${innerHeight}h-${innerWidth}`
})
])
]),
el("g", {
"clip-path": `url(#${SMOLGRAPH}-chart-clip-${clipPathCounter++})`,
}, [pathGroup])
]);
const overlay = rect(
0, marginTop, width, innerHeight,
justClass("overlay")
);
overlay.focus();
const hideTrackers = () => {
for (const child of trackerLayer.children) {
hide(child);
}
};
const overlayEv = (name, handler) => addEv(overlay, name, handler);
const limitX = x => min(max(x, marginLeft), chartRight);
// Gets the X position, limited to the graphing area...
const getScreenPosition = event => {
const domPoint = new DOMPointReadOnly(event[CLIENT_X], event[CLIENT_Y]);
return limitX(domPoint.matrixTransform(svg.getScreenCTM().inverse()).x);
};
const xToPoint = x =>
xScaleData.min + (x - marginLeft) / innerWidth * (xScaleData.max - xScaleData.min);
let xScreenPos = marginLeft;
let timesScaled = 0;
// Assumes an up-to-date xScreenPos
/** @returns An index per data series */
const getNearestIndices = () => {
const xValue = xToPoint(xScreenPos);
return map(dataSeries, (points) => {
const prevIndex = xIsStringy ?
min(floor(xValue), len(firstSeries) - 1) :
binarySearch(points, ([x]) => x - xValue);
const nextIndex = min(len(points) - 1, prevIndex + 1);
const [prevX, nextX] = xIsStringy ?
[prevIndex, nextIndex] :
[points[prevIndex][0], points[nextIndex][0]];
return abs(xValue - prevX) < abs(xValue - nextX) ?
prevIndex :
nextIndex;
});
};
const keyRect = rect(
marginLeft,
marginTop,
0,
KEY_PAD * 2 + TEXT_TOP_OFFSET + CHAR_HEIGHT * (len(lineLabels) - 1),
justClass("key")
);
const updateKeyRect = (maxKeyChars) => {
setAttr(keyRect, "width", KEY_PAD * 2 + KEY_BAR_WIDTH + KEY_BAR_RPAD + CHAR_WIDTH * maxKeyChars);
};
const maxLabelLen = max(...map(lineLabels, k => len(k)));
const keyTexts = [];
const updateKey = keyLabels => {
for (const [elem, label] of zip(keyTexts, keyLabels)) {
elem.textContent = label;
}
updateKeyRect(maxLabelLen);
};
// With tracker positions
const updateKeyWithPositions = positions => {
updateKey(map(zip(lineLabels, positions),
([label, [x, y]]) =>
`${label.padEnd(maxLabelLen)} ${formatTrackerLabel(xIsStringy ? x : xLabel(x), y)}`
));
updateKeyRect(max(...map(keyTexts, elem => elem.getNumberOfChars())));
};
const updateTracker = event => {
hideTrackers();
xScreenPos = getScreenPosition(event);
const xLines = new Set();
const positions = [];
const tups = zip(dataSeries, trackerEls, getNearestIndices());
for (const [series, {line, dot}, nearestIndex] of tups) {
const xPos = scaleX(series[nearestIndex][0], nearestIndex);
const yPos = scaleY(series[nearestIndex][1]);
if (xLines.has(xPos)) {
hide(line);
} else {
setAttrs(line, {
x1: xPos,
y1: marginTop,
x2: xPos,
y2: chartBottom,
[VISIBILITY]: VISIBLE,
});
xLines.add(xPos);
}
setAttrs(dot, {
cx: xPos,
cy: yPos,
[VISIBILITY]: timesScaled ? HIDDEN : VISIBLE,
});
push(positions, series[nearestIndex]);
}
updateKeyWithPositions(positions);
};
// These blocks aren't necessary, but help with minification
{
let currentScale = 1;
let currentXOffset = 0;
const loadNewData = debounce(async () => {
// Invert element transform (scale(...) translate(...)):
const leftUntransformed = (marginLeft / currentScale) - currentXOffset;
const rightUntransformed = (chartRight / currentScale) - currentXOffset;
// Convert SVG x’s to data-domain values
const minXVisible = xToPoint(leftUntransformed);
const maxXVisible = xToPoint(rightUntransformed);
const expectedTimesScaled = timesScaled;
const boundedData = boundData(
loadData ? data : dataStack[0],
minXVisible,
maxXVisible,
xIsStringy
);
if (
expectedTimesScaled === timesScaled &&
len(boundedData[0].data) >= 2 &&
(!loadData || len(boundedData[0].data) < len(data[0].data))
) {
drawGraphData(boundedData);
}
if (!loadData) {
push(dataStack, boundedData);
return;
}
let expectedDataLoadSentinel = ++dataLoadSentinel;
const newData = await loadData(minXVisible, maxXVisible);
if (dataLoadSentinel !== expectedDataLoadSentinel ||
expectedTimesScaled !== timesScaled ||
len(newData[0].data) < 2) {
return;
}
push(dataStack, newData);
drawGraphData();
}, 300);
// Mobile support
{
let gestureStartScale = null;
let gestureFocalX = null;
const zoomAt = (focalScreenX, nextScale) => {
const oldX = focalScreenX / currentScale;
currentScale = nextScale;
const newX = focalScreenX / currentScale;
currentXOffset += newX - oldX;
setAttrs(pathGroup, {
[TRANSFORM]: `scale(${currentScale} 1) translate(${currentXOffset} 0)`
});
loadNewData();
};
const clamp = (v, mn, mx) => max(mn, min(mx, v));
const clampScale = s => clamp(s, 0.05, 200);
overlayEv(`${GESTURE}start`, e => {
gestureStartScale = currentScale;
gestureFocalX = getScreenPosition(e);
});
overlayEv(`${GESTURE}change`, e => {
if (gestureStartScale === null) {
return;
}
const next = clampScale(gestureStartScale * e.scale);
zoomAt(gestureFocalX, next);
});
overlayEv(`${GESTURE}end`, () => {
gestureStartScale = null;
});
let touchStateActive = false;
let touchStateStartDist = 0;
let touchStateStartScale = 1;
let touchStateFocalX = 0;
const touchDistance = (t0, t1) => {
const dx = t0[CLIENT_X] - t1[CLIENT_X];
const dy = t0[CLIENT_Y] - t1[CLIENT_Y];
return Math.hypot(dx, dy);
};
const touchCenterX = (t0, t1) => (t0[CLIENT_X] + t1[CLIENT_X]) / 2;
overlayEv(`${TOUCH}start`, e => {
const touches = e.touches;
if (len(touches) === 2) {
touchStateActive = true;
touchStateStartDist = touchDistance(touches[0], touches[1]);
touchStateStartScale = currentScale;
touchStateFocalX = getScreenPosition({
[CLIENT_X]: touchCenterX(touches[0], touches[1]),
[CLIENT_Y]: (touches[0][CLIENT_Y] + touches[1][CLIENT_Y]) / 2
});
}
});
overlayEv(`${TOUCH}move`, e => {
const touches = e.touches;
if (touchStateActive && len(touches) === 2) {
const dist = touchDistance(touches[0], touches[1]);
const factor = dist / touchStateStartDist;
const next = clampScale(touchStateStartScale * factor);
zoomAt(touchStateFocalX, next);
}
});
overlayEv(`${TOUCH}end`, (e) => {
if (len(e.touches) < 2) {
touchStateActive = false;
}
});
overlayEv(`${TOUCH}cancel`, () => {
touchStateActive = false;
});
}
if (onClick) {
overlayEv(CLICK, event => {
xScreenPos = getScreenPosition(event);
const xValue = xToPoint(xScreenPos);
let points = map(
zip(dataSeries, getNearestIndices()),
([series, index]) => series[index]
);
onClick(event, xValue, points);
});
}
// Scroll support
overlayEv("wheel", async event => {
timesScaled += 1;
updateTracker(event);
xScreenPos = getScreenPosition(event);
const delta = event.wheelDelta;
const zoomFactor = 1 + abs(delta) / 900;
const oldX = xScreenPos / currentScale;
if (delta > 0) {
currentScale *= zoomFactor;
} else {
currentScale /= zoomFactor;
}
const newX = xScreenPos / currentScale;
currentXOffset += newX - oldX;
setAttr(
pathGroup,
TRANSFORM,
`scale(${currentScale} 1) translate(${currentXOffset} 0)`
);
loadNewData();
});
}
overlayEv("mousemove", updateTracker);
overlayEv("mouseout", () => {
hideTrackers();
updateKey(lineLabels);
});
const keyLayer = el("g", justClass("key"), [
keyRect,
...flatmap(lineLabels, (keyLabel, i) => {
const y = marginTop + KEY_PAD + TEXT_TOP_OFFSET + CHAR_HEIGHT * i;
const textEl = text(keyLabel, {
y,
x: marginLeft + KEY_PAD + KEY_BAR_WIDTH + KEY_BAR_RPAD,
});
push(keyTexts, textEl);
return [
textEl,
rect(
marginLeft + KEY_PAD ,
y - CHAR_HEIGHT / 4 - KEY_BAR_HEIGHT / 2,
KEY_BAR_WIDTH,
KEY_BAR_HEIGHT,
{fill: lineColors[i]}
)
];
}),
]);
updateKeyRect(maxLabelLen);
addChildren(svg, [trackerLayer, keyLayer, overlay, projectLink, zoomOutButton]);
hideTrackers();
};
addEv(svg, "dblclick", () => {
if (len(dataStack) > 1) {
dataStack.pop();
}
drawGraphData();
});
addEv(zoomOutButton, CLICK, () => {
dataStack.splice(1);
drawGraphData();
});
drawGraphData();
svg.remove();
return svg;
};