@samizdata/mozaic 0.0.0-stage → 0.2.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/LICENSE +674 -0
- package/README.md +44 -2
- package/dist/annotations/Arrow.svelte +50 -0
- package/dist/annotations/Arrow.svelte.d.ts +18 -0
- package/dist/annotations/Callout.svelte +87 -0
- package/dist/annotations/Callout.svelte.d.ts +22 -0
- package/dist/annotations/Label.svelte +55 -0
- package/dist/annotations/Label.svelte.d.ts +19 -0
- package/dist/annotations/RefBand.svelte +46 -0
- package/dist/annotations/RefBand.svelte.d.ts +18 -0
- package/dist/annotations/RefLine.svelte +72 -0
- package/dist/annotations/RefLine.svelte.d.ts +19 -0
- package/dist/annotations/callout-geometry.d.ts +23 -0
- package/dist/annotations/callout-geometry.js +45 -0
- package/dist/annotations/index.d.ts +6 -0
- package/dist/annotations/index.js +6 -0
- package/dist/core/Canvas.svelte +55 -0
- package/dist/core/Canvas.svelte.d.ts +19 -0
- package/dist/core/Facets.svelte +80 -0
- package/dist/core/Facets.svelte.d.ts +49 -0
- package/dist/core/Html.svelte +38 -0
- package/dist/core/Html.svelte.d.ts +13 -0
- package/dist/core/Plot.svelte +202 -0
- package/dist/core/Plot.svelte.d.ts +60 -0
- package/dist/core/Svg.svelte +58 -0
- package/dist/core/Svg.svelte.d.ts +17 -0
- package/dist/core/accessor.d.ts +5 -0
- package/dist/core/accessor.js +3 -0
- package/dist/core/context.d.ts +25 -0
- package/dist/core/context.js +69 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.js +9 -0
- package/dist/core/scales.d.ts +16 -0
- package/dist/core/scales.js +32 -0
- package/dist/core/types.d.ts +78 -0
- package/dist/core/types.js +1 -0
- package/dist/core/units.d.ts +6 -0
- package/dist/core/units.js +10 -0
- package/dist/embed/Embed.svelte +50 -0
- package/dist/embed/Embed.svelte.d.ts +14 -0
- package/dist/embed/code.d.ts +18 -0
- package/dist/embed/code.js +9 -0
- package/dist/embed/host.d.ts +6 -0
- package/dist/embed/host.js +20 -0
- package/dist/embed/index.d.ts +6 -0
- package/dist/embed/index.js +5 -0
- package/dist/embed/manifest.d.ts +16 -0
- package/dist/embed/manifest.js +1 -0
- package/dist/embed/params.svelte.d.ts +10 -0
- package/dist/embed/params.svelte.js +27 -0
- package/dist/embed/resize.d.ts +13 -0
- package/dist/embed/resize.js +28 -0
- package/dist/export/download.d.ts +13 -0
- package/dist/export/download.js +52 -0
- package/dist/export/figure-to-svg.d.ts +21 -0
- package/dist/export/figure-to-svg.js +187 -0
- package/dist/export/fonts.d.ts +26 -0
- package/dist/export/fonts.js +112 -0
- package/dist/export/index.d.ts +2 -0
- package/dist/export/index.js +2 -0
- package/dist/frame/Byline.svelte +26 -0
- package/dist/frame/Byline.svelte.d.ts +8 -0
- package/dist/frame/Chart.svelte +144 -0
- package/dist/frame/Chart.svelte.d.ts +37 -0
- package/dist/frame/ChartTools.svelte +259 -0
- package/dist/frame/ChartTools.svelte.d.ts +17 -0
- package/dist/frame/DataDownload.svelte +34 -0
- package/dist/frame/DataDownload.svelte.d.ts +34 -0
- package/dist/frame/DataTable.svelte +49 -0
- package/dist/frame/DataTable.svelte.d.ts +34 -0
- package/dist/frame/Footer.svelte +39 -0
- package/dist/frame/Footer.svelte.d.ts +10 -0
- package/dist/frame/Header.svelte +21 -0
- package/dist/frame/Header.svelte.d.ts +9 -0
- package/dist/frame/Logo.svelte +57 -0
- package/dist/frame/Logo.svelte.d.ts +8 -0
- package/dist/frame/Notes.svelte +29 -0
- package/dist/frame/Notes.svelte.d.ts +9 -0
- package/dist/frame/Source.svelte +29 -0
- package/dist/frame/Source.svelte.d.ts +9 -0
- package/dist/frame/Subtitle.svelte +22 -0
- package/dist/frame/Subtitle.svelte.d.ts +9 -0
- package/dist/frame/Title.svelte +31 -0
- package/dist/frame/Title.svelte.d.ts +9 -0
- package/dist/frame/index.d.ts +12 -0
- package/dist/frame/index.js +12 -0
- package/dist/guides/AxisX.svelte +101 -0
- package/dist/guides/AxisX.svelte.d.ts +22 -0
- package/dist/guides/AxisY.svelte +84 -0
- package/dist/guides/AxisY.svelte.d.ts +21 -0
- package/dist/guides/ColorRamp.svelte +93 -0
- package/dist/guides/ColorRamp.svelte.d.ts +25 -0
- package/dist/guides/GridX.svelte +35 -0
- package/dist/guides/GridX.svelte.d.ts +9 -0
- package/dist/guides/GridY.svelte +35 -0
- package/dist/guides/GridY.svelte.d.ts +9 -0
- package/dist/guides/Key.svelte +48 -0
- package/dist/guides/Key.svelte.d.ts +14 -0
- package/dist/guides/Legend.svelte +112 -0
- package/dist/guides/Legend.svelte.d.ts +26 -0
- package/dist/guides/SizeLegend.svelte +62 -0
- package/dist/guides/SizeLegend.svelte.d.ts +15 -0
- package/dist/guides/index.d.ts +9 -0
- package/dist/guides/index.js +9 -0
- package/dist/guides/ticks.d.ts +6 -0
- package/dist/guides/ticks.js +15 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +14 -0
- package/dist/interactive/ButtonGroup.svelte +98 -0
- package/dist/interactive/ButtonGroup.svelte.d.ts +42 -0
- package/dist/interactive/PointerArea.svelte +49 -0
- package/dist/interactive/PointerArea.svelte.d.ts +18 -0
- package/dist/interactive/Scroller.svelte +147 -0
- package/dist/interactive/Scroller.svelte.d.ts +54 -0
- package/dist/interactive/Tooltip.svelte +66 -0
- package/dist/interactive/Tooltip.svelte.d.ts +19 -0
- package/dist/interactive/index.d.ts +4 -0
- package/dist/interactive/index.js +4 -0
- package/dist/interactive/tooltip-position.d.ts +12 -0
- package/dist/interactive/tooltip-position.js +16 -0
- package/dist/locale/definitions.d.ts +145 -0
- package/dist/locale/definitions.js +138 -0
- package/dist/locale/index.d.ts +23 -0
- package/dist/locale/index.js +34 -0
- package/dist/marks/Arcs.svelte +93 -0
- package/dist/marks/Arcs.svelte.d.ts +47 -0
- package/dist/marks/Area.svelte +44 -0
- package/dist/marks/Area.svelte.d.ts +43 -0
- package/dist/marks/Arrows.svelte +93 -0
- package/dist/marks/Arrows.svelte.d.ts +50 -0
- package/dist/marks/Bars.svelte +111 -0
- package/dist/marks/Bars.svelte.d.ts +61 -0
- package/dist/marks/Beeswarm.svelte +84 -0
- package/dist/marks/Beeswarm.svelte.d.ts +42 -0
- package/dist/marks/Dumbbell.svelte +71 -0
- package/dist/marks/Dumbbell.svelte.d.ts +42 -0
- package/dist/marks/GeoPath.svelte +56 -0
- package/dist/marks/GeoPath.svelte.d.ts +42 -0
- package/dist/marks/Hemicycle.svelte +182 -0
- package/dist/marks/Hemicycle.svelte.d.ts +78 -0
- package/dist/marks/Intervals.svelte +128 -0
- package/dist/marks/Intervals.svelte.d.ts +60 -0
- package/dist/marks/Line.svelte +64 -0
- package/dist/marks/Line.svelte.d.ts +43 -0
- package/dist/marks/LineEndLabels.svelte +70 -0
- package/dist/marks/LineEndLabels.svelte.d.ts +41 -0
- package/dist/marks/Points.svelte +70 -0
- package/dist/marks/Points.svelte.d.ts +46 -0
- package/dist/marks/Rects.svelte +92 -0
- package/dist/marks/Rects.svelte.d.ts +47 -0
- package/dist/marks/Rules.svelte +46 -0
- package/dist/marks/Rules.svelte.d.ts +15 -0
- package/dist/marks/Slope.svelte +112 -0
- package/dist/marks/Slope.svelte.d.ts +41 -0
- package/dist/marks/Text.svelte +75 -0
- package/dist/marks/Text.svelte.d.ts +47 -0
- package/dist/marks/Treemap.svelte +89 -0
- package/dist/marks/Treemap.svelte.d.ts +45 -0
- package/dist/marks/Voronoi.svelte +61 -0
- package/dist/marks/Voronoi.svelte.d.ts +39 -0
- package/dist/marks/Waffle.svelte +69 -0
- package/dist/marks/Waffle.svelte.d.ts +41 -0
- package/dist/marks/bar-path.d.ts +5 -0
- package/dist/marks/bar-path.js +15 -0
- package/dist/marks/index.d.ts +20 -0
- package/dist/marks/index.js +20 -0
- package/dist/recipes/ArrowChart.svelte +247 -0
- package/dist/recipes/ArrowChart.svelte.d.ts +73 -0
- package/dist/recipes/BarChart.svelte +122 -0
- package/dist/recipes/BarChart.svelte.d.ts +58 -0
- package/dist/recipes/BarDotChart.svelte +204 -0
- package/dist/recipes/BarDotChart.svelte.d.ts +75 -0
- package/dist/recipes/ColumnChart.svelte +119 -0
- package/dist/recipes/ColumnChart.svelte.d.ts +56 -0
- package/dist/recipes/LineChart.svelte +159 -0
- package/dist/recipes/LineChart.svelte.d.ts +61 -0
- package/dist/recipes/MosaicChart.svelte +256 -0
- package/dist/recipes/MosaicChart.svelte.d.ts +83 -0
- package/dist/recipes/ScatterChart.svelte +167 -0
- package/dist/recipes/ScatterChart.svelte.d.ts +63 -0
- package/dist/recipes/StackedBarChart.svelte +222 -0
- package/dist/recipes/StackedBarChart.svelte.d.ts +83 -0
- package/dist/recipes/index.d.ts +10 -0
- package/dist/recipes/index.js +19 -0
- package/dist/styles/base.d.ts +5 -0
- package/dist/styles/base.js +102 -0
- package/dist/styles/check.d.ts +8 -0
- package/dist/styles/check.js +32 -0
- package/dist/styles/color.d.ts +11 -0
- package/dist/styles/color.js +29 -0
- package/dist/styles/index.d.ts +9 -0
- package/dist/styles/index.js +17 -0
- package/dist/styles/types.d.ts +90 -0
- package/dist/styles/types.js +1 -0
- package/dist/table/Sparkline.svelte +59 -0
- package/dist/table/Sparkline.svelte.d.ts +16 -0
- package/dist/table/Table.svelte +287 -0
- package/dist/table/Table.svelte.d.ts +44 -0
- package/dist/table/csv.d.ts +3 -0
- package/dist/table/csv.js +12 -0
- package/dist/table/index.d.ts +4 -0
- package/dist/table/index.js +3 -0
- package/dist/table/types.d.ts +26 -0
- package/dist/table/types.js +1 -0
- package/dist/transforms/bin.d.ts +15 -0
- package/dist/transforms/bin.js +18 -0
- package/dist/transforms/dodge.d.ts +10 -0
- package/dist/transforms/dodge.js +35 -0
- package/dist/transforms/group.d.ts +14 -0
- package/dist/transforms/group.js +14 -0
- package/dist/transforms/hemicycle.d.ts +30 -0
- package/dist/transforms/hemicycle.js +51 -0
- package/dist/transforms/index.d.ts +8 -0
- package/dist/transforms/index.js +8 -0
- package/dist/transforms/labels.d.ts +6 -0
- package/dist/transforms/labels.js +20 -0
- package/dist/transforms/smooth.d.ts +61 -0
- package/dist/transforms/smooth.js +105 -0
- package/dist/transforms/stack.d.ts +25 -0
- package/dist/transforms/stack.js +61 -0
- package/dist/transforms/treemap.d.ts +21 -0
- package/dist/transforms/treemap.js +31 -0
- package/package.json +152 -3
- package/src/lib/recipes/ArrowChart.svelte +247 -0
- package/src/lib/recipes/BarChart.svelte +122 -0
- package/src/lib/recipes/BarDotChart.svelte +204 -0
- package/src/lib/recipes/ColumnChart.svelte +119 -0
- package/src/lib/recipes/LineChart.svelte +159 -0
- package/src/lib/recipes/MosaicChart.svelte +256 -0
- package/src/lib/recipes/ScatterChart.svelte +167 -0
- package/src/lib/recipes/StackedBarChart.svelte +222 -0
- package/src/lib/recipes/index.ts +20 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web fonts for exported images. A PNG is drawn from the SVG as an image, and an SVG image cannot load
|
|
3
|
+
* the page's fonts, so the `@font-face` rules the chart uses are copied into it with their files inlined.
|
|
4
|
+
*/
|
|
5
|
+
export interface UsedFonts {
|
|
6
|
+
/** Font family names, without quotes. */
|
|
7
|
+
families: Set<string>;
|
|
8
|
+
/** Numeric weights ("400", "600"). */
|
|
9
|
+
weights: Set<number>;
|
|
10
|
+
/** Every character of the chart's text. */
|
|
11
|
+
text: string;
|
|
12
|
+
}
|
|
13
|
+
/** Family names from a CSS `font-family` list. */
|
|
14
|
+
export declare function familyNames(list: string): string[];
|
|
15
|
+
/** Whether a `unicode-range` (U+0000-00FF, U+0131, U+04??) covers any of the characters. */
|
|
16
|
+
export declare function coversText(range: string, text: string): boolean;
|
|
17
|
+
/** The `@font-face` rules in `css` that the chart uses, with their `url(…)`s made absolute against `base`. */
|
|
18
|
+
export declare function pickFontFaces(css: string, base: string, used: UsedFonts): string[];
|
|
19
|
+
/** Families, weights and characters of the text in a rendered chart. */
|
|
20
|
+
export declare function usedFonts(figure: HTMLElement, skip: (el: Element) => boolean): UsedFonts;
|
|
21
|
+
/**
|
|
22
|
+
* A `<style>` with the page's `@font-face` rules that the chart uses, font files inlined, for an SVG drawn as
|
|
23
|
+
* an image. Stylesheets the page cannot read (another origin) are fetched; fonts that cannot be fetched are
|
|
24
|
+
* left out, and the image falls back to the system's fonts for them.
|
|
25
|
+
*/
|
|
26
|
+
export declare function embeddedFontStyle(used: UsedFonts): Promise<string>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web fonts for exported images. A PNG is drawn from the SVG as an image, and an SVG image cannot load
|
|
3
|
+
* the page's fonts, so the `@font-face` rules the chart uses are copied into it with their files inlined.
|
|
4
|
+
*/
|
|
5
|
+
const GENERIC = new Set(['serif', 'sans-serif', 'monospace', 'cursive', 'fantasy', 'system-ui', 'ui-monospace']);
|
|
6
|
+
/** Family names from a CSS `font-family` list. */
|
|
7
|
+
export function familyNames(list) {
|
|
8
|
+
return list
|
|
9
|
+
.split(',')
|
|
10
|
+
.map((f) => f.trim().replace(/^["']|["']$/g, ''))
|
|
11
|
+
.filter((f) => f && !GENERIC.has(f));
|
|
12
|
+
}
|
|
13
|
+
/** Whether a `unicode-range` (U+0000-00FF, U+0131, U+04??) covers any of the characters. */
|
|
14
|
+
export function coversText(range, text) {
|
|
15
|
+
const spans = range.split(',').map((part) => {
|
|
16
|
+
const m = part.trim().replace(/^U\+/i, '');
|
|
17
|
+
const [lo, hi = lo] = m.includes('-') ? m.split('-') : [m.replace(/\?/g, '0'), m.replace(/\?/g, 'F')];
|
|
18
|
+
return [parseInt(lo, 16), parseInt(hi, 16)];
|
|
19
|
+
});
|
|
20
|
+
for (const ch of text) {
|
|
21
|
+
const code = ch.codePointAt(0);
|
|
22
|
+
if (spans.some(([lo, hi]) => code >= lo && code <= hi))
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
/** The `@font-face` rules in `css` that the chart uses, with their `url(…)`s made absolute against `base`. */
|
|
28
|
+
export function pickFontFaces(css, base, used) {
|
|
29
|
+
const families = new Set([...used.families].map((f) => f.toLowerCase()));
|
|
30
|
+
const picked = [];
|
|
31
|
+
for (const [rule] of css.matchAll(/@font-face\s*\{[^}]*\}/g)) {
|
|
32
|
+
const family = rule.match(/font-family\s*:\s*([^;}]+)/i)?.[1];
|
|
33
|
+
if (!family || !familyNames(family).some((f) => families.has(f.toLowerCase())))
|
|
34
|
+
continue;
|
|
35
|
+
const weight = rule.match(/font-weight\s*:\s*([^;}]+)/i)?.[1].trim();
|
|
36
|
+
if (weight) {
|
|
37
|
+
const [lo, hi = lo] = weight.split(/\s+/).map((w) => (w === 'normal' ? 400 : w === 'bold' ? 700 : Number(w)));
|
|
38
|
+
if (![...used.weights].some((w) => w >= lo && w <= hi))
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const range = rule.match(/unicode-range\s*:\s*([^;}]+)/i)?.[1];
|
|
42
|
+
if (range && !coversText(range, used.text))
|
|
43
|
+
continue;
|
|
44
|
+
picked.push(rule.replace(/url\(\s*(["']?)([^)"']+)\1\s*\)/g, (_, __, url) => `url("${new URL(url, base).href}")`));
|
|
45
|
+
}
|
|
46
|
+
return picked;
|
|
47
|
+
}
|
|
48
|
+
/** Families, weights and characters of the text in a rendered chart. */
|
|
49
|
+
export function usedFonts(figure, skip) {
|
|
50
|
+
const used = { families: new Set(), weights: new Set(), text: '' };
|
|
51
|
+
const walker = document.createTreeWalker(figure, NodeFilter.SHOW_TEXT);
|
|
52
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
53
|
+
const el = node.parentElement;
|
|
54
|
+
if (!el || !node.textContent?.trim() || skip(el))
|
|
55
|
+
continue;
|
|
56
|
+
const cs = getComputedStyle(el);
|
|
57
|
+
for (const f of familyNames(cs.fontFamily))
|
|
58
|
+
used.families.add(f);
|
|
59
|
+
used.weights.add(Number(cs.fontWeight) || 400);
|
|
60
|
+
used.text += node.textContent;
|
|
61
|
+
}
|
|
62
|
+
return used;
|
|
63
|
+
}
|
|
64
|
+
async function dataUrl(url) {
|
|
65
|
+
const blob = await (await fetch(url)).blob();
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
const reader = new FileReader();
|
|
68
|
+
reader.onload = () => resolve(String(reader.result));
|
|
69
|
+
reader.onerror = () => reject(reader.error);
|
|
70
|
+
reader.readAsDataURL(blob);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A `<style>` with the page's `@font-face` rules that the chart uses, font files inlined, for an SVG drawn as
|
|
75
|
+
* an image. Stylesheets the page cannot read (another origin) are fetched; fonts that cannot be fetched are
|
|
76
|
+
* left out, and the image falls back to the system's fonts for them.
|
|
77
|
+
*/
|
|
78
|
+
export async function embeddedFontStyle(used) {
|
|
79
|
+
// A set: each chart on a page adds the same stylesheet, and pages may load a font twice.
|
|
80
|
+
const rules = new Set();
|
|
81
|
+
const read = new Set();
|
|
82
|
+
for (const sheet of document.styleSheets) {
|
|
83
|
+
if (sheet.href && read.has(sheet.href))
|
|
84
|
+
continue;
|
|
85
|
+
if (sheet.href)
|
|
86
|
+
read.add(sheet.href);
|
|
87
|
+
const base = sheet.href ?? document.baseURI;
|
|
88
|
+
let css = '';
|
|
89
|
+
try {
|
|
90
|
+
css = [...sheet.cssRules].map((r) => r.cssText).join('\n');
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
if (sheet.href)
|
|
94
|
+
css = await fetch(sheet.href).then((r) => (r.ok ? r.text() : ''), () => '');
|
|
95
|
+
}
|
|
96
|
+
for (const rule of pickFontFaces(css, base, used))
|
|
97
|
+
rules.add(rule);
|
|
98
|
+
}
|
|
99
|
+
const inlined = await Promise.all([...rules].map(async (rule) => {
|
|
100
|
+
const urls = [...rule.matchAll(/url\("([^"]+)"\)/g)].map((m) => m[1]);
|
|
101
|
+
let out = rule;
|
|
102
|
+
for (const url of urls) {
|
|
103
|
+
const data = await dataUrl(url).catch(() => null);
|
|
104
|
+
if (!data)
|
|
105
|
+
return '';
|
|
106
|
+
out = out.replace(url, data);
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}));
|
|
110
|
+
const css = inlined.filter(Boolean).join('\n');
|
|
111
|
+
return css ? `<style>${css}</style>` : '';
|
|
112
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
"Graphic: …" credit line, with the label in the chart's language.
|
|
4
|
+
-->
|
|
5
|
+
<script lang="ts">
|
|
6
|
+
import type { Snippet } from 'svelte';
|
|
7
|
+
import { useChart } from '../core/context.js';
|
|
8
|
+
|
|
9
|
+
let { children }: { children?: Snippet } = $props();
|
|
10
|
+
|
|
11
|
+
const chart = useChart();
|
|
12
|
+
</script>
|
|
13
|
+
|
|
14
|
+
<p class="mz-byline"><span class="mz-label">{chart.locale.strings.graphic}:</span> {@render children?.()}</p>
|
|
15
|
+
|
|
16
|
+
<style>
|
|
17
|
+
.mz-byline {
|
|
18
|
+
margin: 0;
|
|
19
|
+
}
|
|
20
|
+
.mz-label {
|
|
21
|
+
font-family: var(--mz-font-label);
|
|
22
|
+
font-weight: var(--mz-weight-label);
|
|
23
|
+
text-transform: var(--mz-label-transform);
|
|
24
|
+
letter-spacing: var(--mz-label-tracking);
|
|
25
|
+
}
|
|
26
|
+
</style>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
type $$ComponentProps = {
|
|
3
|
+
children?: Snippet;
|
|
4
|
+
};
|
|
5
|
+
/** "Graphic: …" credit line, with the label in the chart's language. */
|
|
6
|
+
declare const Byline: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
7
|
+
type Byline = ReturnType<typeof Byline>;
|
|
8
|
+
export default Byline;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
The outer frame of every chart. Applies the house style and locale, measures
|
|
4
|
+
its width (for breakpoints) and shares theme, locale, medium and breakpoint
|
|
5
|
+
with everything inside via `useChart()`, `useTheme()` and `useLocale()`.
|
|
6
|
+
|
|
7
|
+
It does not care what renders inside: a mozaic `<Plot>`, hand-written SVG,
|
|
8
|
+
a map or a table.
|
|
9
|
+
-->
|
|
10
|
+
<script lang="ts">
|
|
11
|
+
import type { Snippet } from 'svelte';
|
|
12
|
+
import { setChartContext, useDownloads } from '../core/context.js';
|
|
13
|
+
import { createLocale, type LocaleCode } from '../locale/index.js';
|
|
14
|
+
import { base, breakpointFor, tokensToStyle } from '../styles/index.js';
|
|
15
|
+
import type { Medium, Theme } from '../styles/types.js';
|
|
16
|
+
import type { ChartEmbed } from '../embed/code.js';
|
|
17
|
+
import ChartTools from './ChartTools.svelte';
|
|
18
|
+
|
|
19
|
+
interface Props {
|
|
20
|
+
/** House style: a Theme, from a brand or `extendTheme()`. Defaults to the neutral `base`. */
|
|
21
|
+
style?: Theme;
|
|
22
|
+
/** Defaults to the style's default locale. */
|
|
23
|
+
locale?: LocaleCode;
|
|
24
|
+
medium?: Medium;
|
|
25
|
+
/** Text description for screen readers. Describe what the chart shows, not how it looks. */
|
|
26
|
+
alt?: string;
|
|
27
|
+
/** Fixed width in px (print: use `mm()`). Otherwise the chart fills its container. */
|
|
28
|
+
width?: number;
|
|
29
|
+
/** Add the style's web-font stylesheet to the page head. Turn off when the host page already loads the fonts. */
|
|
30
|
+
loadFonts?: boolean;
|
|
31
|
+
/** The chart's embed page: shows an "Embed" button under the chart, with the code to put it on another site. */
|
|
32
|
+
embed?: ChartEmbed;
|
|
33
|
+
/**
|
|
34
|
+
* Show a "Download" button under the chart (PNG or SVG). A string is the file name, without extension;
|
|
35
|
+
* otherwise it comes from the title. Defaults to `setDownloads()` above the chart (off without it).
|
|
36
|
+
*/
|
|
37
|
+
download?: boolean | string;
|
|
38
|
+
class?: string;
|
|
39
|
+
children?: Snippet;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let {
|
|
43
|
+
style = base,
|
|
44
|
+
locale,
|
|
45
|
+
medium = 'web',
|
|
46
|
+
alt,
|
|
47
|
+
width: fixedWidth,
|
|
48
|
+
loadFonts = true,
|
|
49
|
+
embed,
|
|
50
|
+
download,
|
|
51
|
+
class: className = '',
|
|
52
|
+
children
|
|
53
|
+
}: Props = $props();
|
|
54
|
+
|
|
55
|
+
const uid = $props.id();
|
|
56
|
+
const downloads = useDownloads();
|
|
57
|
+
const tools = $state<{ table: Snippet | null }>({ table: null });
|
|
58
|
+
const theme = $derived(style);
|
|
59
|
+
const loc = $derived(createLocale(locale ?? theme.defaultLocale));
|
|
60
|
+
|
|
61
|
+
let measuredWidth = $state(0);
|
|
62
|
+
const measured = $derived(fixedWidth != null || measuredWidth > 0);
|
|
63
|
+
const width = $derived(fixedWidth ?? (measuredWidth || theme.plot.initialWidth));
|
|
64
|
+
const breakpoint = $derived(breakpointFor(width, theme.breakpoints));
|
|
65
|
+
const ids = {
|
|
66
|
+
title: `${uid}-title`,
|
|
67
|
+
desc: `${uid}-desc`,
|
|
68
|
+
hasTitle: false,
|
|
69
|
+
get hasDesc() {
|
|
70
|
+
return !!alt;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
setChartContext({
|
|
75
|
+
get theme() {
|
|
76
|
+
return theme;
|
|
77
|
+
},
|
|
78
|
+
get locale() {
|
|
79
|
+
return loc;
|
|
80
|
+
},
|
|
81
|
+
get medium() {
|
|
82
|
+
return medium;
|
|
83
|
+
},
|
|
84
|
+
get breakpoint() {
|
|
85
|
+
return breakpoint;
|
|
86
|
+
},
|
|
87
|
+
get width() {
|
|
88
|
+
return width;
|
|
89
|
+
},
|
|
90
|
+
get measured() {
|
|
91
|
+
return measured;
|
|
92
|
+
},
|
|
93
|
+
ids,
|
|
94
|
+
tools
|
|
95
|
+
});
|
|
96
|
+
</script>
|
|
97
|
+
|
|
98
|
+
<svelte:head>
|
|
99
|
+
{#if loadFonts && theme.fonts}
|
|
100
|
+
<link rel="stylesheet" href={theme.fonts.stylesheet} />
|
|
101
|
+
{/if}
|
|
102
|
+
</svelte:head>
|
|
103
|
+
|
|
104
|
+
<figure
|
|
105
|
+
class="mz-chart {className}"
|
|
106
|
+
data-mz-style={theme.name}
|
|
107
|
+
data-mz-medium={medium}
|
|
108
|
+
data-mz-breakpoint={breakpoint}
|
|
109
|
+
lang={loc.lang}
|
|
110
|
+
aria-labelledby={ids.title}
|
|
111
|
+
style={tokensToStyle(
|
|
112
|
+
medium === 'print' ? ({ ...theme.tokens, ...theme.print.tokens } as Theme['tokens']) : theme.tokens
|
|
113
|
+
)}
|
|
114
|
+
style:width={fixedWidth != null ? `${fixedWidth}px` : undefined}
|
|
115
|
+
bind:clientWidth={measuredWidth}
|
|
116
|
+
>
|
|
117
|
+
{@render children?.()}
|
|
118
|
+
<ChartTools {embed} download={download ?? downloads} table={tools.table} />
|
|
119
|
+
{#if alt}<p id={ids.desc} class="mz-visually-hidden">{alt}</p>{/if}
|
|
120
|
+
</figure>
|
|
121
|
+
|
|
122
|
+
<style>
|
|
123
|
+
.mz-chart {
|
|
124
|
+
box-sizing: border-box;
|
|
125
|
+
margin: 0;
|
|
126
|
+
display: flex;
|
|
127
|
+
flex-direction: column;
|
|
128
|
+
gap: var(--mz-gap);
|
|
129
|
+
font-family: var(--mz-font-body);
|
|
130
|
+
font-size: var(--mz-size-body);
|
|
131
|
+
color: var(--mz-color-text);
|
|
132
|
+
background: var(--mz-color-bg);
|
|
133
|
+
font-variant-numeric: tabular-nums;
|
|
134
|
+
}
|
|
135
|
+
.mz-visually-hidden {
|
|
136
|
+
position: absolute;
|
|
137
|
+
width: 1px;
|
|
138
|
+
height: 1px;
|
|
139
|
+
margin: -1px;
|
|
140
|
+
overflow: hidden;
|
|
141
|
+
clip-path: inset(50%);
|
|
142
|
+
white-space: nowrap;
|
|
143
|
+
}
|
|
144
|
+
</style>
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import { type LocaleCode } from '../locale/index.js';
|
|
3
|
+
import type { Medium, Theme } from '../styles/types.js';
|
|
4
|
+
import type { ChartEmbed } from '../embed/code.js';
|
|
5
|
+
interface Props {
|
|
6
|
+
/** House style: a Theme, from a brand or `extendTheme()`. Defaults to the neutral `base`. */
|
|
7
|
+
style?: Theme;
|
|
8
|
+
/** Defaults to the style's default locale. */
|
|
9
|
+
locale?: LocaleCode;
|
|
10
|
+
medium?: Medium;
|
|
11
|
+
/** Text description for screen readers. Describe what the chart shows, not how it looks. */
|
|
12
|
+
alt?: string;
|
|
13
|
+
/** Fixed width in px (print: use `mm()`). Otherwise the chart fills its container. */
|
|
14
|
+
width?: number;
|
|
15
|
+
/** Add the style's web-font stylesheet to the page head. Turn off when the host page already loads the fonts. */
|
|
16
|
+
loadFonts?: boolean;
|
|
17
|
+
/** The chart's embed page: shows an "Embed" button under the chart, with the code to put it on another site. */
|
|
18
|
+
embed?: ChartEmbed;
|
|
19
|
+
/**
|
|
20
|
+
* Show a "Download" button under the chart (PNG or SVG). A string is the file name, without extension;
|
|
21
|
+
* otherwise it comes from the title. Defaults to `setDownloads()` above the chart (off without it).
|
|
22
|
+
*/
|
|
23
|
+
download?: boolean | string;
|
|
24
|
+
class?: string;
|
|
25
|
+
children?: Snippet;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The outer frame of every chart. Applies the house style and locale, measures
|
|
29
|
+
* its width (for breakpoints) and shares theme, locale, medium and breakpoint
|
|
30
|
+
* with everything inside via `useChart()`, `useTheme()` and `useLocale()`.
|
|
31
|
+
*
|
|
32
|
+
* It does not care what renders inside: a mozaic `<Plot>`, hand-written SVG,
|
|
33
|
+
* a map or a table.
|
|
34
|
+
*/
|
|
35
|
+
declare const Chart: import("svelte").Component<Props, {}, "">;
|
|
36
|
+
type Chart = ReturnType<typeof Chart>;
|
|
37
|
+
export default Chart;
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
Icon buttons under a chart: "Show the data" (`<DataTable>` in the chart) opens its table; "Embed" (`<Chart embed>`) opens the iframe code that puts the chart on another
|
|
4
|
+
site, with a button that copies it; "Download" (`<Chart download>`, or `setDownloads()` above it) saves the
|
|
5
|
+
chart as PNG or SVG. Hidden in print and in exports; inside `<Embed>` only the data is offered.
|
|
6
|
+
-->
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
import type { Snippet } from 'svelte';
|
|
9
|
+
import { useChart, useEmbedded } from '../core/context.js';
|
|
10
|
+
import { embedCode, type ChartEmbed } from '../embed/code.js';
|
|
11
|
+
import { downloadPng, downloadSvg, fileName } from '../export/download.js';
|
|
12
|
+
|
|
13
|
+
interface Props {
|
|
14
|
+
embed?: ChartEmbed;
|
|
15
|
+
/** Offer PNG and SVG downloads; a string is the file name (without extension), else the title's. */
|
|
16
|
+
download?: boolean | string;
|
|
17
|
+
/** The chart's data table, from `<DataTable>`. */
|
|
18
|
+
table?: Snippet | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let { embed: embedProp, download: downloadProp = false, table }: Props = $props();
|
|
22
|
+
|
|
23
|
+
const chart = useChart();
|
|
24
|
+
const embedded = useEmbedded();
|
|
25
|
+
// An embedded chart does not offer its own embed code or downloads.
|
|
26
|
+
const embed = $derived(embedded ? undefined : embedProp);
|
|
27
|
+
const download = $derived(embedded ? false : downloadProp);
|
|
28
|
+
const strings = $derived(chart.locale.strings);
|
|
29
|
+
const uid = $props.id();
|
|
30
|
+
|
|
31
|
+
let tools = $state<HTMLDivElement>();
|
|
32
|
+
let textarea = $state<HTMLTextAreaElement>();
|
|
33
|
+
let open = $state<'table' | 'embed' | 'download' | null>(null);
|
|
34
|
+
/** Read from the page when the code is opened: URLs need the page's address, defaults its title and height. */
|
|
35
|
+
let page = $state<{ base: string; title: string; height?: number }>();
|
|
36
|
+
let copied = $state(false);
|
|
37
|
+
|
|
38
|
+
const url = (path: string) => (page ? new URL(path, page.base).href : path);
|
|
39
|
+
const code = $derived(
|
|
40
|
+
embed
|
|
41
|
+
? embedCode({
|
|
42
|
+
src: url(embed.src),
|
|
43
|
+
title: embed.title ?? page?.title ?? '',
|
|
44
|
+
scriptSrc: embed.scriptSrc && url(embed.scriptSrc),
|
|
45
|
+
height: embed.height ?? page?.height
|
|
46
|
+
})
|
|
47
|
+
: ''
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const figure = () => tools?.closest('.mz-chart') as HTMLElement | null;
|
|
51
|
+
const title = () => document.getElementById(chart.ids.title)?.textContent?.trim() ?? '';
|
|
52
|
+
|
|
53
|
+
function toggle(tool: 'table' | 'embed' | 'download') {
|
|
54
|
+
open = open === tool ? null : tool;
|
|
55
|
+
if (open !== 'embed') return;
|
|
56
|
+
const fig = figure();
|
|
57
|
+
// The embed page shows the chart without these controls: measure down to what comes before them.
|
|
58
|
+
const above = tools?.previousElementSibling;
|
|
59
|
+
page = {
|
|
60
|
+
base: document.baseURI,
|
|
61
|
+
title: title(),
|
|
62
|
+
height:
|
|
63
|
+
fig && above ? Math.ceil(above.getBoundingClientRect().bottom - fig.getBoundingClientRect().top) : undefined
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function copy() {
|
|
68
|
+
try {
|
|
69
|
+
await navigator.clipboard.writeText(code);
|
|
70
|
+
copied = true;
|
|
71
|
+
setTimeout(() => (copied = false), 2000);
|
|
72
|
+
} catch {
|
|
73
|
+
// No clipboard (an insecure page, or permission refused): select the code to copy by hand.
|
|
74
|
+
textarea?.select();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function save(format: 'png' | 'svg') {
|
|
79
|
+
const fig = figure();
|
|
80
|
+
if (!fig) return;
|
|
81
|
+
const name = `${typeof download === 'string' ? download : fileName(title())}.${format}`;
|
|
82
|
+
if (format === 'svg') downloadSvg(fig, name);
|
|
83
|
+
else void downloadPng(fig, name);
|
|
84
|
+
}
|
|
85
|
+
</script>
|
|
86
|
+
|
|
87
|
+
{#if chart.medium !== 'print' && (table || embed || download)}
|
|
88
|
+
<div class="mz-chart-tools" bind:this={tools}>
|
|
89
|
+
<div class="mz-chart-tools-row">
|
|
90
|
+
{#if table}
|
|
91
|
+
<button
|
|
92
|
+
type="button"
|
|
93
|
+
class="mz-tool"
|
|
94
|
+
aria-label={strings.showData}
|
|
95
|
+
title={strings.showData}
|
|
96
|
+
aria-expanded={open === 'table'}
|
|
97
|
+
aria-controls="{uid}-table"
|
|
98
|
+
onclick={() => toggle('table')}
|
|
99
|
+
>
|
|
100
|
+
<svg viewBox="0 0 16 16" aria-hidden="true"
|
|
101
|
+
><rect x="2" y="2.5" width="12" height="11" rx="1" /><path d="M2 6.5h12M2 10h12M6.5 6.5v7" /></svg
|
|
102
|
+
>
|
|
103
|
+
</button>
|
|
104
|
+
{/if}
|
|
105
|
+
{#if embed}
|
|
106
|
+
<button
|
|
107
|
+
type="button"
|
|
108
|
+
class="mz-tool"
|
|
109
|
+
aria-label={strings.embed}
|
|
110
|
+
title={strings.embed}
|
|
111
|
+
aria-expanded={open === 'embed'}
|
|
112
|
+
aria-controls="{uid}-embed"
|
|
113
|
+
onclick={() => toggle('embed')}
|
|
114
|
+
>
|
|
115
|
+
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M5.5 4 1.5 8l4 4M10.5 4l4 4-4 4" /></svg>
|
|
116
|
+
</button>
|
|
117
|
+
{/if}
|
|
118
|
+
{#if download}
|
|
119
|
+
<button
|
|
120
|
+
type="button"
|
|
121
|
+
class="mz-tool"
|
|
122
|
+
aria-label={strings.downloadImage}
|
|
123
|
+
title={strings.downloadImage}
|
|
124
|
+
aria-expanded={open === 'download'}
|
|
125
|
+
aria-controls="{uid}-download"
|
|
126
|
+
onclick={() => toggle('download')}
|
|
127
|
+
>
|
|
128
|
+
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M8 2v8.5M4.5 7 8 10.5 11.5 7M2.5 13.5h11" /></svg>
|
|
129
|
+
</button>
|
|
130
|
+
{/if}
|
|
131
|
+
</div>
|
|
132
|
+
{#if table}
|
|
133
|
+
<div class="mz-data-table" id="{uid}-table" hidden={open !== 'table'}>
|
|
134
|
+
{@render table()}
|
|
135
|
+
</div>
|
|
136
|
+
{/if}
|
|
137
|
+
{#if embed}
|
|
138
|
+
<div class="mz-embed-code" id="{uid}-embed" hidden={open !== 'embed'}>
|
|
139
|
+
<textarea
|
|
140
|
+
bind:this={textarea}
|
|
141
|
+
readonly
|
|
142
|
+
rows="4"
|
|
143
|
+
spellcheck="false"
|
|
144
|
+
aria-label={strings.embedCode}
|
|
145
|
+
value={code}
|
|
146
|
+
onfocus={(e) => e.currentTarget.select()}></textarea>
|
|
147
|
+
<div class="mz-chart-tools-actions">
|
|
148
|
+
<button type="button" class="mz-primary" onclick={copy}>{strings.copy}</button>
|
|
149
|
+
<span role="status">{copied ? strings.copied : ''}</span>
|
|
150
|
+
</div>
|
|
151
|
+
</div>
|
|
152
|
+
{/if}
|
|
153
|
+
{#if download}
|
|
154
|
+
<div
|
|
155
|
+
class="mz-image-download mz-chart-tools-actions"
|
|
156
|
+
id="{uid}-download"
|
|
157
|
+
role="group"
|
|
158
|
+
aria-label={strings.downloadImage}
|
|
159
|
+
hidden={open !== 'download'}
|
|
160
|
+
>
|
|
161
|
+
<button type="button" class="mz-primary" onclick={() => save('png')}>PNG</button>
|
|
162
|
+
<button type="button" class="mz-primary" onclick={() => save('svg')}>SVG</button>
|
|
163
|
+
</div>
|
|
164
|
+
{/if}
|
|
165
|
+
</div>
|
|
166
|
+
{/if}
|
|
167
|
+
|
|
168
|
+
<style>
|
|
169
|
+
.mz-chart-tools {
|
|
170
|
+
display: flex;
|
|
171
|
+
flex-direction: column;
|
|
172
|
+
gap: 0.5rem;
|
|
173
|
+
font-family: var(--mz-font-label);
|
|
174
|
+
font-size: var(--mz-size-small);
|
|
175
|
+
color: var(--mz-color-muted);
|
|
176
|
+
}
|
|
177
|
+
/* Above the panels' own display. */
|
|
178
|
+
.mz-chart-tools [hidden] {
|
|
179
|
+
display: none;
|
|
180
|
+
}
|
|
181
|
+
.mz-chart-tools-row {
|
|
182
|
+
display: flex;
|
|
183
|
+
gap: 0.375rem;
|
|
184
|
+
}
|
|
185
|
+
.mz-tool {
|
|
186
|
+
display: inline-grid;
|
|
187
|
+
place-items: center;
|
|
188
|
+
width: 1.75rem;
|
|
189
|
+
height: 1.75rem;
|
|
190
|
+
padding: 0;
|
|
191
|
+
border: 1px solid var(--mz-color-border, var(--mz-color-grid));
|
|
192
|
+
border-radius: var(--mz-radius);
|
|
193
|
+
background: var(--mz-color-bg);
|
|
194
|
+
color: var(--mz-color-muted);
|
|
195
|
+
cursor: pointer;
|
|
196
|
+
}
|
|
197
|
+
.mz-tool:hover,
|
|
198
|
+
.mz-tool[aria-expanded='true'] {
|
|
199
|
+
color: var(--mz-color-text);
|
|
200
|
+
border-color: var(--mz-color-axis);
|
|
201
|
+
}
|
|
202
|
+
.mz-tool svg {
|
|
203
|
+
width: 1rem;
|
|
204
|
+
height: 1rem;
|
|
205
|
+
fill: none;
|
|
206
|
+
stroke: currentColor;
|
|
207
|
+
stroke-width: 1.5;
|
|
208
|
+
stroke-linecap: round;
|
|
209
|
+
stroke-linejoin: round;
|
|
210
|
+
}
|
|
211
|
+
button:focus-visible,
|
|
212
|
+
textarea:focus-visible {
|
|
213
|
+
outline: 2px solid var(--mz-color-accent);
|
|
214
|
+
outline-offset: 2px;
|
|
215
|
+
}
|
|
216
|
+
.mz-data-table {
|
|
217
|
+
font-family: var(--mz-font-body);
|
|
218
|
+
color: var(--mz-color-text);
|
|
219
|
+
}
|
|
220
|
+
.mz-embed-code {
|
|
221
|
+
display: flex;
|
|
222
|
+
flex-direction: column;
|
|
223
|
+
gap: 0.5rem;
|
|
224
|
+
}
|
|
225
|
+
textarea {
|
|
226
|
+
box-sizing: border-box;
|
|
227
|
+
width: 100%;
|
|
228
|
+
padding: 0.5rem;
|
|
229
|
+
border: 1px solid var(--mz-color-grid);
|
|
230
|
+
border-radius: var(--mz-radius);
|
|
231
|
+
background: var(--mz-color-bg);
|
|
232
|
+
color: var(--mz-color-text);
|
|
233
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
234
|
+
font-size: 0.75rem;
|
|
235
|
+
line-height: 1.4;
|
|
236
|
+
resize: vertical;
|
|
237
|
+
word-break: break-all;
|
|
238
|
+
}
|
|
239
|
+
.mz-chart-tools-actions {
|
|
240
|
+
display: flex;
|
|
241
|
+
align-items: center;
|
|
242
|
+
gap: 0.75rem;
|
|
243
|
+
}
|
|
244
|
+
.mz-image-download {
|
|
245
|
+
gap: 0.5rem;
|
|
246
|
+
}
|
|
247
|
+
.mz-primary {
|
|
248
|
+
padding: 0.3rem 0.75rem;
|
|
249
|
+
border: 0;
|
|
250
|
+
border-radius: var(--mz-radius);
|
|
251
|
+
background: var(--mz-color-accent);
|
|
252
|
+
color: #ffffff;
|
|
253
|
+
font: inherit;
|
|
254
|
+
font-weight: var(--mz-weight-label);
|
|
255
|
+
text-transform: var(--mz-control-transform);
|
|
256
|
+
letter-spacing: var(--mz-control-tracking);
|
|
257
|
+
cursor: pointer;
|
|
258
|
+
}
|
|
259
|
+
</style>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import { type ChartEmbed } from '../embed/code.js';
|
|
3
|
+
interface Props {
|
|
4
|
+
embed?: ChartEmbed;
|
|
5
|
+
/** Offer PNG and SVG downloads; a string is the file name (without extension), else the title's. */
|
|
6
|
+
download?: boolean | string;
|
|
7
|
+
/** The chart's data table, from `<DataTable>`. */
|
|
8
|
+
table?: Snippet | null;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Icon buttons under a chart: "Show the data" (`<DataTable>` in the chart) opens its table; "Embed" (`<Chart embed>`) opens the iframe code that puts the chart on another
|
|
12
|
+
* site, with a button that copies it; "Download" (`<Chart download>`, or `setDownloads()` above it) saves the
|
|
13
|
+
* chart as PNG or SVG. Hidden in print and in exports; inside `<Embed>` only the data is offered.
|
|
14
|
+
*/
|
|
15
|
+
declare const ChartTools: import("svelte").Component<Props, {}, "">;
|
|
16
|
+
type ChartTools = ReturnType<typeof ChartTools>;
|
|
17
|
+
export default ChartTools;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
A "Download the data (CSV)" link built from the chart's rows. Works without
|
|
4
|
+
JavaScript (the CSV is inlined as a data URL). Hidden in print.
|
|
5
|
+
-->
|
|
6
|
+
<script lang="ts" generics="T">
|
|
7
|
+
import { useChart } from '../core/context.js';
|
|
8
|
+
import { toCSV } from '../table/csv.js';
|
|
9
|
+
import type { Column } from '../table/types.js';
|
|
10
|
+
|
|
11
|
+
interface Props {
|
|
12
|
+
data: T[];
|
|
13
|
+
columns: Pick<Column<T>, 'label' | 'value'>[];
|
|
14
|
+
/** File name without extension. */
|
|
15
|
+
filename?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let { data, columns, filename = 'data' }: Props = $props();
|
|
19
|
+
|
|
20
|
+
const chart = useChart();
|
|
21
|
+
const href = $derived(`data:text/csv;charset=utf-8,${encodeURIComponent('' + toCSV(data, columns))}`);
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
{#if chart.medium !== 'print'}
|
|
25
|
+
<a class="mz-data-download" {href} download="{filename}.csv">{chart.locale.strings.downloadData}</a>
|
|
26
|
+
{/if}
|
|
27
|
+
|
|
28
|
+
<style>
|
|
29
|
+
.mz-data-download {
|
|
30
|
+
color: var(--mz-color-muted);
|
|
31
|
+
font-size: var(--mz-size-small);
|
|
32
|
+
font-weight: var(--mz-weight-strong);
|
|
33
|
+
}
|
|
34
|
+
</style>
|