@opendata-ai/openchart-vanilla 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +327 -0
- package/dist/index.js +4745 -0
- package/dist/index.js.map +1 -0
- package/dist/simulation-worker.js +1196 -0
- package/package.json +58 -0
- package/src/__test-fixtures__/dom.ts +42 -0
- package/src/__test-fixtures__/specs.ts +187 -0
- package/src/__tests__/edit-events.test.ts +747 -0
- package/src/__tests__/events.test.ts +336 -0
- package/src/__tests__/export.test.ts +150 -0
- package/src/__tests__/mount.test.ts +219 -0
- package/src/__tests__/svg-renderer.test.ts +609 -0
- package/src/__tests__/table-mount.test.ts +484 -0
- package/src/__tests__/tooltip.test.ts +201 -0
- package/src/export.ts +105 -0
- package/src/graph/__tests__/canvas-renderer.test.ts +704 -0
- package/src/graph/__tests__/graph-mount.test.ts +213 -0
- package/src/graph/__tests__/interaction.test.ts +205 -0
- package/src/graph/__tests__/keyboard.test.ts +653 -0
- package/src/graph/__tests__/search.test.ts +88 -0
- package/src/graph/__tests__/simulation.test.ts +233 -0
- package/src/graph/__tests__/spatial-index.test.ts +142 -0
- package/src/graph/__tests__/zoom.test.ts +195 -0
- package/src/graph/canvas-renderer.ts +660 -0
- package/src/graph/interaction.ts +359 -0
- package/src/graph/keyboard.ts +208 -0
- package/src/graph/search.ts +50 -0
- package/src/graph/simulation-worker-url.ts +30 -0
- package/src/graph/simulation-worker.ts +265 -0
- package/src/graph/simulation.ts +350 -0
- package/src/graph/spatial-index.ts +121 -0
- package/src/graph/types.ts +44 -0
- package/src/graph/worker-protocol.ts +67 -0
- package/src/graph/zoom.ts +104 -0
- package/src/graph-mount.ts +675 -0
- package/src/index.ts +56 -0
- package/src/mount.ts +1639 -0
- package/src/renderers/table-cells.ts +444 -0
- package/src/resize-observer.ts +46 -0
- package/src/svg-renderer.ts +914 -0
- package/src/table-keyboard.ts +266 -0
- package/src/table-mount.ts +532 -0
- package/src/table-renderer.ts +350 -0
- package/src/tooltip.ts +120 -0
package/src/export.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Export utilities: serialize charts to SVG, PNG, or CSV.
|
|
3
|
+
*
|
|
4
|
+
* - SVG: serializes the rendered DOM element via XMLSerializer
|
|
5
|
+
* - PNG: renders SVG to canvas, then extracts as Blob
|
|
6
|
+
* - CSV: converts a data array to comma-separated text
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Serialize an SVG element to an XML string.
|
|
11
|
+
*
|
|
12
|
+
* @param svgElement - The rendered SVG element to serialize.
|
|
13
|
+
* @returns The SVG markup as a string.
|
|
14
|
+
*/
|
|
15
|
+
export function exportSVG(svgElement: SVGElement): string {
|
|
16
|
+
const serializer = new XMLSerializer();
|
|
17
|
+
return serializer.serializeToString(svgElement);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface PNGExportOptions {
|
|
21
|
+
/** DPI scaling factor. Defaults to 2 for retina-quality output. */
|
|
22
|
+
dpi?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Render an SVG element to a PNG Blob via a canvas.
|
|
27
|
+
*
|
|
28
|
+
* @param svgElement - The rendered SVG element.
|
|
29
|
+
* @param options - Optional DPI scaling.
|
|
30
|
+
* @returns A Promise resolving to the PNG Blob.
|
|
31
|
+
*/
|
|
32
|
+
export async function exportPNG(svgElement: SVGElement, options?: PNGExportOptions): Promise<Blob> {
|
|
33
|
+
const dpi = options?.dpi ?? 2;
|
|
34
|
+
const svgString = exportSVG(svgElement);
|
|
35
|
+
|
|
36
|
+
const width = parseFloat(svgElement.getAttribute('width') || '600');
|
|
37
|
+
const height = parseFloat(svgElement.getAttribute('height') || '400');
|
|
38
|
+
|
|
39
|
+
const canvas = document.createElement('canvas');
|
|
40
|
+
canvas.width = width * dpi;
|
|
41
|
+
canvas.height = height * dpi;
|
|
42
|
+
|
|
43
|
+
const ctx = canvas.getContext('2d');
|
|
44
|
+
if (!ctx) {
|
|
45
|
+
throw new Error('Canvas 2D context not available');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
ctx.scale(dpi, dpi);
|
|
49
|
+
|
|
50
|
+
const img = new Image();
|
|
51
|
+
const blob = new Blob([svgString], { type: 'image/svg+xml;charset=utf-8' });
|
|
52
|
+
const url = URL.createObjectURL(blob);
|
|
53
|
+
|
|
54
|
+
return new Promise<Blob>((resolve, reject) => {
|
|
55
|
+
img.onload = () => {
|
|
56
|
+
ctx.drawImage(img, 0, 0);
|
|
57
|
+
URL.revokeObjectURL(url);
|
|
58
|
+
|
|
59
|
+
canvas.toBlob((result) => {
|
|
60
|
+
if (result) {
|
|
61
|
+
resolve(result);
|
|
62
|
+
} else {
|
|
63
|
+
reject(new Error('Canvas toBlob returned null'));
|
|
64
|
+
}
|
|
65
|
+
}, 'image/png');
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
img.onerror = () => {
|
|
69
|
+
URL.revokeObjectURL(url);
|
|
70
|
+
reject(new Error('Failed to load SVG as image'));
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
img.src = url;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Convert an array of data objects to a CSV string.
|
|
79
|
+
*
|
|
80
|
+
* Uses the keys from the first row as column headers.
|
|
81
|
+
* Values are quoted if they contain commas, quotes, or newlines.
|
|
82
|
+
*
|
|
83
|
+
* @param data - Array of row objects.
|
|
84
|
+
* @returns CSV-formatted string.
|
|
85
|
+
*/
|
|
86
|
+
export function exportCSV(data: Record<string, unknown>[]): string {
|
|
87
|
+
if (data.length === 0) return '';
|
|
88
|
+
|
|
89
|
+
const headers = Object.keys(data[0]);
|
|
90
|
+
const rows = [headers.map(csvEscape).join(',')];
|
|
91
|
+
|
|
92
|
+
for (const row of data) {
|
|
93
|
+
const values = headers.map((h) => csvEscape(String(row[h] ?? '')));
|
|
94
|
+
rows.push(values.join(','));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return rows.join('\n');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function csvEscape(value: string): string {
|
|
101
|
+
if (value.includes(',') || value.includes('"') || value.includes('\n')) {
|
|
102
|
+
return `"${value.replace(/"/g, '""')}"`;
|
|
103
|
+
}
|
|
104
|
+
return value;
|
|
105
|
+
}
|