Repository navigation
Expand file tree
/
Copy pathrender-example.mjs
More file actions
106 lines (99 loc) · 3.82 KB
/
Copy pathrender-example.mjs
File metadata and controls
106 lines (99 loc) · 3.82 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
import { mkdir, writeFile } from "node:fs/promises";
import { mountSampledLineChart } from "./render-chart.mjs";
import { sampleData } from "./dist/sample-data.js";
function geometry(container) {
const path = container.querySelector(".recharts-line-curve");
if (path === null) throw new Error("Recharts did not render a line path");
const d = path.getAttribute("d") ?? "";
return {
finiteCount: (d.match(/[ML]/g) ?? []).length,
pathSegments: (d.match(/M/g) ?? []).length,
};
}
function chartMarkup(container) {
const svg = container.querySelector("svg");
if (svg === null) throw new Error("Recharts did not render an SVG");
return svg.innerHTML;
}
let original;
let sampled;
let renderError;
const cleanupErrors = [];
try {
original = await mountSampledLineChart({
data: sampleData,
maxPoints: 8_000,
width: 940,
height: 310,
title: "Original data",
xLabel: "Sample index",
yLabel: "Value",
}, { rootId: "original-preview" });
sampled = await mountSampledLineChart({
data: sampleData,
maxPoints: 320,
width: 940,
height: 310,
title: "Bounded sample",
xLabel: "Sample index",
yLabel: "Value",
}, { rootId: "sampled-preview" });
const originalGeometry = geometry(original.container);
const sampledGeometry = geometry(sampled.container);
const summary = {
sourceCount: sampleData.length,
selectedCount: sampled.selectedData.length,
originalFiniteCount: originalGeometry.finiteCount,
selectedFiniteCount: sampledGeometry.finiteCount,
originalPathSegments: originalGeometry.pathSegments,
selectedPathSegments: sampledGeometry.pathSegments,
};
const preview = `<svg xmlns="http://www.w3.org/2000/svg" width="1000" height="800" viewBox="0 0 1000 800">
<rect width="1000" height="800" fill="#f8fafc"/>
<text x="30" y="42" font-family="system-ui, sans-serif" font-size="26" font-weight="700" fill="#0f172a">Line sampling comparison</text>
<text x="30" y="68" font-family="system-ui, sans-serif" font-size="14" fill="#475569">Same full domains · missing-data boundaries preserved</text>
<g transform="translate(20 90)">
<rect width="960" height="330" rx="12" fill="#ffffff" stroke="#cbd5e1"/>
<text x="20" y="28" font-family="system-ui, sans-serif" font-size="16" font-weight="600" fill="#0f172a">Original · 8,000 points</text>
<svg x="10" y="34" width="940" height="285" viewBox="0 0 940 310">${chartMarkup(original.container)}</svg>
</g>
<g transform="translate(20 440)">
<rect width="960" height="330" rx="12" fill="#ffffff" stroke="#cbd5e1"/>
<text x="20" y="28" font-family="system-ui, sans-serif" font-size="16" font-weight="600" fill="#0f172a">Bounded sample · 320 points</text>
<svg x="10" y="34" width="940" height="285" viewBox="0 0 940 310">${chartMarkup(sampled.container)}</svg>
</g>
</svg>`;
await mkdir(new URL("./output/", import.meta.url), { recursive: true });
await writeFile(new URL("./output/preview.svg", import.meta.url), preview);
await writeFile(new URL("./output/summary.json", import.meta.url), `${JSON.stringify(summary, null, 2)}\n`);
console.log(JSON.stringify(summary));
} catch (error) {
renderError = error;
} finally {
if (sampled !== undefined) {
try {
await sampled.close();
} catch (error) {
cleanupErrors.push(error);
}
}
if (original !== undefined) {
try {
await original.close();
} catch (error) {
cleanupErrors.push(error);
}
}
}
if (renderError !== undefined && cleanupErrors.length > 0) {
throw new AggregateError(
[renderError, ...cleanupErrors],
"Example rendering and cleanup failed",
{ cause: renderError },
);
}
if (renderError !== undefined) throw renderError;
if (cleanupErrors.length === 1) throw cleanupErrors[0];
if (cleanupErrors.length > 1) {
throw new AggregateError(cleanupErrors, "Example cleanup failed");
}