@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
package/dist/guides/AxisX.svelte
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
<!--
|
|
2
2
|
@component
|
|
3
3
|
Horizontal axis for the plot's x scale. Tick labels use the chart's locale
|
|
4
|
-
unless you pass `format`.
|
|
4
|
+
unless you pass `format`. On a continuous scale, labels that would overlap are
|
|
5
|
+
thinned to every other tick (or every third, and so on); the tick marks stay.
|
|
6
|
+
`unit` names the unit once, after the last labelled tick ("20 c/kWh"), kept
|
|
7
|
+
inside the chart.
|
|
5
8
|
-->
|
|
6
9
|
<script lang="ts">
|
|
7
10
|
import { useChart, usePlot } from '../core/context.js';
|
|
8
11
|
import { estimateTextWidth, scaled } from '../core/scales.js';
|
|
9
|
-
import {
|
|
12
|
+
import { useTextWidth } from '../core/text.svelte.js';
|
|
13
|
+
import { defaultTickFormat, tickStride, tickValues } from './ticks.js';
|
|
10
14
|
|
|
11
15
|
interface Props {
|
|
12
16
|
/** Approximate number of ticks for continuous scales. */
|
|
@@ -21,6 +25,8 @@ unless you pass `format`.
|
|
|
21
25
|
baseline?: boolean;
|
|
22
26
|
/** Axis title, drawn at the end of the axis. */
|
|
23
27
|
label?: string;
|
|
28
|
+
/** Unit written after the last labelled tick, e.g. "lei/m³" or "euro a year". */
|
|
29
|
+
unit?: string;
|
|
24
30
|
class?: string;
|
|
25
31
|
}
|
|
26
32
|
|
|
@@ -36,6 +42,7 @@ unless you pass `format`.
|
|
|
36
42
|
tickPadding: tickPaddingInput,
|
|
37
43
|
baseline: baselineInput,
|
|
38
44
|
label,
|
|
45
|
+
unit,
|
|
39
46
|
class: className = ''
|
|
40
47
|
}: Props = $props();
|
|
41
48
|
|
|
@@ -46,6 +53,27 @@ unless you pass `format`.
|
|
|
46
53
|
const values = $derived(tickValues(plot.x, ticks ?? chart.theme.plot.ticks, explicitValues));
|
|
47
54
|
const fmt = $derived(format ?? defaultTickFormat(values, chart.locale));
|
|
48
55
|
const dir = $derived(position === 'bottom' ? 1 : -1);
|
|
56
|
+
const measure = useTextWidth();
|
|
57
|
+
const raw = $derived(values.map((value, i) => ({ value, px: scaled(plot.x, value), label: fmt(value, i) })));
|
|
58
|
+
// The unit makes the last label longer, so the thinning takes it into account.
|
|
59
|
+
const withUnit = (list: typeof raw) =>
|
|
60
|
+
unit && list.length
|
|
61
|
+
? list.map((t, i) => (i === list.length - 1 ? { ...t, label: `${t.label} ${unit}` } : t))
|
|
62
|
+
: list;
|
|
63
|
+
// Band and point scales name every category, so only continuous scales are thinned.
|
|
64
|
+
const stride = $derived(
|
|
65
|
+
plot.x.bandwidth
|
|
66
|
+
? 1
|
|
67
|
+
: tickStride(
|
|
68
|
+
raw.map((t) => t.px),
|
|
69
|
+
withUnit(raw).map((t) => measure(t.label, chart.theme.plot.fontSize, chart.theme.tokens['--mz-font-body']))
|
|
70
|
+
)
|
|
71
|
+
);
|
|
72
|
+
/** Index of the last tick that gets a label: the one that carries the unit. */
|
|
73
|
+
const lastShown = $derived(raw.length ? raw.length - 1 - ((raw.length - 1) % stride) : -1);
|
|
74
|
+
const items = $derived(
|
|
75
|
+
raw.map((t, i) => (unit && i === lastShown && !plot.x.bandwidth ? { ...t, label: `${t.label} ${unit}` } : t))
|
|
76
|
+
);
|
|
49
77
|
|
|
50
78
|
// Labels that would spill past the chart edge are anchored inwards instead of centred.
|
|
51
79
|
function anchor(px: number, label: string): 'start' | 'middle' | 'end' {
|
|
@@ -58,18 +86,18 @@ unless you pass `format`.
|
|
|
58
86
|
|
|
59
87
|
<g class="mz-axis mz-axis-x {className}" transform="translate(0,{position === 'bottom' ? plot.height : 0})">
|
|
60
88
|
{#if baseline}<line class="mz-axis-line" x2={plot.width} />{/if}
|
|
61
|
-
{#each
|
|
62
|
-
{@const px = scaled(plot.x, value)}
|
|
63
|
-
{@const label = fmt(value, i)}
|
|
89
|
+
{#each items as { value, px, label }, i (value)}
|
|
64
90
|
<g class="mz-tick" transform="translate({px},0)">
|
|
65
91
|
{#if tickSize > 0}<line y2={dir * tickSize} />{/if}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
92
|
+
{#if i % stride === 0}
|
|
93
|
+
<text
|
|
94
|
+
y={dir * (tickSize + tickPadding)}
|
|
95
|
+
dy={position === 'bottom' ? '0.71em' : '0'}
|
|
96
|
+
text-anchor={anchor(px, label)}
|
|
97
|
+
>
|
|
98
|
+
{label}
|
|
99
|
+
</text>
|
|
100
|
+
{/if}
|
|
73
101
|
</g>
|
|
74
102
|
{/each}
|
|
75
103
|
{#if label}
|
|
@@ -11,11 +11,16 @@ interface Props {
|
|
|
11
11
|
baseline?: boolean;
|
|
12
12
|
/** Axis title, drawn at the end of the axis. */
|
|
13
13
|
label?: string;
|
|
14
|
+
/** Unit written after the last labelled tick, e.g. "lei/m³" or "euro a year". */
|
|
15
|
+
unit?: string;
|
|
14
16
|
class?: string;
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
19
|
* Horizontal axis for the plot's x scale. Tick labels use the chart's locale
|
|
18
|
-
* unless you pass `format`.
|
|
20
|
+
* unless you pass `format`. On a continuous scale, labels that would overlap are
|
|
21
|
+
* thinned to every other tick (or every third, and so on); the tick marks stay.
|
|
22
|
+
* `unit` names the unit once, after the last labelled tick ("20 c/kWh"), kept
|
|
23
|
+
* inside the chart.
|
|
19
24
|
*/
|
|
20
25
|
declare const AxisX: import("svelte").Component<Props, {}, "">;
|
|
21
26
|
type AxisX = ReturnType<typeof AxisX>;
|
package/dist/guides/ticks.d.ts
CHANGED
|
@@ -4,3 +4,8 @@ import type { PositionScale } from '../core/types.js';
|
|
|
4
4
|
export declare function tickValues(scale: PositionScale, count: number, explicit?: unknown[]): unknown[];
|
|
5
5
|
/** Locale-aware default label: dates by multiTimeFormat, numbers with grouping, anything else as text. */
|
|
6
6
|
export declare function defaultTickFormat(values: unknown[], locale: Locale): (value: any) => string;
|
|
7
|
+
/**
|
|
8
|
+
* Every how many ticks to label so that no two labels overlap: 1 when all fit, 2 for every other one,
|
|
9
|
+
* and so on. `positions` are the ticks' centres in px (in order), `widths` their labels' widths.
|
|
10
|
+
*/
|
|
11
|
+
export declare function tickStride(positions: number[], widths: number[], gap?: number): number;
|
package/dist/guides/ticks.js
CHANGED
|
@@ -13,3 +13,19 @@ export function defaultTickFormat(values, locale) {
|
|
|
13
13
|
return locale.number;
|
|
14
14
|
return (value) => String(value);
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Every how many ticks to label so that no two labels overlap: 1 when all fit, 2 for every other one,
|
|
18
|
+
* and so on. `positions` are the ticks' centres in px (in order), `widths` their labels' widths.
|
|
19
|
+
*/
|
|
20
|
+
export function tickStride(positions, widths, gap = 8) {
|
|
21
|
+
for (let stride = 1; stride < positions.length; stride++) {
|
|
22
|
+
let fits = true;
|
|
23
|
+
for (let i = stride; i < positions.length && fits; i += stride) {
|
|
24
|
+
const room = Math.abs(positions[i] - positions[i - stride]);
|
|
25
|
+
fits = room >= (widths[i] + widths[i - stride]) / 2 + gap;
|
|
26
|
+
}
|
|
27
|
+
if (fits)
|
|
28
|
+
return stride;
|
|
29
|
+
}
|
|
30
|
+
return Math.max(1, positions.length);
|
|
31
|
+
}
|
|
@@ -39,6 +39,7 @@ values aligned in a column. `children` follow the rows.
|
|
|
39
39
|
let { title, items = [], x, y, visible = true, offset = 12, class: className = '', children }: Props = $props();
|
|
40
40
|
|
|
41
41
|
const plot = usePlot();
|
|
42
|
+
const swatches = $derived(items.some((item) => item.color));
|
|
42
43
|
const flip = $derived(x > plot.width * 0.6);
|
|
43
44
|
const top = $derived(Math.max(0, Math.min(plot.height, y)));
|
|
44
45
|
// Once measured, the tooltip is placed so it stays inside the chart (see tooltipLeft).
|
|
@@ -61,10 +62,13 @@ values aligned in a column. `children` follow the rows.
|
|
|
61
62
|
>
|
|
62
63
|
{#if title}<strong class="mz-tooltip-title">{title}</strong>{/if}
|
|
63
64
|
{#if items.length}
|
|
64
|
-
|
|
65
|
+
<!-- The swatch column only when some row has a colour; otherwise it would indent every row. -->
|
|
66
|
+
<div class="mz-tooltip-items" class:plain={!swatches}>
|
|
65
67
|
{#each items as item, i (i)}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
+
{#if swatches}
|
|
69
|
+
<span class="mz-tooltip-swatch {item.shape ?? 'square'}" style:background={item.color ?? 'transparent'}
|
|
70
|
+
></span>
|
|
71
|
+
{/if}
|
|
68
72
|
<span class="mz-tooltip-label">{item.label}</span>
|
|
69
73
|
<strong class="mz-tooltip-value">{item.value}</strong>
|
|
70
74
|
{/each}
|
|
@@ -104,6 +108,9 @@ values aligned in a column. `children` follow the rows.
|
|
|
104
108
|
column-gap: 0.4rem;
|
|
105
109
|
row-gap: 0.15rem;
|
|
106
110
|
}
|
|
111
|
+
.mz-tooltip-items.plain {
|
|
112
|
+
grid-template-columns: 1fr auto;
|
|
113
|
+
}
|
|
107
114
|
.mz-tooltip-swatch {
|
|
108
115
|
width: 0.7rem;
|
|
109
116
|
height: 0.7rem;
|
package/dist/locale/index.d.ts
CHANGED
|
@@ -13,6 +13,14 @@ export interface Locale {
|
|
|
13
13
|
timeFormat(specifier: string): (d: Date) => string;
|
|
14
14
|
/** 1234.5 → "1,234.5" (en) / "1.234,5" (ro) / "1 234,5" (ru). Trailing zeros removed. */
|
|
15
15
|
number: (n: number) => string;
|
|
16
|
+
/**
|
|
17
|
+
* Exactly `digits` decimals, except that a value which rounds to a whole number at that
|
|
18
|
+
* precision has none: `decimals(1)` gives 8.46 → "8.5" and 11.03 → "11" (not "11.0").
|
|
19
|
+
* Use it for one rounding level across a chart without "26.0" or "26.00".
|
|
20
|
+
*/
|
|
21
|
+
decimals(digits: number): (n: number) => string;
|
|
22
|
+
/** Money: two decimals, or none when the value is a whole number (26.14 → "26.14", 26 → "26", 12.3 → "12.30"). */
|
|
23
|
+
money: (n: number) => string;
|
|
16
24
|
/** 0.123 → "12%". */
|
|
17
25
|
percent: (n: number) => string;
|
|
18
26
|
/** 0.123 → "+12%"; −0.05 → "−5%". */
|
package/dist/locale/index.js
CHANGED
|
@@ -12,6 +12,14 @@ export function createLocale(code) {
|
|
|
12
12
|
throw new Error(`Unknown mozaic locale "${code}". Known locales: ${Object.keys(definitions).join(', ')}`);
|
|
13
13
|
const numbers = formatLocale(def.number);
|
|
14
14
|
const times = timeFormatLocale(def.time);
|
|
15
|
+
const decimals = (digits) => {
|
|
16
|
+
const withDecimals = numbers.format(`,.${digits}f`);
|
|
17
|
+
const whole = numbers.format(',.0f');
|
|
18
|
+
return (n) => {
|
|
19
|
+
const rounded = Number(n.toFixed(digits));
|
|
20
|
+
return Number.isInteger(rounded) ? whole(rounded) : withDecimals(n);
|
|
21
|
+
};
|
|
22
|
+
};
|
|
15
23
|
const locale = {
|
|
16
24
|
code,
|
|
17
25
|
lang: def.lang,
|
|
@@ -19,6 +27,8 @@ export function createLocale(code) {
|
|
|
19
27
|
format: (specifier) => numbers.format(specifier),
|
|
20
28
|
timeFormat: (specifier) => times.format(specifier),
|
|
21
29
|
number: numbers.format(',~f'),
|
|
30
|
+
decimals,
|
|
31
|
+
money: decimals(2),
|
|
22
32
|
percent: numbers.format('.0%'),
|
|
23
33
|
signedPercent: numbers.format('+.0%')
|
|
24
34
|
};
|
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
@component
|
|
3
3
|
A beeswarm: one dot per datum along the x scale, nudged vertically so dots
|
|
4
4
|
do not overlap. With a band y scale, each category gets its own swarm.
|
|
5
|
+
Pass `flag` to draw countries as circular flags instead of dots.
|
|
5
6
|
-->
|
|
6
7
|
<script lang="ts" generics="T">
|
|
7
8
|
import { useChart, usePlot } from '../core/context.js';
|
|
8
9
|
import { scaled } from '../core/scales.js';
|
|
9
10
|
import { access, type Accessor } from '../core/accessor.js';
|
|
10
11
|
import { dodge } from '../transforms/dodge.js';
|
|
12
|
+
import Flag from './Flag.svelte';
|
|
11
13
|
|
|
12
14
|
interface Props {
|
|
13
15
|
data: T[];
|
|
@@ -18,6 +20,8 @@ do not overlap. With a band y scale, each category gets its own swarm.
|
|
|
18
20
|
padding?: number;
|
|
19
21
|
fill?: Accessor<T, string>;
|
|
20
22
|
opacity?: Accessor<T, number>;
|
|
23
|
+
/** The datum's flag from `@samizdata/mozaic/flags`, drawn as a circle of radius `r` instead of a dot. */
|
|
24
|
+
flag?: (d: T) => string | null | undefined;
|
|
21
25
|
title?: (d: T) => string;
|
|
22
26
|
onpointerenter?: (d: T, event: PointerEvent) => void;
|
|
23
27
|
onpointerleave?: (d: T, event: PointerEvent) => void;
|
|
@@ -31,6 +35,7 @@ do not overlap. With a band y scale, each category gets its own swarm.
|
|
|
31
35
|
r = 4,
|
|
32
36
|
padding = 1,
|
|
33
37
|
fill,
|
|
38
|
+
flag,
|
|
34
39
|
opacity = 1,
|
|
35
40
|
title,
|
|
36
41
|
onpointerenter,
|
|
@@ -62,22 +67,36 @@ do not overlap. With a band y scale, each category gets its own swarm.
|
|
|
62
67
|
|
|
63
68
|
<g class="mz-beeswarm {className}">
|
|
64
69
|
{#each dots as dot, i}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
70
|
+
{@const svg = flag?.(dot.d)}
|
|
71
|
+
{#if svg}
|
|
72
|
+
<Flag
|
|
73
|
+
{svg}
|
|
74
|
+
cx={dot.cx}
|
|
75
|
+
cy={dot.cy}
|
|
76
|
+
{r}
|
|
77
|
+
opacity={access(opacity, dot.d, i)}
|
|
78
|
+
title={title?.(dot.d)}
|
|
79
|
+
onpointerenter={onpointerenter ? (e) => onpointerenter(dot.d, e) : undefined}
|
|
80
|
+
onpointerleave={onpointerleave ? (e) => onpointerleave(dot.d, e) : undefined}
|
|
81
|
+
/>
|
|
82
|
+
{:else}
|
|
83
|
+
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
84
|
+
<circle
|
|
85
|
+
cx={dot.cx}
|
|
86
|
+
cy={dot.cy}
|
|
87
|
+
{r}
|
|
88
|
+
fill={fill ? access(fill, dot.d, i) : chart.theme.palettes.categorical[0]}
|
|
89
|
+
fill-opacity={access(opacity, dot.d, i)}
|
|
90
|
+
onpointerenter={onpointerenter ? (e) => onpointerenter(dot.d, e) : undefined}
|
|
91
|
+
onpointerleave={onpointerleave ? (e) => onpointerleave(dot.d, e) : undefined}
|
|
92
|
+
>{#if title}<title>{title(dot.d)}</title>{/if}</circle
|
|
93
|
+
>
|
|
94
|
+
{/if}
|
|
76
95
|
{/each}
|
|
77
96
|
</g>
|
|
78
97
|
|
|
79
98
|
<style>
|
|
80
|
-
circle {
|
|
99
|
+
.mz-beeswarm > circle {
|
|
81
100
|
stroke: var(--mz-color-bg);
|
|
82
101
|
stroke-width: 1;
|
|
83
102
|
}
|
|
@@ -9,6 +9,8 @@ declare function $$render<T>(): {
|
|
|
9
9
|
padding?: number;
|
|
10
10
|
fill?: Accessor<T, string>;
|
|
11
11
|
opacity?: Accessor<T, number>;
|
|
12
|
+
/** The datum's flag from `@samizdata/mozaic/flags`, drawn as a circle of radius `r` instead of a dot. */
|
|
13
|
+
flag?: (d: T) => string | null | undefined;
|
|
12
14
|
title?: (d: T) => string;
|
|
13
15
|
onpointerenter?: (d: T, event: PointerEvent) => void;
|
|
14
16
|
onpointerleave?: (d: T, event: PointerEvent) => void;
|
|
@@ -36,6 +38,7 @@ interface $$IsomorphicComponent {
|
|
|
36
38
|
/**
|
|
37
39
|
* A beeswarm: one dot per datum along the x scale, nudged vertically so dots
|
|
38
40
|
* do not overlap. With a band y scale, each category gets its own swarm.
|
|
41
|
+
* Pass `flag` to draw countries as circular flags instead of dots.
|
|
39
42
|
*/
|
|
40
43
|
declare const Beeswarm: $$IsomorphicComponent;
|
|
41
44
|
type Beeswarm<T> = InstanceType<typeof Beeswarm<T>>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
One flag drawn as a circle at a pixel position, as in ggflags. Pass a flag
|
|
4
|
+
from `@samizdata/mozaic/flags`. A hairline ring keeps white and pale flags
|
|
5
|
+
visible on the background. Used by `Flags`, `Beeswarm`, `BarChart` and
|
|
6
|
+
`StripChart`; use it directly to put a flag beside a label or annotation.
|
|
7
|
+
-->
|
|
8
|
+
<script lang="ts">
|
|
9
|
+
interface Props {
|
|
10
|
+
/** A flag from `@samizdata/mozaic/flags` (inner SVG markup on a 512 × 512 viewBox). */
|
|
11
|
+
svg: string;
|
|
12
|
+
/** Centre in px. */
|
|
13
|
+
cx: number;
|
|
14
|
+
cy: number;
|
|
15
|
+
/** Radius in px. */
|
|
16
|
+
r?: number;
|
|
17
|
+
/** Ring colour; defaults to the border token. Use `none` for no ring. */
|
|
18
|
+
stroke?: string;
|
|
19
|
+
strokeWidth?: number;
|
|
20
|
+
/** Accessible name (adds a <title>), usually the country's name. */
|
|
21
|
+
title?: string;
|
|
22
|
+
opacity?: number;
|
|
23
|
+
onpointerenter?: (event: PointerEvent) => void;
|
|
24
|
+
onpointerleave?: (event: PointerEvent) => void;
|
|
25
|
+
class?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let {
|
|
29
|
+
svg,
|
|
30
|
+
cx,
|
|
31
|
+
cy,
|
|
32
|
+
r = 8,
|
|
33
|
+
stroke = 'var(--mz-color-border)',
|
|
34
|
+
strokeWidth = 1,
|
|
35
|
+
title,
|
|
36
|
+
opacity = 1,
|
|
37
|
+
onpointerenter,
|
|
38
|
+
onpointerleave,
|
|
39
|
+
class: className = ''
|
|
40
|
+
}: Props = $props();
|
|
41
|
+
|
|
42
|
+
const uid = $props.id();
|
|
43
|
+
</script>
|
|
44
|
+
|
|
45
|
+
<g
|
|
46
|
+
class="mz-flag {className}"
|
|
47
|
+
transform="translate({cx - r} {cy - r})"
|
|
48
|
+
{opacity}
|
|
49
|
+
role={title ? 'img' : undefined}
|
|
50
|
+
aria-label={title}
|
|
51
|
+
{onpointerenter}
|
|
52
|
+
{onpointerleave}
|
|
53
|
+
>
|
|
54
|
+
{#if title}<title>{title}</title>{/if}
|
|
55
|
+
<clipPath id="{uid}-clip"><circle cx={r} cy={r} {r} /></clipPath>
|
|
56
|
+
<g clip-path="url(#{uid}-clip)">
|
|
57
|
+
<svg width={r * 2} height={r * 2} viewBox="0 0 512 512" preserveAspectRatio="xMidYMid slice" aria-hidden="true">
|
|
58
|
+
<!-- eslint-disable-next-line svelte/no-at-html-tags -- trusted markup generated from flag-icons -->
|
|
59
|
+
{@html svg}
|
|
60
|
+
</svg>
|
|
61
|
+
</g>
|
|
62
|
+
{#if stroke !== 'none'}
|
|
63
|
+
<circle cx={r} cy={r} r={r - strokeWidth / 2} fill="none" {stroke} stroke-width={strokeWidth} />
|
|
64
|
+
{/if}
|
|
65
|
+
</g>
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
interface Props {
|
|
2
|
+
/** A flag from `@samizdata/mozaic/flags` (inner SVG markup on a 512 × 512 viewBox). */
|
|
3
|
+
svg: string;
|
|
4
|
+
/** Centre in px. */
|
|
5
|
+
cx: number;
|
|
6
|
+
cy: number;
|
|
7
|
+
/** Radius in px. */
|
|
8
|
+
r?: number;
|
|
9
|
+
/** Ring colour; defaults to the border token. Use `none` for no ring. */
|
|
10
|
+
stroke?: string;
|
|
11
|
+
strokeWidth?: number;
|
|
12
|
+
/** Accessible name (adds a <title>), usually the country's name. */
|
|
13
|
+
title?: string;
|
|
14
|
+
opacity?: number;
|
|
15
|
+
onpointerenter?: (event: PointerEvent) => void;
|
|
16
|
+
onpointerleave?: (event: PointerEvent) => void;
|
|
17
|
+
class?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* One flag drawn as a circle at a pixel position, as in ggflags. Pass a flag
|
|
21
|
+
* from `@samizdata/mozaic/flags`. A hairline ring keeps white and pale flags
|
|
22
|
+
* visible on the background. Used by `Flags`, `Beeswarm`, `BarChart` and
|
|
23
|
+
* `StripChart`; use it directly to put a flag beside a label or annotation.
|
|
24
|
+
*/
|
|
25
|
+
declare const Flag: import("svelte").Component<Props, {}, "">;
|
|
26
|
+
type Flag = ReturnType<typeof Flag>;
|
|
27
|
+
export default Flag;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
Flags as points, as in ggflags: one circular flag per datum at its x/y
|
|
4
|
+
position (scatter plots, dot plots, maps with a projection). Pass the flag for
|
|
5
|
+
each datum from `@samizdata/mozaic/flags`; rows without one are skipped. For a
|
|
6
|
+
one-axis strip of flags that do not overlap, use `Beeswarm` with `flag`.
|
|
7
|
+
-->
|
|
8
|
+
<script lang="ts" generics="T">
|
|
9
|
+
import { usePlot } from '../core/context.js';
|
|
10
|
+
import { position } from '../core/scales.js';
|
|
11
|
+
import type { Accessor, Value } from '../core/accessor.js';
|
|
12
|
+
import { access } from '../core/accessor.js';
|
|
13
|
+
import Flag from './Flag.svelte';
|
|
14
|
+
|
|
15
|
+
interface Props {
|
|
16
|
+
data: T[];
|
|
17
|
+
x: Accessor<T, Value>;
|
|
18
|
+
y: Accessor<T, Value>;
|
|
19
|
+
/** The datum's flag from `@samizdata/mozaic/flags`, or nothing to skip it. */
|
|
20
|
+
flag: (d: T) => string | null | undefined;
|
|
21
|
+
/** Radius in px. */
|
|
22
|
+
r?: Accessor<T, number>;
|
|
23
|
+
opacity?: Accessor<T, number>;
|
|
24
|
+
/** Accessible name per flag, usually the country's name. */
|
|
25
|
+
title?: (d: T) => string;
|
|
26
|
+
onpointerenter?: (d: T, event: PointerEvent) => void;
|
|
27
|
+
onpointerleave?: (d: T, event: PointerEvent) => void;
|
|
28
|
+
class?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let {
|
|
32
|
+
data,
|
|
33
|
+
x,
|
|
34
|
+
y,
|
|
35
|
+
flag,
|
|
36
|
+
r = 8,
|
|
37
|
+
opacity = 1,
|
|
38
|
+
title,
|
|
39
|
+
onpointerenter,
|
|
40
|
+
onpointerleave,
|
|
41
|
+
class: className = ''
|
|
42
|
+
}: Props = $props();
|
|
43
|
+
|
|
44
|
+
const plot = usePlot();
|
|
45
|
+
|
|
46
|
+
const items = $derived(
|
|
47
|
+
data.flatMap((d, i) => {
|
|
48
|
+
const svg = flag(d);
|
|
49
|
+
if (!svg) return [];
|
|
50
|
+
const [cx, cy] = position(plot, access(x, d, i), access(y, d, i));
|
|
51
|
+
return Number.isFinite(cx) && Number.isFinite(cy) ? [{ d, i, svg, cx, cy }] : [];
|
|
52
|
+
})
|
|
53
|
+
);
|
|
54
|
+
</script>
|
|
55
|
+
|
|
56
|
+
<g class="mz-flags {className}">
|
|
57
|
+
{#each items as item (item.i)}
|
|
58
|
+
<Flag
|
|
59
|
+
svg={item.svg}
|
|
60
|
+
cx={item.cx}
|
|
61
|
+
cy={item.cy}
|
|
62
|
+
r={access(r, item.d, item.i)}
|
|
63
|
+
opacity={access(opacity, item.d, item.i)}
|
|
64
|
+
title={title?.(item.d)}
|
|
65
|
+
onpointerenter={onpointerenter ? (e) => onpointerenter(item.d, e) : undefined}
|
|
66
|
+
onpointerleave={onpointerleave ? (e) => onpointerleave(item.d, e) : undefined}
|
|
67
|
+
/>
|
|
68
|
+
{/each}
|
|
69
|
+
</g>
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Accessor, Value } from '../core/accessor.js';
|
|
2
|
+
declare function $$render<T>(): {
|
|
3
|
+
props: {
|
|
4
|
+
data: T[];
|
|
5
|
+
x: Accessor<T, Value>;
|
|
6
|
+
y: Accessor<T, Value>;
|
|
7
|
+
/** The datum's flag from `@samizdata/mozaic/flags`, or nothing to skip it. */
|
|
8
|
+
flag: (d: T) => string | null | undefined;
|
|
9
|
+
/** Radius in px. */
|
|
10
|
+
r?: Accessor<T, number>;
|
|
11
|
+
opacity?: Accessor<T, number>;
|
|
12
|
+
/** Accessible name per flag, usually the country's name. */
|
|
13
|
+
title?: (d: T) => string;
|
|
14
|
+
onpointerenter?: (d: T, event: PointerEvent) => void;
|
|
15
|
+
onpointerleave?: (d: T, event: PointerEvent) => void;
|
|
16
|
+
class?: string;
|
|
17
|
+
};
|
|
18
|
+
exports: {};
|
|
19
|
+
bindings: "";
|
|
20
|
+
slots: {};
|
|
21
|
+
events: {};
|
|
22
|
+
};
|
|
23
|
+
declare class __sveltets_Render<T> {
|
|
24
|
+
props(): ReturnType<typeof $$render<T>>['props'];
|
|
25
|
+
events(): ReturnType<typeof $$render<T>>['events'];
|
|
26
|
+
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
27
|
+
bindings(): "";
|
|
28
|
+
exports(): {};
|
|
29
|
+
}
|
|
30
|
+
interface $$IsomorphicComponent {
|
|
31
|
+
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']>> & {
|
|
32
|
+
$$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
|
|
33
|
+
} & ReturnType<__sveltets_Render<T>['exports']>;
|
|
34
|
+
<T>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
|
|
35
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Flags as points, as in ggflags: one circular flag per datum at its x/y
|
|
39
|
+
* position (scatter plots, dot plots, maps with a projection). Pass the flag for
|
|
40
|
+
* each datum from `@samizdata/mozaic/flags`; rows without one are skipped. For a
|
|
41
|
+
* one-axis strip of flags that do not overlap, use `Beeswarm` with `flag`.
|
|
42
|
+
*/
|
|
43
|
+
declare const Flags: $$IsomorphicComponent;
|
|
44
|
+
type Flags<T> = InstanceType<typeof Flags<T>>;
|
|
45
|
+
export default Flags;
|
|
@@ -3,13 +3,15 @@
|
|
|
3
3
|
Direct labels at the end of lines: a dot in the series colour on the last
|
|
4
4
|
point, and the label beside it, in the text colour or (with `colorText`) in
|
|
5
5
|
the series colour so the labels double as the legend. Labels are nudged apart
|
|
6
|
-
so they never overlap; a short leader joins a nudged label to its dot.
|
|
6
|
+
so they never overlap; a short leader joins a nudged label to its dot. A label
|
|
7
|
+
too long for the room right of the plot breaks onto two or more lines.
|
|
7
8
|
-->
|
|
8
9
|
<script lang="ts" generics="T">
|
|
9
10
|
import { useChart, usePlot } from '../core/context.js';
|
|
10
11
|
import { scaled } from '../core/scales.js';
|
|
12
|
+
import { useTextWidth } from '../core/text.svelte.js';
|
|
11
13
|
import { readableColor } from '../styles/color.js';
|
|
12
|
-
import { avoidOverlap } from '../transforms/labels.js';
|
|
14
|
+
import { avoidOverlap, wrapLabel } from '../transforms/labels.js';
|
|
13
15
|
|
|
14
16
|
interface Props {
|
|
15
17
|
/** One item per series, describing its last point. */
|
|
@@ -27,21 +29,47 @@ so they never overlap; a short leader joins a nudged label to its dot.
|
|
|
27
29
|
* backgrounds) where needed to reach 4.5:1 against the background.
|
|
28
30
|
*/
|
|
29
31
|
colorText?: boolean;
|
|
32
|
+
/** Draw the dot on the last point; turn it off when the point has a mark of its own. */
|
|
33
|
+
dot?: boolean;
|
|
30
34
|
class?: string;
|
|
31
35
|
}
|
|
32
36
|
|
|
33
|
-
let {
|
|
37
|
+
let {
|
|
38
|
+
data,
|
|
39
|
+
x,
|
|
40
|
+
y,
|
|
41
|
+
text,
|
|
42
|
+
color,
|
|
43
|
+
dx = 10,
|
|
44
|
+
minGap = 14,
|
|
45
|
+
colorText = false,
|
|
46
|
+
dot = true,
|
|
47
|
+
class: className = ''
|
|
48
|
+
}: Props = $props();
|
|
34
49
|
|
|
35
50
|
const chart = useChart();
|
|
36
51
|
const plot = usePlot();
|
|
37
52
|
const background = $derived(chart.theme.tokens['--mz-color-bg']);
|
|
38
53
|
|
|
54
|
+
const measure = useTextWidth();
|
|
55
|
+
/** Line height of a wrapped label, in em. */
|
|
56
|
+
const leading = 1.15;
|
|
57
|
+
|
|
39
58
|
const labels = $derived.by(() => {
|
|
40
|
-
const
|
|
59
|
+
const size = chart.theme.plot.fontSize;
|
|
60
|
+
const width = (s: string) => measure(s, size, chart.theme.tokens['--mz-font-body']);
|
|
61
|
+
const points = data.map((d) => {
|
|
62
|
+
const px = scaled(plot.x, x(d));
|
|
63
|
+
// Room from the label's start to the chart's right edge, less a pixel or two for the halo.
|
|
64
|
+
const room = plot.width + plot.padding.right - (px + dx) - 2;
|
|
65
|
+
return { d, px, py: scaled(plot.y, y(d)), lines: wrapLabel(text(d), room, width) };
|
|
66
|
+
});
|
|
67
|
+
const lineGap = (size * leading) / minGap;
|
|
41
68
|
const placed = avoidOverlap(
|
|
42
69
|
points.map((p) => p.py),
|
|
43
70
|
minGap,
|
|
44
|
-
[0, plot.height]
|
|
71
|
+
[0, plot.height],
|
|
72
|
+
points.map((p) => 1 + (p.lines.length - 1) * lineGap)
|
|
45
73
|
);
|
|
46
74
|
return points.map((p, i) => ({ ...p, ly: placed[i] }));
|
|
47
75
|
});
|
|
@@ -52,12 +80,16 @@ so they never overlap; a short leader joins a nudged label to its dot.
|
|
|
52
80
|
{#if Math.abs(label.ly - label.py) > 2}
|
|
53
81
|
<path class="mz-leader" d="M{label.px + 5},{label.py}L{label.px + dx - 3},{label.ly}" />
|
|
54
82
|
{/if}
|
|
55
|
-
<circle cx={label.px} cy={label.py} r={4} fill={color(label.d)} />
|
|
83
|
+
{#if dot}<circle cx={label.px} cy={label.py} r={4} fill={color(label.d)} />{/if}
|
|
56
84
|
<text
|
|
57
85
|
x={label.px + dx}
|
|
58
86
|
y={label.ly}
|
|
59
|
-
dy=
|
|
60
|
-
style:fill={colorText ? readableColor(color(label.d), background) : undefined}
|
|
87
|
+
dy={label.lines.length === 1 ? '0.32em' : undefined}
|
|
88
|
+
style:fill={colorText ? readableColor(color(label.d), background) : undefined}
|
|
89
|
+
>{#if label.lines.length === 1}{label.lines[0]}{:else}{#each label.lines as line, i (i)}<tspan
|
|
90
|
+
x={label.px + dx}
|
|
91
|
+
dy={i === 0 ? `${0.32 - ((label.lines.length - 1) * leading) / 2}em` : `${leading}em`}>{line}</tspan
|
|
92
|
+
>{/each}{/if}</text
|
|
61
93
|
>
|
|
62
94
|
{/each}
|
|
63
95
|
</g>
|
|
@@ -15,6 +15,8 @@ declare function $$render<T>(): {
|
|
|
15
15
|
* backgrounds) where needed to reach 4.5:1 against the background.
|
|
16
16
|
*/
|
|
17
17
|
colorText?: boolean;
|
|
18
|
+
/** Draw the dot on the last point; turn it off when the point has a mark of its own. */
|
|
19
|
+
dot?: boolean;
|
|
18
20
|
class?: string;
|
|
19
21
|
};
|
|
20
22
|
exports: {};
|
|
@@ -40,7 +42,8 @@ interface $$IsomorphicComponent {
|
|
|
40
42
|
* Direct labels at the end of lines: a dot in the series colour on the last
|
|
41
43
|
* point, and the label beside it, in the text colour or (with `colorText`) in
|
|
42
44
|
* the series colour so the labels double as the legend. Labels are nudged apart
|
|
43
|
-
* so they never overlap; a short leader joins a nudged label to its dot.
|
|
45
|
+
* so they never overlap; a short leader joins a nudged label to its dot. A label
|
|
46
|
+
* too long for the room right of the plot breaks onto two or more lines.
|
|
44
47
|
*/
|
|
45
48
|
declare const LineEndLabels: $$IsomorphicComponent;
|
|
46
49
|
type LineEndLabels<T> = InstanceType<typeof LineEndLabels<T>>;
|
package/dist/marks/index.d.ts
CHANGED
|
@@ -15,6 +15,8 @@ export { default as Dumbbell } from './Dumbbell.svelte';
|
|
|
15
15
|
export { default as Arrows } from './Arrows.svelte';
|
|
16
16
|
export { default as Slope } from './Slope.svelte';
|
|
17
17
|
export { default as Beeswarm } from './Beeswarm.svelte';
|
|
18
|
+
export { default as Flag } from './Flag.svelte';
|
|
19
|
+
export { default as Flags } from './Flags.svelte';
|
|
18
20
|
export { default as Hemicycle } from './Hemicycle.svelte';
|
|
19
21
|
export { default as Intervals } from './Intervals.svelte';
|
|
20
22
|
export { barPath } from './bar-path.js';
|
package/dist/marks/index.js
CHANGED
|
@@ -15,6 +15,8 @@ export { default as Dumbbell } from './Dumbbell.svelte';
|
|
|
15
15
|
export { default as Arrows } from './Arrows.svelte';
|
|
16
16
|
export { default as Slope } from './Slope.svelte';
|
|
17
17
|
export { default as Beeswarm } from './Beeswarm.svelte';
|
|
18
|
+
export { default as Flag } from './Flag.svelte';
|
|
19
|
+
export { default as Flags } from './Flags.svelte';
|
|
18
20
|
export { default as Hemicycle } from './Hemicycle.svelte';
|
|
19
21
|
export { default as Intervals } from './Intervals.svelte';
|
|
20
22
|
export { barPath } from './bar-path.js';
|