@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.
- 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 +72 -0
- package/dist/annotations/RefLine.svelte.d.ts +19 -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 +84 -0
- package/dist/guides/AxisY.svelte.d.ts +21 -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 +66 -0
- package/dist/interactive/Tooltip.svelte.d.ts +19 -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 +69 -0
- package/dist/marks/Waffle.svelte.d.ts +41 -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
|
@@ -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>
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
Recipe: horizontal bars for the latest value of each category, a dot for an
|
|
4
|
+
earlier value, and the latest value with its change written at the right
|
|
5
|
+
("29.2% −3.2"). Good for two survey waves where the latest one matters most.
|
|
6
|
+
Rows missing a value show only the bar or the dot. Use it as is, or run
|
|
7
|
+
`mozaic eject BarDotChart` to copy this file into your project and change
|
|
8
|
+
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 { Bars, Points } from '../marks/index.js';
|
|
18
|
+
import { AxisX, AxisY, GridX, Legend } 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
|
+
/** Latest value, drawn as a bar. */
|
|
28
|
+
value: (d: T) => Maybe;
|
|
29
|
+
/** Earlier value, drawn as a dot. */
|
|
30
|
+
previous: (d: T) => Maybe;
|
|
31
|
+
/** Legend names for the bars and the dots (e.g. the two fieldwork dates). */
|
|
32
|
+
valueLabel?: string;
|
|
33
|
+
previousLabel?: 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
|
+
/** Change label format; defaults to one decimal with a sign. */
|
|
48
|
+
formatChange?: (n: number) => string;
|
|
49
|
+
/** Axis tick format; defaults to `format`. */
|
|
50
|
+
tickFormat?: (n: number) => string;
|
|
51
|
+
/** Bar colour; defaults to a mid step of the sequential palette. Use the context grey for "don't know" rows. */
|
|
52
|
+
color?: (d: T) => string;
|
|
53
|
+
/** Dot colour; defaults to the darkest sequential step. */
|
|
54
|
+
dotColor?: string;
|
|
55
|
+
/** Sort by the latest value, largest first, or keep data order (default). */
|
|
56
|
+
sort?: boolean;
|
|
57
|
+
/** x domain; defaults to zero and the largest value, rounded. */
|
|
58
|
+
domain?: [number, number];
|
|
59
|
+
rowHeight?: number;
|
|
60
|
+
/** Extra SVG drawn on top (annotations); receives the plot context. */
|
|
61
|
+
annotations?: Snippet<[PlotContext]>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let {
|
|
65
|
+
data,
|
|
66
|
+
label,
|
|
67
|
+
value,
|
|
68
|
+
previous,
|
|
69
|
+
valueLabel,
|
|
70
|
+
previousLabel,
|
|
71
|
+
style = base,
|
|
72
|
+
locale,
|
|
73
|
+
medium,
|
|
74
|
+
width,
|
|
75
|
+
title,
|
|
76
|
+
subtitle,
|
|
77
|
+
notes,
|
|
78
|
+
source,
|
|
79
|
+
alt,
|
|
80
|
+
embed,
|
|
81
|
+
format,
|
|
82
|
+
formatChange,
|
|
83
|
+
tickFormat,
|
|
84
|
+
color,
|
|
85
|
+
dotColor,
|
|
86
|
+
sort = false,
|
|
87
|
+
domain,
|
|
88
|
+
rowHeight = 32,
|
|
89
|
+
annotations
|
|
90
|
+
}: Props = $props();
|
|
91
|
+
|
|
92
|
+
const theme = $derived(style);
|
|
93
|
+
const numbers = $derived(createLocale(locale ?? theme.defaultLocale));
|
|
94
|
+
const fmt = $derived(format ?? numbers.number);
|
|
95
|
+
const signed = $derived(
|
|
96
|
+
formatChange ?? ((n: number) => (Math.abs(n) < 0.05 ? numbers.format('.1f')(0) : numbers.format('+.1f')(n)))
|
|
97
|
+
);
|
|
98
|
+
const fontSize = $derived(theme.plot.fontSize);
|
|
99
|
+
const has = (v: Maybe): v is number => v !== null && v !== undefined && Number.isFinite(v);
|
|
100
|
+
const sequential = $derived(theme.palettes.sequential);
|
|
101
|
+
const barColor = $derived((d: T) => color?.(d) ?? sequential[Math.floor(sequential.length / 2)]);
|
|
102
|
+
const dot = $derived(dotColor ?? sequential[sequential.length - 1]);
|
|
103
|
+
|
|
104
|
+
const rows = $derived(sort ? data.toSorted((a, b) => (value(b) ?? -Infinity) - (value(a) ?? -Infinity)) : data);
|
|
105
|
+
const all = $derived(rows.flatMap((d) => [value(d), previous(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)).padding(0.3));
|
|
112
|
+
const text = (d: T) => {
|
|
113
|
+
const v = value(d);
|
|
114
|
+
const p = previous(d);
|
|
115
|
+
return { value: has(v) ? fmt(v) : '–', change: has(v) && has(p) ? signed(v - p) : '' };
|
|
116
|
+
};
|
|
117
|
+
const left = $derived(Math.max(...rows.map((d) => estimateTextWidth(label(d), fontSize)), 0) + 12);
|
|
118
|
+
const right = $derived(
|
|
119
|
+
Math.max(
|
|
120
|
+
...rows.map((d) => {
|
|
121
|
+
const t = text(d);
|
|
122
|
+
return estimateTextWidth(`${t.value} ${t.change}`, fontSize);
|
|
123
|
+
}),
|
|
124
|
+
0
|
|
125
|
+
) + 12
|
|
126
|
+
);
|
|
127
|
+
</script>
|
|
128
|
+
|
|
129
|
+
<Chart {style} {locale} {medium} {width} {alt} {embed}>
|
|
130
|
+
<Header>
|
|
131
|
+
{#if title}<Title>{title}</Title>{/if}
|
|
132
|
+
{#if subtitle}<Subtitle>{subtitle}</Subtitle>{/if}
|
|
133
|
+
{#if valueLabel || previousLabel}
|
|
134
|
+
<div class="mz-bar-dot-legend">
|
|
135
|
+
{#if valueLabel}<Legend
|
|
136
|
+
items={[{ label: valueLabel, color: sequential[Math.floor(sequential.length / 2)] }]}
|
|
137
|
+
/>{/if}
|
|
138
|
+
{#if previousLabel}<Legend shape="circle" items={[{ label: previousLabel, color: dot }]} />{/if}
|
|
139
|
+
</div>
|
|
140
|
+
{/if}
|
|
141
|
+
</Header>
|
|
142
|
+
|
|
143
|
+
<Plot {x} {y} height={rows.length * rowHeight} padding={{ left, right, top: 4, bottom: 24 }}>
|
|
144
|
+
{#snippet children(plot)}
|
|
145
|
+
<Svg>
|
|
146
|
+
<GridX ticks={4} />
|
|
147
|
+
<AxisX ticks={4} format={tickFormat ?? fmt} />
|
|
148
|
+
<AxisY />
|
|
149
|
+
<Bars
|
|
150
|
+
data={rows.filter((d) => has(value(d)))}
|
|
151
|
+
y={label}
|
|
152
|
+
x={(d) => value(d)!}
|
|
153
|
+
fill={barColor}
|
|
154
|
+
title={(d) => `${label(d)}: ${fmt(value(d)!)}`}
|
|
155
|
+
/>
|
|
156
|
+
<Points
|
|
157
|
+
data={rows.filter((d) => has(previous(d)))}
|
|
158
|
+
y={label}
|
|
159
|
+
x={(d) => previous(d)!}
|
|
160
|
+
r={4}
|
|
161
|
+
fill={dot}
|
|
162
|
+
stroke={theme.tokens['--mz-color-bg']}
|
|
163
|
+
strokeWidth={1.5}
|
|
164
|
+
title={(d) => `${label(d)}${previousLabel ? `, ${previousLabel}` : ''}: ${fmt(previous(d)!)}`}
|
|
165
|
+
/>
|
|
166
|
+
{#each rows as d (label(d))}
|
|
167
|
+
{@const t = text(d)}
|
|
168
|
+
<text class="mz-bar-dot-value" x={plot.width + 8} y={scaled(plot.y, label(d))} dy="0.35em">
|
|
169
|
+
<tspan class="strong">{t.value}</tspan>
|
|
170
|
+
{#if t.change}<tspan class="change" dx="6">{t.change}</tspan>{/if}
|
|
171
|
+
</text>
|
|
172
|
+
{/each}
|
|
173
|
+
{@render annotations?.(plot)}
|
|
174
|
+
</Svg>
|
|
175
|
+
{/snippet}
|
|
176
|
+
</Plot>
|
|
177
|
+
|
|
178
|
+
{#if notes || source}
|
|
179
|
+
<Footer>
|
|
180
|
+
{#if notes}<Notes>{notes}</Notes>{/if}
|
|
181
|
+
{#if source}<Source>{source}</Source>{/if}
|
|
182
|
+
</Footer>
|
|
183
|
+
{/if}
|
|
184
|
+
</Chart>
|
|
185
|
+
|
|
186
|
+
<style>
|
|
187
|
+
.mz-bar-dot-legend {
|
|
188
|
+
display: flex;
|
|
189
|
+
flex-wrap: wrap;
|
|
190
|
+
gap: 0 1rem;
|
|
191
|
+
}
|
|
192
|
+
.mz-bar-dot-value {
|
|
193
|
+
font-family: var(--mz-font-body);
|
|
194
|
+
font-size: var(--mz-size-axis);
|
|
195
|
+
fill: var(--mz-color-text);
|
|
196
|
+
font-variant-numeric: tabular-nums;
|
|
197
|
+
}
|
|
198
|
+
.strong {
|
|
199
|
+
font-weight: var(--mz-weight-strong);
|
|
200
|
+
}
|
|
201
|
+
.change {
|
|
202
|
+
fill: var(--mz-color-muted);
|
|
203
|
+
}
|
|
204
|
+
</style>
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
Recipe: a vertical column chart (one series), e.g. a value per year, with
|
|
4
|
+
gridlines, hover tooltip and the full frame. `mozaic eject ColumnChart`
|
|
5
|
+
copies this file into your project.
|
|
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, scaled, type PlotContext } from '../core/index.js';
|
|
14
|
+
import { Bars, Rules } from '../marks/index.js';
|
|
15
|
+
import { AxisX, AxisY, GridY } 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
|
+
/** Category on the x axis (e.g. the year). */
|
|
23
|
+
label: (d: T) => string;
|
|
24
|
+
value: (d: T) => number;
|
|
25
|
+
style?: Theme;
|
|
26
|
+
locale?: LocaleCode;
|
|
27
|
+
medium?: Medium;
|
|
28
|
+
width?: number;
|
|
29
|
+
title?: string;
|
|
30
|
+
subtitle?: string;
|
|
31
|
+
notes?: string;
|
|
32
|
+
source?: string;
|
|
33
|
+
alt?: string;
|
|
34
|
+
/** The chart's embed page, for the embed code under the chart (see `<Chart embed>`). */
|
|
35
|
+
embed?: ChartEmbed;
|
|
36
|
+
format?: (n: number) => string;
|
|
37
|
+
color?: (d: T) => string;
|
|
38
|
+
height?: number;
|
|
39
|
+
/** Show every nth x label (useful for long series). */
|
|
40
|
+
labelEvery?: number;
|
|
41
|
+
annotations?: Snippet<[PlotContext]>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let {
|
|
45
|
+
data,
|
|
46
|
+
label,
|
|
47
|
+
value,
|
|
48
|
+
style = base,
|
|
49
|
+
locale,
|
|
50
|
+
medium,
|
|
51
|
+
width,
|
|
52
|
+
title,
|
|
53
|
+
subtitle,
|
|
54
|
+
notes,
|
|
55
|
+
source,
|
|
56
|
+
alt,
|
|
57
|
+
embed,
|
|
58
|
+
format,
|
|
59
|
+
color,
|
|
60
|
+
height = 280,
|
|
61
|
+
labelEvery,
|
|
62
|
+
annotations
|
|
63
|
+
}: Props = $props();
|
|
64
|
+
|
|
65
|
+
const theme = $derived(style);
|
|
66
|
+
const fmt = $derived(format ?? createLocale(locale ?? theme.defaultLocale).number);
|
|
67
|
+
const x = $derived(scaleBand<string>().domain(data.map(label)).paddingInner(0.2).paddingOuter(0.1));
|
|
68
|
+
const y = $derived(
|
|
69
|
+
scaleLinear()
|
|
70
|
+
.domain([Math.min(0, min(data, value) ?? 0), Math.max(0, max(data, value) ?? 1)])
|
|
71
|
+
.nice()
|
|
72
|
+
);
|
|
73
|
+
let hovered = $state<T | null>(null);
|
|
74
|
+
</script>
|
|
75
|
+
|
|
76
|
+
<Chart {style} {locale} {medium} {width} {alt} {embed}>
|
|
77
|
+
{#if title || subtitle}
|
|
78
|
+
<Header>
|
|
79
|
+
{#if title}<Title>{title}</Title>{/if}
|
|
80
|
+
{#if subtitle}<Subtitle>{subtitle}</Subtitle>{/if}
|
|
81
|
+
</Header>
|
|
82
|
+
{/if}
|
|
83
|
+
|
|
84
|
+
<Plot {x} {y} {height} padding={{ left: 40, right: 8, top: 8, bottom: 28 }}>
|
|
85
|
+
{#snippet children(plot)}
|
|
86
|
+
{@const every = labelEvery ?? Math.max(1, Math.ceil((data.length * 36) / Math.max(1, plot.width)))}
|
|
87
|
+
<Svg>
|
|
88
|
+
<GridY ticks={5} />
|
|
89
|
+
<AxisY ticks={5} format={fmt} />
|
|
90
|
+
<AxisX tickValues={data.map(label).filter((_, i) => i % every === 0)} />
|
|
91
|
+
<Bars
|
|
92
|
+
{data}
|
|
93
|
+
x={label}
|
|
94
|
+
y={value}
|
|
95
|
+
fill={color}
|
|
96
|
+
opacity={(d) => (hovered && hovered !== d ? 0.5 : 1)}
|
|
97
|
+
onpointerenter={(d) => (hovered = d)}
|
|
98
|
+
onpointerleave={() => (hovered = null)}
|
|
99
|
+
/>
|
|
100
|
+
<Rules y={[0]} />
|
|
101
|
+
{@render annotations?.(plot)}
|
|
102
|
+
</Svg>
|
|
103
|
+
<Html>
|
|
104
|
+
{#if hovered}
|
|
105
|
+
<Tooltip x={scaled(plot.x, label(hovered))} y={plot.y(value(hovered)) ?? 0}>
|
|
106
|
+
<strong>{label(hovered)}</strong><br />{fmt(value(hovered))}
|
|
107
|
+
</Tooltip>
|
|
108
|
+
{/if}
|
|
109
|
+
</Html>
|
|
110
|
+
{/snippet}
|
|
111
|
+
</Plot>
|
|
112
|
+
|
|
113
|
+
{#if notes || source}
|
|
114
|
+
<Footer>
|
|
115
|
+
{#if notes}<Notes>{notes}</Notes>{/if}
|
|
116
|
+
{#if source}<Source>{source}</Source>{/if}
|
|
117
|
+
</Footer>
|
|
118
|
+
{/if}
|
|
119
|
+
</Chart>
|