@samizdata/mozaic 0.11.0 → 0.12.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.
- package/dist/flags/index.d.ts +16 -0
- package/dist/flags/index.js +16 -0
- package/dist/flags/svgs.d.ts +261 -0
- package/dist/flags/svgs.js +265 -0
- package/dist/guides/AxisX.svelte +18 -3
- package/dist/guides/AxisX.svelte.d.ts +4 -0
- package/dist/interactive/Tooltip.svelte +10 -3
- package/dist/locale/index.d.ts +8 -0
- package/dist/locale/index.js +10 -0
- package/dist/marks/Beeswarm.svelte +31 -12
- package/dist/marks/Beeswarm.svelte.d.ts +3 -0
- package/dist/marks/Flag.svelte +65 -0
- package/dist/marks/Flag.svelte.d.ts +27 -0
- package/dist/marks/Flags.svelte +69 -0
- package/dist/marks/Flags.svelte.d.ts +45 -0
- package/dist/marks/index.d.ts +2 -0
- package/dist/marks/index.js +2 -0
- package/dist/recipes/ArrowChart.svelte +4 -1
- package/dist/recipes/ArrowChart.svelte.d.ts +2 -0
- package/dist/recipes/BarChart.svelte +101 -17
- package/dist/recipes/BarChart.svelte.d.ts +20 -2
- package/dist/recipes/BarDotChart.svelte +4 -1
- package/dist/recipes/BarDotChart.svelte.d.ts +2 -0
- package/dist/recipes/ColumnChart.svelte +5 -2
- package/dist/recipes/ColumnChart.svelte.d.ts +2 -0
- package/dist/recipes/LineChart.svelte +8 -2
- package/dist/recipes/LineChart.svelte.d.ts +2 -0
- package/dist/recipes/ScatterChart.svelte +177 -17
- package/dist/recipes/ScatterChart.svelte.d.ts +40 -1
- package/dist/recipes/StripChart.svelte +301 -0
- package/dist/recipes/StripChart.svelte.d.ts +89 -0
- package/dist/recipes/index.d.ts +2 -1
- package/dist/recipes/index.js +3 -1
- package/package.json +174 -170
- package/src/lib/recipes/ArrowChart.svelte +4 -1
- package/src/lib/recipes/BarChart.svelte +101 -17
- package/src/lib/recipes/BarDotChart.svelte +4 -1
- package/src/lib/recipes/ColumnChart.svelte +5 -2
- package/src/lib/recipes/LineChart.svelte +8 -2
- package/src/lib/recipes/ScatterChart.svelte +177 -17
- package/src/lib/recipes/StripChart.svelte +301 -0
- package/src/lib/recipes/index.ts +3 -1
package/package.json
CHANGED
|
@@ -1,171 +1,175 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
2
|
+
"name": "@samizdata/mozaic",
|
|
3
|
+
"version": "0.12.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
|
+
"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.
|
|
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,14 +93,45 @@ 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
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
81
|
-
const
|
|
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)');
|
|
82
135
|
let hovered = $state<T | null>(null);
|
|
83
136
|
</script>
|
|
84
137
|
|
|
@@ -90,13 +143,34 @@ file into your project and change anything.
|
|
|
90
143
|
</Header>
|
|
91
144
|
{/if}
|
|
92
145
|
|
|
93
|
-
<Plot {x} {y} height={rows.length * rowHeight} padding
|
|
146
|
+
<Plot {x} {y} height={rows.length * rowHeight} {padding}>
|
|
94
147
|
{#snippet children(plot)}
|
|
148
|
+
{@const inside = insideFor(plot.width)}
|
|
95
149
|
<Svg>
|
|
96
|
-
<AxisY />
|
|
150
|
+
<AxisY tickPadding={flagRoom ? theme.plot.tickPadding + flagRoom : undefined} />
|
|
151
|
+
{#if flagRoom}
|
|
152
|
+
{#each rows as d, i (i)}
|
|
153
|
+
{@const svg = flag?.(d)}
|
|
154
|
+
{#if svg}
|
|
155
|
+
<Flag {svg} cx={-(flagR + flagGap)} cy={scaled(plot.y, label(d))} r={flagR} />
|
|
156
|
+
{/if}
|
|
157
|
+
{/each}
|
|
158
|
+
{/if}
|
|
159
|
+
{#if ranged.length}
|
|
160
|
+
<Bars
|
|
161
|
+
class="mz-bars-range"
|
|
162
|
+
data={ranged}
|
|
163
|
+
x={high}
|
|
164
|
+
y={label}
|
|
165
|
+
fill={color}
|
|
166
|
+
opacity={(d) => (hovered && hovered !== d ? 0.2 : 0.4)}
|
|
167
|
+
onpointerenter={(d) => (hovered = d)}
|
|
168
|
+
onpointerleave={() => (hovered = null)}
|
|
169
|
+
/>
|
|
170
|
+
{/if}
|
|
97
171
|
<Bars
|
|
98
172
|
data={rows}
|
|
99
|
-
x={
|
|
173
|
+
x={low}
|
|
100
174
|
y={label}
|
|
101
175
|
fill={color}
|
|
102
176
|
opacity={(d) => (hovered && hovered !== d ? 0.5 : 1)}
|
|
@@ -104,13 +178,23 @@ file into your project and change anything.
|
|
|
104
178
|
onpointerleave={() => (hovered = null)}
|
|
105
179
|
/>
|
|
106
180
|
<Rules x={[0]} />
|
|
107
|
-
<Text data={rows} x={
|
|
181
|
+
<Text data={rows.filter((d) => !inside(d))} x={high} y={label} text={valueLabel} dx={4} />
|
|
182
|
+
<Text
|
|
183
|
+
class="mz-bar-value-inside"
|
|
184
|
+
data={rows.filter(inside)}
|
|
185
|
+
x={low}
|
|
186
|
+
y={label}
|
|
187
|
+
text={valueLabel}
|
|
188
|
+
dx={-6}
|
|
189
|
+
anchor="end"
|
|
190
|
+
fill={inkOn}
|
|
191
|
+
/>
|
|
108
192
|
{@render annotations?.(plot)}
|
|
109
193
|
</Svg>
|
|
110
194
|
<Html>
|
|
111
195
|
{#if hovered}
|
|
112
|
-
<Tooltip x={plot.x(
|
|
113
|
-
<strong>{label(hovered)}</strong><br />{
|
|
196
|
+
<Tooltip x={plot.x(high(hovered)) ?? 0} y={scaled(plot.y, label(hovered))}>
|
|
197
|
+
<strong>{label(hovered)}</strong><br />{valueLabel(hovered)}{unit ? ` ${unit}` : ''}
|
|
114
198
|
</Tooltip>
|
|
115
199
|
{/if}
|
|
116
200
|
</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,
|
|
@@ -87,7 +90,7 @@ copies this file into your project.
|
|
|
87
90
|
{@const every = labelEvery ?? Math.max(1, Math.ceil((data.length * 36) / Math.max(1, plot.width)))}
|
|
88
91
|
<Svg>
|
|
89
92
|
<GridY ticks={5} />
|
|
90
|
-
<AxisY ticks={5} format={fmt} />
|
|
93
|
+
<AxisY ticks={5} format={fmt} {unit} />
|
|
91
94
|
<AxisX tickValues={data.map(label).filter((_, i) => i % every === 0)} />
|
|
92
95
|
<Bars
|
|
93
96
|
{data}
|
|
@@ -104,7 +107,7 @@ copies this file into your project.
|
|
|
104
107
|
<Html>
|
|
105
108
|
{#if hovered}
|
|
106
109
|
<Tooltip x={scaled(plot.x, label(hovered))} y={plot.y(value(hovered)) ?? 0}>
|
|
107
|
-
<strong>{label(hovered)}</strong><br />{fmt(value(hovered))}
|
|
110
|
+
<strong>{label(hovered)}</strong><br />{fmt(value(hovered))}{unit ? ` ${unit}` : ''}
|
|
108
111
|
</Tooltip>
|
|
109
112
|
{/if}
|
|
110
113
|
</Html>
|