@samizdata/mozaic 0.0.0-stage → 0.3.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 +107 -0
  12. package/dist/annotations/RefLine.svelte.d.ts +27 -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 +107 -0
  90. package/dist/guides/AxisY.svelte.d.ts +25 -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 +116 -0
  116. package/dist/interactive/Tooltip.svelte.d.ts +34 -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 +77 -0
  162. package/dist/marks/Waffle.svelte.d.ts +45 -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,107 @@
1
+ <!--
2
+ @component
3
+ Vertical axis for the plot's y scale. With `inside`, labels sit just above
4
+ the gridlines inside the plot area, which saves left padding on small screens.
5
+ `unit` names the unit once, after the top tick label ("50 lei/m³"); on a
6
+ left axis it runs into the plot, so it needs no extra padding.
7
+ -->
8
+ <script lang="ts">
9
+ import { useChart, usePlot } from '../core/context.js';
10
+ import { scaled } from '../core/scales.js';
11
+ import { defaultTickFormat, tickValues } from './ticks.js';
12
+
13
+ interface Props {
14
+ ticks?: number;
15
+ tickValues?: unknown[];
16
+ format?: (value: any, index: number) => string;
17
+ position?: 'left' | 'right';
18
+ /** Put labels above the gridlines, inside the plot area. */
19
+ inside?: boolean;
20
+ tickSize?: number;
21
+ tickPadding?: number;
22
+ baseline?: boolean;
23
+ /** Axis title, drawn above the axis. */
24
+ label?: string;
25
+ /** Unit written after the top tick label, e.g. "lei/m³" or "%". */
26
+ unit?: string;
27
+ class?: string;
28
+ }
29
+
30
+ const chart = useChart();
31
+ const plot = usePlot();
32
+
33
+ let {
34
+ ticks,
35
+ tickValues: explicitValues,
36
+ format,
37
+ position = 'left',
38
+ inside = false,
39
+ tickSize: tickSizeInput,
40
+ tickPadding: tickPaddingInput,
41
+ baseline: baselineInput,
42
+ label,
43
+ unit,
44
+ class: className = ''
45
+ }: Props = $props();
46
+
47
+ // Theme defaults are derived (not destructuring defaults) so they follow style changes.
48
+ const tickSize = $derived(tickSizeInput ?? chart.theme.plot.tickSize);
49
+ const tickPadding = $derived(tickPaddingInput ?? chart.theme.plot.tickPadding);
50
+ const baseline = $derived(baselineInput ?? chart.theme.plot.axisLine);
51
+ const values = $derived(tickValues(plot.y, ticks ?? chart.theme.plot.ticks, explicitValues));
52
+ const fmt = $derived(format ?? defaultTickFormat(values, chart.locale));
53
+ const dir = $derived(position === 'left' ? -1 : 1);
54
+ const anchor = $derived(inside ? (position === 'left' ? 'start' : 'end') : position === 'left' ? 'end' : 'start');
55
+ // The tick nearest the top of the plot carries the unit.
56
+ const top = $derived(
57
+ unit
58
+ ? values.reduce((a, b) => ((scaled(plot.y, b) ?? Infinity) < (scaled(plot.y, a) ?? Infinity) ? b : a), values[0])
59
+ : undefined
60
+ );
61
+ // Outside a left axis, the unit starts where the numbers end and runs into the plot.
62
+ const unitApart = $derived(position === 'left' && !inside);
63
+ </script>
64
+
65
+ <g class="mz-axis mz-axis-y {className}" transform="translate({position === 'left' ? 0 : plot.width},0)">
66
+ {#if baseline}<line class="mz-axis-line" y2={plot.height} />{/if}
67
+ {#each values as value, i (value)}
68
+ {@const withUnit = unit && value === top && !unitApart ? `${fmt(value, i)} ${unit}` : fmt(value, i)}
69
+ <g class="mz-tick" transform="translate(0,{scaled(plot.y, value)})">
70
+ {#if tickSize > 0 && !inside}<line x2={dir * tickSize} />{/if}
71
+ {#if inside}
72
+ <text x={0} dy="-0.35em" text-anchor={anchor}>{withUnit}</text>
73
+ {:else}
74
+ <text x={dir * (tickSize + tickPadding)} dy="0.32em" text-anchor={anchor}>{withUnit}</text>
75
+ {/if}
76
+ {#if unit && value === top && unitApart}
77
+ <text class="mz-axis-unit" x={-(tickSize + tickPadding) + 4} dy="0.32em" text-anchor="start">{unit}</text>
78
+ {/if}
79
+ </g>
80
+ {/each}
81
+ {#if label}
82
+ <text class="mz-axis-label" y={-12} text-anchor={position === 'left' ? 'start' : 'end'}>{label}</text>
83
+ {/if}
84
+ </g>
85
+
86
+ <style>
87
+ .mz-axis text {
88
+ fill: var(--mz-color-muted);
89
+ font-size: var(--mz-size-axis);
90
+ }
91
+ .mz-axis-unit {
92
+ paint-order: stroke;
93
+ stroke: var(--mz-color-bg);
94
+ stroke-width: 3px;
95
+ stroke-linejoin: round;
96
+ }
97
+ .mz-axis line {
98
+ stroke: var(--mz-color-axis);
99
+ shape-rendering: crispEdges;
100
+ }
101
+ .mz-axis-label {
102
+ font-family: var(--mz-font-label);
103
+ font-weight: var(--mz-weight-label);
104
+ text-transform: var(--mz-label-transform);
105
+ letter-spacing: var(--mz-label-tracking);
106
+ }
107
+ </style>
@@ -0,0 +1,25 @@
1
+ interface Props {
2
+ ticks?: number;
3
+ tickValues?: unknown[];
4
+ format?: (value: any, index: number) => string;
5
+ position?: 'left' | 'right';
6
+ /** Put labels above the gridlines, inside the plot area. */
7
+ inside?: boolean;
8
+ tickSize?: number;
9
+ tickPadding?: number;
10
+ baseline?: boolean;
11
+ /** Axis title, drawn above the axis. */
12
+ label?: string;
13
+ /** Unit written after the top tick label, e.g. "lei/m³" or "%". */
14
+ unit?: string;
15
+ class?: string;
16
+ }
17
+ /**
18
+ * Vertical axis for the plot's y scale. With `inside`, labels sit just above
19
+ * the gridlines inside the plot area, which saves left padding on small screens.
20
+ * `unit` names the unit once, after the top tick label ("50 lei/m³"); on a
21
+ * left axis it runs into the plot, so it needs no extra padding.
22
+ */
23
+ declare const AxisY: import("svelte").Component<Props, {}, "">;
24
+ type AxisY = ReturnType<typeof AxisY>;
25
+ export default AxisY;
@@ -0,0 +1,93 @@
1
+ <!--
2
+ @component
3
+ A continuous colour legend: a gradient bar with ticks, for sequential or
4
+ diverging colour scales (`scaleSequential`, `scaleDiverging`, or a linear
5
+ scale with a colour range). Drawn as SVG so it prints and exports.
6
+ -->
7
+ <script lang="ts">
8
+ import { useChart } from '../core/context.js';
9
+
10
+ type ColorScale = { (value: number): string; domain(): number[]; ticks?: (count?: number) => number[] };
11
+
12
+ interface Props {
13
+ scale: ColorScale;
14
+ /** Width in px. */
15
+ width?: number;
16
+ /** Bar height in px. */
17
+ height?: number;
18
+ ticks?: number;
19
+ tickValues?: number[];
20
+ format?: (value: number) => string;
21
+ title?: string;
22
+ class?: string;
23
+ }
24
+
25
+ let {
26
+ scale,
27
+ width = 240,
28
+ height = 10,
29
+ ticks = 5,
30
+ tickValues,
31
+ format,
32
+ title,
33
+ class: className = ''
34
+ }: Props = $props();
35
+
36
+ const chart = useChart();
37
+ const uid = $props.id();
38
+ const STEPS = 32;
39
+
40
+ const domain = $derived(scale.domain());
41
+ const lo = $derived(domain[0]);
42
+ const hi = $derived(domain[domain.length - 1]);
43
+ const stops = $derived(
44
+ Array.from({ length: STEPS + 1 }, (_, i) => ({ offset: i / STEPS, color: scale(lo + ((hi - lo) * i) / STEPS) }))
45
+ );
46
+ const values = $derived(tickValues ?? scale.ticks?.(ticks) ?? domain);
47
+ const fmt = $derived(format ?? chart.locale.number);
48
+ const xOf = (v: number) => (hi === lo ? 0 : ((v - lo) / (hi - lo)) * width);
49
+ const top = $derived(title ? 16 : 0);
50
+ </script>
51
+
52
+ <svg
53
+ class="mz-color-ramp {className}"
54
+ width={width + 16}
55
+ height={top + height + 20}
56
+ viewBox="-8 0 {width + 16} {top + height + 20}"
57
+ role="img"
58
+ aria-label={title}
59
+ >
60
+ <defs>
61
+ <linearGradient id="{uid}-ramp">
62
+ {#each stops as stop}<stop offset={stop.offset} stop-color={stop.color} />{/each}
63
+ </linearGradient>
64
+ </defs>
65
+ {#if title}<text class="mz-ramp-title" x={0} y={11}>{title}</text>{/if}
66
+ <rect y={top} {width} {height} fill="url(#{uid}-ramp)" />
67
+ {#each values as v}
68
+ <line x1={xOf(v)} x2={xOf(v)} y1={top} y2={top + height + 3} />
69
+ <text x={xOf(v)} y={top + height + 14} text-anchor="middle">{fmt(v)}</text>
70
+ {/each}
71
+ </svg>
72
+
73
+ <style>
74
+ .mz-color-ramp {
75
+ display: block;
76
+ overflow: visible;
77
+ font-family: var(--mz-font-body);
78
+ }
79
+ text {
80
+ fill: var(--mz-color-muted);
81
+ font-size: var(--mz-size-axis);
82
+ }
83
+ .mz-ramp-title {
84
+ fill: var(--mz-color-text);
85
+ font-family: var(--mz-font-label);
86
+ font-weight: var(--mz-weight-label);
87
+ text-transform: var(--mz-label-transform);
88
+ letter-spacing: var(--mz-label-tracking);
89
+ }
90
+ line {
91
+ stroke: var(--mz-color-bg);
92
+ }
93
+ </style>
@@ -0,0 +1,25 @@
1
+ type ColorScale = {
2
+ (value: number): string;
3
+ domain(): number[];
4
+ ticks?: (count?: number) => number[];
5
+ };
6
+ interface Props {
7
+ scale: ColorScale;
8
+ /** Width in px. */
9
+ width?: number;
10
+ /** Bar height in px. */
11
+ height?: number;
12
+ ticks?: number;
13
+ tickValues?: number[];
14
+ format?: (value: number) => string;
15
+ title?: string;
16
+ class?: string;
17
+ }
18
+ /**
19
+ * A continuous colour legend: a gradient bar with ticks, for sequential or
20
+ * diverging colour scales (`scaleSequential`, `scaleDiverging`, or a linear
21
+ * scale with a colour range). Drawn as SVG so it prints and exports.
22
+ */
23
+ declare const ColorRamp: import("svelte").Component<Props, {}, "">;
24
+ type ColorRamp = ReturnType<typeof ColorRamp>;
25
+ export default ColorRamp;
@@ -0,0 +1,35 @@
1
+ <!--
2
+ @component
3
+ Vertical gridlines at the x scale's ticks. Hairline and recessive by default.
4
+ -->
5
+ <script lang="ts">
6
+ import { useChart, usePlot } from '../core/context.js';
7
+ import { scaled } from '../core/scales.js';
8
+ import { tickValues } from './ticks.js';
9
+
10
+ let {
11
+ ticks,
12
+ tickValues: explicitValues,
13
+ class: className = ''
14
+ }: { ticks?: number; tickValues?: unknown[]; class?: string } = $props();
15
+
16
+ const chart = useChart();
17
+ const plot = usePlot();
18
+ const values = $derived(tickValues(plot.x, ticks ?? chart.theme.plot.ticks, explicitValues));
19
+ </script>
20
+
21
+ <g class="mz-grid mz-grid-x {className}">
22
+ {#each values as value (value)}
23
+ {@const px = scaled(plot.x, value)}
24
+ <line x1={px} x2={px} y2={plot.height} />
25
+ {/each}
26
+ </g>
27
+
28
+ <style>
29
+ .mz-grid line {
30
+ stroke: var(--mz-color-grid);
31
+ stroke-width: 1;
32
+ stroke-dasharray: var(--mz-grid-dash);
33
+ shape-rendering: crispEdges;
34
+ }
35
+ </style>
@@ -0,0 +1,9 @@
1
+ type $$ComponentProps = {
2
+ ticks?: number;
3
+ tickValues?: unknown[];
4
+ class?: string;
5
+ };
6
+ /** Vertical gridlines at the x scale's ticks. Hairline and recessive by default. */
7
+ declare const GridX: import("svelte").Component<$$ComponentProps, {}, "">;
8
+ type GridX = ReturnType<typeof GridX>;
9
+ export default GridX;
@@ -0,0 +1,35 @@
1
+ <!--
2
+ @component
3
+ Horizontal gridlines at the y scale's ticks. Hairline and recessive by default.
4
+ -->
5
+ <script lang="ts">
6
+ import { useChart, usePlot } from '../core/context.js';
7
+ import { scaled } from '../core/scales.js';
8
+ import { tickValues } from './ticks.js';
9
+
10
+ let {
11
+ ticks,
12
+ tickValues: explicitValues,
13
+ class: className = ''
14
+ }: { ticks?: number; tickValues?: unknown[]; class?: string } = $props();
15
+
16
+ const chart = useChart();
17
+ const plot = usePlot();
18
+ const values = $derived(tickValues(plot.y, ticks ?? chart.theme.plot.ticks, explicitValues));
19
+ </script>
20
+
21
+ <g class="mz-grid mz-grid-y {className}">
22
+ {#each values as value (value)}
23
+ {@const py = scaled(plot.y, value)}
24
+ <line y1={py} y2={py} x2={plot.width} />
25
+ {/each}
26
+ </g>
27
+
28
+ <style>
29
+ .mz-grid line {
30
+ stroke: var(--mz-color-grid);
31
+ stroke-width: 1;
32
+ stroke-dasharray: var(--mz-grid-dash);
33
+ shape-rendering: crispEdges;
34
+ }
35
+ </style>
@@ -0,0 +1,9 @@
1
+ type $$ComponentProps = {
2
+ ticks?: number;
3
+ tickValues?: unknown[];
4
+ class?: string;
5
+ };
6
+ /** Horizontal gridlines at the y scale's ticks. Hairline and recessive by default. */
7
+ declare const GridY: import("svelte").Component<$$ComponentProps, {}, "">;
8
+ type GridY = ReturnType<typeof GridY>;
9
+ export default GridY;
@@ -0,0 +1,48 @@
1
+ <!--
2
+ @component
3
+ An inline legend entry for use inside text, e.g. a subtitle:
4
+ `Spending on <Key color={a}>health</Key> and <Key color={b}>education</Key>`.
5
+ The swatch carries the colour; the text stays in the text colour.
6
+ -->
7
+ <script lang="ts">
8
+ import type { Snippet } from 'svelte';
9
+
10
+ let {
11
+ color,
12
+ shape = 'square',
13
+ children
14
+ }: { color: string; shape?: 'square' | 'line' | 'circle'; children?: Snippet } = $props();
15
+ </script>
16
+
17
+ <span class="mz-key"
18
+ ><span class="mz-key-swatch mz-key-{shape}" style:background={color} aria-hidden="true"
19
+ ></span>{@render children?.()}</span
20
+ >
21
+
22
+ <style>
23
+ .mz-key {
24
+ white-space: nowrap;
25
+ font-weight: var(--mz-weight-strong);
26
+ color: var(--mz-color-text);
27
+ }
28
+ .mz-key-swatch {
29
+ display: inline-block;
30
+ margin-right: 0.3em;
31
+ vertical-align: 0.05em;
32
+ }
33
+ .mz-key-square {
34
+ width: 0.7em;
35
+ height: 0.7em;
36
+ border-radius: min(var(--mz-radius), 2px);
37
+ }
38
+ .mz-key-circle {
39
+ width: 0.65em;
40
+ height: 0.65em;
41
+ border-radius: 50%;
42
+ }
43
+ .mz-key-line {
44
+ width: 1em;
45
+ height: 2px;
46
+ vertical-align: 0.3em;
47
+ }
48
+ </style>
@@ -0,0 +1,14 @@
1
+ import type { Snippet } from 'svelte';
2
+ type $$ComponentProps = {
3
+ color: string;
4
+ shape?: 'square' | 'line' | 'circle';
5
+ children?: Snippet;
6
+ };
7
+ /**
8
+ * An inline legend entry for use inside text, e.g. a subtitle:
9
+ * `Spending on <Key color={a}>health</Key> and <Key color={b}>education</Key>`.
10
+ * The swatch carries the colour; the text stays in the text colour.
11
+ */
12
+ declare const Key: import("svelte").Component<$$ComponentProps, {}, "">;
13
+ type Key = ReturnType<typeof Key>;
14
+ export default Key;
@@ -0,0 +1,112 @@
1
+ <!--
2
+ @component
3
+ A row of swatches with labels. Pass `items` directly, or a d3 ordinal or
4
+ threshold colour `scale` (with `format` for threshold values). Text uses
5
+ text colours; only the swatch carries the data colour.
6
+ -->
7
+ <script lang="ts">
8
+ import { useChart } from '../core/context.js';
9
+
10
+ type Item = { label: string; color: string };
11
+ type ColorScale = {
12
+ domain(): any[];
13
+ range(): string[];
14
+ invertExtent?: (color: string) => [any, any];
15
+ };
16
+
17
+ interface Props {
18
+ items?: Item[];
19
+ scale?: ColorScale;
20
+ /** Formats threshold values (or ordinal domain values). */
21
+ format?: (value: any) => string;
22
+ shape?: 'square' | 'line' | 'circle';
23
+ title?: string;
24
+ class?: string;
25
+ }
26
+
27
+ let { items, scale, format, shape = 'square', title, class: className = '' }: Props = $props();
28
+
29
+ const chart = useChart();
30
+
31
+ function fromScale(s: ColorScale): Item[] {
32
+ const fmt = format ?? ((v: unknown) => (typeof v === 'number' ? chart.locale.number(v) : String(v)));
33
+ if (s.invertExtent) {
34
+ // Threshold scale: n thresholds, n + 1 colours.
35
+ const thresholds = s.domain();
36
+ return s.range().map((color, i) => {
37
+ const label =
38
+ i === 0
39
+ ? `< ${fmt(thresholds[0])}`
40
+ : i === thresholds.length
41
+ ? `≥ ${fmt(thresholds[i - 1])}`
42
+ : `${fmt(thresholds[i - 1])}–${fmt(thresholds[i])}`;
43
+ return { label, color };
44
+ });
45
+ }
46
+ const range = s.range();
47
+ return s.domain().map((value, i) => ({ label: fmt(value), color: range[i % range.length] }));
48
+ }
49
+
50
+ const resolved = $derived(items ?? (scale ? fromScale(scale) : []));
51
+ </script>
52
+
53
+ <div class="mz-legend {className}">
54
+ {#if title}<span class="mz-legend-title">{title}</span>{/if}
55
+ <ul>
56
+ {#each resolved as item (item.label)}
57
+ <li>
58
+ <span class="mz-swatch mz-swatch-{shape}" style:background={item.color}></span>
59
+ {item.label}
60
+ </li>
61
+ {/each}
62
+ </ul>
63
+ </div>
64
+
65
+ <style>
66
+ .mz-legend {
67
+ display: flex;
68
+ flex-wrap: wrap;
69
+ align-items: center;
70
+ gap: 0.25rem 0.75rem;
71
+ font-size: var(--mz-size-small);
72
+ color: var(--mz-color-text);
73
+ }
74
+ .mz-legend-title {
75
+ font-family: var(--mz-font-label);
76
+ font-weight: var(--mz-weight-label);
77
+ text-transform: var(--mz-label-transform);
78
+ letter-spacing: var(--mz-label-tracking);
79
+ }
80
+ ul {
81
+ display: flex;
82
+ flex-wrap: wrap;
83
+ gap: 0.25rem 0.75rem;
84
+ margin: 0;
85
+ padding: 0;
86
+ list-style: none;
87
+ }
88
+ li {
89
+ display: flex;
90
+ align-items: center;
91
+ gap: 0.375rem;
92
+ }
93
+ .mz-swatch {
94
+ display: inline-block;
95
+ flex: none;
96
+ }
97
+ .mz-swatch-square {
98
+ width: 0.75rem;
99
+ height: 0.75rem;
100
+ border-radius: min(var(--mz-radius), 2px);
101
+ }
102
+ .mz-swatch-circle {
103
+ width: 0.625rem;
104
+ height: 0.625rem;
105
+ border-radius: 50%;
106
+ }
107
+ .mz-swatch-line {
108
+ width: 1rem;
109
+ height: 2px;
110
+ border-radius: 1px;
111
+ }
112
+ </style>
@@ -0,0 +1,26 @@
1
+ type Item = {
2
+ label: string;
3
+ color: string;
4
+ };
5
+ type ColorScale = {
6
+ domain(): any[];
7
+ range(): string[];
8
+ invertExtent?: (color: string) => [any, any];
9
+ };
10
+ interface Props {
11
+ items?: Item[];
12
+ scale?: ColorScale;
13
+ /** Formats threshold values (or ordinal domain values). */
14
+ format?: (value: any) => string;
15
+ shape?: 'square' | 'line' | 'circle';
16
+ title?: string;
17
+ class?: string;
18
+ }
19
+ /**
20
+ * A row of swatches with labels. Pass `items` directly, or a d3 ordinal or
21
+ * threshold colour `scale` (with `format` for threshold values). Text uses
22
+ * text colours; only the swatch carries the data colour.
23
+ */
24
+ declare const Legend: import("svelte").Component<Props, {}, "">;
25
+ type Legend = ReturnType<typeof Legend>;
26
+ export default Legend;
@@ -0,0 +1,62 @@
1
+ <!--
2
+ @component
3
+ Nested circles explaining a radius scale (usually `scaleSqrt`), for
4
+ proportional symbol maps and bubble charts. Drawn as SVG.
5
+ -->
6
+ <script lang="ts">
7
+ import { useChart } from '../core/context.js';
8
+
9
+ interface Props {
10
+ /** Value → radius in px. */
11
+ scale: (value: number) => number;
12
+ values: number[];
13
+ format?: (value: number) => string;
14
+ title?: string;
15
+ class?: string;
16
+ }
17
+
18
+ let { scale, values, format, title, class: className = '' }: Props = $props();
19
+
20
+ const chart = useChart();
21
+ const sorted = $derived([...values].sort((a, b) => b - a));
22
+ const maxR = $derived(Math.max(...sorted.map((v) => scale(v)), 1));
23
+ const top = $derived(title ? 16 : 0);
24
+ const fmt = $derived(format ?? chart.locale.number);
25
+ </script>
26
+
27
+ <svg class="mz-size-legend {className}" width={maxR * 2 + 60} height={top + maxR * 2 + 2} role="img" aria-label={title}>
28
+ {#if title}<text class="mz-size-title" x={0} y={11}>{title}</text>{/if}
29
+ {#each sorted as v}
30
+ {@const r = scale(v)}
31
+ <circle cx={maxR + 1} cy={top + maxR * 2 + 1 - r} {r} />
32
+ <line x1={maxR + 1} x2={maxR * 2 + 8} y1={top + maxR * 2 + 1 - r * 2} y2={top + maxR * 2 + 1 - r * 2} />
33
+ <text x={maxR * 2 + 11} y={top + maxR * 2 + 1 - r * 2} dy="0.32em">{fmt(v)}</text>
34
+ {/each}
35
+ </svg>
36
+
37
+ <style>
38
+ .mz-size-legend {
39
+ display: block;
40
+ overflow: visible;
41
+ font-family: var(--mz-font-body);
42
+ }
43
+ circle {
44
+ fill: none;
45
+ stroke: var(--mz-color-muted);
46
+ }
47
+ line {
48
+ stroke: var(--mz-color-axis);
49
+ stroke-dasharray: 2 2;
50
+ }
51
+ text {
52
+ fill: var(--mz-color-muted);
53
+ font-size: var(--mz-size-axis);
54
+ }
55
+ .mz-size-title {
56
+ fill: var(--mz-color-text);
57
+ font-family: var(--mz-font-label);
58
+ font-weight: var(--mz-weight-label);
59
+ text-transform: var(--mz-label-transform);
60
+ letter-spacing: var(--mz-label-tracking);
61
+ }
62
+ </style>
@@ -0,0 +1,15 @@
1
+ interface Props {
2
+ /** Value → radius in px. */
3
+ scale: (value: number) => number;
4
+ values: number[];
5
+ format?: (value: number) => string;
6
+ title?: string;
7
+ class?: string;
8
+ }
9
+ /**
10
+ * Nested circles explaining a radius scale (usually `scaleSqrt`), for
11
+ * proportional symbol maps and bubble charts. Drawn as SVG.
12
+ */
13
+ declare const SizeLegend: import("svelte").Component<Props, {}, "">;
14
+ type SizeLegend = ReturnType<typeof SizeLegend>;
15
+ export default SizeLegend;
@@ -0,0 +1,9 @@
1
+ export { default as AxisX } from './AxisX.svelte';
2
+ export { default as AxisY } from './AxisY.svelte';
3
+ export { default as GridX } from './GridX.svelte';
4
+ export { default as GridY } from './GridY.svelte';
5
+ export { default as Legend } from './Legend.svelte';
6
+ export { default as ColorRamp } from './ColorRamp.svelte';
7
+ export { default as SizeLegend } from './SizeLegend.svelte';
8
+ export { default as Key } from './Key.svelte';
9
+ export { defaultTickFormat, tickValues } from './ticks.js';
@@ -0,0 +1,9 @@
1
+ export { default as AxisX } from './AxisX.svelte';
2
+ export { default as AxisY } from './AxisY.svelte';
3
+ export { default as GridX } from './GridX.svelte';
4
+ export { default as GridY } from './GridY.svelte';
5
+ export { default as Legend } from './Legend.svelte';
6
+ export { default as ColorRamp } from './ColorRamp.svelte';
7
+ export { default as SizeLegend } from './SizeLegend.svelte';
8
+ export { default as Key } from './Key.svelte';
9
+ export { defaultTickFormat, tickValues } from './ticks.js';
@@ -0,0 +1,6 @@
1
+ import { type Locale } from '../locale/index.js';
2
+ import type { PositionScale } from '../core/types.js';
3
+ /** Explicit values win; otherwise the scale's own ticks, or the whole domain for band/point scales. */
4
+ export declare function tickValues(scale: PositionScale, count: number, explicit?: unknown[]): unknown[];
5
+ /** Locale-aware default label: dates by multiTimeFormat, numbers with grouping, anything else as text. */
6
+ export declare function defaultTickFormat(values: unknown[], locale: Locale): (value: any) => string;
@@ -0,0 +1,15 @@
1
+ import { multiTimeFormat } from '../locale/index.js';
2
+ /** Explicit values win; otherwise the scale's own ticks, or the whole domain for band/point scales. */
3
+ export function tickValues(scale, count, explicit) {
4
+ if (explicit)
5
+ return explicit;
6
+ return scale.ticks ? scale.ticks(count) : scale.domain();
7
+ }
8
+ /** Locale-aware default label: dates by multiTimeFormat, numbers with grouping, anything else as text. */
9
+ export function defaultTickFormat(values, locale) {
10
+ if (values[0] instanceof Date)
11
+ return multiTimeFormat(locale);
12
+ if (typeof values[0] === 'number')
13
+ return locale.number;
14
+ return (value) => String(value);
15
+ }