@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
package/README.md CHANGED
@@ -1,3 +1,45 @@
1
- # Temporary Holding Version
1
+ # mozaic
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Chart components for the web and print, built on Svelte 5 and d3.
4
+
5
+ ```bash
6
+ pnpm add mozaic d3-scale d3-array d3-shape
7
+ ```
8
+
9
+ ```svelte
10
+ <script lang="ts">
11
+ import { scaleBand, scaleLinear } from 'd3-scale';
12
+ import { Chart, Header, Title } from '@samizdata/mozaic/frame';
13
+ import { Plot, Svg } from '@samizdata/mozaic/core';
14
+ import { Bars } from '@samizdata/mozaic/marks';
15
+ import { AxisX, AxisY } from '@samizdata/mozaic/guides';
16
+ import { theme } from '../lib/theme'; // your house style: a Theme from extendTheme()
17
+ </script>
18
+
19
+ <Chart style={theme} locale="en" alt="…">
20
+ <Header><Title>…</Title></Header>
21
+ <Plot x={scaleLinear().domain([0, 100])} y={scaleBand().domain(['a', 'b'])} height={80}>
22
+ <Svg><AxisX /><AxisY /><Bars data={rows} x={(d) => d.value} y={(d) => d.name} /></Svg>
23
+ </Plot>
24
+ </Chart>
25
+ ```
26
+
27
+ ## Modules
28
+
29
+ | Import | Contents |
30
+ | ------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
31
+ | `@samizdata/mozaic/core` | `Plot`, `Svg`, `Html`, `Canvas`, `Facets`, `usePlot`, `useChart`, `useTheme`, `useLocale`, `scaled`, `position`, `mm`, `pt` |
32
+ | `@samizdata/mozaic/frame` | `Chart`, `Header`, `Title`, `Subtitle`, `Footer`, `Source`, `Notes`, `Byline`, `Logo`, `DataTable`, `DataDownload` |
33
+ | `@samizdata/mozaic/marks` | `Bars`, `Line`, `Area`, `Points`, `Rects`, `Arcs`, `Text`, `Rules`, `GeoPath`, `Voronoi`, `LineEndLabels`, `Treemap`, `Waffle`, `Dumbbell`, `Arrows`, `Slope`, `Beeswarm`, `Hemicycle`, `Intervals` |
34
+ | `@samizdata/mozaic/guides` | `AxisX`, `AxisY`, `GridX`, `GridY`, `Legend`, `ColorRamp`, `SizeLegend`, `Key` |
35
+ | `@samizdata/mozaic/annotations` | `Callout`, `Arrow`, `Label`, `RefLine`, `RefBand` |
36
+ | `@samizdata/mozaic/interactive` | `PointerArea`, `Tooltip`, `ButtonGroup`, `Scroller` |
37
+ | `@samizdata/mozaic/table` | `Table`, `Sparkline`, `toCSV` |
38
+ | `@samizdata/mozaic/transforms` | `stack`, `bin`, `groupBy`, `rollup`, `indexTo`, `dodge`, `treemapLayout`, `avoidOverlap`, `hemicycleLayout`, `kernelSmooth`, `loess` |
39
+ | `@samizdata/mozaic/styles` | `base`, `extendTheme`, `checkTheme`, `rampSteps`, `contrastRatio` (house styles live with each brand) |
40
+ | `@samizdata/mozaic/locale` | `createLocale` (`en`, `ro`, `ru`) |
41
+ | `@samizdata/mozaic/embed` | `Embed`, `useEmbedParams`, `embedCode`, `hostScript`, `observeHeight` |
42
+ | `@samizdata/mozaic/export` | `figureToSvg`, `downloadSvg`, `downloadPng`, `figureToPng` |
43
+ | `@samizdata/mozaic/recipes` | `BarChart`, `ColumnChart`, `LineChart`, `ScatterChart`, `ArrowChart`, `BarDotChart`, `StackedBarChart`, `MosaicChart` |
44
+
45
+ Requires Svelte 5.57+. The full guides, API reference and gallery are in the repository's docs site.
@@ -0,0 +1,50 @@
1
+ <!--
2
+ @component
3
+ A curved arrow between two data points, without a label. Use with `<Text>`
4
+ when the label needs custom placement; otherwise prefer `<Callout>`.
5
+ -->
6
+ <script lang="ts">
7
+ import { usePlot } from '../core/context.js';
8
+ import { position } from '../core/scales.js';
9
+ import { calloutGeometry } from './callout-geometry.js';
10
+
11
+ interface Props {
12
+ /** Start point in data space. */
13
+ from: [unknown, unknown];
14
+ /** End point (arrowhead) in data space. */
15
+ to: [unknown, unknown];
16
+ /** Pixel offsets applied to the start point. */
17
+ dx?: number;
18
+ dy?: number;
19
+ curve?: 'left' | 'right' | 'none' | number;
20
+ class?: string;
21
+ }
22
+
23
+ let { from, to, dx = 0, dy = 0, curve = 'right', class: className = '' }: Props = $props();
24
+
25
+ const plot = usePlot();
26
+ const bend = $derived(curve === 'right' ? 0.3 : curve === 'left' ? -0.3 : curve === 'none' ? 0 : curve);
27
+ const geometry = $derived.by(() => {
28
+ const [sx, sy] = position(plot, from[0], from[1]);
29
+ const [tx, ty] = position(plot, to[0], to[1]);
30
+ const lx = sx + dx;
31
+ const ly = sy + dy;
32
+ // A zero-size "label" at the start point, so the arrow starts exactly there.
33
+ return calloutGeometry({ tx, ty, lx, ly, top: ly, bottom: ly, anchor: 'middle', bend, gap: 0 });
34
+ });
35
+ </script>
36
+
37
+ <g class="mz-arrow {className}">
38
+ <path d={geometry.arrow} />
39
+ <path d={geometry.head} />
40
+ </g>
41
+
42
+ <style>
43
+ path {
44
+ fill: none;
45
+ stroke: var(--mz-color-text);
46
+ stroke-width: 1;
47
+ stroke-linecap: round;
48
+ stroke-linejoin: round;
49
+ }
50
+ </style>
@@ -0,0 +1,18 @@
1
+ interface Props {
2
+ /** Start point in data space. */
3
+ from: [unknown, unknown];
4
+ /** End point (arrowhead) in data space. */
5
+ to: [unknown, unknown];
6
+ /** Pixel offsets applied to the start point. */
7
+ dx?: number;
8
+ dy?: number;
9
+ curve?: 'left' | 'right' | 'none' | number;
10
+ class?: string;
11
+ }
12
+ /**
13
+ * A curved arrow between two data points, without a label. Use with `<Text>`
14
+ * when the label needs custom placement; otherwise prefer `<Callout>`.
15
+ */
16
+ declare const Arrow: import("svelte").Component<Props, {}, "">;
17
+ type Arrow = ReturnType<typeof Arrow>;
18
+ export default Arrow;
@@ -0,0 +1,87 @@
1
+ <!--
2
+ @component
3
+ A text annotation with an optional curved arrow pointing at a data point.
4
+ `x`/`y` are in data space (band scales point at the band centre); `dx`/`dy`
5
+ place the label in pixels relative to that point. Newlines split the label.
6
+ -->
7
+ <script lang="ts">
8
+ import { useChart, usePlot } from '../core/context.js';
9
+ import { scaled } from '../core/scales.js';
10
+ import { calloutGeometry } from './callout-geometry.js';
11
+
12
+ interface Props {
13
+ x: unknown;
14
+ y: unknown;
15
+ label: string;
16
+ dx?: number;
17
+ dy?: number;
18
+ /** Which way the arrow bends, or a number (positive = clockwise). */
19
+ curve?: 'left' | 'right' | 'none' | number;
20
+ arrow?: boolean;
21
+ /** Defaults to `start` when the label is right of the target, `end` when left. */
22
+ anchor?: 'start' | 'middle' | 'end';
23
+ fontSize?: number;
24
+ class?: string;
25
+ }
26
+
27
+ let {
28
+ x,
29
+ y,
30
+ label,
31
+ dx = 0,
32
+ dy = -32,
33
+ curve = 'right',
34
+ arrow = true,
35
+ anchor,
36
+ fontSize,
37
+ class: className = ''
38
+ }: Props = $props();
39
+
40
+ const chart = useChart();
41
+ const plot = usePlot();
42
+
43
+ const size = $derived(fontSize ?? chart.theme.plot.fontSize);
44
+ const lines = $derived(label.split('\n'));
45
+ const lineHeight = $derived(size * 1.25);
46
+ const textAnchor = $derived(anchor ?? (dx > 0 ? 'start' : dx < 0 ? 'end' : 'middle'));
47
+ const bend = $derived(curve === 'right' ? 0.3 : curve === 'left' ? -0.3 : curve === 'none' ? 0 : curve);
48
+
49
+ const tx = $derived(scaled(plot.x, x));
50
+ const ty = $derived(scaled(plot.y, y));
51
+ const lx = $derived(tx + dx);
52
+ const ly = $derived(ty + dy);
53
+ // The first baseline sits at ly; the block extends down by the extra lines.
54
+ const top = $derived(ly - size * 0.8);
55
+ const bottom = $derived(ly + (lines.length - 1) * lineHeight + size * 0.25);
56
+
57
+ const geometry = $derived(calloutGeometry({ tx, ty, lx, ly, top, bottom, anchor: textAnchor, bend }));
58
+ </script>
59
+
60
+ <g class="mz-callout {className}">
61
+ {#if arrow}
62
+ <path class="mz-callout-arrow" d={geometry.arrow} />
63
+ <path class="mz-callout-arrow" d={geometry.head} />
64
+ {/if}
65
+ <text x={lx} y={ly} text-anchor={textAnchor} font-size={size}>
66
+ {#each lines as line, i}
67
+ <tspan x={lx} dy={i === 0 ? 0 : lineHeight}>{line}</tspan>
68
+ {/each}
69
+ </text>
70
+ </g>
71
+
72
+ <style>
73
+ .mz-callout-arrow {
74
+ fill: none;
75
+ stroke: var(--mz-color-text);
76
+ stroke-width: 1;
77
+ stroke-linecap: round;
78
+ stroke-linejoin: round;
79
+ }
80
+ text {
81
+ fill: var(--mz-color-text);
82
+ paint-order: stroke;
83
+ stroke: var(--mz-color-bg);
84
+ stroke-width: 3px;
85
+ stroke-linejoin: round;
86
+ }
87
+ </style>
@@ -0,0 +1,22 @@
1
+ interface Props {
2
+ x: unknown;
3
+ y: unknown;
4
+ label: string;
5
+ dx?: number;
6
+ dy?: number;
7
+ /** Which way the arrow bends, or a number (positive = clockwise). */
8
+ curve?: 'left' | 'right' | 'none' | number;
9
+ arrow?: boolean;
10
+ /** Defaults to `start` when the label is right of the target, `end` when left. */
11
+ anchor?: 'start' | 'middle' | 'end';
12
+ fontSize?: number;
13
+ class?: string;
14
+ }
15
+ /**
16
+ * A text annotation with an optional curved arrow pointing at a data point.
17
+ * `x`/`y` are in data space (band scales point at the band centre); `dx`/`dy`
18
+ * place the label in pixels relative to that point. Newlines split the label.
19
+ */
20
+ declare const Callout: import("svelte").Component<Props, {}, "">;
21
+ type Callout = ReturnType<typeof Callout>;
22
+ export default Callout;
@@ -0,0 +1,55 @@
1
+ <!--
2
+ @component
3
+ A label next to a data point, with an optional marker dot. For labelling a
4
+ few highlighted points in a scatter plot. Text has a halo for legibility.
5
+ -->
6
+ <script lang="ts">
7
+ import { usePlot } from '../core/context.js';
8
+ import { position } from '../core/scales.js';
9
+
10
+ interface Props {
11
+ x: unknown;
12
+ y: unknown;
13
+ label: string;
14
+ dx?: number;
15
+ dy?: number;
16
+ anchor?: 'start' | 'middle' | 'end';
17
+ /** Draw a ring around the point. */
18
+ marker?: boolean;
19
+ bold?: boolean;
20
+ class?: string;
21
+ }
22
+
23
+ let { x, y, label, dx = 6, dy = 0, anchor, marker = false, bold = false, class: className = '' }: Props = $props();
24
+
25
+ const plot = usePlot();
26
+ const p = $derived(position(plot, x, y));
27
+ const textAnchor = $derived(anchor ?? (dx > 0 ? 'start' : dx < 0 ? 'end' : 'middle'));
28
+ </script>
29
+
30
+ <g class="mz-label {className}">
31
+ {#if marker}<circle cx={p[0]} cy={p[1]} r={6} />{/if}
32
+ <text
33
+ x={p[0] + dx}
34
+ y={p[1] + dy}
35
+ dy="0.32em"
36
+ text-anchor={textAnchor}
37
+ style:font-weight={bold ? 'var(--mz-weight-strong)' : undefined}>{label}</text
38
+ >
39
+ </g>
40
+
41
+ <style>
42
+ circle {
43
+ fill: none;
44
+ stroke: var(--mz-color-text);
45
+ stroke-width: 1.5;
46
+ }
47
+ text {
48
+ fill: var(--mz-color-text);
49
+ font-size: var(--mz-size-axis);
50
+ paint-order: stroke;
51
+ stroke: var(--mz-color-bg);
52
+ stroke-width: 3px;
53
+ stroke-linejoin: round;
54
+ }
55
+ </style>
@@ -0,0 +1,19 @@
1
+ interface Props {
2
+ x: unknown;
3
+ y: unknown;
4
+ label: string;
5
+ dx?: number;
6
+ dy?: number;
7
+ anchor?: 'start' | 'middle' | 'end';
8
+ /** Draw a ring around the point. */
9
+ marker?: boolean;
10
+ bold?: boolean;
11
+ class?: string;
12
+ }
13
+ /**
14
+ * A label next to a data point, with an optional marker dot. For labelling a
15
+ * few highlighted points in a scatter plot. Text has a halo for legibility.
16
+ */
17
+ declare const Label: import("svelte").Component<Props, {}, "">;
18
+ type Label = ReturnType<typeof Label>;
19
+ export default Label;
@@ -0,0 +1,46 @@
1
+ <!--
2
+ @component
3
+ A shaded band between two x values (a period: a recession, a lockdown) or
4
+ two y values (a target range), with an optional label. Draw it first in
5
+ `<Svg>` so marks sit on top.
6
+ -->
7
+ <script lang="ts">
8
+ import { usePlot } from '../core/context.js';
9
+
10
+ interface Props {
11
+ x1?: unknown;
12
+ x2?: unknown;
13
+ y1?: unknown;
14
+ y2?: unknown;
15
+ label?: string;
16
+ fill?: string;
17
+ opacity?: number;
18
+ class?: string;
19
+ }
20
+
21
+ let { x1, x2, y1, y2, label, fill = 'var(--mz-color-grid)', opacity = 0.6, class: className = '' }: Props = $props();
22
+
23
+ const plot = usePlot();
24
+
25
+ const box = $derived.by(() => {
26
+ const xs = x1 !== undefined && x2 !== undefined ? [plot.x(x1) ?? 0, plot.x(x2) ?? 0] : [0, plot.width];
27
+ const ys = y1 !== undefined && y2 !== undefined ? [plot.y(y1) ?? 0, plot.y(y2) ?? 0] : [0, plot.height];
28
+ return { x: Math.min(...xs), y: Math.min(...ys), width: Math.abs(xs[1] - xs[0]), height: Math.abs(ys[1] - ys[0]) };
29
+ });
30
+ </script>
31
+
32
+ <g class="mz-ref-band {className}">
33
+ <rect x={box.x} y={box.y} width={box.width} height={box.height} style:fill fill-opacity={opacity} />
34
+ {#if label}<text x={box.x + 4} y={box.y + 4} dy="0.9em">{label}</text>{/if}
35
+ </g>
36
+
37
+ <style>
38
+ text {
39
+ fill: var(--mz-color-muted);
40
+ font-size: var(--mz-size-axis);
41
+ font-family: var(--mz-font-label);
42
+ font-weight: var(--mz-weight-label);
43
+ text-transform: var(--mz-label-transform);
44
+ letter-spacing: var(--mz-label-tracking);
45
+ }
46
+ </style>
@@ -0,0 +1,18 @@
1
+ interface Props {
2
+ x1?: unknown;
3
+ x2?: unknown;
4
+ y1?: unknown;
5
+ y2?: unknown;
6
+ label?: string;
7
+ fill?: string;
8
+ opacity?: number;
9
+ class?: string;
10
+ }
11
+ /**
12
+ * A shaded band between two x values (a period: a recession, a lockdown) or
13
+ * two y values (a target range), with an optional label. Draw it first in
14
+ * `<Svg>` so marks sit on top.
15
+ */
16
+ declare const RefBand: import("svelte").Component<Props, {}, "">;
17
+ type RefBand = ReturnType<typeof RefBand>;
18
+ export default RefBand;
@@ -0,0 +1,72 @@
1
+ <!--
2
+ @component
3
+ A labelled reference line at an x value (vertical) or y value (horizontal):
4
+ a target, an average, an event date.
5
+ -->
6
+ <script lang="ts">
7
+ import { usePlot } from '../core/context.js';
8
+ import { scaled } from '../core/scales.js';
9
+
10
+ interface Props {
11
+ /** Vertical line at this x value. */
12
+ x?: unknown;
13
+ /** Horizontal line at this y value. */
14
+ y?: unknown;
15
+ label?: string;
16
+ /** Where the label sits along the line. */
17
+ labelPosition?: 'start' | 'end';
18
+ dash?: string;
19
+ stroke?: string;
20
+ class?: string;
21
+ }
22
+
23
+ let {
24
+ x,
25
+ y,
26
+ label,
27
+ labelPosition = 'end',
28
+ dash = '4 3',
29
+ stroke = 'var(--mz-color-text)',
30
+ class: className = ''
31
+ }: Props = $props();
32
+
33
+ const plot = usePlot();
34
+ const px = $derived(x === undefined ? undefined : scaled(plot.x, x));
35
+ const py = $derived(y === undefined ? undefined : scaled(plot.y, y));
36
+ </script>
37
+
38
+ <g class="mz-ref-line {className}">
39
+ {#if px !== undefined}
40
+ <line x1={px} x2={px} y2={plot.height} style:stroke stroke-dasharray={dash} />
41
+ {#if label}
42
+ <text x={px + 4} y={labelPosition === 'end' ? 0 : plot.height - 4} dy={labelPosition === 'end' ? '0.9em' : '0'}
43
+ >{label}</text
44
+ >
45
+ {/if}
46
+ {/if}
47
+ {#if py !== undefined}
48
+ <line y1={py} y2={py} x2={plot.width} style:stroke stroke-dasharray={dash} />
49
+ {#if label}
50
+ <text
51
+ x={labelPosition === 'end' ? plot.width : 0}
52
+ y={py - 4}
53
+ text-anchor={labelPosition === 'end' ? 'end' : 'start'}>{label}</text
54
+ >
55
+ {/if}
56
+ {/if}
57
+ </g>
58
+
59
+ <style>
60
+ line {
61
+ stroke-width: 1;
62
+ shape-rendering: crispEdges;
63
+ }
64
+ text {
65
+ fill: var(--mz-color-text);
66
+ font-size: var(--mz-size-axis);
67
+ paint-order: stroke;
68
+ stroke: var(--mz-color-bg);
69
+ stroke-width: 3px;
70
+ stroke-linejoin: round;
71
+ }
72
+ </style>
@@ -0,0 +1,19 @@
1
+ interface Props {
2
+ /** Vertical line at this x value. */
3
+ x?: unknown;
4
+ /** Horizontal line at this y value. */
5
+ y?: unknown;
6
+ label?: string;
7
+ /** Where the label sits along the line. */
8
+ labelPosition?: 'start' | 'end';
9
+ dash?: string;
10
+ stroke?: string;
11
+ class?: string;
12
+ }
13
+ /**
14
+ * A labelled reference line at an x value (vertical) or y value (horizontal):
15
+ * a target, an average, an event date.
16
+ */
17
+ declare const RefLine: import("svelte").Component<Props, {}, "">;
18
+ type RefLine = ReturnType<typeof RefLine>;
19
+ export default RefLine;
@@ -0,0 +1,23 @@
1
+ export interface CalloutGeometry {
2
+ /** Curved connector from the label to the target. */
3
+ arrow: string;
4
+ /** Arrowhead at the target end. */
5
+ head: string;
6
+ }
7
+ /**
8
+ * Connector from a label at (lx, ly) to a target at (tx, ty). The label block
9
+ * spans `top`–`bottom` vertically; `anchor` is the label's text-anchor.
10
+ * `bend` > 0 curves clockwise, < 0 anticlockwise, 0 is straight.
11
+ */
12
+ export declare function calloutGeometry({ tx, ty, lx, ly, top, bottom, anchor, bend, gap, headSize }: {
13
+ tx: number;
14
+ ty: number;
15
+ lx: number;
16
+ ly: number;
17
+ top: number;
18
+ bottom: number;
19
+ anchor: 'start' | 'middle' | 'end';
20
+ bend: number;
21
+ gap?: number;
22
+ headSize?: number;
23
+ }): CalloutGeometry;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Connector from a label at (lx, ly) to a target at (tx, ty). The label block
3
+ * spans `top`–`bottom` vertically; `anchor` is the label's text-anchor.
4
+ * `bend` > 0 curves clockwise, < 0 anticlockwise, 0 is straight.
5
+ */
6
+ export function calloutGeometry({ tx, ty, lx, ly, top, bottom, anchor, bend, gap = 4, headSize = 6 }) {
7
+ const dx = lx - tx;
8
+ const dy = ly - ty;
9
+ // Start from the side of the label that faces the target.
10
+ let sx;
11
+ let sy;
12
+ if (Math.abs(dy) >= Math.abs(dx) || anchor === 'middle') {
13
+ sx = lx;
14
+ sy = dy < 0 ? bottom + gap : top - gap;
15
+ }
16
+ else {
17
+ sx = anchor === 'start' ? lx - gap : lx + gap;
18
+ sy = (top + bottom) / 2;
19
+ }
20
+ // Stop just short of the target.
21
+ const len = Math.hypot(tx - sx, ty - sy) || 1;
22
+ const ex = tx - ((tx - sx) / len) * gap;
23
+ const ey = ty - ((ty - sy) / len) * gap;
24
+ // Control point: the midpoint pushed sideways by `bend`.
25
+ const mx = (sx + ex) / 2;
26
+ const my = (sy + ey) / 2;
27
+ const cx = mx + (ey - sy) * bend;
28
+ const cy = my - (ex - sx) * bend;
29
+ // Arrowhead along the curve's tangent at the end (control point → end).
30
+ const tl = Math.hypot(ex - cx, ey - cy) || 1;
31
+ const ux = (ex - cx) / tl;
32
+ const uy = (ey - cy) / tl;
33
+ const spread = (28 * Math.PI) / 180;
34
+ const wing = (angle) => {
35
+ const cos = Math.cos(angle);
36
+ const sin = Math.sin(angle);
37
+ return [ex - headSize * (ux * cos - uy * sin), ey - headSize * (ux * sin + uy * cos)];
38
+ };
39
+ const [ax, ay] = wing(spread);
40
+ const [bx, by] = wing(-spread);
41
+ return {
42
+ arrow: `M${sx},${sy}Q${cx},${cy} ${ex},${ey}`,
43
+ head: `M${ax},${ay}L${ex},${ey}L${bx},${by}`
44
+ };
45
+ }
@@ -0,0 +1,6 @@
1
+ export { default as Callout } from './Callout.svelte';
2
+ export { default as Arrow } from './Arrow.svelte';
3
+ export { default as Label } from './Label.svelte';
4
+ export { default as RefLine } from './RefLine.svelte';
5
+ export { default as RefBand } from './RefBand.svelte';
6
+ export { calloutGeometry } from './callout-geometry.js';
@@ -0,0 +1,6 @@
1
+ export { default as Callout } from './Callout.svelte';
2
+ export { default as Arrow } from './Arrow.svelte';
3
+ export { default as Label } from './Label.svelte';
4
+ export { default as RefLine } from './RefLine.svelte';
5
+ export { default as RefBand } from './RefBand.svelte';
6
+ export { calloutGeometry } from './callout-geometry.js';
@@ -0,0 +1,55 @@
1
+ <!--
2
+ @component
3
+ A canvas layer over the plot area for very large datasets (thousands of
4
+ points), where SVG gets slow. `draw` runs whenever anything it reads from
5
+ the plot changes; the context is already scaled for high-DPI screens and
6
+ translated to plot-area coordinates. Canvas content does not print: draw an
7
+ SVG fallback when `plot.medium === 'print'`.
8
+
9
+ ```svelte
10
+ <Canvas draw={(ctx, plot) => { for (const d of data) { ctx.fillRect(plot.x(d.x), plot.y(d.y), 2, 2); } }} />
11
+ ```
12
+ -->
13
+ <script lang="ts">
14
+ import { usePlot } from './context.js';
15
+ import type { PlotContext } from './types.js';
16
+
17
+ interface Props {
18
+ draw: (ctx: CanvasRenderingContext2D, plot: PlotContext) => void;
19
+ class?: string;
20
+ }
21
+
22
+ let { draw, class: className = '' }: Props = $props();
23
+
24
+ const plot = usePlot();
25
+ let canvas = $state<HTMLCanvasElement>();
26
+
27
+ $effect(() => {
28
+ if (!canvas) return;
29
+ const dpr = window.devicePixelRatio || 1;
30
+ canvas.width = Math.round(plot.width * dpr);
31
+ canvas.height = Math.round(plot.height * dpr);
32
+ const ctx = canvas.getContext('2d');
33
+ if (!ctx) return;
34
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
35
+ ctx.clearRect(0, 0, plot.width, plot.height);
36
+ draw(ctx, plot);
37
+ });
38
+ </script>
39
+
40
+ <canvas
41
+ bind:this={canvas}
42
+ class="mz-layer mz-canvas {className}"
43
+ aria-hidden="true"
44
+ style:left="{plot.padding.left}px"
45
+ style:top="{plot.padding.top}px"
46
+ style:width="{plot.width}px"
47
+ style:height="{plot.height}px"
48
+ ></canvas>
49
+
50
+ <style>
51
+ .mz-canvas {
52
+ position: absolute;
53
+ pointer-events: none;
54
+ }
55
+ </style>
@@ -0,0 +1,19 @@
1
+ import type { PlotContext } from './types.js';
2
+ interface Props {
3
+ draw: (ctx: CanvasRenderingContext2D, plot: PlotContext) => void;
4
+ class?: string;
5
+ }
6
+ /**
7
+ * A canvas layer over the plot area for very large datasets (thousands of
8
+ * points), where SVG gets slow. `draw` runs whenever anything it reads from
9
+ * the plot changes; the context is already scaled for high-DPI screens and
10
+ * translated to plot-area coordinates. Canvas content does not print: draw an
11
+ * SVG fallback when `plot.medium === 'print'`.
12
+ *
13
+ * ```svelte
14
+ * <Canvas draw={(ctx, plot) => { for (const d of data) { ctx.fillRect(plot.x(d.x), plot.y(d.y), 2, 2); } }} />
15
+ * ```
16
+ */
17
+ declare const Canvas: import("svelte").Component<Props, {}, "">;
18
+ type Canvas = ReturnType<typeof Canvas>;
19
+ export default Canvas;