@samizdata/mozaic 0.11.0 → 0.12.1

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 (43) hide show
  1. package/README.md +1 -1
  2. package/dist/flags/index.d.ts +16 -0
  3. package/dist/flags/index.js +16 -0
  4. package/dist/flags/svgs.d.ts +261 -0
  5. package/dist/flags/svgs.js +265 -0
  6. package/dist/guides/AxisX.svelte +18 -3
  7. package/dist/guides/AxisX.svelte.d.ts +4 -0
  8. package/dist/interactive/Tooltip.svelte +10 -3
  9. package/dist/locale/index.d.ts +8 -0
  10. package/dist/locale/index.js +10 -0
  11. package/dist/marks/Beeswarm.svelte +31 -12
  12. package/dist/marks/Beeswarm.svelte.d.ts +3 -0
  13. package/dist/marks/Flag.svelte +65 -0
  14. package/dist/marks/Flag.svelte.d.ts +27 -0
  15. package/dist/marks/Flags.svelte +69 -0
  16. package/dist/marks/Flags.svelte.d.ts +45 -0
  17. package/dist/marks/index.d.ts +2 -0
  18. package/dist/marks/index.js +2 -0
  19. package/dist/recipes/ArrowChart.svelte +4 -1
  20. package/dist/recipes/ArrowChart.svelte.d.ts +2 -0
  21. package/dist/recipes/BarChart.svelte +103 -18
  22. package/dist/recipes/BarChart.svelte.d.ts +20 -2
  23. package/dist/recipes/BarDotChart.svelte +4 -1
  24. package/dist/recipes/BarDotChart.svelte.d.ts +2 -0
  25. package/dist/recipes/ColumnChart.svelte +7 -3
  26. package/dist/recipes/ColumnChart.svelte.d.ts +2 -0
  27. package/dist/recipes/LineChart.svelte +8 -2
  28. package/dist/recipes/LineChart.svelte.d.ts +2 -0
  29. package/dist/recipes/ScatterChart.svelte +179 -18
  30. package/dist/recipes/ScatterChart.svelte.d.ts +40 -1
  31. package/dist/recipes/StripChart.svelte +301 -0
  32. package/dist/recipes/StripChart.svelte.d.ts +89 -0
  33. package/dist/recipes/index.d.ts +2 -1
  34. package/dist/recipes/index.js +3 -1
  35. package/package.json +174 -170
  36. package/src/lib/recipes/ArrowChart.svelte +4 -1
  37. package/src/lib/recipes/BarChart.svelte +103 -18
  38. package/src/lib/recipes/BarDotChart.svelte +4 -1
  39. package/src/lib/recipes/ColumnChart.svelte +7 -3
  40. package/src/lib/recipes/LineChart.svelte +8 -2
  41. package/src/lib/recipes/ScatterChart.svelte +179 -18
  42. package/src/lib/recipes/StripChart.svelte +301 -0
  43. package/src/lib/recipes/index.ts +3 -1
package/package.json CHANGED
@@ -1,171 +1,175 @@
1
1
  {
2
- "name": "@samizdata/mozaic",
3
- "version": "0.11.0",
4
- "description": "Chart components for the web and print, built on Svelte 5 and d3, and the mozaic CLI (export embeds to SVG, PDF and PNG; eject recipes).",
5
- "license": "GPL-3.0-or-later",
6
- "author": "Nicu Calcea",
7
- "repository": {
8
- "type": "git",
9
- "url": "git+https://github.com/samizdata-co/mozaic.git",
10
- "directory": "packages/mozaic"
11
- },
12
- "homepage": "https://github.com/samizdata-co/mozaic#readme",
13
- "bugs": "https://github.com/samizdata-co/mozaic/issues",
14
- "type": "module",
15
- "scripts": {
16
- "dev": "vite dev",
17
- "package": "svelte-kit sync && svelte-package && publint",
18
- "prepare": "svelte-kit sync || echo ''",
19
- "prepack": "pnpm package",
20
- "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json && tsc -p cli/jsconfig.json",
21
- "test:unit": "vitest",
22
- "test": "vitest --run",
23
- "test:coverage": "vitest --run --coverage"
24
- },
25
- "bin": {
26
- "mozaic": "./cli/bin/mozaic.js"
27
- },
28
- "files": [
29
- "dist",
30
- "src/lib/recipes",
31
- "cli/bin",
32
- "cli/src",
33
- "!dist/**/*.test.*",
34
- "!dist/**/*.spec.*"
35
- ],
36
- "sideEffects": [
37
- "**/*.css"
38
- ],
39
- "svelte": "./dist/index.js",
40
- "types": "./dist/index.d.ts",
41
- "exports": {
42
- ".": {
43
- "mozaic-dev": "./src/lib/index.ts",
44
- "types": "./dist/index.d.ts",
45
- "svelte": "./dist/index.js"
46
- },
47
- "./core": {
48
- "mozaic-dev": "./src/lib/core/index.ts",
49
- "types": "./dist/core/index.d.ts",
50
- "svelte": "./dist/core/index.js"
51
- },
52
- "./marks": {
53
- "mozaic-dev": "./src/lib/marks/index.ts",
54
- "types": "./dist/marks/index.d.ts",
55
- "svelte": "./dist/marks/index.js"
56
- },
57
- "./guides": {
58
- "mozaic-dev": "./src/lib/guides/index.ts",
59
- "types": "./dist/guides/index.d.ts",
60
- "svelte": "./dist/guides/index.js"
61
- },
62
- "./annotations": {
63
- "mozaic-dev": "./src/lib/annotations/index.ts",
64
- "types": "./dist/annotations/index.d.ts",
65
- "svelte": "./dist/annotations/index.js"
66
- },
67
- "./interactive": {
68
- "mozaic-dev": "./src/lib/interactive/index.ts",
69
- "types": "./dist/interactive/index.d.ts",
70
- "svelte": "./dist/interactive/index.js"
71
- },
72
- "./frame": {
73
- "mozaic-dev": "./src/lib/frame/index.ts",
74
- "types": "./dist/frame/index.d.ts",
75
- "svelte": "./dist/frame/index.js"
76
- },
77
- "./table": {
78
- "mozaic-dev": "./src/lib/table/index.ts",
79
- "types": "./dist/table/index.d.ts",
80
- "svelte": "./dist/table/index.js"
81
- },
82
- "./embed": {
83
- "mozaic-dev": "./src/lib/embed/index.ts",
84
- "types": "./dist/embed/index.d.ts",
85
- "svelte": "./dist/embed/index.js"
86
- },
87
- "./export": {
88
- "mozaic-dev": "./src/lib/export/index.ts",
89
- "types": "./dist/export/index.d.ts",
90
- "svelte": "./dist/export/index.js"
91
- },
92
- "./recipes": {
93
- "mozaic-dev": "./src/lib/recipes/index.ts",
94
- "types": "./dist/recipes/index.d.ts",
95
- "svelte": "./dist/recipes/index.js"
96
- },
97
- "./styles": {
98
- "mozaic-dev": "./src/lib/styles/index.ts",
99
- "types": "./dist/styles/index.d.ts",
100
- "default": "./dist/styles/index.js"
101
- },
102
- "./locale": {
103
- "mozaic-dev": "./src/lib/locale/index.ts",
104
- "types": "./dist/locale/index.d.ts",
105
- "default": "./dist/locale/index.js"
106
- },
107
- "./transforms": {
108
- "mozaic-dev": "./src/lib/transforms/index.ts",
109
- "types": "./dist/transforms/index.d.ts",
110
- "default": "./dist/transforms/index.js"
111
- },
112
- "./cli": {
113
- "types": "./cli/src/index.d.ts",
114
- "default": "./cli/src/index.js"
115
- },
116
- "./package.json": "./package.json"
117
- },
118
- "peerDependencies": {
119
- "svelte": "^5.57.0"
120
- },
121
- "dependencies": {
122
- "d3-array": "^3.2.4",
123
- "d3-delaunay": "^6.0.4",
124
- "d3-format": "^3.1.0",
125
- "d3-geo": "^3.1.1",
126
- "d3-hierarchy": "^3.1.2",
127
- "d3-scale": "^4.0.2",
128
- "d3-shape": "^3.2.0",
129
- "d3-time-format": "^4.1.0",
130
- "playwright-core": "1.64.1"
131
- },
132
- "devDependencies": {
133
- "@sveltejs/adapter-auto": "^8.0.0",
134
- "@sveltejs/kit": "^3.1.0",
135
- "@sveltejs/package": "^3.0.1",
136
- "@sveltejs/vite-plugin-svelte": "^7.3.1",
137
- "@types/d3-array": "^3.2.1",
138
- "@types/d3-delaunay": "^6.0.4",
139
- "@types/d3-format": "^3.0.4",
140
- "@types/d3-geo": "^3.1.0",
141
- "@types/d3-hierarchy": "^3.1.7",
142
- "@types/d3-scale": "^4.0.8",
143
- "@types/d3-scale-chromatic": "^3.1.0",
144
- "@types/d3-shape": "^3.1.6",
145
- "@types/d3-time-format": "^4.0.3",
146
- "@types/geojson": "^7946.0.14",
147
- "@types/node": "^24.0.0",
148
- "@vitest/coverage-v8": "^4.1.12",
149
- "d3-scale-chromatic": "^3.1.0",
150
- "linkedom": "^0.18.14",
151
- "publint": "^0.3.21",
152
- "svelte": "^5.57.2",
153
- "svelte-check": "^4.6.0",
154
- "typescript": "^6.0.3",
155
- "vite": "^8.3.5",
156
- "vitest": "^4.1.8"
157
- },
158
- "keywords": [
159
- "svelte",
160
- "charts",
161
- "dataviz",
162
- "d3"
163
- ],
164
- "imports": {
165
- "#lib": "./src/lib/index.js",
166
- "#lib/*": "./src/lib/*"
167
- },
168
- "engines": {
169
- "node": ">=22.17"
170
- }
171
- }
2
+ "name": "@samizdata/mozaic",
3
+ "version": "0.12.1",
4
+ "description": "Chart components for the web and print, built on Svelte 5 and d3, and the mozaic CLI (export embeds to SVG, PDF and PNG; eject recipes).",
5
+ "license": "GPL-3.0-or-later",
6
+ "author": "Nicu Calcea",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/samizdata-co/mozaic.git",
10
+ "directory": "packages/mozaic"
11
+ },
12
+ "homepage": "https://github.com/samizdata-co/mozaic#readme",
13
+ "bugs": "https://github.com/samizdata-co/mozaic/issues",
14
+ "type": "module",
15
+ "bin": {
16
+ "mozaic": "./cli/bin/mozaic.js"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "src/lib/recipes",
21
+ "cli/bin",
22
+ "cli/src",
23
+ "!dist/**/*.test.*",
24
+ "!dist/**/*.spec.*"
25
+ ],
26
+ "sideEffects": [
27
+ "**/*.css"
28
+ ],
29
+ "svelte": "./dist/index.js",
30
+ "types": "./dist/index.d.ts",
31
+ "exports": {
32
+ ".": {
33
+ "mozaic-dev": "./src/lib/index.ts",
34
+ "types": "./dist/index.d.ts",
35
+ "svelte": "./dist/index.js"
36
+ },
37
+ "./core": {
38
+ "mozaic-dev": "./src/lib/core/index.ts",
39
+ "types": "./dist/core/index.d.ts",
40
+ "svelte": "./dist/core/index.js"
41
+ },
42
+ "./marks": {
43
+ "mozaic-dev": "./src/lib/marks/index.ts",
44
+ "types": "./dist/marks/index.d.ts",
45
+ "svelte": "./dist/marks/index.js"
46
+ },
47
+ "./guides": {
48
+ "mozaic-dev": "./src/lib/guides/index.ts",
49
+ "types": "./dist/guides/index.d.ts",
50
+ "svelte": "./dist/guides/index.js"
51
+ },
52
+ "./annotations": {
53
+ "mozaic-dev": "./src/lib/annotations/index.ts",
54
+ "types": "./dist/annotations/index.d.ts",
55
+ "svelte": "./dist/annotations/index.js"
56
+ },
57
+ "./interactive": {
58
+ "mozaic-dev": "./src/lib/interactive/index.ts",
59
+ "types": "./dist/interactive/index.d.ts",
60
+ "svelte": "./dist/interactive/index.js"
61
+ },
62
+ "./frame": {
63
+ "mozaic-dev": "./src/lib/frame/index.ts",
64
+ "types": "./dist/frame/index.d.ts",
65
+ "svelte": "./dist/frame/index.js"
66
+ },
67
+ "./table": {
68
+ "mozaic-dev": "./src/lib/table/index.ts",
69
+ "types": "./dist/table/index.d.ts",
70
+ "svelte": "./dist/table/index.js"
71
+ },
72
+ "./embed": {
73
+ "mozaic-dev": "./src/lib/embed/index.ts",
74
+ "types": "./dist/embed/index.d.ts",
75
+ "svelte": "./dist/embed/index.js"
76
+ },
77
+ "./export": {
78
+ "mozaic-dev": "./src/lib/export/index.ts",
79
+ "types": "./dist/export/index.d.ts",
80
+ "svelte": "./dist/export/index.js"
81
+ },
82
+ "./recipes": {
83
+ "mozaic-dev": "./src/lib/recipes/index.ts",
84
+ "types": "./dist/recipes/index.d.ts",
85
+ "svelte": "./dist/recipes/index.js"
86
+ },
87
+ "./styles": {
88
+ "mozaic-dev": "./src/lib/styles/index.ts",
89
+ "types": "./dist/styles/index.d.ts",
90
+ "default": "./dist/styles/index.js"
91
+ },
92
+ "./locale": {
93
+ "mozaic-dev": "./src/lib/locale/index.ts",
94
+ "types": "./dist/locale/index.d.ts",
95
+ "default": "./dist/locale/index.js"
96
+ },
97
+ "./flags": {
98
+ "mozaic-dev": "./src/lib/flags/index.ts",
99
+ "types": "./dist/flags/index.d.ts",
100
+ "default": "./dist/flags/index.js"
101
+ },
102
+ "./transforms": {
103
+ "mozaic-dev": "./src/lib/transforms/index.ts",
104
+ "types": "./dist/transforms/index.d.ts",
105
+ "default": "./dist/transforms/index.js"
106
+ },
107
+ "./cli": {
108
+ "types": "./cli/src/index.d.ts",
109
+ "default": "./cli/src/index.js"
110
+ },
111
+ "./package.json": "./package.json"
112
+ },
113
+ "peerDependencies": {
114
+ "svelte": "^5.57.0"
115
+ },
116
+ "dependencies": {
117
+ "d3-array": "^3.2.4",
118
+ "d3-delaunay": "^6.0.4",
119
+ "d3-format": "^3.1.0",
120
+ "d3-geo": "^3.1.1",
121
+ "d3-hierarchy": "^3.1.2",
122
+ "d3-scale": "^4.0.2",
123
+ "d3-shape": "^3.2.0",
124
+ "d3-time-format": "^4.1.0",
125
+ "playwright-core": "1.64.1"
126
+ },
127
+ "devDependencies": {
128
+ "@sveltejs/adapter-auto": "^8.0.0",
129
+ "@sveltejs/kit": "^3.1.0",
130
+ "@sveltejs/package": "^3.0.1",
131
+ "@sveltejs/vite-plugin-svelte": "^7.3.1",
132
+ "@types/d3-array": "^3.2.1",
133
+ "@types/d3-delaunay": "^6.0.4",
134
+ "@types/d3-format": "^3.0.4",
135
+ "@types/d3-geo": "^3.1.0",
136
+ "@types/d3-hierarchy": "^3.1.7",
137
+ "@types/d3-scale": "^4.0.8",
138
+ "@types/d3-scale-chromatic": "^3.1.0",
139
+ "@types/d3-shape": "^3.1.6",
140
+ "@types/d3-time-format": "^4.0.3",
141
+ "@types/geojson": "^7946.0.14",
142
+ "@types/node": "^24.0.0",
143
+ "@vitest/coverage-v8": "^4.1.12",
144
+ "d3-scale-chromatic": "^3.1.0",
145
+ "flag-icons": "7.6.0",
146
+ "linkedom": "^0.18.14",
147
+ "publint": "^0.3.21",
148
+ "svelte": "^5.57.2",
149
+ "svelte-check": "^4.6.0",
150
+ "typescript": "^6.0.3",
151
+ "vite": "^8.3.5",
152
+ "vitest": "^4.1.8"
153
+ },
154
+ "keywords": [
155
+ "svelte",
156
+ "charts",
157
+ "dataviz",
158
+ "d3"
159
+ ],
160
+ "imports": {
161
+ "#lib": "./src/lib/index.js",
162
+ "#lib/*": "./src/lib/*"
163
+ },
164
+ "engines": {
165
+ "node": ">=22.17"
166
+ },
167
+ "scripts": {
168
+ "dev": "vite dev",
169
+ "package": "svelte-kit sync && svelte-package && publint",
170
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json && tsc -p cli/jsconfig.json",
171
+ "test:unit": "vitest",
172
+ "test": "vitest --run",
173
+ "test:coverage": "vitest --run --coverage"
174
+ }
175
+ }
@@ -44,6 +44,8 @@ project and change anything.
44
44
  /** The chart's embed page, for the embed code under the chart (see `<Chart embed>`). */
45
45
  embed?: ChartEmbed;
46
46
  /** Value label format; defaults to locale numbers. */
47
+ /** Unit after the last value-axis tick label and the tooltip values, e.g. "lei/m³". */
48
+ unit?: string;
47
49
  format?: (n: number) => string;
48
50
  /** Axis tick format; defaults to `format`. */
49
51
  tickFormat?: (n: number) => string;
@@ -77,6 +79,7 @@ project and change anything.
77
79
  source,
78
80
  alt,
79
81
  embed,
82
+ unit,
80
83
  format,
81
84
  tickFormat,
82
85
  color,
@@ -165,7 +168,7 @@ project and change anything.
165
168
  {#snippet children(plot)}
166
169
  <Svg>
167
170
  <GridX ticks={4} />
168
- <AxisX ticks={4} format={tickFormat ?? fmt} />
171
+ <AxisX ticks={4} format={tickFormat ?? fmt} {unit} />
169
172
  <AxisY />
170
173
  <Arrows
171
174
  data={both}
@@ -1,7 +1,12 @@
1
1
  <!--
2
2
  @component
3
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
4
+ the full frame. Rows known only as a range (`range`) get a solid bar to the
5
+ low end, a lighter one to the high end and a "low–high" label. Value labels
6
+ sit inside the end of each bar where they fit (in white or the text colour,
7
+ whichever reads on the bar) and just past it otherwise, and the bars run to the
8
+ largest value, so there is no empty space on the right. Pass `flag`
9
+ to put a circular flag between each label and its bar. Use it as is, or run `mozaic eject BarChart` to copy this
5
10
  file into your project and change anything.
6
11
  -->
7
12
  <script lang="ts" generics="T">
@@ -11,11 +16,11 @@ file into your project and change anything.
11
16
  import { Chart, Header, Title, Subtitle, Footer, Source, Notes, type SourceItem } from '../frame/index.js';
12
17
  import type { ChartEmbed } from '../embed/index.js';
13
18
  import { Plot, Svg, Html, estimateTextWidth, useTextWidth, scaled, type PlotContext } from '../core/index.js';
14
- import { Bars, Rules, Text } from '../marks/index.js';
19
+ import { Bars, Flag, Rules, Text } from '../marks/index.js';
15
20
  import { AxisY } from '../guides/index.js';
16
21
  import { Tooltip } from '../interactive/index.js';
17
22
  import { createLocale, type LocaleCode } from '../locale/index.js';
18
- import { base, type Medium, type Theme } from '../styles/index.js';
23
+ import { base, contrastRatio, type Medium, type Theme } from '../styles/index.js';
19
24
 
20
25
  interface Props {
21
26
  data: T[];
@@ -33,10 +38,23 @@ file into your project and change anything.
33
38
  alt?: string;
34
39
  /** The chart's embed page, for the embed code under the chart (see `<Chart embed>`). */
35
40
  embed?: ChartEmbed;
36
- /** Value label format; defaults to locale numbers. */
41
+ /** Value label format; defaults to locale numbers. Use the locale's `money` for prices. */
37
42
  format?: (n: number) => string;
43
+ /** Unit after the value in the tooltip, e.g. "lei/m³" (bar charts have no value axis). */
44
+ unit?: string;
38
45
  /** Bar colour; defaults to the first categorical colour. */
39
46
  color?: (d: T) => string;
47
+ /**
48
+ * Low and high ends for rows known only as a range (several suppliers,
49
+ * regions or estimates). The bar is solid to the low end and lighter to
50
+ * the high end, and the label reads "low–high". Return `null` for rows
51
+ * with a single value; `value` still sets the sort order.
52
+ */
53
+ range?: (d: T) => [number, number] | null | undefined;
54
+ /** The row's flag from `@samizdata/mozaic/flags`, drawn between its label and its bar. */
55
+ flag?: (d: T) => string | null | undefined;
56
+ /** Put value labels inside the bars where they fit (default true); otherwise always after them. */
57
+ labelsInside?: boolean;
40
58
  /** Sort largest first (default) or keep data order. */
41
59
  sort?: boolean;
42
60
  rowHeight?: number;
@@ -59,7 +77,11 @@ file into your project and change anything.
59
77
  alt,
60
78
  embed,
61
79
  format,
80
+ unit,
62
81
  color,
82
+ range,
83
+ flag,
84
+ labelsInside = true,
63
85
  sort = true,
64
86
  rowHeight = 28,
65
87
  annotations
@@ -71,15 +93,47 @@ file into your project and change anything.
71
93
  const labelWidth = (s: string) => measure(s, theme.plot.fontSize, theme.tokens['--mz-font-body']);
72
94
  const fmt = $derived(format ?? createLocale(locale ?? theme.defaultLocale).number);
73
95
  const rows = $derived(sort ? data.toSorted((a, b) => value(b) - value(a)) : data);
74
- const x = $derived(
75
- scaleLinear()
76
- .domain([Math.min(0, min(rows, value) ?? 0), Math.max(0, max(rows, value) ?? 1)])
77
- .nice()
78
- );
96
+ const span = (d: T) => range?.(d) ?? null;
97
+ /** Where the solid bar ends: the value, or the low end of a range. */
98
+ const low = (d: T) => span(d)?.[0] ?? value(d);
99
+ /** Where the bar and its label end: the value, or the high end of a range. */
100
+ const high = (d: T) => span(d)?.[1] ?? value(d);
101
+ const ranged = $derived(rows.filter((d) => span(d)));
102
+ const valueLabel = (d: T) => {
103
+ const r = span(d);
104
+ return r ? `${fmt(r[0])}–${fmt(r[1])}` : fmt(value(d));
105
+ };
106
+ // No value axis is drawn, so the scale runs exactly to the data: rounding it up would only leave space.
107
+ const x = $derived(scaleLinear().domain([Math.min(0, min(rows, low) ?? 0), Math.max(0, max(rows, high) ?? 1)]));
79
108
  const y = $derived(scaleBand<string>().domain(rows.map(label)).paddingInner(0.25).paddingOuter(0.1));
80
- const left = $derived(Math.max(...rows.map((d) => labelWidth(label(d))), 0) + 12);
81
- const right = $derived(Math.max(...rows.map((d) => estimateTextWidth(fmt(value(d)), theme.plot.fontSize)), 0) + 12);
82
- let hovered = $state<T | null>(null);
109
+ /** Flag radius and the room the flags take between the labels and the bars. */
110
+ const flagR = $derived(Math.round(theme.plot.fontSize * 0.6));
111
+ const flagGap = 4;
112
+ const flagRoom = $derived(flag && rows.some((d) => flag(d)) ? flagR * 2 + flagGap : 0);
113
+ const left = $derived(Math.max(...rows.map((d) => labelWidth(label(d))), 0) + 12 + flagRoom);
114
+ const textWidth = (d: T) => estimateTextWidth(valueLabel(d), theme.plot.fontSize);
115
+ /** Labels that fit inside their solid bar, for a plot area `w` px wide. Ranges keep theirs outside. */
116
+ const insideFor = (w: number) => {
117
+ const scale = x.copy().range([0, w]);
118
+ return (d: T) => labelsInside && !span(d) && low(d) > 0 && textWidth(d) + 12 <= scale(low(d)) - scale(0);
119
+ };
120
+ /** Room on the right for the labels that stay outside, for a plot area `w` px wide. */
121
+ const rightFor = (w: number) => {
122
+ const scale = x.copy().range([0, w]);
123
+ const inside = insideFor(w);
124
+ return Math.max(0, ...rows.filter((d) => !inside(d)).map((d) => scale(high(d)) + 4 + textWidth(d) - w)) + 4;
125
+ };
126
+ // The room changes the bar lengths, which changes which labels fit: two passes settle it.
127
+ const padding = (size: { width: number }) => {
128
+ const w = size.width - left;
129
+ const first = rightFor(w);
130
+ return { left, right: Math.max(first, rightFor(w - first)), top: 0, bottom: 0 };
131
+ };
132
+ const barColor = (d: T) => color?.(d) ?? theme.palettes.categorical[0];
133
+ /** White on dark bars, the text colour on light ones. */
134
+ const inkOn = (d: T) => (contrastRatio(barColor(d), '#ffffff') >= 4.5 ? '#ffffff' : 'var(--mz-color-text)');
135
+ // Raw, not a proxy: marks compare the hovered row with each datum by identity.
136
+ let hovered = $state.raw<T | null>(null);
83
137
  </script>
84
138
 
85
139
  <Chart {style} {locale} {medium} {width} {alt} {embed}>
@@ -90,13 +144,34 @@ file into your project and change anything.
90
144
  </Header>
91
145
  {/if}
92
146
 
93
- <Plot {x} {y} height={rows.length * rowHeight} padding={{ left, right, top: 0, bottom: 0 }}>
147
+ <Plot {x} {y} height={rows.length * rowHeight} {padding}>
94
148
  {#snippet children(plot)}
149
+ {@const inside = insideFor(plot.width)}
95
150
  <Svg>
96
- <AxisY />
151
+ <AxisY tickPadding={flagRoom ? theme.plot.tickPadding + flagRoom : undefined} />
152
+ {#if flagRoom}
153
+ {#each rows as d, i (i)}
154
+ {@const svg = flag?.(d)}
155
+ {#if svg}
156
+ <Flag {svg} cx={-(flagR + flagGap)} cy={scaled(plot.y, label(d))} r={flagR} />
157
+ {/if}
158
+ {/each}
159
+ {/if}
160
+ {#if ranged.length}
161
+ <Bars
162
+ class="mz-bars-range"
163
+ data={ranged}
164
+ x={high}
165
+ y={label}
166
+ fill={color}
167
+ opacity={(d) => (hovered && hovered !== d ? 0.2 : 0.4)}
168
+ onpointerenter={(d) => (hovered = d)}
169
+ onpointerleave={() => (hovered = null)}
170
+ />
171
+ {/if}
97
172
  <Bars
98
173
  data={rows}
99
- x={value}
174
+ x={low}
100
175
  y={label}
101
176
  fill={color}
102
177
  opacity={(d) => (hovered && hovered !== d ? 0.5 : 1)}
@@ -104,13 +179,23 @@ file into your project and change anything.
104
179
  onpointerleave={() => (hovered = null)}
105
180
  />
106
181
  <Rules x={[0]} />
107
- <Text data={rows} x={value} y={label} text={(d) => fmt(value(d))} dx={4} />
182
+ <Text data={rows.filter((d) => !inside(d))} x={high} y={label} text={valueLabel} dx={4} />
183
+ <Text
184
+ class="mz-bar-value-inside"
185
+ data={rows.filter(inside)}
186
+ x={low}
187
+ y={label}
188
+ text={valueLabel}
189
+ dx={-6}
190
+ anchor="end"
191
+ fill={inkOn}
192
+ />
108
193
  {@render annotations?.(plot)}
109
194
  </Svg>
110
195
  <Html>
111
196
  {#if hovered}
112
- <Tooltip x={plot.x(value(hovered)) ?? 0} y={scaled(plot.y, label(hovered))}>
113
- <strong>{label(hovered)}</strong><br />{fmt(value(hovered))}
197
+ <Tooltip x={plot.x(high(hovered)) ?? 0} y={scaled(plot.y, label(hovered))}>
198
+ <strong>{label(hovered)}</strong><br />{valueLabel(hovered)}{unit ? ` ${unit}` : ''}
114
199
  </Tooltip>
115
200
  {/if}
116
201
  </Html>
@@ -44,6 +44,8 @@ anything.
44
44
  /** The chart's embed page, for the embed code under the chart (see `<Chart embed>`). */
45
45
  embed?: ChartEmbed;
46
46
  /** Value label format; defaults to locale numbers. */
47
+ /** Unit after the last value-axis tick label and the tooltip values, e.g. "lei/m³". */
48
+ unit?: string;
47
49
  format?: (n: number) => string;
48
50
  /** Change label format; defaults to one decimal with a sign. */
49
51
  formatChange?: (n: number) => string;
@@ -79,6 +81,7 @@ anything.
79
81
  source,
80
82
  alt,
81
83
  embed,
84
+ unit,
82
85
  format,
83
86
  formatChange,
84
87
  tickFormat,
@@ -148,7 +151,7 @@ anything.
148
151
  {#snippet children(plot)}
149
152
  <Svg>
150
153
  <GridX ticks={4} />
151
- <AxisX ticks={4} format={tickFormat ?? fmt} />
154
+ <AxisX ticks={4} format={tickFormat ?? fmt} {unit} />
152
155
  <AxisY />
153
156
  <Bars
154
157
  data={rows.filter((d) => has(value(d)))}
@@ -34,6 +34,8 @@ copies this file into your project.
34
34
  alt?: string;
35
35
  /** The chart's embed page, for the embed code under the chart (see `<Chart embed>`). */
36
36
  embed?: ChartEmbed;
37
+ /** Unit after the last value-axis tick label and the tooltip values, e.g. "lei/m³". */
38
+ unit?: string;
37
39
  format?: (n: number) => string;
38
40
  color?: (d: T) => string;
39
41
  height?: number;
@@ -56,6 +58,7 @@ copies this file into your project.
56
58
  source,
57
59
  alt,
58
60
  embed,
61
+ unit,
59
62
  format,
60
63
  color,
61
64
  height = 280,
@@ -71,7 +74,8 @@ copies this file into your project.
71
74
  .domain([Math.min(0, min(data, value) ?? 0), Math.max(0, max(data, value) ?? 1)])
72
75
  .nice()
73
76
  );
74
- let hovered = $state<T | null>(null);
77
+ // Raw, not a proxy: marks compare the hovered row with each datum by identity.
78
+ let hovered = $state.raw<T | null>(null);
75
79
  </script>
76
80
 
77
81
  <Chart {style} {locale} {medium} {width} {alt} {embed}>
@@ -87,7 +91,7 @@ copies this file into your project.
87
91
  {@const every = labelEvery ?? Math.max(1, Math.ceil((data.length * 36) / Math.max(1, plot.width)))}
88
92
  <Svg>
89
93
  <GridY ticks={5} />
90
- <AxisY ticks={5} format={fmt} />
94
+ <AxisY ticks={5} format={fmt} {unit} />
91
95
  <AxisX tickValues={data.map(label).filter((_, i) => i % every === 0)} />
92
96
  <Bars
93
97
  {data}
@@ -104,7 +108,7 @@ copies this file into your project.
104
108
  <Html>
105
109
  {#if hovered}
106
110
  <Tooltip x={scaled(plot.x, label(hovered))} y={plot.y(value(hovered)) ?? 0}>
107
- <strong>{label(hovered)}</strong><br />{fmt(value(hovered))}
111
+ <strong>{label(hovered)}</strong><br />{fmt(value(hovered))}{unit ? ` ${unit}` : ''}
108
112
  </Tooltip>
109
113
  {/if}
110
114
  </Html>