@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,41 @@
1
+ declare function $$render<T>(): {
2
+ props: {
3
+ data: T[];
4
+ value: (d: T) => number;
5
+ fill?: (d: T, index: number) => string;
6
+ /** Squares per row. */
7
+ columns?: number;
8
+ /** Total number of squares; defaults to the sum of values. Remaining squares use `emptyFill`. */
9
+ total?: number;
10
+ emptyFill?: string;
11
+ /** Gap between squares in px. */
12
+ gap?: number;
13
+ title?: (d: T) => string;
14
+ class?: string;
15
+ };
16
+ exports: {};
17
+ bindings: "";
18
+ slots: {};
19
+ events: {};
20
+ };
21
+ declare class __sveltets_Render<T> {
22
+ props(): ReturnType<typeof $$render<T>>['props'];
23
+ events(): ReturnType<typeof $$render<T>>['events'];
24
+ slots(): ReturnType<typeof $$render<T>>['slots'];
25
+ bindings(): "";
26
+ exports(): {};
27
+ }
28
+ interface $$IsomorphicComponent {
29
+ new <T>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
30
+ $$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
31
+ } & ReturnType<__sveltets_Render<T>['exports']>;
32
+ <T>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
33
+ z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
34
+ }
35
+ /**
36
+ * A waffle chart: a grid of squares, one per unit, coloured by category in
37
+ * data order. Good for parts of a whole ("37 of 100 people").
38
+ */
39
+ declare const Waffle: $$IsomorphicComponent;
40
+ type Waffle<T> = InstanceType<typeof Waffle<T>>;
41
+ export default Waffle;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * SVG path for a bar from `base` to `value` along the value axis, `t0`–`t1`
3
+ * across it, with rounded corners only at the data end (square at the baseline).
4
+ */
5
+ export declare function barPath(orientation: 'horizontal' | 'vertical', base: number, value: number, t0: number, t1: number, radius: number): string;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * SVG path for a bar from `base` to `value` along the value axis, `t0`–`t1`
3
+ * across it, with rounded corners only at the data end (square at the baseline).
4
+ */
5
+ export function barPath(orientation, base, value, t0, t1, radius) {
6
+ const length = Math.abs(value - base);
7
+ const r = Math.max(0, Math.min(radius, (t1 - t0) / 2, length));
8
+ const s = value >= base ? 1 : -1;
9
+ if (orientation === 'horizontal') {
10
+ // Grows along x; data end at x = value.
11
+ return `M${base},${t0}H${value - s * r}Q${value},${t0} ${value},${t0 + r}V${t1 - r}Q${value},${t1} ${value - s * r},${t1}H${base}Z`;
12
+ }
13
+ // Grows along y; data end at y = value.
14
+ return `M${t0},${base}V${value + s * r}Q${t0},${value} ${t0 + r},${value}H${t1 - r}Q${t1},${value} ${t1},${value + s * r}V${base}Z`;
15
+ }
@@ -0,0 +1,20 @@
1
+ export { default as Bars } from './Bars.svelte';
2
+ export { default as Line } from './Line.svelte';
3
+ export { default as Area } from './Area.svelte';
4
+ export { default as Points } from './Points.svelte';
5
+ export { default as Rects } from './Rects.svelte';
6
+ export { default as Arcs } from './Arcs.svelte';
7
+ export { default as Text } from './Text.svelte';
8
+ export { default as Rules } from './Rules.svelte';
9
+ export { default as GeoPath } from './GeoPath.svelte';
10
+ export { default as Voronoi } from './Voronoi.svelte';
11
+ export { default as LineEndLabels } from './LineEndLabels.svelte';
12
+ export { default as Treemap } from './Treemap.svelte';
13
+ export { default as Waffle } from './Waffle.svelte';
14
+ export { default as Dumbbell } from './Dumbbell.svelte';
15
+ export { default as Arrows } from './Arrows.svelte';
16
+ export { default as Slope } from './Slope.svelte';
17
+ export { default as Beeswarm } from './Beeswarm.svelte';
18
+ export { default as Hemicycle } from './Hemicycle.svelte';
19
+ export { default as Intervals } from './Intervals.svelte';
20
+ export { barPath } from './bar-path.js';
@@ -0,0 +1,20 @@
1
+ export { default as Bars } from './Bars.svelte';
2
+ export { default as Line } from './Line.svelte';
3
+ export { default as Area } from './Area.svelte';
4
+ export { default as Points } from './Points.svelte';
5
+ export { default as Rects } from './Rects.svelte';
6
+ export { default as Arcs } from './Arcs.svelte';
7
+ export { default as Text } from './Text.svelte';
8
+ export { default as Rules } from './Rules.svelte';
9
+ export { default as GeoPath } from './GeoPath.svelte';
10
+ export { default as Voronoi } from './Voronoi.svelte';
11
+ export { default as LineEndLabels } from './LineEndLabels.svelte';
12
+ export { default as Treemap } from './Treemap.svelte';
13
+ export { default as Waffle } from './Waffle.svelte';
14
+ export { default as Dumbbell } from './Dumbbell.svelte';
15
+ export { default as Arrows } from './Arrows.svelte';
16
+ export { default as Slope } from './Slope.svelte';
17
+ export { default as Beeswarm } from './Beeswarm.svelte';
18
+ export { default as Hemicycle } from './Hemicycle.svelte';
19
+ export { default as Intervals } from './Intervals.svelte';
20
+ export { barPath } from './bar-path.js';
@@ -0,0 +1,247 @@
1
+ <!--
2
+ @component
3
+ Recipe: an arrow plot. One row per category, with an arrow from its value at
4
+ one date to its value at a later date, the later value written beside the
5
+ arrowhead and the two dates labelled above the first arrow. Rows with only
6
+ one of the two values get a dot (hollow for the start, filled for the end).
7
+ Use it as is, or run `mozaic eject ArrowChart` to copy this file into your
8
+ project and change anything.
9
+ -->
10
+ <script lang="ts" generics="T">
11
+ import type { Snippet } from 'svelte';
12
+ import { scaleBand, scaleLinear } from 'd3-scale';
13
+ import { max, min } from 'd3-array';
14
+ import { Chart, Header, Title, Subtitle, Footer, Source, Notes } from '../frame/index.js';
15
+ import type { ChartEmbed } from '../embed/index.js';
16
+ import { Plot, Svg, estimateTextWidth, scaled, type PlotContext } from '../core/index.js';
17
+ import { Arrows } from '../marks/index.js';
18
+ import { AxisX, AxisY, GridX } from '../guides/index.js';
19
+ import { createLocale, type LocaleCode } from '../locale/index.js';
20
+ import { base, type Medium, type Theme } from '../styles/index.js';
21
+
22
+ type Maybe = number | null | undefined;
23
+
24
+ interface Props {
25
+ data: T[];
26
+ label: (d: T) => string;
27
+ /** Value at the earlier date (the arrow's tail). Missing values leave a single dot. */
28
+ start: (d: T) => Maybe;
29
+ /** Value at the later date (the arrowhead). */
30
+ end: (d: T) => Maybe;
31
+ /** Names of the two dates, shown above the first complete arrow (e.g. "2013" and "2023"). */
32
+ startLabel?: string;
33
+ endLabel?: string;
34
+ style?: Theme;
35
+ locale?: LocaleCode;
36
+ medium?: Medium;
37
+ width?: number;
38
+ title?: string;
39
+ subtitle?: string;
40
+ notes?: string;
41
+ source?: string;
42
+ alt?: string;
43
+ /** The chart's embed page, for the embed code under the chart (see `<Chart embed>`). */
44
+ embed?: ChartEmbed;
45
+ /** Value label format; defaults to locale numbers. */
46
+ format?: (n: number) => string;
47
+ /** Axis tick format; defaults to `format`. */
48
+ tickFormat?: (n: number) => string;
49
+ /** Arrow colour; defaults to the first categorical colour. Grey out rows to highlight others. */
50
+ color?: (d: T) => string;
51
+ /** Values written beside the arrows: the end value (default), both ends, or none. */
52
+ values?: 'end' | 'both' | 'none';
53
+ /** Row order: as given (default), or by end value, start value or difference, largest first. */
54
+ sort?: 'none' | 'end' | 'start' | 'difference';
55
+ /** x domain; defaults to zero and the largest value, rounded. */
56
+ domain?: [number, number];
57
+ rowHeight?: number;
58
+ /** Extra SVG drawn on top of the arrows (annotations); receives the plot context. */
59
+ annotations?: Snippet<[PlotContext]>;
60
+ }
61
+
62
+ let {
63
+ data,
64
+ label,
65
+ start,
66
+ end,
67
+ startLabel,
68
+ endLabel,
69
+ style = base,
70
+ locale,
71
+ medium,
72
+ width,
73
+ title,
74
+ subtitle,
75
+ notes,
76
+ source,
77
+ alt,
78
+ embed,
79
+ format,
80
+ tickFormat,
81
+ color,
82
+ values = 'end',
83
+ sort = 'none',
84
+ domain,
85
+ rowHeight = 32,
86
+ annotations
87
+ }: Props = $props();
88
+
89
+ const theme = $derived(style);
90
+ const fmt = $derived(format ?? createLocale(locale ?? theme.defaultLocale).number);
91
+ const fontSize = $derived(theme.plot.fontSize);
92
+ const has = (v: Maybe): v is number => v !== null && v !== undefined && Number.isFinite(v);
93
+ const colorOf = $derived((d: T) => color?.(d) ?? theme.palettes.categorical[0]);
94
+
95
+ const rows = $derived.by(() => {
96
+ const key = {
97
+ none: undefined,
98
+ end: (d: T) => end(d) ?? -Infinity,
99
+ start: (d: T) => start(d) ?? -Infinity,
100
+ difference: (d: T) => (has(end(d)) && has(start(d)) ? end(d)! - start(d)! : -Infinity)
101
+ }[sort];
102
+ return key ? data.toSorted((a, b) => key(b) - key(a)) : data;
103
+ });
104
+ const both = $derived(rows.filter((d) => has(start(d)) && has(end(d))));
105
+ const all = $derived(rows.flatMap((d) => [start(d), end(d)]).filter(has));
106
+ const x = $derived(
107
+ scaleLinear()
108
+ .domain(domain ?? [Math.min(0, min(all) ?? 0), Math.max(0, max(all) ?? 1)])
109
+ .nice()
110
+ );
111
+ const y = $derived(scaleBand<string>().domain(rows.map(label)).paddingInner(0.25).paddingOuter(0.1));
112
+ const textWidth = (s: string) => estimateTextWidth(s, fontSize);
113
+ const left = $derived(Math.max(...rows.map((d) => textWidth(label(d))), 0) + 12);
114
+ const right = $derived(Math.max(...all.map((v) => textWidth(fmt(v))), 0) + 12);
115
+ const labelled = $derived(Boolean(startLabel || endLabel) && both.length > 0);
116
+
117
+ /** A value beside one end of an arrow, on the side away from the other end; flipped if it would leave the plot. */
118
+ function valueLabel(plot: PlotContext, at: number, other: number | undefined, text: string) {
119
+ const px = plot.x(at) ?? 0;
120
+ const ox = other === undefined ? px - 1 : (plot.x(other) ?? 0);
121
+ const outward = px >= ox ? 1 : -1;
122
+ const gap = 6 + (other === undefined ? 4 : 0);
123
+ const w = textWidth(text);
124
+ const fits = outward > 0 || px - gap - w >= 0;
125
+ const side = fits ? outward : 1;
126
+ const x0 = side > 0 ? Math.max(px, ox) + gap : px - gap;
127
+ return { x: x0, anchor: side > 0 ? 'start' : 'end', text };
128
+ }
129
+
130
+ /** The two date labels above the first arrow: centred on each end, or pushed apart when they would touch. */
131
+ function dateLabels(plot: PlotContext) {
132
+ const d = both[0];
133
+ const sx = plot.x(start(d)!) ?? 0;
134
+ const ex = plot.x(end(d)!) ?? 0;
135
+ const ws = textWidth(startLabel ?? '');
136
+ const we = textWidth(endLabel ?? '');
137
+ const apart = Math.abs(ex - sx) >= (ws + we) / 2 + 8;
138
+ const dir = ex >= sx ? 1 : -1;
139
+ return {
140
+ y: scaled(plot.y, label(d)) - plot.y.bandwidth!() / 2 - 4,
141
+ start: { x: sx, anchor: apart ? 'middle' : dir > 0 ? 'end' : 'start' },
142
+ end: { x: ex, anchor: apart ? 'middle' : dir > 0 ? 'start' : 'end' }
143
+ };
144
+ }
145
+ </script>
146
+
147
+ <Chart {style} {locale} {medium} {width} {alt} {embed}>
148
+ {#if title || subtitle}
149
+ <Header>
150
+ {#if title}<Title>{title}</Title>{/if}
151
+ {#if subtitle}<Subtitle>{subtitle}</Subtitle>{/if}
152
+ </Header>
153
+ {/if}
154
+
155
+ <Plot
156
+ {x}
157
+ {y}
158
+ height={rows.length * rowHeight}
159
+ padding={{ left, right, top: labelled ? fontSize + 6 : 0, bottom: 24 }}
160
+ >
161
+ {#snippet children(plot)}
162
+ <Svg>
163
+ <GridX ticks={4} />
164
+ <AxisX ticks={4} format={tickFormat ?? fmt} />
165
+ <AxisY />
166
+ <Arrows
167
+ data={both}
168
+ y={label}
169
+ x1={(d) => start(d)!}
170
+ x2={(d) => end(d)!}
171
+ stroke={colorOf}
172
+ title={(d) => `${label(d)}: ${fmt(start(d)!)} → ${fmt(end(d)!)}`}
173
+ />
174
+ {#each rows as d (label(d))}
175
+ {@const s = start(d)}
176
+ {@const e = end(d)}
177
+ {@const cy = scaled(plot.y, label(d))}
178
+ {#if has(s) && !has(e)}
179
+ <circle class="mz-arrow-dot-start" cx={plot.x(s)} {cy} r={4} stroke={colorOf(d)}>
180
+ <title>{label(d)}: {fmt(s)}</title>
181
+ </circle>
182
+ {:else if has(e) && !has(s)}
183
+ <circle class="mz-arrow-dot-end" cx={plot.x(e)} {cy} r={4} fill={colorOf(d)}>
184
+ <title>{label(d)}: {fmt(e)}</title>
185
+ </circle>
186
+ {/if}
187
+ {#if values !== 'none'}
188
+ {#if has(e)}
189
+ {@const v = valueLabel(plot, e, has(s) ? s : undefined, fmt(e))}
190
+ <text class="mz-arrow-value strong" x={v.x} y={cy} dy="0.35em" text-anchor={v.anchor}>{v.text}</text>
191
+ {:else if has(s)}
192
+ {@const v = valueLabel(plot, s, undefined, fmt(s))}
193
+ <text class="mz-arrow-value earlier" x={v.x} y={cy} dy="0.35em" text-anchor={v.anchor}>{v.text}</text>
194
+ {/if}
195
+ {#if values === 'both' && has(s) && has(e)}
196
+ {@const v = valueLabel(plot, s, e, fmt(s))}
197
+ <text class="mz-arrow-value" x={v.x} y={cy} dy="0.35em" text-anchor={v.anchor}>{v.text}</text>
198
+ {/if}
199
+ {/if}
200
+ {/each}
201
+ {#if labelled}
202
+ {@const l = dateLabels(plot)}
203
+ {#if startLabel}
204
+ <text class="mz-arrow-date" x={l.start.x} y={l.y} text-anchor={l.start.anchor}>{startLabel}</text>
205
+ {/if}
206
+ {#if endLabel}
207
+ <text class="mz-arrow-date strong" x={l.end.x} y={l.y} text-anchor={l.end.anchor}>{endLabel}</text>
208
+ {/if}
209
+ {/if}
210
+ {@render annotations?.(plot)}
211
+ </Svg>
212
+ {/snippet}
213
+ </Plot>
214
+
215
+ {#if notes || source}
216
+ <Footer>
217
+ {#if notes}<Notes>{notes}</Notes>{/if}
218
+ {#if source}<Source>{source}</Source>{/if}
219
+ </Footer>
220
+ {/if}
221
+ </Chart>
222
+
223
+ <style>
224
+ .mz-arrow-value,
225
+ .mz-arrow-date {
226
+ font-family: var(--mz-font-body);
227
+ font-size: var(--mz-size-axis);
228
+ fill: var(--mz-color-text);
229
+ font-variant-numeric: tabular-nums;
230
+ }
231
+ /* Date labels, and the value of a row with only the earlier date. */
232
+ .mz-arrow-date,
233
+ .earlier {
234
+ fill: var(--mz-color-muted);
235
+ }
236
+ .strong {
237
+ font-weight: var(--mz-weight-strong);
238
+ }
239
+ .mz-arrow-dot-start {
240
+ fill: var(--mz-color-bg);
241
+ stroke-width: 2;
242
+ }
243
+ .mz-arrow-dot-end {
244
+ stroke: var(--mz-color-bg);
245
+ stroke-width: 1.5;
246
+ }
247
+ </style>
@@ -0,0 +1,73 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type { ChartEmbed } from '../embed/index.js';
3
+ import { type PlotContext } from '../core/index.js';
4
+ import { type LocaleCode } from '../locale/index.js';
5
+ import { type Medium, type Theme } from '../styles/index.js';
6
+ declare function $$render<T>(): {
7
+ props: {
8
+ data: T[];
9
+ label: (d: T) => string;
10
+ /** Value at the earlier date (the arrow's tail). Missing values leave a single dot. */
11
+ start: (d: T) => number | null | undefined;
12
+ /** Value at the later date (the arrowhead). */
13
+ end: (d: T) => number | null | undefined;
14
+ /** Names of the two dates, shown above the first complete arrow (e.g. "2013" and "2023"). */
15
+ startLabel?: string;
16
+ endLabel?: string;
17
+ style?: Theme;
18
+ locale?: LocaleCode;
19
+ medium?: Medium;
20
+ width?: number;
21
+ title?: string;
22
+ subtitle?: string;
23
+ notes?: string;
24
+ source?: string;
25
+ alt?: string;
26
+ /** The chart's embed page, for the embed code under the chart (see `<Chart embed>`). */
27
+ embed?: ChartEmbed;
28
+ /** Value label format; defaults to locale numbers. */
29
+ format?: (n: number) => string;
30
+ /** Axis tick format; defaults to `format`. */
31
+ tickFormat?: (n: number) => string;
32
+ /** Arrow colour; defaults to the first categorical colour. Grey out rows to highlight others. */
33
+ color?: (d: T) => string;
34
+ /** Values written beside the arrows: the end value (default), both ends, or none. */
35
+ values?: "end" | "both" | "none";
36
+ /** Row order: as given (default), or by end value, start value or difference, largest first. */
37
+ sort?: "none" | "end" | "start" | "difference";
38
+ /** x domain; defaults to zero and the largest value, rounded. */
39
+ domain?: [number, number];
40
+ rowHeight?: number;
41
+ /** Extra SVG drawn on top of the arrows (annotations); receives the plot context. */
42
+ annotations?: Snippet<[PlotContext]>;
43
+ };
44
+ exports: {};
45
+ bindings: "";
46
+ slots: {};
47
+ events: {};
48
+ };
49
+ declare class __sveltets_Render<T> {
50
+ props(): ReturnType<typeof $$render<T>>['props'];
51
+ events(): ReturnType<typeof $$render<T>>['events'];
52
+ slots(): ReturnType<typeof $$render<T>>['slots'];
53
+ bindings(): "";
54
+ exports(): {};
55
+ }
56
+ interface $$IsomorphicComponent {
57
+ new <T>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
58
+ $$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
59
+ } & ReturnType<__sveltets_Render<T>['exports']>;
60
+ <T>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
61
+ z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
62
+ }
63
+ /**
64
+ * Recipe: an arrow plot. One row per category, with an arrow from its value at
65
+ * one date to its value at a later date, the later value written beside the
66
+ * arrowhead and the two dates labelled above the first arrow. Rows with only
67
+ * one of the two values get a dot (hollow for the start, filled for the end).
68
+ * Use it as is, or run `mozaic eject ArrowChart` to copy this file into your
69
+ * project and change anything.
70
+ */
71
+ declare const ArrowChart: $$IsomorphicComponent;
72
+ type ArrowChart<T> = InstanceType<typeof ArrowChart<T>>;
73
+ export default ArrowChart;
@@ -0,0 +1,122 @@
1
+ <!--
2
+ @component
3
+ Recipe: a sorted horizontal bar chart with value labels, hover tooltip and
4
+ the full frame. Use it as is, or run `mozaic eject BarChart` to copy this
5
+ file into your project and change anything.
6
+ -->
7
+ <script lang="ts" generics="T">
8
+ import type { Snippet } from 'svelte';
9
+ import { scaleBand, scaleLinear } from 'd3-scale';
10
+ import { max, min } from 'd3-array';
11
+ import { Chart, Header, Title, Subtitle, Footer, Source, Notes } from '../frame/index.js';
12
+ import type { ChartEmbed } from '../embed/index.js';
13
+ import { Plot, Svg, Html, estimateTextWidth, scaled, type PlotContext } from '../core/index.js';
14
+ import { Bars, Rules, Text } from '../marks/index.js';
15
+ import { AxisY } from '../guides/index.js';
16
+ import { Tooltip } from '../interactive/index.js';
17
+ import { createLocale, type LocaleCode } from '../locale/index.js';
18
+ import { base, type Medium, type Theme } from '../styles/index.js';
19
+
20
+ interface Props {
21
+ data: T[];
22
+ label: (d: T) => string;
23
+ value: (d: T) => number;
24
+ style?: Theme;
25
+ locale?: LocaleCode;
26
+ medium?: Medium;
27
+ width?: number;
28
+ title?: string;
29
+ subtitle?: string;
30
+ notes?: string;
31
+ source?: string;
32
+ alt?: string;
33
+ /** The chart's embed page, for the embed code under the chart (see `<Chart embed>`). */
34
+ embed?: ChartEmbed;
35
+ /** Value label format; defaults to locale numbers. */
36
+ format?: (n: number) => string;
37
+ /** Bar colour; defaults to the first categorical colour. */
38
+ color?: (d: T) => string;
39
+ /** Sort largest first (default) or keep data order. */
40
+ sort?: boolean;
41
+ rowHeight?: number;
42
+ /** Extra SVG drawn on top of the bars (annotations); receives the plot context. */
43
+ annotations?: Snippet<[PlotContext]>;
44
+ }
45
+
46
+ let {
47
+ data,
48
+ label,
49
+ value,
50
+ style = base,
51
+ locale,
52
+ medium,
53
+ width,
54
+ title,
55
+ subtitle,
56
+ notes,
57
+ source,
58
+ alt,
59
+ embed,
60
+ format,
61
+ color,
62
+ sort = true,
63
+ rowHeight = 28,
64
+ annotations
65
+ }: Props = $props();
66
+
67
+ const theme = $derived(style);
68
+ const fmt = $derived(format ?? createLocale(locale ?? theme.defaultLocale).number);
69
+ const rows = $derived(sort ? data.toSorted((a, b) => value(b) - value(a)) : data);
70
+ const x = $derived(
71
+ scaleLinear()
72
+ .domain([Math.min(0, min(rows, value) ?? 0), Math.max(0, max(rows, value) ?? 1)])
73
+ .nice()
74
+ );
75
+ const y = $derived(scaleBand<string>().domain(rows.map(label)).paddingInner(0.25).paddingOuter(0.1));
76
+ const left = $derived(Math.max(...rows.map((d) => estimateTextWidth(label(d), theme.plot.fontSize)), 0) + 12);
77
+ const right = $derived(Math.max(...rows.map((d) => estimateTextWidth(fmt(value(d)), theme.plot.fontSize)), 0) + 12);
78
+ let hovered = $state<T | null>(null);
79
+ </script>
80
+
81
+ <Chart {style} {locale} {medium} {width} {alt} {embed}>
82
+ {#if title || subtitle}
83
+ <Header>
84
+ {#if title}<Title>{title}</Title>{/if}
85
+ {#if subtitle}<Subtitle>{subtitle}</Subtitle>{/if}
86
+ </Header>
87
+ {/if}
88
+
89
+ <Plot {x} {y} height={rows.length * rowHeight} padding={{ left, right, top: 0, bottom: 0 }}>
90
+ {#snippet children(plot)}
91
+ <Svg>
92
+ <AxisY />
93
+ <Bars
94
+ data={rows}
95
+ x={value}
96
+ y={label}
97
+ fill={color}
98
+ opacity={(d) => (hovered && hovered !== d ? 0.5 : 1)}
99
+ onpointerenter={(d) => (hovered = d)}
100
+ onpointerleave={() => (hovered = null)}
101
+ />
102
+ <Rules x={[0]} />
103
+ <Text data={rows} x={value} y={label} text={(d) => fmt(value(d))} dx={4} />
104
+ {@render annotations?.(plot)}
105
+ </Svg>
106
+ <Html>
107
+ {#if hovered}
108
+ <Tooltip x={plot.x(value(hovered)) ?? 0} y={scaled(plot.y, label(hovered))}>
109
+ <strong>{label(hovered)}</strong><br />{fmt(value(hovered))}
110
+ </Tooltip>
111
+ {/if}
112
+ </Html>
113
+ {/snippet}
114
+ </Plot>
115
+
116
+ {#if notes || source}
117
+ <Footer>
118
+ {#if notes}<Notes>{notes}</Notes>{/if}
119
+ {#if source}<Source>{source}</Source>{/if}
120
+ </Footer>
121
+ {/if}
122
+ </Chart>
@@ -0,0 +1,58 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type { ChartEmbed } from '../embed/index.js';
3
+ import { type PlotContext } from '../core/index.js';
4
+ import { type LocaleCode } from '../locale/index.js';
5
+ import { type Medium, type Theme } from '../styles/index.js';
6
+ declare function $$render<T>(): {
7
+ props: {
8
+ data: T[];
9
+ label: (d: T) => string;
10
+ value: (d: T) => number;
11
+ style?: Theme;
12
+ locale?: LocaleCode;
13
+ medium?: Medium;
14
+ width?: number;
15
+ title?: string;
16
+ subtitle?: string;
17
+ notes?: string;
18
+ source?: string;
19
+ alt?: string;
20
+ /** The chart's embed page, for the embed code under the chart (see `<Chart embed>`). */
21
+ embed?: ChartEmbed;
22
+ /** Value label format; defaults to locale numbers. */
23
+ format?: (n: number) => string;
24
+ /** Bar colour; defaults to the first categorical colour. */
25
+ color?: (d: T) => string;
26
+ /** Sort largest first (default) or keep data order. */
27
+ sort?: boolean;
28
+ rowHeight?: number;
29
+ /** Extra SVG drawn on top of the bars (annotations); receives the plot context. */
30
+ annotations?: Snippet<[PlotContext]>;
31
+ };
32
+ exports: {};
33
+ bindings: "";
34
+ slots: {};
35
+ events: {};
36
+ };
37
+ declare class __sveltets_Render<T> {
38
+ props(): ReturnType<typeof $$render<T>>['props'];
39
+ events(): ReturnType<typeof $$render<T>>['events'];
40
+ slots(): ReturnType<typeof $$render<T>>['slots'];
41
+ bindings(): "";
42
+ exports(): {};
43
+ }
44
+ interface $$IsomorphicComponent {
45
+ new <T>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
46
+ $$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
47
+ } & ReturnType<__sveltets_Render<T>['exports']>;
48
+ <T>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
49
+ z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
50
+ }
51
+ /**
52
+ * Recipe: a sorted horizontal bar chart with value labels, hover tooltip and
53
+ * the full frame. Use it as is, or run `mozaic eject BarChart` to copy this
54
+ * file into your project and change anything.
55
+ */
56
+ declare const BarChart: $$IsomorphicComponent;
57
+ type BarChart<T> = InstanceType<typeof BarChart<T>>;
58
+ export default BarChart;