@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,59 @@
1
+ <!--
2
+ @component
3
+ A tiny inline line chart, e.g. in a table cell. Not a full chart: no axes.
4
+ The last point gets a dot.
5
+ -->
6
+ <script lang="ts">
7
+ import { extent } from 'd3-array';
8
+ import { scaleLinear } from 'd3-scale';
9
+ import { line } from 'd3-shape';
10
+
11
+ interface Props {
12
+ values: number[];
13
+ width?: number;
14
+ height?: number;
15
+ stroke?: string;
16
+ /** Shared y domain, so several sparklines are comparable. */
17
+ domain?: [number, number];
18
+ label?: string;
19
+ }
20
+
21
+ let { values, width = 80, height = 20, stroke = 'var(--mz-color-text)', domain, label }: Props = $props();
22
+
23
+ const x = $derived(
24
+ scaleLinear()
25
+ .domain([0, Math.max(1, values.length - 1)])
26
+ .range([2, width - 3])
27
+ );
28
+ const y = $derived(
29
+ scaleLinear()
30
+ .domain(domain ?? (extent(values) as [number, number]))
31
+ .range([height - 3, 3])
32
+ );
33
+ const d = $derived(
34
+ line<number>()
35
+ .x((_, i) => x(i))
36
+ .y((v) => y(v))(values) ?? ''
37
+ );
38
+ const last = $derived(values.length - 1);
39
+ </script>
40
+
41
+ <svg
42
+ class="mz-sparkline"
43
+ {width}
44
+ {height}
45
+ viewBox="0 0 {width} {height}"
46
+ role={label ? 'img' : 'presentation'}
47
+ aria-label={label}
48
+ >
49
+ <path {d} fill="none" style:stroke stroke-width="1.5" stroke-linejoin="round" stroke-linecap="round" />
50
+ {#if last >= 0}<circle cx={x(last)} cy={y(values[last])} r="2.25" style:fill={stroke} />{/if}
51
+ </svg>
52
+
53
+ <style>
54
+ .mz-sparkline {
55
+ display: inline-block;
56
+ vertical-align: middle;
57
+ overflow: visible;
58
+ }
59
+ </style>
@@ -0,0 +1,16 @@
1
+ interface Props {
2
+ values: number[];
3
+ width?: number;
4
+ height?: number;
5
+ stroke?: string;
6
+ /** Shared y domain, so several sparklines are comparable. */
7
+ domain?: [number, number];
8
+ label?: string;
9
+ }
10
+ /**
11
+ * A tiny inline line chart, e.g. in a table cell. Not a full chart: no axes.
12
+ * The last point gets a dot.
13
+ */
14
+ declare const Sparkline: import("svelte").Component<Props, {}, "">;
15
+ type Sparkline = ReturnType<typeof Sparkline>;
16
+ export default Sparkline;
@@ -0,0 +1,287 @@
1
+ <!--
2
+ @component
3
+ A data table with sortable columns, locale-aware formatting, optional search
4
+ and a "show all" toggle, plus inline bar, heat and sparkline cells. Use it
5
+ on its own or inside `<Chart>`.
6
+ -->
7
+ <script lang="ts" generics="T">
8
+ import { untrack } from 'svelte';
9
+ import { ascending, descending, max as d3max } from 'd3-array';
10
+ import { useChart } from '../core/context.js';
11
+ import Sparkline from './Sparkline.svelte';
12
+ import type { Column } from './types.js';
13
+
14
+ interface Props {
15
+ data: T[];
16
+ columns: Column<T>[];
17
+ /** Initial sort column key. */
18
+ sortBy?: string;
19
+ sortDirection?: 'ascending' | 'descending';
20
+ /** Show a search box. */
21
+ search?: boolean;
22
+ /** Rows shown before "Show all". */
23
+ maxRows?: number;
24
+ /** Visible caption (also the accessible name). */
25
+ caption?: string;
26
+ stickyHeader?: boolean;
27
+ class?: string;
28
+ }
29
+
30
+ let {
31
+ data,
32
+ columns,
33
+ sortBy,
34
+ sortDirection = 'descending',
35
+ search = false,
36
+ maxRows,
37
+ caption,
38
+ stickyHeader = false,
39
+ class: className = ''
40
+ }: Props = $props();
41
+
42
+ const chart = useChart();
43
+ const strings = $derived(chart.locale.strings);
44
+
45
+ // Sort state starts from the props, then follows the reader's clicks.
46
+ let sortKey = $state<string | undefined>(untrack(() => sortBy));
47
+ let direction = $state<'ascending' | 'descending'>(untrack(() => sortDirection));
48
+ let query = $state('');
49
+ let expanded = $state(false);
50
+
51
+ const isNumeric = (c: Column<T>) => data.some((d) => typeof c.value(d) === 'number');
52
+ // Bars and sparklines start at the left edge, so their headers do too.
53
+ const align = (c: Column<T>) =>
54
+ c.align ?? (isNumeric(c) && c.type !== 'sparkline' && c.type !== 'bar' ? 'right' : 'left');
55
+ const maxima = $derived(
56
+ Object.fromEntries(columns.map((c) => [c.key, c.max ?? (d3max(data, (d) => Math.abs(Number(c.value(d)))) || 1)]))
57
+ );
58
+
59
+ const filtered = $derived.by(() => {
60
+ const q = query.trim().toLocaleLowerCase(chart.locale.lang);
61
+ if (!q) return data;
62
+ const searchable = columns.filter((c) => c.searchable ?? !isNumeric(c));
63
+ return data.filter((d) =>
64
+ searchable.some((c) =>
65
+ String(c.value(d) ?? '')
66
+ .toLocaleLowerCase(chart.locale.lang)
67
+ .includes(q)
68
+ )
69
+ );
70
+ });
71
+
72
+ const sorted = $derived.by(() => {
73
+ const column = columns.find((c) => c.key === sortKey);
74
+ if (!column) return filtered;
75
+ const compare = direction === 'ascending' ? ascending : descending;
76
+ return filtered.toSorted((a, b) => {
77
+ const va = column.value(a);
78
+ const vb = column.value(b);
79
+ if (typeof va === 'string' && typeof vb === 'string')
80
+ return (direction === 'ascending' ? 1 : -1) * va.localeCompare(vb, chart.locale.lang);
81
+ return compare(va as number, vb as number);
82
+ });
83
+ });
84
+
85
+ const visible = $derived(maxRows && !expanded ? sorted.slice(0, maxRows) : sorted);
86
+
87
+ function toggleSort(column: Column<T>) {
88
+ if (sortKey === column.key) direction = direction === 'ascending' ? 'descending' : 'ascending';
89
+ else {
90
+ sortKey = column.key;
91
+ direction = isNumeric(column) ? 'descending' : 'ascending';
92
+ }
93
+ }
94
+
95
+ function text(column: Column<T>, d: T): string {
96
+ const v = column.value(d);
97
+ if (column.format) return column.format(v, d);
98
+ if (typeof v === 'number') return chart.locale.number(v);
99
+ if (v instanceof Date) return chart.locale.timeFormat('%e %b %Y')(v);
100
+ return v == null ? '' : String(v);
101
+ }
102
+
103
+ function contrast(color: string): string {
104
+ // Near-black or white text on a heat cell: whichever has the higher WCAG contrast ratio.
105
+ const m = color.match(/^#([0-9a-f]{6})$/i);
106
+ if (!m) return 'inherit';
107
+ const n = parseInt(m[1], 16);
108
+ const [r, g, b] = [(n >> 16) & 255, (n >> 8) & 255, n & 255].map((c) => {
109
+ const s = c / 255;
110
+ return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
111
+ });
112
+ const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
113
+ const dark = 0.0056; // luminance of #111111
114
+ return (luminance + 0.05) / (dark + 0.05) >= 1.05 / (luminance + 0.05) ? '#111111' : '#ffffff';
115
+ }
116
+ </script>
117
+
118
+ <div class="mz-table-wrap {className}">
119
+ {#if search && chart.medium !== 'print'}
120
+ <input
121
+ class="mz-table-search"
122
+ type="search"
123
+ placeholder={strings.search}
124
+ aria-label={strings.search}
125
+ bind:value={query}
126
+ />
127
+ {/if}
128
+ <div class="mz-table-scroll">
129
+ <table class="mz-table" class:sticky={stickyHeader}>
130
+ {#if caption}<caption>{caption}</caption>{/if}
131
+ <thead>
132
+ <tr>
133
+ {#each columns as column (column.key)}
134
+ <th
135
+ scope="col"
136
+ style:width={column.width}
137
+ style:text-align={align(column)}
138
+ aria-sort={sortKey === column.key ? direction : undefined}
139
+ >
140
+ {#if column.sortable !== false && chart.medium !== 'print'}
141
+ <button type="button" onclick={() => toggleSort(column)}>
142
+ {column.label}<span class="mz-sort" aria-hidden="true"
143
+ >{sortKey === column.key ? (direction === 'ascending' ? '▲' : '▼') : ''}</span
144
+ >
145
+ </button>
146
+ {:else}
147
+ {column.label}
148
+ {/if}
149
+ </th>
150
+ {/each}
151
+ </tr>
152
+ </thead>
153
+ <tbody>
154
+ {#each visible as d}
155
+ <tr>
156
+ {#each columns as column, ci (column.key)}
157
+ {@const v = column.value(d)}
158
+ {#if column.type === 'heat' && column.color}
159
+ {@const bg = column.color(v, d)}
160
+ <td style:text-align={align(column)} style:background={bg} style:color={contrast(bg)}
161
+ >{text(column, d)}</td
162
+ >
163
+ {:else if column.type === 'bar'}
164
+ <td class="mz-bar-cell">
165
+ <span
166
+ class="mz-cell-bar"
167
+ style:width="{(Math.abs(Number(v)) / maxima[column.key]) * 100}%"
168
+ style:background={column.color?.(v, d) ?? chart.theme.palettes.categorical[0]}
169
+ ></span>
170
+ <span class="mz-cell-value">{text(column, d)}</span>
171
+ </td>
172
+ {:else if column.type === 'sparkline' && column.series}
173
+ <td><Sparkline values={column.series(d)} /></td>
174
+ {:else if ci === 0}
175
+ <th scope="row" style:text-align={align(column)}>{text(column, d)}</th>
176
+ {:else}
177
+ <td style:text-align={align(column)}>{text(column, d)}</td>
178
+ {/if}
179
+ {/each}
180
+ </tr>
181
+ {/each}
182
+ {#if visible.length === 0}
183
+ <tr><td colspan={columns.length} class="mz-empty">{strings.noData}</td></tr>
184
+ {/if}
185
+ </tbody>
186
+ </table>
187
+ </div>
188
+ {#if maxRows && sorted.length > maxRows && chart.medium !== 'print'}
189
+ <button class="mz-table-more" type="button" onclick={() => (expanded = !expanded)}>
190
+ {expanded ? strings.showFewer : strings.showAll(sorted.length)}
191
+ </button>
192
+ {/if}
193
+ </div>
194
+
195
+ <style>
196
+ .mz-table-wrap {
197
+ font-family: var(--mz-font-body, system-ui, sans-serif);
198
+ font-size: var(--mz-size-small, 0.8125rem);
199
+ color: var(--mz-color-text, #1a1a1a);
200
+ }
201
+ .mz-table-scroll {
202
+ overflow-x: auto;
203
+ }
204
+ .mz-table {
205
+ width: 100%;
206
+ border-collapse: collapse;
207
+ font-variant-numeric: tabular-nums;
208
+ }
209
+ caption {
210
+ text-align: left;
211
+ font-weight: var(--mz-weight-strong, 700);
212
+ padding-bottom: 0.375rem;
213
+ }
214
+ th,
215
+ td {
216
+ padding: 0.375rem 0.5rem;
217
+ border-bottom: 1px solid var(--mz-color-grid, #e4e4e4);
218
+ vertical-align: middle;
219
+ }
220
+ thead th {
221
+ font-family: var(--mz-font-label, inherit);
222
+ font-weight: var(--mz-weight-label, 700);
223
+ text-transform: var(--mz-label-transform, none);
224
+ letter-spacing: var(--mz-label-tracking, normal);
225
+ color: var(--mz-color-muted, #5c5c5c);
226
+ border-bottom: 1px solid var(--mz-color-text, #1a1a1a);
227
+ white-space: nowrap;
228
+ }
229
+ tbody th {
230
+ font-weight: var(--mz-weight-strong, 600);
231
+ }
232
+ .sticky thead th {
233
+ position: sticky;
234
+ top: 0;
235
+ background: var(--mz-color-bg, #fff);
236
+ }
237
+ thead button {
238
+ all: unset;
239
+ cursor: pointer;
240
+ }
241
+ thead button:focus-visible {
242
+ outline: 2px solid var(--mz-color-accent, #2a78d6);
243
+ }
244
+ .mz-sort {
245
+ display: inline-block;
246
+ width: 1em;
247
+ font-size: 0.7em;
248
+ text-align: center;
249
+ }
250
+ .mz-bar-cell {
251
+ position: relative;
252
+ min-width: 6rem;
253
+ }
254
+ .mz-cell-bar {
255
+ position: absolute;
256
+ left: 0.5rem;
257
+ top: 25%;
258
+ height: 50%;
259
+ max-width: calc(100% - 1rem);
260
+ border-radius: 0 min(var(--mz-radius, 2px), 2px) min(var(--mz-radius, 2px), 2px) 0;
261
+ opacity: 0.35;
262
+ }
263
+ .mz-cell-value {
264
+ position: relative;
265
+ }
266
+ .mz-empty {
267
+ text-align: center;
268
+ color: var(--mz-color-muted, #5c5c5c);
269
+ }
270
+ .mz-table-search,
271
+ .mz-table-more {
272
+ margin: 0.5rem 0;
273
+ padding: 0.35rem 0.6rem;
274
+ border: 1px solid var(--mz-color-border, #e4e4e4);
275
+ border-radius: var(--mz-radius, 4px);
276
+ background: var(--mz-color-bg, #fff);
277
+ color: inherit;
278
+ font: inherit;
279
+ }
280
+ .mz-table-more {
281
+ cursor: pointer;
282
+ font-family: var(--mz-font-label, inherit);
283
+ font-weight: var(--mz-weight-label, 600);
284
+ text-transform: var(--mz-control-transform, none);
285
+ letter-spacing: var(--mz-control-tracking, normal);
286
+ }
287
+ </style>
@@ -0,0 +1,44 @@
1
+ import type { Column } from './types.js';
2
+ declare function $$render<T>(): {
3
+ props: {
4
+ data: T[];
5
+ columns: Column<T>[];
6
+ /** Initial sort column key. */
7
+ sortBy?: string;
8
+ sortDirection?: "ascending" | "descending";
9
+ /** Show a search box. */
10
+ search?: boolean;
11
+ /** Rows shown before "Show all". */
12
+ maxRows?: number;
13
+ /** Visible caption (also the accessible name). */
14
+ caption?: string;
15
+ stickyHeader?: boolean;
16
+ class?: string;
17
+ };
18
+ exports: {};
19
+ bindings: "";
20
+ slots: {};
21
+ events: {};
22
+ };
23
+ declare class __sveltets_Render<T> {
24
+ props(): ReturnType<typeof $$render<T>>['props'];
25
+ events(): ReturnType<typeof $$render<T>>['events'];
26
+ slots(): ReturnType<typeof $$render<T>>['slots'];
27
+ bindings(): "";
28
+ exports(): {};
29
+ }
30
+ interface $$IsomorphicComponent {
31
+ 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']>> & {
32
+ $$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
33
+ } & ReturnType<__sveltets_Render<T>['exports']>;
34
+ <T>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
35
+ z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
36
+ }
37
+ /**
38
+ * A data table with sortable columns, locale-aware formatting, optional search
39
+ * and a "show all" toggle, plus inline bar, heat and sparkline cells. Use it
40
+ * on its own or inside `<Chart>`.
41
+ */
42
+ declare const Table: $$IsomorphicComponent;
43
+ type Table<T> = InstanceType<typeof Table<T>>;
44
+ export default Table;
@@ -0,0 +1,3 @@
1
+ import type { Column } from './types.js';
2
+ /** CSV text from rows and columns (raw values, not formatted). */
3
+ export declare function toCSV<T>(rows: T[], columns: Pick<Column<T>, 'label' | 'value'>[]): string;
@@ -0,0 +1,12 @@
1
+ function escape(value) {
2
+ if (value == null)
3
+ return '';
4
+ const text = value instanceof Date ? value.toISOString().slice(0, 10) : String(value);
5
+ return /[",\n\r]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
6
+ }
7
+ /** CSV text from rows and columns (raw values, not formatted). */
8
+ export function toCSV(rows, columns) {
9
+ const header = columns.map((c) => escape(c.label)).join(',');
10
+ const body = rows.map((d) => columns.map((c) => escape(c.value(d))).join(','));
11
+ return [header, ...body].join('\n') + '\n';
12
+ }
@@ -0,0 +1,4 @@
1
+ export { default as Table } from './Table.svelte';
2
+ export { default as Sparkline } from './Sparkline.svelte';
3
+ export { toCSV } from './csv.js';
4
+ export type { Column } from './types.js';
@@ -0,0 +1,3 @@
1
+ export { default as Table } from './Table.svelte';
2
+ export { default as Sparkline } from './Sparkline.svelte';
3
+ export { toCSV } from './csv.js';
@@ -0,0 +1,26 @@
1
+ export interface Column<T> {
2
+ /** Unique id for the column. */
3
+ key: string;
4
+ label: string;
5
+ value: (d: T) => unknown;
6
+ /** Text shown in the cell; defaults to locale number formatting for numbers. */
7
+ format?: (value: any, d: T) => string;
8
+ /** Defaults to right for numbers, left otherwise. */
9
+ align?: 'left' | 'right' | 'center';
10
+ sortable?: boolean;
11
+ /**
12
+ * Cell display: plain text, an inline bar (numbers), a heat cell coloured by
13
+ * `color`, or a sparkline from `series`.
14
+ */
15
+ type?: 'text' | 'bar' | 'heat' | 'sparkline';
16
+ /** For `bar`: the value that fills the cell; defaults to the column maximum. */
17
+ max?: number;
18
+ /** For `heat` (and optionally `bar`): value → CSS colour. */
19
+ color?: (value: any, d: T) => string;
20
+ /** For `sparkline`: the values to plot. */
21
+ series?: (d: T) => number[];
22
+ /** CSS width, e.g. "30%". */
23
+ width?: string;
24
+ /** Include in the search filter (text columns by default). */
25
+ searchable?: boolean;
26
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ export interface Bin<T> {
2
+ x0: number;
3
+ x1: number;
4
+ count: number;
5
+ values: T[];
6
+ }
7
+ /**
8
+ * Histogram bins. `thresholds` is a target bin count, or explicit bin edges
9
+ * including both ends (`[0, 5, 10]` → bins 0–5 and 5–10; the last bin includes its upper edge).
10
+ * Returns `{ x0, x1, count, values }` per bin.
11
+ */
12
+ export declare function bin<T>(data: T[], value: (d: T) => number, options?: {
13
+ thresholds?: number | number[];
14
+ domain?: [number, number];
15
+ }): Bin<T>[];
@@ -0,0 +1,18 @@
1
+ import { bin as d3bin, extent } from 'd3-array';
2
+ /**
3
+ * Histogram bins. `thresholds` is a target bin count, or explicit bin edges
4
+ * including both ends (`[0, 5, 10]` → bins 0–5 and 5–10; the last bin includes its upper edge).
5
+ * Returns `{ x0, x1, count, values }` per bin.
6
+ */
7
+ export function bin(data, value, options = {}) {
8
+ const generator = d3bin().value(value);
9
+ const edges = Array.isArray(options.thresholds) ? options.thresholds : undefined;
10
+ const domain = options.domain ?? (edges ? [edges[0], edges[edges.length - 1]] : extent(data, value));
11
+ if (domain[0] !== undefined)
12
+ generator.domain(domain);
13
+ if (edges)
14
+ generator.thresholds(edges.slice(1, -1));
15
+ else if (typeof options.thresholds === 'number')
16
+ generator.thresholds(options.thresholds);
17
+ return generator(data).map((b) => ({ x0: b.x0 ?? 0, x1: b.x1 ?? 0, count: b.length, values: [...b] }));
18
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Beeswarm layout: given pixel positions along one axis, returns offsets on
3
+ * the other axis so circles of `radius` do not overlap. Offsets alternate
4
+ * either side of 0 (or only positive with `side: 'positive'`).
5
+ * Returns offsets in the original order.
6
+ */
7
+ export declare function dodge(positions: number[], radius: number, options?: {
8
+ padding?: number;
9
+ side?: 'both' | 'positive';
10
+ }): number[];
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Beeswarm layout: given pixel positions along one axis, returns offsets on
3
+ * the other axis so circles of `radius` do not overlap. Offsets alternate
4
+ * either side of 0 (or only positive with `side: 'positive'`).
5
+ * Returns offsets in the original order.
6
+ */
7
+ export function dodge(positions, radius, options = {}) {
8
+ const { padding = 1, side = 'both' } = options;
9
+ const diameter = radius * 2 + padding;
10
+ const d2 = diameter * diameter;
11
+ const order = positions.map((p, i) => ({ p, i })).sort((a, b) => a.p - b.p);
12
+ const placed = [];
13
+ const result = new Array(positions.length);
14
+ for (const { p, i } of order) {
15
+ // Neighbours close enough on the main axis to collide.
16
+ const near = placed.filter((q) => Math.abs(q.p - p) < diameter);
17
+ let y = 0;
18
+ if (near.some((q) => (q.p - p) ** 2 + (q.y - y) ** 2 < d2)) {
19
+ // Candidate offsets: just touching each neighbour, smallest |y| first.
20
+ const candidates = [];
21
+ for (const q of near) {
22
+ const dy = Math.sqrt(Math.max(0, d2 - (q.p - p) ** 2));
23
+ candidates.push(q.y + dy);
24
+ if (side === 'both')
25
+ candidates.push(q.y - dy);
26
+ }
27
+ candidates.sort((a, b) => Math.abs(a) - Math.abs(b));
28
+ y =
29
+ candidates.find((c) => (side === 'both' || c >= 0) && near.every((q) => (q.p - p) ** 2 + (q.y - c) ** 2 >= d2 - 1e-6)) ?? 0;
30
+ }
31
+ placed.push({ p, y });
32
+ result[i] = y;
33
+ }
34
+ return result;
35
+ }
@@ -0,0 +1,14 @@
1
+ /** Group rows by a key: `[{ key, values }]`, in order of first appearance. */
2
+ export declare function groupBy<T, K>(data: T[], key: (d: T) => K): {
3
+ key: K;
4
+ values: T[];
5
+ }[];
6
+ /** Group and reduce: `rollup(rows, d => d.region, v => sum(v, d => d.value))` → `[{ key, value }]`. */
7
+ export declare function rollup<T, K, V>(data: T[], key: (d: T) => K, reduce: (values: T[]) => V): {
8
+ key: K;
9
+ value: V;
10
+ }[];
11
+ /** Rebase a series so the value at `baseIndex` equals `base` (default 100). */
12
+ export declare function indexTo<T>(data: T[], value: (d: T) => number, baseIndex?: number, base?: number): (T & {
13
+ index: number;
14
+ })[];
@@ -0,0 +1,14 @@
1
+ import { group as d3group, rollups } from 'd3-array';
2
+ /** Group rows by a key: `[{ key, values }]`, in order of first appearance. */
3
+ export function groupBy(data, key) {
4
+ return Array.from(d3group(data, key), ([k, values]) => ({ key: k, values }));
5
+ }
6
+ /** Group and reduce: `rollup(rows, d => d.region, v => sum(v, d => d.value))` → `[{ key, value }]`. */
7
+ export function rollup(data, key, reduce) {
8
+ return rollups(data, reduce, key).map(([k, value]) => ({ key: k, value }));
9
+ }
10
+ /** Rebase a series so the value at `baseIndex` equals `base` (default 100). */
11
+ export function indexTo(data, value, baseIndex = 0, base = 100) {
12
+ const start = value(data[baseIndex]);
13
+ return data.map((d) => ({ ...d, index: start ? (value(d) / start) * base : NaN }));
14
+ }
@@ -0,0 +1,30 @@
1
+ /** One seat in a hemicycle, in unit coordinates: outer radius 1, centre (0, 0), y up. */
2
+ export interface Seat {
3
+ /** Position in seat order: 0 is the far left, the last seat is the far right. */
4
+ index: number;
5
+ /** Row, 0 = innermost. */
6
+ row: number;
7
+ /** Angle in radians, π (left) to 0 (right). */
8
+ angle: number;
9
+ x: number;
10
+ y: number;
11
+ }
12
+ export interface HemicycleOptions {
13
+ /** Number of rows. Defaults to the fewest rows that fit the seats. */
14
+ rows?: number;
15
+ /** Inner radius as a fraction of the outer radius (0–1). */
16
+ innerRadius?: number;
17
+ }
18
+ export interface HemicycleLayout {
19
+ seats: Seat[];
20
+ rows: number;
21
+ /** Distance between rows, in unit coordinates: the largest seat diameter that never overlaps. */
22
+ spacing: number;
23
+ }
24
+ /**
25
+ * Seat positions for a parliament chart. Rows are spread evenly between the
26
+ * inner and outer radius, each row gets seats in proportion to its length,
27
+ * and seats are numbered by angle from left to right, so filling parties in
28
+ * order gives each party a wedge.
29
+ */
30
+ export declare function hemicycleLayout(total: number, { rows, innerRadius }?: HemicycleOptions): HemicycleLayout;
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Seat positions for a parliament chart. Rows are spread evenly between the
3
+ * inner and outer radius, each row gets seats in proportion to its length,
4
+ * and seats are numbered by angle from left to right, so filling parties in
5
+ * order gives each party a wedge.
6
+ */
7
+ export function hemicycleLayout(total, { rows, innerRadius = 0.4 } = {}) {
8
+ const n = Math.max(0, Math.floor(total));
9
+ if (n === 0)
10
+ return { seats: [], rows: 0, spacing: 0 };
11
+ const r0 = Math.min(0.9, Math.max(0, innerRadius));
12
+ const geometry = (count) => {
13
+ const spacing = (1 - r0) / count;
14
+ const radii = Array.from({ length: count }, (_, i) => r0 + spacing * (i + 0.5));
15
+ const capacity = radii.map((r) => Math.floor((Math.PI * r) / spacing) + 1);
16
+ return { spacing, radii, capacity };
17
+ };
18
+ let count = rows && rows > 0 ? Math.floor(rows) : 1;
19
+ if (!rows)
20
+ while (geometry(count).capacity.reduce((a, b) => a + b, 0) < n)
21
+ count++;
22
+ const { spacing, radii, capacity } = geometry(count);
23
+ // Seats per row in proportion to row length (largest remainder), never above capacity
24
+ // unless the caller forced too few rows.
25
+ const length = radii.reduce((a, b) => a + b, 0);
26
+ const exact = radii.map((r) => (n * r) / length);
27
+ const perRow = exact.map(Math.floor);
28
+ const order = exact.map((e, i) => ({ i, rest: e - Math.floor(e) })).sort((a, b) => b.rest - a.rest || b.i - a.i);
29
+ for (let k = 0, left = n - perRow.reduce((a, b) => a + b, 0); left > 0; k++, left--)
30
+ perRow[order[k % count].i]++;
31
+ if (!rows) {
32
+ // Move overflow from full rows outwards (rare, with very few seats).
33
+ for (let i = 0; i < count - 1; i++) {
34
+ const extra = perRow[i] - capacity[i];
35
+ if (extra > 0) {
36
+ perRow[i] -= extra;
37
+ perRow[i + 1] += extra;
38
+ }
39
+ }
40
+ }
41
+ const seats = [];
42
+ radii.forEach((r, row) => {
43
+ const m = perRow[row];
44
+ for (let j = 0; j < m; j++) {
45
+ const angle = m === 1 ? Math.PI / 2 : Math.PI * (1 - j / (m - 1));
46
+ seats.push({ row, angle, x: r * Math.cos(angle), y: r * Math.sin(angle) });
47
+ }
48
+ });
49
+ seats.sort((a, b) => b.angle - a.angle || a.row - b.row);
50
+ return { seats: seats.map((s, index) => ({ index, ...s })), rows: count, spacing };
51
+ }
@@ -0,0 +1,8 @@
1
+ export { avoidOverlap } from './labels.js';
2
+ export { stack, type StackedRow, type StackOptions } from './stack.js';
3
+ export { bin, type Bin } from './bin.js';
4
+ export { groupBy, indexTo, rollup } from './group.js';
5
+ export { dodge } from './dodge.js';
6
+ export { treemapLayout, type TreemapCell } from './treemap.js';
7
+ export { hemicycleLayout, type HemicycleLayout, type HemicycleOptions, type Seat } from './hemicycle.js';
8
+ export { kernelSmooth, loess, type Kernel, type KernelSmoothOptions, type LoessOptions, type Smoothed } from './smooth.js';