@samizdata/mozaic 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +44 -2
- package/dist/annotations/Arrow.svelte +50 -0
- package/dist/annotations/Arrow.svelte.d.ts +18 -0
- package/dist/annotations/Callout.svelte +87 -0
- package/dist/annotations/Callout.svelte.d.ts +22 -0
- package/dist/annotations/Label.svelte +55 -0
- package/dist/annotations/Label.svelte.d.ts +19 -0
- package/dist/annotations/RefBand.svelte +46 -0
- package/dist/annotations/RefBand.svelte.d.ts +18 -0
- package/dist/annotations/RefLine.svelte +107 -0
- package/dist/annotations/RefLine.svelte.d.ts +27 -0
- package/dist/annotations/callout-geometry.d.ts +23 -0
- package/dist/annotations/callout-geometry.js +45 -0
- package/dist/annotations/index.d.ts +6 -0
- package/dist/annotations/index.js +6 -0
- package/dist/core/Canvas.svelte +55 -0
- package/dist/core/Canvas.svelte.d.ts +19 -0
- package/dist/core/Facets.svelte +80 -0
- package/dist/core/Facets.svelte.d.ts +49 -0
- package/dist/core/Html.svelte +38 -0
- package/dist/core/Html.svelte.d.ts +13 -0
- package/dist/core/Plot.svelte +202 -0
- package/dist/core/Plot.svelte.d.ts +60 -0
- package/dist/core/Svg.svelte +58 -0
- package/dist/core/Svg.svelte.d.ts +17 -0
- package/dist/core/accessor.d.ts +5 -0
- package/dist/core/accessor.js +3 -0
- package/dist/core/context.d.ts +25 -0
- package/dist/core/context.js +69 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.js +9 -0
- package/dist/core/scales.d.ts +16 -0
- package/dist/core/scales.js +32 -0
- package/dist/core/types.d.ts +78 -0
- package/dist/core/types.js +1 -0
- package/dist/core/units.d.ts +6 -0
- package/dist/core/units.js +10 -0
- package/dist/embed/Embed.svelte +50 -0
- package/dist/embed/Embed.svelte.d.ts +14 -0
- package/dist/embed/code.d.ts +18 -0
- package/dist/embed/code.js +9 -0
- package/dist/embed/host.d.ts +6 -0
- package/dist/embed/host.js +20 -0
- package/dist/embed/index.d.ts +6 -0
- package/dist/embed/index.js +5 -0
- package/dist/embed/manifest.d.ts +16 -0
- package/dist/embed/manifest.js +1 -0
- package/dist/embed/params.svelte.d.ts +10 -0
- package/dist/embed/params.svelte.js +27 -0
- package/dist/embed/resize.d.ts +13 -0
- package/dist/embed/resize.js +28 -0
- package/dist/export/download.d.ts +13 -0
- package/dist/export/download.js +52 -0
- package/dist/export/figure-to-svg.d.ts +21 -0
- package/dist/export/figure-to-svg.js +187 -0
- package/dist/export/fonts.d.ts +26 -0
- package/dist/export/fonts.js +112 -0
- package/dist/export/index.d.ts +2 -0
- package/dist/export/index.js +2 -0
- package/dist/frame/Byline.svelte +26 -0
- package/dist/frame/Byline.svelte.d.ts +8 -0
- package/dist/frame/Chart.svelte +144 -0
- package/dist/frame/Chart.svelte.d.ts +37 -0
- package/dist/frame/ChartTools.svelte +259 -0
- package/dist/frame/ChartTools.svelte.d.ts +17 -0
- package/dist/frame/DataDownload.svelte +34 -0
- package/dist/frame/DataDownload.svelte.d.ts +34 -0
- package/dist/frame/DataTable.svelte +49 -0
- package/dist/frame/DataTable.svelte.d.ts +34 -0
- package/dist/frame/Footer.svelte +39 -0
- package/dist/frame/Footer.svelte.d.ts +10 -0
- package/dist/frame/Header.svelte +21 -0
- package/dist/frame/Header.svelte.d.ts +9 -0
- package/dist/frame/Logo.svelte +57 -0
- package/dist/frame/Logo.svelte.d.ts +8 -0
- package/dist/frame/Notes.svelte +29 -0
- package/dist/frame/Notes.svelte.d.ts +9 -0
- package/dist/frame/Source.svelte +29 -0
- package/dist/frame/Source.svelte.d.ts +9 -0
- package/dist/frame/Subtitle.svelte +22 -0
- package/dist/frame/Subtitle.svelte.d.ts +9 -0
- package/dist/frame/Title.svelte +31 -0
- package/dist/frame/Title.svelte.d.ts +9 -0
- package/dist/frame/index.d.ts +12 -0
- package/dist/frame/index.js +12 -0
- package/dist/guides/AxisX.svelte +101 -0
- package/dist/guides/AxisX.svelte.d.ts +22 -0
- package/dist/guides/AxisY.svelte +107 -0
- package/dist/guides/AxisY.svelte.d.ts +25 -0
- package/dist/guides/ColorRamp.svelte +93 -0
- package/dist/guides/ColorRamp.svelte.d.ts +25 -0
- package/dist/guides/GridX.svelte +35 -0
- package/dist/guides/GridX.svelte.d.ts +9 -0
- package/dist/guides/GridY.svelte +35 -0
- package/dist/guides/GridY.svelte.d.ts +9 -0
- package/dist/guides/Key.svelte +48 -0
- package/dist/guides/Key.svelte.d.ts +14 -0
- package/dist/guides/Legend.svelte +112 -0
- package/dist/guides/Legend.svelte.d.ts +26 -0
- package/dist/guides/SizeLegend.svelte +62 -0
- package/dist/guides/SizeLegend.svelte.d.ts +15 -0
- package/dist/guides/index.d.ts +9 -0
- package/dist/guides/index.js +9 -0
- package/dist/guides/ticks.d.ts +6 -0
- package/dist/guides/ticks.js +15 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +14 -0
- package/dist/interactive/ButtonGroup.svelte +98 -0
- package/dist/interactive/ButtonGroup.svelte.d.ts +42 -0
- package/dist/interactive/PointerArea.svelte +49 -0
- package/dist/interactive/PointerArea.svelte.d.ts +18 -0
- package/dist/interactive/Scroller.svelte +147 -0
- package/dist/interactive/Scroller.svelte.d.ts +54 -0
- package/dist/interactive/Tooltip.svelte +116 -0
- package/dist/interactive/Tooltip.svelte.d.ts +34 -0
- package/dist/interactive/index.d.ts +4 -0
- package/dist/interactive/index.js +4 -0
- package/dist/interactive/tooltip-position.d.ts +12 -0
- package/dist/interactive/tooltip-position.js +16 -0
- package/dist/locale/definitions.d.ts +145 -0
- package/dist/locale/definitions.js +138 -0
- package/dist/locale/index.d.ts +23 -0
- package/dist/locale/index.js +34 -0
- package/dist/marks/Arcs.svelte +93 -0
- package/dist/marks/Arcs.svelte.d.ts +47 -0
- package/dist/marks/Area.svelte +44 -0
- package/dist/marks/Area.svelte.d.ts +43 -0
- package/dist/marks/Arrows.svelte +93 -0
- package/dist/marks/Arrows.svelte.d.ts +50 -0
- package/dist/marks/Bars.svelte +111 -0
- package/dist/marks/Bars.svelte.d.ts +61 -0
- package/dist/marks/Beeswarm.svelte +84 -0
- package/dist/marks/Beeswarm.svelte.d.ts +42 -0
- package/dist/marks/Dumbbell.svelte +71 -0
- package/dist/marks/Dumbbell.svelte.d.ts +42 -0
- package/dist/marks/GeoPath.svelte +56 -0
- package/dist/marks/GeoPath.svelte.d.ts +42 -0
- package/dist/marks/Hemicycle.svelte +182 -0
- package/dist/marks/Hemicycle.svelte.d.ts +78 -0
- package/dist/marks/Intervals.svelte +128 -0
- package/dist/marks/Intervals.svelte.d.ts +60 -0
- package/dist/marks/Line.svelte +64 -0
- package/dist/marks/Line.svelte.d.ts +43 -0
- package/dist/marks/LineEndLabels.svelte +70 -0
- package/dist/marks/LineEndLabels.svelte.d.ts +41 -0
- package/dist/marks/Points.svelte +70 -0
- package/dist/marks/Points.svelte.d.ts +46 -0
- package/dist/marks/Rects.svelte +92 -0
- package/dist/marks/Rects.svelte.d.ts +47 -0
- package/dist/marks/Rules.svelte +46 -0
- package/dist/marks/Rules.svelte.d.ts +15 -0
- package/dist/marks/Slope.svelte +112 -0
- package/dist/marks/Slope.svelte.d.ts +41 -0
- package/dist/marks/Text.svelte +75 -0
- package/dist/marks/Text.svelte.d.ts +47 -0
- package/dist/marks/Treemap.svelte +89 -0
- package/dist/marks/Treemap.svelte.d.ts +45 -0
- package/dist/marks/Voronoi.svelte +61 -0
- package/dist/marks/Voronoi.svelte.d.ts +39 -0
- package/dist/marks/Waffle.svelte +77 -0
- package/dist/marks/Waffle.svelte.d.ts +45 -0
- package/dist/marks/bar-path.d.ts +5 -0
- package/dist/marks/bar-path.js +15 -0
- package/dist/marks/index.d.ts +20 -0
- package/dist/marks/index.js +20 -0
- package/dist/recipes/ArrowChart.svelte +247 -0
- package/dist/recipes/ArrowChart.svelte.d.ts +73 -0
- package/dist/recipes/BarChart.svelte +122 -0
- package/dist/recipes/BarChart.svelte.d.ts +58 -0
- package/dist/recipes/BarDotChart.svelte +204 -0
- package/dist/recipes/BarDotChart.svelte.d.ts +75 -0
- package/dist/recipes/ColumnChart.svelte +119 -0
- package/dist/recipes/ColumnChart.svelte.d.ts +56 -0
- package/dist/recipes/LineChart.svelte +159 -0
- package/dist/recipes/LineChart.svelte.d.ts +61 -0
- package/dist/recipes/MosaicChart.svelte +256 -0
- package/dist/recipes/MosaicChart.svelte.d.ts +83 -0
- package/dist/recipes/ScatterChart.svelte +167 -0
- package/dist/recipes/ScatterChart.svelte.d.ts +63 -0
- package/dist/recipes/StackedBarChart.svelte +222 -0
- package/dist/recipes/StackedBarChart.svelte.d.ts +83 -0
- package/dist/recipes/index.d.ts +10 -0
- package/dist/recipes/index.js +19 -0
- package/dist/styles/base.d.ts +5 -0
- package/dist/styles/base.js +102 -0
- package/dist/styles/check.d.ts +8 -0
- package/dist/styles/check.js +32 -0
- package/dist/styles/color.d.ts +11 -0
- package/dist/styles/color.js +29 -0
- package/dist/styles/index.d.ts +9 -0
- package/dist/styles/index.js +17 -0
- package/dist/styles/types.d.ts +90 -0
- package/dist/styles/types.js +1 -0
- package/dist/table/Sparkline.svelte +59 -0
- package/dist/table/Sparkline.svelte.d.ts +16 -0
- package/dist/table/Table.svelte +287 -0
- package/dist/table/Table.svelte.d.ts +44 -0
- package/dist/table/csv.d.ts +3 -0
- package/dist/table/csv.js +12 -0
- package/dist/table/index.d.ts +4 -0
- package/dist/table/index.js +3 -0
- package/dist/table/types.d.ts +26 -0
- package/dist/table/types.js +1 -0
- package/dist/transforms/bin.d.ts +15 -0
- package/dist/transforms/bin.js +18 -0
- package/dist/transforms/dodge.d.ts +10 -0
- package/dist/transforms/dodge.js +35 -0
- package/dist/transforms/group.d.ts +14 -0
- package/dist/transforms/group.js +14 -0
- package/dist/transforms/hemicycle.d.ts +30 -0
- package/dist/transforms/hemicycle.js +51 -0
- package/dist/transforms/index.d.ts +8 -0
- package/dist/transforms/index.js +8 -0
- package/dist/transforms/labels.d.ts +6 -0
- package/dist/transforms/labels.js +20 -0
- package/dist/transforms/smooth.d.ts +61 -0
- package/dist/transforms/smooth.js +105 -0
- package/dist/transforms/stack.d.ts +25 -0
- package/dist/transforms/stack.js +61 -0
- package/dist/transforms/treemap.d.ts +21 -0
- package/dist/transforms/treemap.js +31 -0
- package/package.json +152 -3
- package/src/lib/recipes/ArrowChart.svelte +247 -0
- package/src/lib/recipes/BarChart.svelte +122 -0
- package/src/lib/recipes/BarDotChart.svelte +204 -0
- package/src/lib/recipes/ColumnChart.svelte +119 -0
- package/src/lib/recipes/LineChart.svelte +159 -0
- package/src/lib/recipes/MosaicChart.svelte +256 -0
- package/src/lib/recipes/ScatterChart.svelte +167 -0
- package/src/lib/recipes/StackedBarChart.svelte +222 -0
- package/src/lib/recipes/index.ts +20 -0
package/package.json
CHANGED
|
@@ -1,6 +1,155 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samizdata/mozaic",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Chart components for the web and print, built on Svelte 5 and d3.",
|
|
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
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src/lib/recipes",
|
|
18
|
+
"!dist/**/*.test.*",
|
|
19
|
+
"!dist/**/*.spec.*"
|
|
20
|
+
],
|
|
21
|
+
"sideEffects": [
|
|
22
|
+
"**/*.css"
|
|
23
|
+
],
|
|
24
|
+
"svelte": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"mozaic-dev": "./src/lib/index.ts",
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"svelte": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./core": {
|
|
33
|
+
"mozaic-dev": "./src/lib/core/index.ts",
|
|
34
|
+
"types": "./dist/core/index.d.ts",
|
|
35
|
+
"svelte": "./dist/core/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./marks": {
|
|
38
|
+
"mozaic-dev": "./src/lib/marks/index.ts",
|
|
39
|
+
"types": "./dist/marks/index.d.ts",
|
|
40
|
+
"svelte": "./dist/marks/index.js"
|
|
41
|
+
},
|
|
42
|
+
"./guides": {
|
|
43
|
+
"mozaic-dev": "./src/lib/guides/index.ts",
|
|
44
|
+
"types": "./dist/guides/index.d.ts",
|
|
45
|
+
"svelte": "./dist/guides/index.js"
|
|
46
|
+
},
|
|
47
|
+
"./annotations": {
|
|
48
|
+
"mozaic-dev": "./src/lib/annotations/index.ts",
|
|
49
|
+
"types": "./dist/annotations/index.d.ts",
|
|
50
|
+
"svelte": "./dist/annotations/index.js"
|
|
51
|
+
},
|
|
52
|
+
"./interactive": {
|
|
53
|
+
"mozaic-dev": "./src/lib/interactive/index.ts",
|
|
54
|
+
"types": "./dist/interactive/index.d.ts",
|
|
55
|
+
"svelte": "./dist/interactive/index.js"
|
|
56
|
+
},
|
|
57
|
+
"./frame": {
|
|
58
|
+
"mozaic-dev": "./src/lib/frame/index.ts",
|
|
59
|
+
"types": "./dist/frame/index.d.ts",
|
|
60
|
+
"svelte": "./dist/frame/index.js"
|
|
61
|
+
},
|
|
62
|
+
"./table": {
|
|
63
|
+
"mozaic-dev": "./src/lib/table/index.ts",
|
|
64
|
+
"types": "./dist/table/index.d.ts",
|
|
65
|
+
"svelte": "./dist/table/index.js"
|
|
66
|
+
},
|
|
67
|
+
"./embed": {
|
|
68
|
+
"mozaic-dev": "./src/lib/embed/index.ts",
|
|
69
|
+
"types": "./dist/embed/index.d.ts",
|
|
70
|
+
"svelte": "./dist/embed/index.js"
|
|
71
|
+
},
|
|
72
|
+
"./export": {
|
|
73
|
+
"mozaic-dev": "./src/lib/export/index.ts",
|
|
74
|
+
"types": "./dist/export/index.d.ts",
|
|
75
|
+
"svelte": "./dist/export/index.js"
|
|
76
|
+
},
|
|
77
|
+
"./recipes": {
|
|
78
|
+
"mozaic-dev": "./src/lib/recipes/index.ts",
|
|
79
|
+
"types": "./dist/recipes/index.d.ts",
|
|
80
|
+
"svelte": "./dist/recipes/index.js"
|
|
81
|
+
},
|
|
82
|
+
"./styles": {
|
|
83
|
+
"mozaic-dev": "./src/lib/styles/index.ts",
|
|
84
|
+
"types": "./dist/styles/index.d.ts",
|
|
85
|
+
"default": "./dist/styles/index.js"
|
|
86
|
+
},
|
|
87
|
+
"./locale": {
|
|
88
|
+
"mozaic-dev": "./src/lib/locale/index.ts",
|
|
89
|
+
"types": "./dist/locale/index.d.ts",
|
|
90
|
+
"default": "./dist/locale/index.js"
|
|
91
|
+
},
|
|
92
|
+
"./transforms": {
|
|
93
|
+
"mozaic-dev": "./src/lib/transforms/index.ts",
|
|
94
|
+
"types": "./dist/transforms/index.d.ts",
|
|
95
|
+
"default": "./dist/transforms/index.js"
|
|
96
|
+
},
|
|
97
|
+
"./package.json": "./package.json"
|
|
98
|
+
},
|
|
99
|
+
"peerDependencies": {
|
|
100
|
+
"svelte": "^5.57.0"
|
|
101
|
+
},
|
|
102
|
+
"dependencies": {
|
|
103
|
+
"d3-array": "^3.2.4",
|
|
104
|
+
"d3-delaunay": "^6.0.4",
|
|
105
|
+
"d3-format": "^3.1.0",
|
|
106
|
+
"d3-geo": "^3.1.1",
|
|
107
|
+
"d3-hierarchy": "^3.1.2",
|
|
108
|
+
"d3-scale": "^4.0.2",
|
|
109
|
+
"d3-shape": "^3.2.0",
|
|
110
|
+
"d3-time-format": "^4.1.0"
|
|
111
|
+
},
|
|
112
|
+
"devDependencies": {
|
|
113
|
+
"@sveltejs/adapter-auto": "^8.0.0",
|
|
114
|
+
"@sveltejs/kit": "^3.1.0",
|
|
115
|
+
"@sveltejs/package": "^3.0.1",
|
|
116
|
+
"@sveltejs/vite-plugin-svelte": "^7.3.1",
|
|
117
|
+
"@types/d3-array": "^3.2.1",
|
|
118
|
+
"@types/d3-delaunay": "^6.0.4",
|
|
119
|
+
"@types/d3-format": "^3.0.4",
|
|
120
|
+
"@types/d3-geo": "^3.1.0",
|
|
121
|
+
"@types/d3-hierarchy": "^3.1.7",
|
|
122
|
+
"@types/d3-scale": "^4.0.8",
|
|
123
|
+
"@types/d3-scale-chromatic": "^3.1.0",
|
|
124
|
+
"@types/d3-shape": "^3.1.6",
|
|
125
|
+
"@types/d3-time-format": "^4.0.3",
|
|
126
|
+
"@types/geojson": "^7946.0.14",
|
|
127
|
+
"@vitest/coverage-v8": "^4.1.12",
|
|
128
|
+
"d3-scale-chromatic": "^3.1.0",
|
|
129
|
+
"linkedom": "^0.18.14",
|
|
130
|
+
"publint": "^0.3.21",
|
|
131
|
+
"svelte": "^5.57.2",
|
|
132
|
+
"svelte-check": "^4.6.0",
|
|
133
|
+
"typescript": "^6.0.3",
|
|
134
|
+
"vite": "^8.3.5",
|
|
135
|
+
"vitest": "^4.1.8"
|
|
136
|
+
},
|
|
137
|
+
"keywords": [
|
|
138
|
+
"svelte",
|
|
139
|
+
"charts",
|
|
140
|
+
"dataviz",
|
|
141
|
+
"d3"
|
|
142
|
+
],
|
|
143
|
+
"imports": {
|
|
144
|
+
"#lib": "./src/lib/index.js",
|
|
145
|
+
"#lib/*": "./src/lib/*"
|
|
146
|
+
},
|
|
147
|
+
"scripts": {
|
|
148
|
+
"dev": "vite dev",
|
|
149
|
+
"package": "svelte-kit sync && svelte-package && publint",
|
|
150
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
151
|
+
"test:unit": "vitest",
|
|
152
|
+
"test": "vitest --run",
|
|
153
|
+
"test:coverage": "vitest --run --coverage"
|
|
154
|
+
}
|
|
6
155
|
}
|
|
@@ -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,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>
|