@samizdata/mozaic 0.10.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/cli/src/index.d.ts +10 -0
- package/cli/src/index.js +4 -0
- 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 +40 -12
- package/dist/guides/AxisX.svelte.d.ts +6 -1
- package/dist/guides/ticks.d.ts +5 -0
- package/dist/guides/ticks.js +16 -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/LineEndLabels.svelte +40 -8
- package/dist/marks/LineEndLabels.svelte.d.ts +4 -1
- 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/dist/transforms/index.d.ts +1 -1
- package/dist/transforms/index.js +1 -1
- package/dist/transforms/labels.d.ts +8 -2
- package/dist/transforms/labels.js +35 -7
- package/package.json +11 -1
- 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
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { type SourceItem } from '../frame/index.js';
|
|
2
|
+
import type { ChartEmbed } from '../embed/index.js';
|
|
3
|
+
import { type LocaleCode } from '../locale/index.js';
|
|
4
|
+
import { type Medium, type Theme } from '../styles/index.js';
|
|
5
|
+
declare function $$render<T>(): {
|
|
6
|
+
props: {
|
|
7
|
+
data: T[];
|
|
8
|
+
value: (d: T) => number;
|
|
9
|
+
/** Name shown in the tooltip and in highlighted labels. */
|
|
10
|
+
label: (d: T) => string;
|
|
11
|
+
/** The item's flag from `@samizdata/mozaic/flags`, drawn instead of a dot. */
|
|
12
|
+
flag?: (d: T) => string | null | undefined;
|
|
13
|
+
/** Items to label above the strip. */
|
|
14
|
+
highlight?: (d: T) => boolean;
|
|
15
|
+
/** Text of a highlighted label; defaults to the name and value. */
|
|
16
|
+
labelText?: (d: T) => string;
|
|
17
|
+
/** Highlighted labels to set in bold, e.g. the story's own country; the rest are regular. */
|
|
18
|
+
emphasize?: (d: T) => boolean;
|
|
19
|
+
/** Dot colour, and the colour of the item's label (darkened to 4.5:1 where needed). */
|
|
20
|
+
color?: (d: T) => string | undefined;
|
|
21
|
+
/** Dashed vertical lines with a label under the strip, e.g. an average. */
|
|
22
|
+
references?: {
|
|
23
|
+
value: number;
|
|
24
|
+
label: string;
|
|
25
|
+
}[];
|
|
26
|
+
/** Value axis domain; defaults to zero to the largest value. */
|
|
27
|
+
domain?: [number, number];
|
|
28
|
+
/** Unit after the last axis tick label and the tooltip value, e.g. "c/kWh". */
|
|
29
|
+
unit?: string;
|
|
30
|
+
/** Axis title at the end of the value axis. Leave it out when the subtitle gives the unit. */
|
|
31
|
+
axisLabel?: string;
|
|
32
|
+
/**
|
|
33
|
+
* What the low and high ends of the axis mean, e.g. `["Cheaper", "More expensive"]`,
|
|
34
|
+
* drawn under the axis as "← Cheaper" and "More expensive →". Leave an end empty for a single
|
|
35
|
+
* arrow; the low end is dropped when both do not fit.
|
|
36
|
+
*/
|
|
37
|
+
directions?: [string, string];
|
|
38
|
+
/** Very soft vertical gridlines at the ticks (default true). */
|
|
39
|
+
grid?: boolean;
|
|
40
|
+
/** Dot or flag radius in px; defaults to a size that keeps flags recognisable. */
|
|
41
|
+
r?: number;
|
|
42
|
+
style?: Theme;
|
|
43
|
+
locale?: LocaleCode;
|
|
44
|
+
medium?: Medium;
|
|
45
|
+
width?: number;
|
|
46
|
+
title?: string;
|
|
47
|
+
subtitle?: string;
|
|
48
|
+
notes?: string;
|
|
49
|
+
/** Plain text, or sources with optional links (see `<Source>`). */
|
|
50
|
+
source?: string | SourceItem[];
|
|
51
|
+
alt?: string;
|
|
52
|
+
/** The chart's embed page, for the embed code under the chart (see `<Chart embed>`). */
|
|
53
|
+
embed?: ChartEmbed;
|
|
54
|
+
/** Value format for labels, tooltips and references; defaults to locale numbers. */
|
|
55
|
+
format?: (n: number) => string;
|
|
56
|
+
};
|
|
57
|
+
exports: {};
|
|
58
|
+
bindings: "";
|
|
59
|
+
slots: {};
|
|
60
|
+
events: {};
|
|
61
|
+
};
|
|
62
|
+
declare class __sveltets_Render<T> {
|
|
63
|
+
props(): ReturnType<typeof $$render<T>>['props'];
|
|
64
|
+
events(): ReturnType<typeof $$render<T>>['events'];
|
|
65
|
+
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
66
|
+
bindings(): "";
|
|
67
|
+
exports(): {};
|
|
68
|
+
}
|
|
69
|
+
interface $$IsomorphicComponent {
|
|
70
|
+
new <T>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
|
|
71
|
+
$$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
|
|
72
|
+
} & ReturnType<__sveltets_Render<T>['exports']>;
|
|
73
|
+
<T>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
|
|
74
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Recipe: a strip (one-axis dot plot) for comparing many items on one value,
|
|
78
|
+
* such as a price across countries. Items sit along the x axis, nudged up and
|
|
79
|
+
* down so they do not overlap (a beeswarm), as dots or as circular flags
|
|
80
|
+
* (`flag`, as in ggflags). Highlighted items get a label with a leader line
|
|
81
|
+
* above the strip; reference values (an average) get a dashed line labelled
|
|
82
|
+
* below it. Arrows under the axis (`directions`) say what each end means, and
|
|
83
|
+
* very soft gridlines help read values off the axis. Much shorter than a bar chart of the same items, so use it when the
|
|
84
|
+
* reader needs the overall spread and a few named items, not every value.
|
|
85
|
+
* `mozaic eject StripChart` copies this file into your project.
|
|
86
|
+
*/
|
|
87
|
+
declare const StripChart: $$IsomorphicComponent;
|
|
88
|
+
type StripChart<T> = InstanceType<typeof StripChart<T>>;
|
|
89
|
+
export default StripChart;
|
package/dist/recipes/index.d.ts
CHANGED
|
@@ -7,5 +7,6 @@ export { default as BarDotChart } from './BarDotChart.svelte';
|
|
|
7
7
|
export { default as StackedBarChart } from './StackedBarChart.svelte';
|
|
8
8
|
export { default as MosaicChart } from './MosaicChart.svelte';
|
|
9
9
|
export { default as PercentileChart } from './PercentileChart.svelte';
|
|
10
|
+
export { default as StripChart } from './StripChart.svelte';
|
|
10
11
|
/** Recipe names accepted by `mozaic eject`. */
|
|
11
|
-
export declare const recipes: readonly ["BarChart", "ColumnChart", "LineChart", "ScatterChart", "ArrowChart", "BarDotChart", "StackedBarChart", "MosaicChart", "PercentileChart"];
|
|
12
|
+
export declare const recipes: readonly ["BarChart", "ColumnChart", "LineChart", "ScatterChart", "ArrowChart", "BarDotChart", "StackedBarChart", "MosaicChart", "PercentileChart", "StripChart"];
|
package/dist/recipes/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export { default as BarDotChart } from './BarDotChart.svelte';
|
|
|
7
7
|
export { default as StackedBarChart } from './StackedBarChart.svelte';
|
|
8
8
|
export { default as MosaicChart } from './MosaicChart.svelte';
|
|
9
9
|
export { default as PercentileChart } from './PercentileChart.svelte';
|
|
10
|
+
export { default as StripChart } from './StripChart.svelte';
|
|
10
11
|
/** Recipe names accepted by `mozaic eject`. */
|
|
11
12
|
export const recipes = [
|
|
12
13
|
'BarChart',
|
|
@@ -17,5 +18,6 @@ export const recipes = [
|
|
|
17
18
|
'BarDotChart',
|
|
18
19
|
'StackedBarChart',
|
|
19
20
|
'MosaicChart',
|
|
20
|
-
'PercentileChart'
|
|
21
|
+
'PercentileChart',
|
|
22
|
+
'StripChart'
|
|
21
23
|
];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { avoidOverlap } from './labels.js';
|
|
1
|
+
export { avoidOverlap, wrapLabel } from './labels.js';
|
|
2
2
|
export { stack, type StackedRow, type StackOptions } from './stack.js';
|
|
3
3
|
export { bin, type Bin } from './bin.js';
|
|
4
4
|
export { groupBy, indexTo, rollup } from './group.js';
|
package/dist/transforms/index.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Nudge 1-D label positions apart so neighbours are at least `gap` px apart,
|
|
3
3
|
* keeping them inside [min, max] where possible. Returns positions in the
|
|
4
|
-
* original order. Used for direct line labels.
|
|
4
|
+
* original order. Used for direct line labels. `sizes` gives labels of
|
|
5
|
+
* several lines their room: a label of size 2 needs twice the gap.
|
|
5
6
|
*/
|
|
6
|
-
export declare function avoidOverlap(positions: number[], gap: number, [min, max]?: [number, number]): number[];
|
|
7
|
+
export declare function avoidOverlap(positions: number[], gap: number, [min, max]?: [number, number], sizes?: number[]): number[];
|
|
8
|
+
/**
|
|
9
|
+
* Break `text` at spaces into lines no wider than `maxWidth` (by `measure`), for a label that would
|
|
10
|
+
* otherwise run off the chart. A word wider than `maxWidth` stays whole on its own line.
|
|
11
|
+
*/
|
|
12
|
+
export declare function wrapLabel(text: string, maxWidth: number, measure: (text: string) => number): string[];
|
|
@@ -1,20 +1,48 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Nudge 1-D label positions apart so neighbours are at least `gap` px apart,
|
|
3
3
|
* keeping them inside [min, max] where possible. Returns positions in the
|
|
4
|
-
* original order. Used for direct line labels.
|
|
4
|
+
* original order. Used for direct line labels. `sizes` gives labels of
|
|
5
|
+
* several lines their room: a label of size 2 needs twice the gap.
|
|
5
6
|
*/
|
|
6
|
-
export function avoidOverlap(positions, gap, [min, max] = [-Infinity, Infinity]) {
|
|
7
|
+
export function avoidOverlap(positions, gap, [min, max] = [-Infinity, Infinity], sizes) {
|
|
7
8
|
const order = positions.map((p, i) => ({ p, i })).sort((a, b) => a.p - b.p);
|
|
8
9
|
const placed = order.map((o) => Math.min(max, Math.max(min, o.p)));
|
|
10
|
+
// With `sizes` (each label's height, in multiples of `gap`), neighbours are kept half of each apart.
|
|
11
|
+
const size = (k) => sizes?.[order[k].i] ?? 1;
|
|
12
|
+
const apart = (k) => (gap * (size(k - 1) + size(k))) / 2;
|
|
13
|
+
const edge = (k) => (gap * (size(k) - 1)) / 2;
|
|
9
14
|
// Push down from the top, then back up from the bottom if we ran past `max`.
|
|
10
15
|
for (let k = 1; k < placed.length; k++)
|
|
11
|
-
placed[k] = Math.max(placed[k], placed[k - 1] +
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
placed[k] = Math.max(placed[k], placed[k - 1] + apart(k));
|
|
17
|
+
const last = placed.length - 1;
|
|
18
|
+
if (placed.length && placed[last] > max - edge(last)) {
|
|
19
|
+
placed[last] = max - edge(last);
|
|
20
|
+
for (let k = last - 1; k >= 0; k--)
|
|
21
|
+
placed[k] = Math.min(placed[k], placed[k + 1] - apart(k + 1));
|
|
16
22
|
}
|
|
17
23
|
const result = new Array(positions.length);
|
|
18
24
|
order.forEach((o, k) => (result[o.i] = placed[k]));
|
|
19
25
|
return result;
|
|
20
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Break `text` at spaces into lines no wider than `maxWidth` (by `measure`), for a label that would
|
|
29
|
+
* otherwise run off the chart. A word wider than `maxWidth` stays whole on its own line.
|
|
30
|
+
*/
|
|
31
|
+
export function wrapLabel(text, maxWidth, measure) {
|
|
32
|
+
if (measure(text) <= maxWidth)
|
|
33
|
+
return [text];
|
|
34
|
+
const lines = [];
|
|
35
|
+
let line = '';
|
|
36
|
+
for (const word of text.split(' ')) {
|
|
37
|
+
const next = line ? `${line} ${word}` : word;
|
|
38
|
+
if (line && measure(next) > maxWidth) {
|
|
39
|
+
lines.push(line);
|
|
40
|
+
line = word;
|
|
41
|
+
}
|
|
42
|
+
else
|
|
43
|
+
line = next;
|
|
44
|
+
}
|
|
45
|
+
if (line)
|
|
46
|
+
lines.push(line);
|
|
47
|
+
return lines;
|
|
48
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samizdata/mozaic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
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
5
|
"license": "GPL-3.0-or-later",
|
|
6
6
|
"author": "Nicu Calcea",
|
|
@@ -94,11 +94,20 @@
|
|
|
94
94
|
"types": "./dist/locale/index.d.ts",
|
|
95
95
|
"default": "./dist/locale/index.js"
|
|
96
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
|
+
},
|
|
97
102
|
"./transforms": {
|
|
98
103
|
"mozaic-dev": "./src/lib/transforms/index.ts",
|
|
99
104
|
"types": "./dist/transforms/index.d.ts",
|
|
100
105
|
"default": "./dist/transforms/index.js"
|
|
101
106
|
},
|
|
107
|
+
"./cli": {
|
|
108
|
+
"types": "./cli/src/index.d.ts",
|
|
109
|
+
"default": "./cli/src/index.js"
|
|
110
|
+
},
|
|
102
111
|
"./package.json": "./package.json"
|
|
103
112
|
},
|
|
104
113
|
"peerDependencies": {
|
|
@@ -133,6 +142,7 @@
|
|
|
133
142
|
"@types/node": "^24.0.0",
|
|
134
143
|
"@vitest/coverage-v8": "^4.1.12",
|
|
135
144
|
"d3-scale-chromatic": "^3.1.0",
|
|
145
|
+
"flag-icons": "7.6.0",
|
|
136
146
|
"linkedom": "^0.18.14",
|
|
137
147
|
"publint": "^0.3.21",
|
|
138
148
|
"svelte": "^5.57.2",
|
|
@@ -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>
|
|
@@ -38,6 +38,8 @@ project.
|
|
|
38
38
|
alt?: string;
|
|
39
39
|
/** The chart's embed page, for the embed code under the chart (see `<Chart embed>`). */
|
|
40
40
|
embed?: ChartEmbed;
|
|
41
|
+
/** Unit after the last value-axis tick label and the tooltip values, e.g. "lei/m³". */
|
|
42
|
+
unit?: string;
|
|
41
43
|
format?: (n: number) => string;
|
|
42
44
|
/** Tooltip date format (d3-time-format specifier). */
|
|
43
45
|
dateFormat?: string;
|
|
@@ -64,6 +66,7 @@ project.
|
|
|
64
66
|
source,
|
|
65
67
|
alt,
|
|
66
68
|
embed,
|
|
69
|
+
unit,
|
|
67
70
|
format,
|
|
68
71
|
dateFormat = '%B %Y',
|
|
69
72
|
color,
|
|
@@ -110,7 +113,7 @@ project.
|
|
|
110
113
|
{#snippet children(plot)}
|
|
111
114
|
<Svg>
|
|
112
115
|
<GridY ticks={5} />
|
|
113
|
-
<AxisY ticks={5} format={fmt} inside={plot.breakpoint === 's'} />
|
|
116
|
+
<AxisY ticks={5} format={fmt} inside={plot.breakpoint === 's'} {unit} />
|
|
114
117
|
<AxisX ticks={plot.breakpoint === 's' ? 4 : 7} />
|
|
115
118
|
{#if hover}
|
|
116
119
|
<line x1={plot.x(xValue(hover))} x2={plot.x(xValue(hover))} y2={plot.height} stroke="var(--mz-color-axis)" />
|
|
@@ -142,7 +145,10 @@ project.
|
|
|
142
145
|
{#each series as s, i}
|
|
143
146
|
{@const p = valueAt(s, date)}
|
|
144
147
|
{#if p}
|
|
145
|
-
<div
|
|
148
|
+
<div>
|
|
149
|
+
<span style:color={colour(s, i)}>●</span>
|
|
150
|
+
{name(s)}: <strong>{fmt(yValue(p))}{unit ? ` ${unit}` : ''}</strong>
|
|
151
|
+
</div>
|
|
146
152
|
{/if}
|
|
147
153
|
{/each}
|
|
148
154
|
</Tooltip>
|