@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.
Files changed (232) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +44 -2
  3. package/dist/annotations/Arrow.svelte +50 -0
  4. package/dist/annotations/Arrow.svelte.d.ts +18 -0
  5. package/dist/annotations/Callout.svelte +87 -0
  6. package/dist/annotations/Callout.svelte.d.ts +22 -0
  7. package/dist/annotations/Label.svelte +55 -0
  8. package/dist/annotations/Label.svelte.d.ts +19 -0
  9. package/dist/annotations/RefBand.svelte +46 -0
  10. package/dist/annotations/RefBand.svelte.d.ts +18 -0
  11. package/dist/annotations/RefLine.svelte +72 -0
  12. package/dist/annotations/RefLine.svelte.d.ts +19 -0
  13. package/dist/annotations/callout-geometry.d.ts +23 -0
  14. package/dist/annotations/callout-geometry.js +45 -0
  15. package/dist/annotations/index.d.ts +6 -0
  16. package/dist/annotations/index.js +6 -0
  17. package/dist/core/Canvas.svelte +55 -0
  18. package/dist/core/Canvas.svelte.d.ts +19 -0
  19. package/dist/core/Facets.svelte +80 -0
  20. package/dist/core/Facets.svelte.d.ts +49 -0
  21. package/dist/core/Html.svelte +38 -0
  22. package/dist/core/Html.svelte.d.ts +13 -0
  23. package/dist/core/Plot.svelte +202 -0
  24. package/dist/core/Plot.svelte.d.ts +60 -0
  25. package/dist/core/Svg.svelte +58 -0
  26. package/dist/core/Svg.svelte.d.ts +17 -0
  27. package/dist/core/accessor.d.ts +5 -0
  28. package/dist/core/accessor.js +3 -0
  29. package/dist/core/context.d.ts +25 -0
  30. package/dist/core/context.js +69 -0
  31. package/dist/core/index.d.ts +10 -0
  32. package/dist/core/index.js +9 -0
  33. package/dist/core/scales.d.ts +16 -0
  34. package/dist/core/scales.js +32 -0
  35. package/dist/core/types.d.ts +78 -0
  36. package/dist/core/types.js +1 -0
  37. package/dist/core/units.d.ts +6 -0
  38. package/dist/core/units.js +10 -0
  39. package/dist/embed/Embed.svelte +50 -0
  40. package/dist/embed/Embed.svelte.d.ts +14 -0
  41. package/dist/embed/code.d.ts +18 -0
  42. package/dist/embed/code.js +9 -0
  43. package/dist/embed/host.d.ts +6 -0
  44. package/dist/embed/host.js +20 -0
  45. package/dist/embed/index.d.ts +6 -0
  46. package/dist/embed/index.js +5 -0
  47. package/dist/embed/manifest.d.ts +16 -0
  48. package/dist/embed/manifest.js +1 -0
  49. package/dist/embed/params.svelte.d.ts +10 -0
  50. package/dist/embed/params.svelte.js +27 -0
  51. package/dist/embed/resize.d.ts +13 -0
  52. package/dist/embed/resize.js +28 -0
  53. package/dist/export/download.d.ts +13 -0
  54. package/dist/export/download.js +52 -0
  55. package/dist/export/figure-to-svg.d.ts +21 -0
  56. package/dist/export/figure-to-svg.js +187 -0
  57. package/dist/export/fonts.d.ts +26 -0
  58. package/dist/export/fonts.js +112 -0
  59. package/dist/export/index.d.ts +2 -0
  60. package/dist/export/index.js +2 -0
  61. package/dist/frame/Byline.svelte +26 -0
  62. package/dist/frame/Byline.svelte.d.ts +8 -0
  63. package/dist/frame/Chart.svelte +144 -0
  64. package/dist/frame/Chart.svelte.d.ts +37 -0
  65. package/dist/frame/ChartTools.svelte +259 -0
  66. package/dist/frame/ChartTools.svelte.d.ts +17 -0
  67. package/dist/frame/DataDownload.svelte +34 -0
  68. package/dist/frame/DataDownload.svelte.d.ts +34 -0
  69. package/dist/frame/DataTable.svelte +49 -0
  70. package/dist/frame/DataTable.svelte.d.ts +34 -0
  71. package/dist/frame/Footer.svelte +39 -0
  72. package/dist/frame/Footer.svelte.d.ts +10 -0
  73. package/dist/frame/Header.svelte +21 -0
  74. package/dist/frame/Header.svelte.d.ts +9 -0
  75. package/dist/frame/Logo.svelte +57 -0
  76. package/dist/frame/Logo.svelte.d.ts +8 -0
  77. package/dist/frame/Notes.svelte +29 -0
  78. package/dist/frame/Notes.svelte.d.ts +9 -0
  79. package/dist/frame/Source.svelte +29 -0
  80. package/dist/frame/Source.svelte.d.ts +9 -0
  81. package/dist/frame/Subtitle.svelte +22 -0
  82. package/dist/frame/Subtitle.svelte.d.ts +9 -0
  83. package/dist/frame/Title.svelte +31 -0
  84. package/dist/frame/Title.svelte.d.ts +9 -0
  85. package/dist/frame/index.d.ts +12 -0
  86. package/dist/frame/index.js +12 -0
  87. package/dist/guides/AxisX.svelte +101 -0
  88. package/dist/guides/AxisX.svelte.d.ts +22 -0
  89. package/dist/guides/AxisY.svelte +84 -0
  90. package/dist/guides/AxisY.svelte.d.ts +21 -0
  91. package/dist/guides/ColorRamp.svelte +93 -0
  92. package/dist/guides/ColorRamp.svelte.d.ts +25 -0
  93. package/dist/guides/GridX.svelte +35 -0
  94. package/dist/guides/GridX.svelte.d.ts +9 -0
  95. package/dist/guides/GridY.svelte +35 -0
  96. package/dist/guides/GridY.svelte.d.ts +9 -0
  97. package/dist/guides/Key.svelte +48 -0
  98. package/dist/guides/Key.svelte.d.ts +14 -0
  99. package/dist/guides/Legend.svelte +112 -0
  100. package/dist/guides/Legend.svelte.d.ts +26 -0
  101. package/dist/guides/SizeLegend.svelte +62 -0
  102. package/dist/guides/SizeLegend.svelte.d.ts +15 -0
  103. package/dist/guides/index.d.ts +9 -0
  104. package/dist/guides/index.js +9 -0
  105. package/dist/guides/ticks.d.ts +6 -0
  106. package/dist/guides/ticks.js +15 -0
  107. package/dist/index.d.ts +13 -0
  108. package/dist/index.js +14 -0
  109. package/dist/interactive/ButtonGroup.svelte +98 -0
  110. package/dist/interactive/ButtonGroup.svelte.d.ts +42 -0
  111. package/dist/interactive/PointerArea.svelte +49 -0
  112. package/dist/interactive/PointerArea.svelte.d.ts +18 -0
  113. package/dist/interactive/Scroller.svelte +147 -0
  114. package/dist/interactive/Scroller.svelte.d.ts +54 -0
  115. package/dist/interactive/Tooltip.svelte +66 -0
  116. package/dist/interactive/Tooltip.svelte.d.ts +19 -0
  117. package/dist/interactive/index.d.ts +4 -0
  118. package/dist/interactive/index.js +4 -0
  119. package/dist/interactive/tooltip-position.d.ts +12 -0
  120. package/dist/interactive/tooltip-position.js +16 -0
  121. package/dist/locale/definitions.d.ts +145 -0
  122. package/dist/locale/definitions.js +138 -0
  123. package/dist/locale/index.d.ts +23 -0
  124. package/dist/locale/index.js +34 -0
  125. package/dist/marks/Arcs.svelte +93 -0
  126. package/dist/marks/Arcs.svelte.d.ts +47 -0
  127. package/dist/marks/Area.svelte +44 -0
  128. package/dist/marks/Area.svelte.d.ts +43 -0
  129. package/dist/marks/Arrows.svelte +93 -0
  130. package/dist/marks/Arrows.svelte.d.ts +50 -0
  131. package/dist/marks/Bars.svelte +111 -0
  132. package/dist/marks/Bars.svelte.d.ts +61 -0
  133. package/dist/marks/Beeswarm.svelte +84 -0
  134. package/dist/marks/Beeswarm.svelte.d.ts +42 -0
  135. package/dist/marks/Dumbbell.svelte +71 -0
  136. package/dist/marks/Dumbbell.svelte.d.ts +42 -0
  137. package/dist/marks/GeoPath.svelte +56 -0
  138. package/dist/marks/GeoPath.svelte.d.ts +42 -0
  139. package/dist/marks/Hemicycle.svelte +182 -0
  140. package/dist/marks/Hemicycle.svelte.d.ts +78 -0
  141. package/dist/marks/Intervals.svelte +128 -0
  142. package/dist/marks/Intervals.svelte.d.ts +60 -0
  143. package/dist/marks/Line.svelte +64 -0
  144. package/dist/marks/Line.svelte.d.ts +43 -0
  145. package/dist/marks/LineEndLabels.svelte +70 -0
  146. package/dist/marks/LineEndLabels.svelte.d.ts +41 -0
  147. package/dist/marks/Points.svelte +70 -0
  148. package/dist/marks/Points.svelte.d.ts +46 -0
  149. package/dist/marks/Rects.svelte +92 -0
  150. package/dist/marks/Rects.svelte.d.ts +47 -0
  151. package/dist/marks/Rules.svelte +46 -0
  152. package/dist/marks/Rules.svelte.d.ts +15 -0
  153. package/dist/marks/Slope.svelte +112 -0
  154. package/dist/marks/Slope.svelte.d.ts +41 -0
  155. package/dist/marks/Text.svelte +75 -0
  156. package/dist/marks/Text.svelte.d.ts +47 -0
  157. package/dist/marks/Treemap.svelte +89 -0
  158. package/dist/marks/Treemap.svelte.d.ts +45 -0
  159. package/dist/marks/Voronoi.svelte +61 -0
  160. package/dist/marks/Voronoi.svelte.d.ts +39 -0
  161. package/dist/marks/Waffle.svelte +69 -0
  162. package/dist/marks/Waffle.svelte.d.ts +41 -0
  163. package/dist/marks/bar-path.d.ts +5 -0
  164. package/dist/marks/bar-path.js +15 -0
  165. package/dist/marks/index.d.ts +20 -0
  166. package/dist/marks/index.js +20 -0
  167. package/dist/recipes/ArrowChart.svelte +247 -0
  168. package/dist/recipes/ArrowChart.svelte.d.ts +73 -0
  169. package/dist/recipes/BarChart.svelte +122 -0
  170. package/dist/recipes/BarChart.svelte.d.ts +58 -0
  171. package/dist/recipes/BarDotChart.svelte +204 -0
  172. package/dist/recipes/BarDotChart.svelte.d.ts +75 -0
  173. package/dist/recipes/ColumnChart.svelte +119 -0
  174. package/dist/recipes/ColumnChart.svelte.d.ts +56 -0
  175. package/dist/recipes/LineChart.svelte +159 -0
  176. package/dist/recipes/LineChart.svelte.d.ts +61 -0
  177. package/dist/recipes/MosaicChart.svelte +256 -0
  178. package/dist/recipes/MosaicChart.svelte.d.ts +83 -0
  179. package/dist/recipes/ScatterChart.svelte +167 -0
  180. package/dist/recipes/ScatterChart.svelte.d.ts +63 -0
  181. package/dist/recipes/StackedBarChart.svelte +222 -0
  182. package/dist/recipes/StackedBarChart.svelte.d.ts +83 -0
  183. package/dist/recipes/index.d.ts +10 -0
  184. package/dist/recipes/index.js +19 -0
  185. package/dist/styles/base.d.ts +5 -0
  186. package/dist/styles/base.js +102 -0
  187. package/dist/styles/check.d.ts +8 -0
  188. package/dist/styles/check.js +32 -0
  189. package/dist/styles/color.d.ts +11 -0
  190. package/dist/styles/color.js +29 -0
  191. package/dist/styles/index.d.ts +9 -0
  192. package/dist/styles/index.js +17 -0
  193. package/dist/styles/types.d.ts +90 -0
  194. package/dist/styles/types.js +1 -0
  195. package/dist/table/Sparkline.svelte +59 -0
  196. package/dist/table/Sparkline.svelte.d.ts +16 -0
  197. package/dist/table/Table.svelte +287 -0
  198. package/dist/table/Table.svelte.d.ts +44 -0
  199. package/dist/table/csv.d.ts +3 -0
  200. package/dist/table/csv.js +12 -0
  201. package/dist/table/index.d.ts +4 -0
  202. package/dist/table/index.js +3 -0
  203. package/dist/table/types.d.ts +26 -0
  204. package/dist/table/types.js +1 -0
  205. package/dist/transforms/bin.d.ts +15 -0
  206. package/dist/transforms/bin.js +18 -0
  207. package/dist/transforms/dodge.d.ts +10 -0
  208. package/dist/transforms/dodge.js +35 -0
  209. package/dist/transforms/group.d.ts +14 -0
  210. package/dist/transforms/group.js +14 -0
  211. package/dist/transforms/hemicycle.d.ts +30 -0
  212. package/dist/transforms/hemicycle.js +51 -0
  213. package/dist/transforms/index.d.ts +8 -0
  214. package/dist/transforms/index.js +8 -0
  215. package/dist/transforms/labels.d.ts +6 -0
  216. package/dist/transforms/labels.js +20 -0
  217. package/dist/transforms/smooth.d.ts +61 -0
  218. package/dist/transforms/smooth.js +105 -0
  219. package/dist/transforms/stack.d.ts +25 -0
  220. package/dist/transforms/stack.js +61 -0
  221. package/dist/transforms/treemap.d.ts +21 -0
  222. package/dist/transforms/treemap.js +31 -0
  223. package/package.json +152 -3
  224. package/src/lib/recipes/ArrowChart.svelte +247 -0
  225. package/src/lib/recipes/BarChart.svelte +122 -0
  226. package/src/lib/recipes/BarDotChart.svelte +204 -0
  227. package/src/lib/recipes/ColumnChart.svelte +119 -0
  228. package/src/lib/recipes/LineChart.svelte +159 -0
  229. package/src/lib/recipes/MosaicChart.svelte +256 -0
  230. package/src/lib/recipes/ScatterChart.svelte +167 -0
  231. package/src/lib/recipes/StackedBarChart.svelte +222 -0
  232. package/src/lib/recipes/index.ts +20 -0
@@ -0,0 +1,80 @@
1
+ <!--
2
+ @component
3
+ Small multiples: a responsive grid with one cell per group. Render a `<Plot>`
4
+ in each cell from the children snippet. For comparable panels, build the
5
+ scales once from all the data and pass the same scales to every cell.
6
+
7
+ ```svelte
8
+ <Facets groups={regions} columns={{ s: 2, l: 4 }} title={(g) => g.name}>
9
+ {#snippet children(group)}
10
+ <Plot {x} {y} height={120}><Svg><Line data={group.values} … /></Svg></Plot>
11
+ {/snippet}
12
+ </Facets>
13
+ ```
14
+ -->
15
+ <script lang="ts" generics="G">
16
+ import type { Snippet } from 'svelte';
17
+ import { setInitialWidth, useChart } from './context.js';
18
+ import type { Breakpoint } from './types.js';
19
+
20
+ interface Props {
21
+ groups: G[];
22
+ /** Column count, fixed or per breakpoint (smaller breakpoints inherit upwards). */
23
+ columns?: number | Partial<Record<Breakpoint, number>>;
24
+ /** CSS gap between cells. */
25
+ gap?: string;
26
+ /** Panel title. */
27
+ title?: (group: G, index: number) => string;
28
+ class?: string;
29
+ children: Snippet<[G, number]>;
30
+ }
31
+
32
+ let {
33
+ groups,
34
+ columns = { s: 2, m: 3, l: 4 },
35
+ gap = '1rem 1.25rem',
36
+ title,
37
+ class: className = '',
38
+ children
39
+ }: Props = $props();
40
+
41
+ const chart = useChart();
42
+
43
+ const count = $derived.by(() => {
44
+ if (typeof columns === 'number') return columns;
45
+ const order: Breakpoint[] = ['s', 'm', 'l'];
46
+ let n = 1;
47
+ for (const bp of order.slice(0, order.indexOf(chart.breakpoint) + 1)) n = columns[bp] ?? n;
48
+ return Math.max(1, Math.min(n, groups.length));
49
+ });
50
+
51
+ // Rough cell width so server-rendered plots are close to their final size.
52
+ setInitialWidth(() => (chart.width - 16 * (count - 1)) / count);
53
+ </script>
54
+
55
+ <div class="mz-facets {className}" style:grid-template-columns="repeat({count}, minmax(0, 1fr))" style:gap>
56
+ {#each groups as group, i}
57
+ <div class="mz-facet">
58
+ {#if title}<h3 class="mz-facet-title">{title(group, i)}</h3>{/if}
59
+ {@render children(group, i)}
60
+ </div>
61
+ {/each}
62
+ </div>
63
+
64
+ <style>
65
+ .mz-facets {
66
+ display: grid;
67
+ }
68
+ .mz-facet {
69
+ min-width: 0;
70
+ }
71
+ .mz-facet-title {
72
+ margin: 0 0 0.25rem;
73
+ font-size: var(--mz-size-small);
74
+ font-family: var(--mz-font-label);
75
+ font-weight: var(--mz-weight-label);
76
+ text-transform: var(--mz-label-transform);
77
+ letter-spacing: var(--mz-label-tracking);
78
+ color: var(--mz-color-text);
79
+ }
80
+ </style>
@@ -0,0 +1,49 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type { Breakpoint } from './types.js';
3
+ declare function $$render<G>(): {
4
+ props: {
5
+ groups: G[];
6
+ /** Column count, fixed or per breakpoint (smaller breakpoints inherit upwards). */
7
+ columns?: number | Partial<Record<Breakpoint, number>>;
8
+ /** CSS gap between cells. */
9
+ gap?: string;
10
+ /** Panel title. */
11
+ title?: (group: G, index: number) => string;
12
+ class?: string;
13
+ children: Snippet<[G, number]>;
14
+ };
15
+ exports: {};
16
+ bindings: "";
17
+ slots: {};
18
+ events: {};
19
+ };
20
+ declare class __sveltets_Render<G> {
21
+ props(): ReturnType<typeof $$render<G>>['props'];
22
+ events(): ReturnType<typeof $$render<G>>['events'];
23
+ slots(): ReturnType<typeof $$render<G>>['slots'];
24
+ bindings(): "";
25
+ exports(): {};
26
+ }
27
+ interface $$IsomorphicComponent {
28
+ new <G>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<G>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<G>['props']>, ReturnType<__sveltets_Render<G>['events']>, ReturnType<__sveltets_Render<G>['slots']>> & {
29
+ $$bindings?: ReturnType<__sveltets_Render<G>['bindings']>;
30
+ } & ReturnType<__sveltets_Render<G>['exports']>;
31
+ <G>(internal: unknown, props: ReturnType<__sveltets_Render<G>['props']> & {}): ReturnType<__sveltets_Render<G>['exports']>;
32
+ z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
33
+ }
34
+ /**
35
+ * Small multiples: a responsive grid with one cell per group. Render a `<Plot>`
36
+ * in each cell from the children snippet. For comparable panels, build the
37
+ * scales once from all the data and pass the same scales to every cell.
38
+ *
39
+ * ```svelte
40
+ * <Facets groups={regions} columns={{ s: 2, l: 4 }} title={(g) => g.name}>
41
+ * {#snippet children(group)}
42
+ * <Plot {x} {y} height={120}><Svg><Line data={group.values} … /></Svg></Plot>
43
+ * {/snippet}
44
+ * </Facets>
45
+ * ```
46
+ */
47
+ declare const Facets: $$IsomorphicComponent;
48
+ type Facets<G> = InstanceType<typeof Facets<G>>;
49
+ export default Facets;
@@ -0,0 +1,38 @@
1
+ <!--
2
+ @component
3
+ An HTML layer positioned exactly over the plot area, for tooltips and other
4
+ interactive elements. It ignores pointer events itself; children opt in.
5
+ Not rendered in print, and not part of SVG exports.
6
+ -->
7
+ <script lang="ts">
8
+ import type { Snippet } from 'svelte';
9
+ import { usePlot } from './context.js';
10
+
11
+ let { class: className = '', children }: { class?: string; children?: Snippet } = $props();
12
+
13
+ const plot = usePlot();
14
+
15
+ // Before the plot is measured (server render, no JS yet) its size is a guess, so position the
16
+ // layer in percentages of the plot box, which scales with the page instead of overflowing it.
17
+ const x = (px: number) => (plot.measured ? `${px}px` : `${(px / (plot.fullWidth || 1)) * 100}%`);
18
+ const y = (px: number) => (plot.measured ? `${px}px` : `${(px / (plot.fullHeight || 1)) * 100}%`);
19
+ </script>
20
+
21
+ {#if plot.medium !== 'print'}
22
+ <div
23
+ class="mz-layer mz-html {className}"
24
+ style:left={x(plot.padding.left)}
25
+ style:top={y(plot.padding.top)}
26
+ style:width={x(plot.width)}
27
+ style:height={y(plot.height)}
28
+ >
29
+ {@render children?.()}
30
+ </div>
31
+ {/if}
32
+
33
+ <style>
34
+ .mz-html {
35
+ position: absolute;
36
+ pointer-events: none;
37
+ }
38
+ </style>
@@ -0,0 +1,13 @@
1
+ import type { Snippet } from 'svelte';
2
+ type $$ComponentProps = {
3
+ class?: string;
4
+ children?: Snippet;
5
+ };
6
+ /**
7
+ * An HTML layer positioned exactly over the plot area, for tooltips and other
8
+ * interactive elements. It ignores pointer events itself; children opt in.
9
+ * Not rendered in print, and not part of SVG exports.
10
+ */
11
+ declare const Html: import("svelte").Component<$$ComponentProps, {}, "">;
12
+ type Html = ReturnType<typeof Html>;
13
+ export default Html;
@@ -0,0 +1,202 @@
1
+ <!--
2
+ @component
3
+ The plot area. Measures its width, applies padding, sets the ranges of the
4
+ d3 scales you pass in, and shares everything with its layers and marks via
5
+ `usePlot()`. Put `<Svg>`, `<Html>` (and later `<Canvas>`) layers inside.
6
+
7
+ ```svelte
8
+ <Plot x={scaleLinear().domain([0, 100])} y={scaleBand().domain(names)} height={300}>
9
+ <Svg>…marks…</Svg>
10
+ <Html>…tooltips…</Html>
11
+ </Plot>
12
+ ```
13
+
14
+ The children snippet receives the plot context, so chart code can use the
15
+ ranged scales directly (e.g. to position a tooltip):
16
+
17
+ ```svelte
18
+ <Plot {x} {y}>
19
+ {#snippet children(plot)}
20
+ <Html><Tooltip x={plot.x(d.value)} y={scaled(plot.y, d.name)}>…</Tooltip></Html>
21
+ {/snippet}
22
+ </Plot>
23
+ ```
24
+ -->
25
+ <script lang="ts">
26
+ import type { Snippet } from 'svelte';
27
+ import { scaleIdentity } from 'd3-scale';
28
+ import { geoPath, type GeoPermissibleObjects } from 'd3-geo';
29
+ import { setPlotContext, useChart, useInitialWidth } from './context.js';
30
+ import { isOrdinal, isScale } from './scales.js';
31
+ import type {
32
+ Breakpoint,
33
+ Dimensions,
34
+ Padding,
35
+ PlotContext,
36
+ PositionScale,
37
+ ProjectionInput,
38
+ RangeInput,
39
+ ScaleInput
40
+ } from './types.js';
41
+
42
+ type SizeInfo = { width: number; breakpoint: Breakpoint };
43
+
44
+ interface Props {
45
+ /** x position scale: a d3 scale without a range (Plot sets it), or `(dims) => scale` for full control. */
46
+ x?: ScaleInput;
47
+ /** y position scale. Continuous scales get range [height, 0]; band/point scales get [0, height]. */
48
+ y?: ScaleInput;
49
+ /** Override the default x range. */
50
+ xRange?: RangeInput;
51
+ /** Override the default y range. */
52
+ yRange?: RangeInput;
53
+ /** Extra named scales, e.g. `{ color: scaleOrdinal(...) }`. Plain functions are called with the dimensions. */
54
+ scales?: Record<string, unknown>;
55
+ /** A d3-geo projection, or `(dims) => projection`. */
56
+ projection?: ProjectionInput;
57
+ /** GeoJSON to fit the projection to the plot area. */
58
+ fit?: GeoPermissibleObjects;
59
+ /** Plot-area height in px, or a function of the outer width and breakpoint. */
60
+ height?: number | ((size: SizeInfo) => number);
61
+ /** Plot-area width / height. Used when `height` is not given. */
62
+ aspectRatio?: number;
63
+ /** Space around the plot area for axes and labels. Merged over the theme's padding. */
64
+ padding?: Partial<Padding> | ((size: SizeInfo) => Partial<Padding>);
65
+ /** Fixed outer width in px (print, tests). Otherwise the container is measured. */
66
+ width?: number;
67
+ class?: string;
68
+ /** Receives the plot context (ranged scales, dimensions). */
69
+ children?: Snippet<[PlotContext]>;
70
+ }
71
+
72
+ let {
73
+ x: xInput,
74
+ y: yInput,
75
+ xRange,
76
+ yRange,
77
+ scales: scaleInputs = {},
78
+ projection: projectionInput,
79
+ fit,
80
+ height: heightInput,
81
+ aspectRatio,
82
+ padding: paddingInput = {},
83
+ width: fixedWidth,
84
+ class: className = '',
85
+ children
86
+ }: Props = $props();
87
+
88
+ const chart = useChart();
89
+ const initialWidth = useInitialWidth();
90
+
91
+ let measuredWidth = $state(0);
92
+ const measured = $derived(fixedWidth != null || measuredWidth > 0);
93
+ const fullWidth = $derived(fixedWidth ?? (measuredWidth || initialWidth?.() || chart.width));
94
+ const size = $derived<SizeInfo>({ width: fullWidth, breakpoint: chart.breakpoint });
95
+
96
+ const padding = $derived<Padding>({
97
+ ...chart.theme.plot.padding,
98
+ ...(typeof paddingInput === 'function' ? paddingInput(size) : paddingInput)
99
+ });
100
+ const width = $derived(Math.max(0, fullWidth - padding.left - padding.right));
101
+ const height = $derived(
102
+ Math.max(
103
+ 0,
104
+ typeof heightInput === 'function'
105
+ ? heightInput(size)
106
+ : (heightInput ?? (aspectRatio ? width / aspectRatio : chart.theme.plot.height))
107
+ )
108
+ );
109
+ const fullHeight = $derived(height + padding.top + padding.bottom);
110
+ const dims = $derived<Dimensions>({ width, height, padding, fullWidth, fullHeight, breakpoint: chart.breakpoint });
111
+
112
+ function resolveRange(input: RangeInput | undefined, fallback: [number, number]): [number, number] {
113
+ if (!input) return fallback;
114
+ return typeof input === 'function' ? input(dims) : input;
115
+ }
116
+
117
+ function resolveScale(input: ScaleInput | undefined, range: [number, number]): PositionScale {
118
+ if (!input) return scaleIdentity().range(range) as unknown as PositionScale;
119
+ if (isScale(input)) return input.copy().range(range);
120
+ return input(dims);
121
+ }
122
+
123
+ const x = $derived(resolveScale(xInput, resolveRange(xRange, [0, width])));
124
+ const y = $derived(resolveScale(yInput, resolveRange(yRange, isOrdinal(yInput) ? [0, height] : [height, 0])));
125
+
126
+ const scales = $derived(
127
+ Object.fromEntries(
128
+ Object.entries(scaleInputs).map(([name, s]) => [
129
+ name,
130
+ typeof s === 'function' && !isScale(s) ? (s as (d: Dimensions) => unknown)(dims) : s
131
+ ])
132
+ )
133
+ );
134
+
135
+ const geo = $derived.by(() => {
136
+ if (!projectionInput) return { projection: undefined, path: undefined };
137
+ const projection = 'scale' in projectionInput ? projectionInput : projectionInput(dims);
138
+ if (fit) projection.fitSize([width, height], fit);
139
+ // A new geoPath on every change so dependants re-render (the projection object is mutated in place).
140
+ return { projection, path: geoPath(projection) };
141
+ });
142
+
143
+ const context: PlotContext = {
144
+ get width() {
145
+ return width;
146
+ },
147
+ get height() {
148
+ return height;
149
+ },
150
+ get padding() {
151
+ return padding;
152
+ },
153
+ get fullWidth() {
154
+ return fullWidth;
155
+ },
156
+ get fullHeight() {
157
+ return fullHeight;
158
+ },
159
+ get measured() {
160
+ return measured;
161
+ },
162
+ get breakpoint() {
163
+ return chart.breakpoint;
164
+ },
165
+ get medium() {
166
+ return chart.medium;
167
+ },
168
+ get x() {
169
+ return x;
170
+ },
171
+ get y() {
172
+ return y;
173
+ },
174
+ get scales() {
175
+ return scales;
176
+ },
177
+ get projection() {
178
+ return geo.projection;
179
+ },
180
+ get path() {
181
+ return geo.path;
182
+ }
183
+ };
184
+ setPlotContext(context);
185
+ </script>
186
+
187
+ <div
188
+ class="mz-plot {className}"
189
+ bind:clientWidth={measuredWidth}
190
+ style:width={fixedWidth != null ? `${fixedWidth}px` : undefined}
191
+ style:height={measured ? `${fullHeight}px` : undefined}
192
+ style:aspect-ratio={measured ? undefined : `${fullWidth} / ${fullHeight}`}
193
+ >
194
+ {@render children?.(context)}
195
+ </div>
196
+
197
+ <style>
198
+ .mz-plot {
199
+ position: relative;
200
+ width: 100%;
201
+ }
202
+ </style>
@@ -0,0 +1,60 @@
1
+ import type { Snippet } from 'svelte';
2
+ import { type GeoPermissibleObjects } from 'd3-geo';
3
+ import type { Breakpoint, Padding, PlotContext, ProjectionInput, RangeInput, ScaleInput } from './types.js';
4
+ type SizeInfo = {
5
+ width: number;
6
+ breakpoint: Breakpoint;
7
+ };
8
+ interface Props {
9
+ /** x position scale: a d3 scale without a range (Plot sets it), or `(dims) => scale` for full control. */
10
+ x?: ScaleInput;
11
+ /** y position scale. Continuous scales get range [height, 0]; band/point scales get [0, height]. */
12
+ y?: ScaleInput;
13
+ /** Override the default x range. */
14
+ xRange?: RangeInput;
15
+ /** Override the default y range. */
16
+ yRange?: RangeInput;
17
+ /** Extra named scales, e.g. `{ color: scaleOrdinal(...) }`. Plain functions are called with the dimensions. */
18
+ scales?: Record<string, unknown>;
19
+ /** A d3-geo projection, or `(dims) => projection`. */
20
+ projection?: ProjectionInput;
21
+ /** GeoJSON to fit the projection to the plot area. */
22
+ fit?: GeoPermissibleObjects;
23
+ /** Plot-area height in px, or a function of the outer width and breakpoint. */
24
+ height?: number | ((size: SizeInfo) => number);
25
+ /** Plot-area width / height. Used when `height` is not given. */
26
+ aspectRatio?: number;
27
+ /** Space around the plot area for axes and labels. Merged over the theme's padding. */
28
+ padding?: Partial<Padding> | ((size: SizeInfo) => Partial<Padding>);
29
+ /** Fixed outer width in px (print, tests). Otherwise the container is measured. */
30
+ width?: number;
31
+ class?: string;
32
+ /** Receives the plot context (ranged scales, dimensions). */
33
+ children?: Snippet<[PlotContext]>;
34
+ }
35
+ /**
36
+ * The plot area. Measures its width, applies padding, sets the ranges of the
37
+ * d3 scales you pass in, and shares everything with its layers and marks via
38
+ * `usePlot()`. Put `<Svg>`, `<Html>` (and later `<Canvas>`) layers inside.
39
+ *
40
+ * ```svelte
41
+ * <Plot x={scaleLinear().domain([0, 100])} y={scaleBand().domain(names)} height={300}>
42
+ * <Svg>…marks…</Svg>
43
+ * <Html>…tooltips…</Html>
44
+ * </Plot>
45
+ * ```
46
+ *
47
+ * The children snippet receives the plot context, so chart code can use the
48
+ * ranged scales directly (e.g. to position a tooltip):
49
+ *
50
+ * ```svelte
51
+ * <Plot {x} {y}>
52
+ * {#snippet children(plot)}
53
+ * <Html><Tooltip x={plot.x(d.value)} y={scaled(plot.y, d.name)}>…</Tooltip></Html>
54
+ * {/snippet}
55
+ * </Plot>
56
+ * ```
57
+ */
58
+ declare const Plot: import("svelte").Component<Props, {}, "">;
59
+ type Plot = ReturnType<typeof Plot>;
60
+ export default Plot;
@@ -0,0 +1,58 @@
1
+ <!--
2
+ @component
3
+ An SVG layer covering the whole `<Plot>`. Children are drawn in plot-area
4
+ coordinates (the padding is already applied). Anything that must print
5
+ belongs in an `<Svg>` layer.
6
+ -->
7
+ <script lang="ts">
8
+ import type { Snippet } from 'svelte';
9
+ import { useChart, usePlot } from './context.js';
10
+
11
+ interface Props {
12
+ /** Content for `<defs>` (gradients, patterns, clip paths). */
13
+ defs?: Snippet;
14
+ /** Hide this layer from assistive technology (e.g. a second, decorative layer). */
15
+ decorative?: boolean;
16
+ class?: string;
17
+ children?: Snippet;
18
+ }
19
+
20
+ let { defs, decorative = false, class: className = '', children }: Props = $props();
21
+
22
+ const plot = usePlot();
23
+ const chart = useChart();
24
+ </script>
25
+
26
+ <svg
27
+ class="mz-layer mz-svg {className}"
28
+ xmlns="http://www.w3.org/2000/svg"
29
+ width={plot.fullWidth}
30
+ height={plot.fullHeight}
31
+ viewBox="0 0 {plot.fullWidth} {plot.fullHeight}"
32
+ role={decorative ? 'presentation' : 'img'}
33
+ aria-hidden={decorative ? 'true' : undefined}
34
+ aria-labelledby={decorative
35
+ ? undefined
36
+ : chart.ids.hasTitle
37
+ ? chart.ids.title
38
+ : chart.ids.hasDesc
39
+ ? chart.ids.desc
40
+ : undefined}
41
+ aria-describedby={!decorative && chart.ids.hasTitle && chart.ids.hasDesc ? chart.ids.desc : undefined}
42
+ >
43
+ {#if defs}<defs>{@render defs()}</defs>{/if}
44
+ <g transform="translate({plot.padding.left},{plot.padding.top})">
45
+ {@render children?.()}
46
+ </g>
47
+ </svg>
48
+
49
+ <style>
50
+ .mz-svg {
51
+ position: absolute;
52
+ inset: 0;
53
+ width: 100%;
54
+ height: 100%;
55
+ overflow: visible;
56
+ font-family: var(--mz-font-body);
57
+ }
58
+ </style>
@@ -0,0 +1,17 @@
1
+ import type { Snippet } from 'svelte';
2
+ interface Props {
3
+ /** Content for `<defs>` (gradients, patterns, clip paths). */
4
+ defs?: Snippet;
5
+ /** Hide this layer from assistive technology (e.g. a second, decorative layer). */
6
+ decorative?: boolean;
7
+ class?: string;
8
+ children?: Snippet;
9
+ }
10
+ /**
11
+ * An SVG layer covering the whole `<Plot>`. Children are drawn in plot-area
12
+ * coordinates (the padding is already applied). Anything that must print
13
+ * belongs in an `<Svg>` layer.
14
+ */
15
+ declare const Svg: import("svelte").Component<Props, {}, "">;
16
+ type Svg = ReturnType<typeof Svg>;
17
+ export default Svg;
@@ -0,0 +1,5 @@
1
+ /** Data values that can be mapped through a position scale. */
2
+ export type Value = string | number | Date | boolean | null | undefined;
3
+ /** A constant, or a function of the datum and its index. (Avoid `V = any`: it erases the callback's types.) */
4
+ export type Accessor<T, V> = V | ((d: T, i: number) => V);
5
+ export declare function access<T, V>(accessor: Accessor<T, V>, d: T, i: number): V;
@@ -0,0 +1,3 @@
1
+ export function access(accessor, d, i) {
2
+ return typeof accessor === 'function' ? accessor(d, i) : accessor;
3
+ }
@@ -0,0 +1,25 @@
1
+ import { type Locale } from '../locale/index.js';
2
+ import type { Theme } from '../styles/types.js';
3
+ import type { ChartContext, PlotContext } from './types.js';
4
+ export declare function setChartContext(ctx: ChartContext): void;
5
+ /** The surrounding `<Chart>`'s theme, locale, medium and breakpoint. Falls back to the base style. */
6
+ export declare function useChart(): ChartContext;
7
+ /** The active house style. Read properties inside `$derived` or markup so they stay reactive. */
8
+ export declare function useTheme(): Theme;
9
+ /** The active locale's formatters and strings. */
10
+ export declare function useLocale(): Locale;
11
+ export declare function setPlotContext(ctx: PlotContext): void;
12
+ /** Dimensions, scales and projection of the surrounding `<Plot>`. */
13
+ export declare function usePlot(): PlotContext;
14
+ /** Layout components (e.g. `<Facets>`) tell nested plots their expected width before measurement. */
15
+ export declare function setInitialWidth(get: () => number): void;
16
+ export declare function useInitialWidth(): (() => number) | undefined;
17
+ /** `<Embed>` marks its charts as embedded, so they leave out controls meant for the chart's own site. */
18
+ export declare function setEmbedded(): void;
19
+ export declare function useEmbedded(): boolean;
20
+ /**
21
+ * Show "Download PNG · SVG" under every chart inside this component (a site's layout, say), unless a chart
22
+ * sets `<Chart download={false}>`. Charts inside `<Embed>` never show it.
23
+ */
24
+ export declare function setDownloads(on?: boolean): void;
25
+ export declare function useDownloads(): boolean;
@@ -0,0 +1,69 @@
1
+ import { getContext, setContext } from 'svelte';
2
+ import { createLocale } from '../locale/index.js';
3
+ import { base, breakpointFor } from '../styles/index.js';
4
+ const CHART = Symbol('mozaic:chart');
5
+ const PLOT = Symbol('mozaic:plot');
6
+ const INITIAL_WIDTH = Symbol('mozaic:initial-width');
7
+ const EMBEDDED = Symbol('mozaic:embedded');
8
+ const DOWNLOADS = Symbol('mozaic:downloads');
9
+ /** Used when a component renders outside `<Chart>` (tests, quick sketches). */
10
+ const fallbackChart = {
11
+ theme: base,
12
+ locale: createLocale(base.defaultLocale),
13
+ medium: 'web',
14
+ breakpoint: breakpointFor(base.plot.initialWidth, base.breakpoints),
15
+ width: base.plot.initialWidth,
16
+ measured: false,
17
+ ids: { title: '', desc: '' }
18
+ };
19
+ export function setChartContext(ctx) {
20
+ setContext(CHART, ctx);
21
+ }
22
+ /** The surrounding `<Chart>`'s theme, locale, medium and breakpoint. Falls back to the base style. */
23
+ export function useChart() {
24
+ return getContext(CHART) ?? fallbackChart;
25
+ }
26
+ /** The active house style. Read properties inside `$derived` or markup so they stay reactive. */
27
+ export function useTheme() {
28
+ const chart = useChart();
29
+ return new Proxy({}, { get: (_, key) => chart.theme[key] });
30
+ }
31
+ /** The active locale's formatters and strings. */
32
+ export function useLocale() {
33
+ const chart = useChart();
34
+ return new Proxy({}, { get: (_, key) => chart.locale[key] });
35
+ }
36
+ export function setPlotContext(ctx) {
37
+ setContext(PLOT, ctx);
38
+ }
39
+ /** Dimensions, scales and projection of the surrounding `<Plot>`. */
40
+ export function usePlot() {
41
+ const ctx = getContext(PLOT);
42
+ if (!ctx)
43
+ throw new Error('usePlot() must be called inside a <Plot>.');
44
+ return ctx;
45
+ }
46
+ /** Layout components (e.g. `<Facets>`) tell nested plots their expected width before measurement. */
47
+ export function setInitialWidth(get) {
48
+ setContext(INITIAL_WIDTH, get);
49
+ }
50
+ export function useInitialWidth() {
51
+ return getContext(INITIAL_WIDTH);
52
+ }
53
+ /** `<Embed>` marks its charts as embedded, so they leave out controls meant for the chart's own site. */
54
+ export function setEmbedded() {
55
+ setContext(EMBEDDED, true);
56
+ }
57
+ export function useEmbedded() {
58
+ return getContext(EMBEDDED) ?? false;
59
+ }
60
+ /**
61
+ * Show "Download PNG · SVG" under every chart inside this component (a site's layout, say), unless a chart
62
+ * sets `<Chart download={false}>`. Charts inside `<Embed>` never show it.
63
+ */
64
+ export function setDownloads(on = true) {
65
+ setContext(DOWNLOADS, on);
66
+ }
67
+ export function useDownloads() {
68
+ return getContext(DOWNLOADS) ?? false;
69
+ }
@@ -0,0 +1,10 @@
1
+ export { default as Plot } from './Plot.svelte';
2
+ export { default as Svg } from './Svg.svelte';
3
+ export { default as Html } from './Html.svelte';
4
+ export { default as Canvas } from './Canvas.svelte';
5
+ export { default as Facets } from './Facets.svelte';
6
+ export { setChartContext, setDownloads, setInitialWidth, setPlotContext, useChart, useDownloads, useInitialWidth, useLocale, usePlot, useTheme } from './context.js';
7
+ export { estimateTextWidth, isOrdinal, isScale, position, scaled } from './scales.js';
8
+ export { mm, pt, PX_PER_MM } from './units.js';
9
+ export { access, type Accessor, type Value } from './accessor.js';
10
+ export type { Breakpoint, ChartContext, Dimensions, Medium, Padding, PlotContext, PositionScale, ProjectionInput, RangeInput, ScaleInput } from './types.js';
@@ -0,0 +1,9 @@
1
+ export { default as Plot } from './Plot.svelte';
2
+ export { default as Svg } from './Svg.svelte';
3
+ export { default as Html } from './Html.svelte';
4
+ export { default as Canvas } from './Canvas.svelte';
5
+ export { default as Facets } from './Facets.svelte';
6
+ export { setChartContext, setDownloads, setInitialWidth, setPlotContext, useChart, useDownloads, useInitialWidth, useLocale, usePlot, useTheme } from './context.js';
7
+ export { estimateTextWidth, isOrdinal, isScale, position, scaled } from './scales.js';
8
+ export { mm, pt, PX_PER_MM } from './units.js';
9
+ export { access } from './accessor.js';
@@ -0,0 +1,16 @@
1
+ import type { PlotContext, PositionScale } from './types.js';
2
+ export declare function isScale(value: unknown): value is PositionScale;
3
+ /** True for band and point scales (they have a bandwidth). */
4
+ export declare function isOrdinal(scale: unknown): boolean;
5
+ /**
6
+ * Pixel position of a value. For band scales, returns the point at `align`
7
+ * across the band (0.5 = centre), so marks can treat all scales alike.
8
+ */
9
+ export declare function scaled(scale: PositionScale, value: unknown, align?: number): number;
10
+ /** Rough text width for layout decisions (padding for long labels). Deterministic on server and client. */
11
+ export declare function estimateTextWidth(text: string, fontSize: number, averageCharWidth?: number): number;
12
+ /**
13
+ * Pixel position of a data point. With a projection on the plot, `x`/`y` are
14
+ * longitude/latitude; otherwise they go through the x and y scales.
15
+ */
16
+ export declare function position(plot: PlotContext, x: unknown, y: unknown): [number, number];