@samizdata/mozaic 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/frame/Title.svelte +0 -1
- package/dist/guides/Legend.svelte +45 -4
- package/dist/guides/Legend.svelte.d.ts +11 -1
- package/dist/interactive/PointerArea.svelte +18 -3
- package/dist/interactive/PointerArea.svelte.d.ts +3 -1
- package/dist/interactive/index.d.ts +1 -0
- package/dist/interactive/index.js +1 -0
- package/dist/interactive/touch-hold.d.ts +16 -0
- package/dist/interactive/touch-hold.js +45 -0
- package/dist/marks/Waffle.svelte +49 -1
- package/dist/marks/Waffle.svelte.d.ts +10 -2
- package/dist/recipes/StackedBarChart.svelte +78 -20
- package/dist/recipes/StackedBarChart.svelte.d.ts +4 -0
- package/package.json +1 -1
- package/src/lib/recipes/StackedBarChart.svelte +78 -20
package/dist/frame/Title.svelte
CHANGED
|
@@ -3,9 +3,13 @@
|
|
|
3
3
|
A row of swatches with labels. Pass `items` directly, or a d3 ordinal or
|
|
4
4
|
threshold colour `scale` (with `format` for threshold values). Text uses
|
|
5
5
|
text colours; only the swatch carries the data colour.
|
|
6
|
+
Bind `active` (start it at `null`) to link the legend to a mark: pointing at an
|
|
7
|
+
item sets `active` to its index, and an active item stays at full strength
|
|
8
|
+
while the others are dimmed.
|
|
6
9
|
-->
|
|
7
10
|
<script lang="ts">
|
|
8
11
|
import { useChart } from '../core/context.js';
|
|
12
|
+
import { touchHold } from '../interactive/touch-hold.js';
|
|
9
13
|
|
|
10
14
|
type Item = { label: string; color: string };
|
|
11
15
|
type ColorScale = {
|
|
@@ -21,10 +25,26 @@ text colours; only the swatch carries the data colour.
|
|
|
21
25
|
format?: (value: any) => string;
|
|
22
26
|
shape?: 'square' | 'line' | 'circle';
|
|
23
27
|
title?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Index of the highlighted item, or null for none. Leave it undefined for a static legend;
|
|
30
|
+
* bind it (`bind:active`, starting at null) to highlight items under the pointer.
|
|
31
|
+
*/
|
|
32
|
+
active?: number | null;
|
|
33
|
+
/** Opacity of the other items while one is highlighted. */
|
|
34
|
+
dim?: number;
|
|
24
35
|
class?: string;
|
|
25
36
|
}
|
|
26
37
|
|
|
27
|
-
let {
|
|
38
|
+
let {
|
|
39
|
+
items,
|
|
40
|
+
scale,
|
|
41
|
+
format,
|
|
42
|
+
shape = 'square',
|
|
43
|
+
title,
|
|
44
|
+
active = $bindable(),
|
|
45
|
+
dim = 0.35,
|
|
46
|
+
class: className = ''
|
|
47
|
+
}: Props = $props();
|
|
28
48
|
|
|
29
49
|
const chart = useChart();
|
|
30
50
|
|
|
@@ -48,13 +68,30 @@ text colours; only the swatch carries the data colour.
|
|
|
48
68
|
}
|
|
49
69
|
|
|
50
70
|
const resolved = $derived(items ?? (scale ? fromScale(scale) : []));
|
|
71
|
+
|
|
72
|
+
const live = $derived(active !== undefined && chart.medium !== 'print');
|
|
73
|
+
let list = $state<HTMLUListElement>();
|
|
74
|
+
const hold = touchHold(
|
|
75
|
+
() => list,
|
|
76
|
+
() => (active = null)
|
|
77
|
+
);
|
|
78
|
+
$effect(() => hold.destroy);
|
|
51
79
|
</script>
|
|
52
80
|
|
|
53
81
|
<div class="mz-legend {className}">
|
|
54
82
|
{#if title}<span class="mz-legend-title">{title}</span>{/if}
|
|
55
|
-
<ul
|
|
56
|
-
{
|
|
57
|
-
|
|
83
|
+
<ul
|
|
84
|
+
bind:this={list}
|
|
85
|
+
class:mz-legend-live={live}
|
|
86
|
+
onpointerdown={live ? hold.down : undefined}
|
|
87
|
+
onpointercancel={live ? hold.cancel : undefined}
|
|
88
|
+
onpointerleave={live ? hold.leave : undefined}
|
|
89
|
+
>
|
|
90
|
+
{#each resolved as item, i (item.label)}
|
|
91
|
+
<li
|
|
92
|
+
style:opacity={active !== null && active !== undefined && active !== i ? dim : undefined}
|
|
93
|
+
onpointerenter={live ? () => (active = i) : undefined}
|
|
94
|
+
>
|
|
58
95
|
<span class="mz-swatch mz-swatch-{shape}" style:background={item.color}></span>
|
|
59
96
|
{item.label}
|
|
60
97
|
</li>
|
|
@@ -85,6 +122,10 @@ text colours; only the swatch carries the data colour.
|
|
|
85
122
|
padding: 0;
|
|
86
123
|
list-style: none;
|
|
87
124
|
}
|
|
125
|
+
.mz-legend-live li {
|
|
126
|
+
cursor: default;
|
|
127
|
+
transition: opacity 120ms;
|
|
128
|
+
}
|
|
88
129
|
li {
|
|
89
130
|
display: flex;
|
|
90
131
|
align-items: center;
|
|
@@ -14,13 +14,23 @@ interface Props {
|
|
|
14
14
|
format?: (value: any) => string;
|
|
15
15
|
shape?: 'square' | 'line' | 'circle';
|
|
16
16
|
title?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Index of the highlighted item, or null for none. Leave it undefined for a static legend;
|
|
19
|
+
* bind it (`bind:active`, starting at null) to highlight items under the pointer.
|
|
20
|
+
*/
|
|
21
|
+
active?: number | null;
|
|
22
|
+
/** Opacity of the other items while one is highlighted. */
|
|
23
|
+
dim?: number;
|
|
17
24
|
class?: string;
|
|
18
25
|
}
|
|
19
26
|
/**
|
|
20
27
|
* A row of swatches with labels. Pass `items` directly, or a d3 ordinal or
|
|
21
28
|
* threshold colour `scale` (with `format` for threshold values). Text uses
|
|
22
29
|
* text colours; only the swatch carries the data colour.
|
|
30
|
+
* Bind `active` (start it at `null`) to link the legend to a mark: pointing at an
|
|
31
|
+
* item sets `active` to its index, and an active item stays at full strength
|
|
32
|
+
* while the others are dimmed.
|
|
23
33
|
*/
|
|
24
|
-
declare const Legend: import("svelte").Component<Props, {}, "">;
|
|
34
|
+
declare const Legend: import("svelte").Component<Props, {}, "active">;
|
|
25
35
|
type Legend = ReturnType<typeof Legend>;
|
|
26
36
|
export default Legend;
|
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
@component
|
|
3
3
|
An invisible rectangle over the plot area (inside `<Svg>`) that reports the
|
|
4
4
|
pointer position in plot pixels. Invert it with your scales to find the
|
|
5
|
-
nearest datum. Works with mouse, pen and touch
|
|
5
|
+
nearest datum. Works with mouse, pen and touch: with a mouse `onleave` fires
|
|
6
|
+
when the pointer leaves the plot; with touch, after the finger lifts the
|
|
7
|
+
tooltip stays and `onleave` fires on the next tap outside the plot.
|
|
6
8
|
-->
|
|
7
9
|
<script lang="ts">
|
|
8
10
|
import { usePlot } from '../core/context.js';
|
|
11
|
+
import { touchHold } from './touch-hold.js';
|
|
9
12
|
|
|
10
13
|
export type PointerPosition = { x: number; y: number; event: PointerEvent };
|
|
11
14
|
|
|
@@ -19,6 +22,13 @@ nearest datum. Works with mouse, pen and touch.
|
|
|
19
22
|
|
|
20
23
|
const plot = usePlot();
|
|
21
24
|
|
|
25
|
+
let area = $state<SVGRectElement>();
|
|
26
|
+
const hold = touchHold(
|
|
27
|
+
() => area,
|
|
28
|
+
(e) => onleave?.(e)
|
|
29
|
+
);
|
|
30
|
+
$effect(() => hold.destroy);
|
|
31
|
+
|
|
22
32
|
function report(event: PointerEvent) {
|
|
23
33
|
const rect = (event.currentTarget as SVGRectElement).getBoundingClientRect();
|
|
24
34
|
// The SVG may be scaled (before it is measured), so convert screen px to plot px.
|
|
@@ -31,13 +41,18 @@ nearest datum. Works with mouse, pen and touch.
|
|
|
31
41
|
{#if plot.medium !== 'print'}
|
|
32
42
|
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
33
43
|
<rect
|
|
44
|
+
bind:this={area}
|
|
34
45
|
class="mz-pointer-area {className}"
|
|
35
46
|
width={plot.width}
|
|
36
47
|
height={plot.height}
|
|
37
48
|
fill="transparent"
|
|
38
49
|
onpointermove={report}
|
|
39
|
-
onpointerdown={
|
|
40
|
-
|
|
50
|
+
onpointerdown={(e) => {
|
|
51
|
+
hold.down();
|
|
52
|
+
report(e);
|
|
53
|
+
}}
|
|
54
|
+
onpointercancel={hold.cancel}
|
|
55
|
+
onpointerleave={hold.leave}
|
|
41
56
|
/>
|
|
42
57
|
{/if}
|
|
43
58
|
|
|
@@ -11,7 +11,9 @@ interface Props {
|
|
|
11
11
|
/**
|
|
12
12
|
* An invisible rectangle over the plot area (inside `<Svg>`) that reports the
|
|
13
13
|
* pointer position in plot pixels. Invert it with your scales to find the
|
|
14
|
-
* nearest datum. Works with mouse, pen and touch
|
|
14
|
+
* nearest datum. Works with mouse, pen and touch: with a mouse `onleave` fires
|
|
15
|
+
* when the pointer leaves the plot; with touch, after the finger lifts the
|
|
16
|
+
* tooltip stays and `onleave` fires on the next tap outside the plot.
|
|
15
17
|
*/
|
|
16
18
|
declare const PointerArea: import("svelte").Component<Props, {}, "">;
|
|
17
19
|
type PointerArea = ReturnType<typeof PointerArea>;
|
|
@@ -2,3 +2,4 @@ export { default as PointerArea, type PointerPosition } from './PointerArea.svel
|
|
|
2
2
|
export { default as Tooltip } from './Tooltip.svelte';
|
|
3
3
|
export { default as ButtonGroup } from './ButtonGroup.svelte';
|
|
4
4
|
export { default as Scroller } from './Scroller.svelte';
|
|
5
|
+
export { touchHold } from './touch-hold.js';
|
|
@@ -2,3 +2,4 @@ export { default as PointerArea } from './PointerArea.svelte';
|
|
|
2
2
|
export { default as Tooltip } from './Tooltip.svelte';
|
|
3
3
|
export { default as ButtonGroup } from './ButtonGroup.svelte';
|
|
4
4
|
export { default as Scroller } from './Scroller.svelte';
|
|
5
|
+
export { touchHold } from './touch-hold.js';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hover state that survives lifting a finger. With a mouse, `leave` clears the
|
|
3
|
+
* hover at once. With touch, the browser reports a leave as soon as the finger
|
|
4
|
+
* lifts, which would hide a tooltip before it can be read; instead the hover
|
|
5
|
+
* stays until the next tap outside `root()`. A touch that turns into a scroll
|
|
6
|
+
* (`pointercancel`) still clears it.
|
|
7
|
+
*
|
|
8
|
+
* Wire `down` to `pointerdown`, `cancel` to `pointercancel` and `leave` to
|
|
9
|
+
* `pointerleave`; call `destroy` when the component goes away.
|
|
10
|
+
*/
|
|
11
|
+
export declare function touchHold(root: () => Element | null | undefined, clear: (event: PointerEvent) => void): {
|
|
12
|
+
down(): void;
|
|
13
|
+
cancel(): void;
|
|
14
|
+
leave(event: PointerEvent): void;
|
|
15
|
+
destroy: () => void;
|
|
16
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hover state that survives lifting a finger. With a mouse, `leave` clears the
|
|
3
|
+
* hover at once. With touch, the browser reports a leave as soon as the finger
|
|
4
|
+
* lifts, which would hide a tooltip before it can be read; instead the hover
|
|
5
|
+
* stays until the next tap outside `root()`. A touch that turns into a scroll
|
|
6
|
+
* (`pointercancel`) still clears it.
|
|
7
|
+
*
|
|
8
|
+
* Wire `down` to `pointerdown`, `cancel` to `pointercancel` and `leave` to
|
|
9
|
+
* `pointerleave`; call `destroy` when the component goes away.
|
|
10
|
+
*/
|
|
11
|
+
export function touchHold(root, clear) {
|
|
12
|
+
let stop;
|
|
13
|
+
let cancelled = false;
|
|
14
|
+
function release() {
|
|
15
|
+
stop?.();
|
|
16
|
+
stop = undefined;
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
down() {
|
|
20
|
+
cancelled = false;
|
|
21
|
+
},
|
|
22
|
+
cancel() {
|
|
23
|
+
cancelled = true;
|
|
24
|
+
},
|
|
25
|
+
leave(event) {
|
|
26
|
+
release();
|
|
27
|
+
if (event.pointerType === 'touch' && !cancelled && typeof document !== 'undefined') {
|
|
28
|
+
const outside = (e) => {
|
|
29
|
+
const el = root();
|
|
30
|
+
if (el && e.target instanceof Node && el.contains(e.target))
|
|
31
|
+
return;
|
|
32
|
+
release();
|
|
33
|
+
clear(e);
|
|
34
|
+
};
|
|
35
|
+
// Capture, so the clear runs before a handler on the tapped element sets a new hover.
|
|
36
|
+
document.addEventListener('pointerdown', outside, true);
|
|
37
|
+
stop = () => document.removeEventListener('pointerdown', outside, true);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
cancelled = false;
|
|
41
|
+
clear(event);
|
|
42
|
+
},
|
|
43
|
+
destroy: release
|
|
44
|
+
};
|
|
45
|
+
}
|
package/dist/marks/Waffle.svelte
CHANGED
|
@@ -4,9 +4,12 @@ A waffle chart: a grid of squares, one per unit, coloured by category in
|
|
|
4
4
|
data order. Good for parts of a whole ("37 of 100 people"). With
|
|
5
5
|
`origin="bottom"` the squares fill from the bottom row up, so the first
|
|
6
6
|
category sits at the base and waffles of different totals share a baseline.
|
|
7
|
+
Pointing at (or tapping) a category's squares highlights them and dims the
|
|
8
|
+
rest; `bind:active` to share the highlight with a legend or another waffle.
|
|
7
9
|
-->
|
|
8
10
|
<script lang="ts" generics="T">
|
|
9
11
|
import { useChart, usePlot } from '../core/context.js';
|
|
12
|
+
import { touchHold } from '../interactive/touch-hold.js';
|
|
10
13
|
|
|
11
14
|
interface Props {
|
|
12
15
|
data: T[];
|
|
@@ -22,6 +25,12 @@ category sits at the base and waffles of different totals share a baseline.
|
|
|
22
25
|
/** Row the squares start filling from: the top (default) or the bottom of the plot. */
|
|
23
26
|
origin?: 'top' | 'bottom';
|
|
24
27
|
title?: (d: T) => string;
|
|
28
|
+
/** Index of the highlighted category (in `data`), or null. Set by the pointer; bind to it to link a legend. */
|
|
29
|
+
active?: number | null;
|
|
30
|
+
/** Opacity of the other categories while one is highlighted. */
|
|
31
|
+
dim?: number;
|
|
32
|
+
/** Highlight categories under the pointer. Off in print. */
|
|
33
|
+
interactive?: boolean;
|
|
25
34
|
class?: string;
|
|
26
35
|
}
|
|
27
36
|
|
|
@@ -35,6 +44,9 @@ category sits at the base and waffles of different totals share a baseline.
|
|
|
35
44
|
gap = 2,
|
|
36
45
|
origin = 'top',
|
|
37
46
|
title,
|
|
47
|
+
active = $bindable(null),
|
|
48
|
+
dim = 0.25,
|
|
49
|
+
interactive = true,
|
|
38
50
|
class: className = ''
|
|
39
51
|
}: Props = $props();
|
|
40
52
|
|
|
@@ -54,11 +66,39 @@ category sits at the base and waffles of different totals share a baseline.
|
|
|
54
66
|
while (i < count) out.push({ i: i++, d: undefined, k: -1 });
|
|
55
67
|
return out;
|
|
56
68
|
});
|
|
69
|
+
|
|
70
|
+
const live = $derived(interactive && plot.medium !== 'print');
|
|
71
|
+
let group = $state<SVGGElement>();
|
|
72
|
+
const hold = touchHold(
|
|
73
|
+
() => group,
|
|
74
|
+
() => (active = null)
|
|
75
|
+
);
|
|
76
|
+
$effect(() => hold.destroy);
|
|
57
77
|
</script>
|
|
58
78
|
|
|
59
|
-
|
|
79
|
+
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
80
|
+
<g
|
|
81
|
+
bind:this={group}
|
|
82
|
+
class="mz-waffle {className}"
|
|
83
|
+
class:mz-waffle-live={live}
|
|
84
|
+
onpointerdown={live ? hold.down : undefined}
|
|
85
|
+
onpointercancel={live ? hold.cancel : undefined}
|
|
86
|
+
onpointerleave={live ? hold.leave : undefined}
|
|
87
|
+
>
|
|
88
|
+
{#if live}
|
|
89
|
+
<!-- Catches the pointer in the gaps, so moving between squares does not clear the highlight. -->
|
|
90
|
+
<rect
|
|
91
|
+
class="mz-waffle-bg"
|
|
92
|
+
y={origin === 'bottom' ? plot.height - rows * cell : 0}
|
|
93
|
+
width={columns * cell}
|
|
94
|
+
height={rows * cell}
|
|
95
|
+
fill="transparent"
|
|
96
|
+
/>
|
|
97
|
+
{/if}
|
|
60
98
|
{#each squares as sq}
|
|
99
|
+
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
61
100
|
<rect
|
|
101
|
+
class="mz-waffle-square"
|
|
62
102
|
x={(sq.i % columns) * cell + gap / 2}
|
|
63
103
|
y={(origin === 'bottom'
|
|
64
104
|
? plot.height - (Math.floor(sq.i / columns) + 1) * cell
|
|
@@ -71,7 +111,15 @@ category sits at the base and waffles of different totals share a baseline.
|
|
|
71
111
|
: fill
|
|
72
112
|
? fill(sq.d, sq.k)
|
|
73
113
|
: chart.theme.palettes.categorical[sq.k % chart.theme.palettes.categorical.length]}
|
|
114
|
+
opacity={active !== null && active !== undefined && sq.k !== active ? dim : 1}
|
|
115
|
+
onpointerenter={live ? () => (active = sq.d === undefined ? null : sq.k) : undefined}
|
|
74
116
|
>{#if title && sq.d !== undefined}<title>{title(sq.d)}</title>{/if}</rect
|
|
75
117
|
>
|
|
76
118
|
{/each}
|
|
77
119
|
</g>
|
|
120
|
+
|
|
121
|
+
<style>
|
|
122
|
+
.mz-waffle-live rect {
|
|
123
|
+
transition: opacity 120ms;
|
|
124
|
+
}
|
|
125
|
+
</style>
|
|
@@ -13,10 +13,16 @@ declare function $$render<T>(): {
|
|
|
13
13
|
/** Row the squares start filling from: the top (default) or the bottom of the plot. */
|
|
14
14
|
origin?: "top" | "bottom";
|
|
15
15
|
title?: (d: T) => string;
|
|
16
|
+
/** Index of the highlighted category (in `data`), or null. Set by the pointer; bind to it to link a legend. */
|
|
17
|
+
active?: number | null;
|
|
18
|
+
/** Opacity of the other categories while one is highlighted. */
|
|
19
|
+
dim?: number;
|
|
20
|
+
/** Highlight categories under the pointer. Off in print. */
|
|
21
|
+
interactive?: boolean;
|
|
16
22
|
class?: string;
|
|
17
23
|
};
|
|
18
24
|
exports: {};
|
|
19
|
-
bindings: "";
|
|
25
|
+
bindings: "active";
|
|
20
26
|
slots: {};
|
|
21
27
|
events: {};
|
|
22
28
|
};
|
|
@@ -24,7 +30,7 @@ declare class __sveltets_Render<T> {
|
|
|
24
30
|
props(): ReturnType<typeof $$render<T>>['props'];
|
|
25
31
|
events(): ReturnType<typeof $$render<T>>['events'];
|
|
26
32
|
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
27
|
-
bindings(): "";
|
|
33
|
+
bindings(): "active";
|
|
28
34
|
exports(): {};
|
|
29
35
|
}
|
|
30
36
|
interface $$IsomorphicComponent {
|
|
@@ -39,6 +45,8 @@ interface $$IsomorphicComponent {
|
|
|
39
45
|
* data order. Good for parts of a whole ("37 of 100 people"). With
|
|
40
46
|
* `origin="bottom"` the squares fill from the bottom row up, so the first
|
|
41
47
|
* category sits at the base and waffles of different totals share a baseline.
|
|
48
|
+
* Pointing at (or tapping) a category's squares highlights them and dims the
|
|
49
|
+
* rest; `bind:active` to share the highlight with a legend or another waffle.
|
|
42
50
|
*/
|
|
43
51
|
declare const Waffle: $$IsomorphicComponent;
|
|
44
52
|
type Waffle<T> = InstanceType<typeof Waffle<T>>;
|
|
@@ -5,6 +5,8 @@ of each series, with the series name and share written inside segments wide
|
|
|
5
5
|
enough to hold them. Optional: a reference line (e.g. at 50%), brackets above
|
|
6
6
|
the first bar that group neighbouring series ("pro-European", "pro-Russian"),
|
|
7
7
|
and a legend that lists groups of series instead of every series.
|
|
8
|
+
Pointing at (or tapping) a segment highlights its series in every bar and in
|
|
9
|
+
the default legend; pointing at a legend item does the same.
|
|
8
10
|
Use it as is, or run `mozaic eject StackedBarChart` to copy this file into
|
|
9
11
|
your project and change anything.
|
|
10
12
|
-->
|
|
@@ -17,6 +19,7 @@ your project and change anything.
|
|
|
17
19
|
import { Rects } from '../marks/index.js';
|
|
18
20
|
import { AxisX, AxisY, Legend } from '../guides/index.js';
|
|
19
21
|
import { RefLine } from '../annotations/index.js';
|
|
22
|
+
import { touchHold } from '../interactive/index.js';
|
|
20
23
|
import { stack } from '../transforms/index.js';
|
|
21
24
|
import { createLocale, type LocaleCode } from '../locale/index.js';
|
|
22
25
|
import { base, contrastRatio, type Medium, type Theme } from '../styles/index.js';
|
|
@@ -63,6 +66,8 @@ your project and change anything.
|
|
|
63
66
|
/** Share format (0–1); defaults to a locale percentage with one decimal. */
|
|
64
67
|
format?: (share: number) => string;
|
|
65
68
|
rowHeight?: number;
|
|
69
|
+
/** Highlight the series under the pointer. Off in print. */
|
|
70
|
+
interactive?: boolean;
|
|
66
71
|
/** Extra SVG drawn on top of the bars; receives the plot context. */
|
|
67
72
|
annotations?: Snippet<[PlotContext]>;
|
|
68
73
|
}
|
|
@@ -92,6 +97,7 @@ your project and change anything.
|
|
|
92
97
|
embed,
|
|
93
98
|
format,
|
|
94
99
|
rowHeight = 64,
|
|
100
|
+
interactive = true,
|
|
95
101
|
annotations
|
|
96
102
|
}: Props = $props();
|
|
97
103
|
|
|
@@ -111,6 +117,17 @@ your project and change anything.
|
|
|
111
117
|
const textOn = (fill: string) => (contrastRatio(fill, paper) >= contrastRatio(fill, ink) ? paper : ink);
|
|
112
118
|
const legendItems = $derived(legend ?? order.map((s) => ({ label: seriesLabel(s), color: colorOf(s) })));
|
|
113
119
|
|
|
120
|
+
// The highlighted series, shared by the bars and (when it lists the series) the legend.
|
|
121
|
+
let active = $state<string | null>(null);
|
|
122
|
+
const live = $derived(interactive && medium !== 'print');
|
|
123
|
+
const dimmed = (s: string) => active !== null && s !== active;
|
|
124
|
+
let bars = $state<SVGGElement>();
|
|
125
|
+
const hold = touchHold(
|
|
126
|
+
() => bars,
|
|
127
|
+
() => (active = null)
|
|
128
|
+
);
|
|
129
|
+
$effect(() => hold.destroy);
|
|
130
|
+
|
|
114
131
|
const rows = $derived([...new Set(data.map(row))]);
|
|
115
132
|
const single = $derived(rows.length === 1);
|
|
116
133
|
const x = scaleLinear().domain([0, 1]);
|
|
@@ -150,7 +167,19 @@ your project and change anything.
|
|
|
150
167
|
<Header>
|
|
151
168
|
{#if title}<Title>{title}</Title>{/if}
|
|
152
169
|
{#if subtitle}<Subtitle>{subtitle}</Subtitle>{/if}
|
|
153
|
-
{#if legendItems.length > 1}
|
|
170
|
+
{#if legendItems.length > 1}
|
|
171
|
+
{#if live && !legend}
|
|
172
|
+
<Legend
|
|
173
|
+
items={legendItems}
|
|
174
|
+
bind:active={
|
|
175
|
+
() => (active === null ? null : order.indexOf(active)),
|
|
176
|
+
(i) => (active = i === null || i === undefined ? null : (order[i] ?? null))
|
|
177
|
+
}
|
|
178
|
+
/>
|
|
179
|
+
{:else}
|
|
180
|
+
<Legend items={legendItems} />
|
|
181
|
+
{/if}
|
|
182
|
+
{/if}
|
|
154
183
|
</Header>
|
|
155
184
|
{/if}
|
|
156
185
|
|
|
@@ -159,25 +188,44 @@ your project and change anything.
|
|
|
159
188
|
<Svg>
|
|
160
189
|
<AxisX ticks={4} format={(v) => loc.format('.0%')(v)} />
|
|
161
190
|
{#if !single}<AxisY />{/if}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
{
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
191
|
+
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
192
|
+
<g
|
|
193
|
+
bind:this={bars}
|
|
194
|
+
class="mz-stacked-bars"
|
|
195
|
+
class:mz-stacked-live={live}
|
|
196
|
+
onpointerdown={live ? hold.down : undefined}
|
|
197
|
+
onpointercancel={live ? hold.cancel : undefined}
|
|
198
|
+
onpointerleave={live ? hold.leave : undefined}
|
|
199
|
+
>
|
|
200
|
+
<Rects
|
|
201
|
+
data={segments}
|
|
202
|
+
x={(s) => s.start}
|
|
203
|
+
x2={(s) => s.end}
|
|
204
|
+
y={(s) => s.key as string}
|
|
205
|
+
fill={(s) => colorOf(s.series)}
|
|
206
|
+
stroke={paper}
|
|
207
|
+
opacity={(s) => (dimmed(s.series) ? 0.25 : 1)}
|
|
208
|
+
title={(s) => `${single ? '' : `${s.key} · `}${seriesLabel(s.series)}: ${fmt(s.end - s.start)}`}
|
|
209
|
+
onpointerenter={live ? (s) => (active = s.series) : undefined}
|
|
210
|
+
/>
|
|
211
|
+
{#each segments as s (`${s.key}\u0000${s.series}`)}
|
|
212
|
+
{@const l = segmentLabel(plot, s)}
|
|
213
|
+
{#if l}
|
|
214
|
+
{@const cy = scaled(plot.y, s.key)}
|
|
215
|
+
<text
|
|
216
|
+
class="mz-stacked-label"
|
|
217
|
+
x={l.x}
|
|
218
|
+
y={cy}
|
|
219
|
+
fill={textOn(colorOf(s.series))}
|
|
220
|
+
opacity={dimmed(s.series) ? 0.25 : 1}
|
|
221
|
+
text-anchor="middle"
|
|
222
|
+
>
|
|
223
|
+
<tspan x={l.x} dy="-0.25em">{l.name}</tspan>
|
|
224
|
+
<tspan class="strong" x={l.x} dy="1.2em">{l.share}</tspan>
|
|
225
|
+
</text>
|
|
226
|
+
{/if}
|
|
227
|
+
{/each}
|
|
228
|
+
</g>
|
|
181
229
|
{#if reference !== undefined}
|
|
182
230
|
<RefLine x={reference} label={referenceLabel} />
|
|
183
231
|
{/if}
|
|
@@ -201,6 +249,16 @@ your project and change anything.
|
|
|
201
249
|
</Chart>
|
|
202
250
|
|
|
203
251
|
<style>
|
|
252
|
+
.mz-stacked-label {
|
|
253
|
+
/* Let the pointer reach the segment under the label. */
|
|
254
|
+
pointer-events: none;
|
|
255
|
+
}
|
|
256
|
+
.mz-stacked-live :global(rect),
|
|
257
|
+
.mz-stacked-live .mz-stacked-label {
|
|
258
|
+
transition:
|
|
259
|
+
fill-opacity 120ms,
|
|
260
|
+
opacity 120ms;
|
|
261
|
+
}
|
|
204
262
|
.mz-stacked-label,
|
|
205
263
|
.mz-stacked-bracket text {
|
|
206
264
|
font-family: var(--mz-font-body);
|
|
@@ -47,6 +47,8 @@ declare function $$render<T>(): {
|
|
|
47
47
|
/** Share format (0–1); defaults to a locale percentage with one decimal. */
|
|
48
48
|
format?: (share: number) => string;
|
|
49
49
|
rowHeight?: number;
|
|
50
|
+
/** Highlight the series under the pointer. Off in print. */
|
|
51
|
+
interactive?: boolean;
|
|
50
52
|
/** Extra SVG drawn on top of the bars; receives the plot context. */
|
|
51
53
|
annotations?: Snippet<[PlotContext]>;
|
|
52
54
|
};
|
|
@@ -75,6 +77,8 @@ interface $$IsomorphicComponent {
|
|
|
75
77
|
* enough to hold them. Optional: a reference line (e.g. at 50%), brackets above
|
|
76
78
|
* the first bar that group neighbouring series ("pro-European", "pro-Russian"),
|
|
77
79
|
* and a legend that lists groups of series instead of every series.
|
|
80
|
+
* Pointing at (or tapping) a segment highlights its series in every bar and in
|
|
81
|
+
* the default legend; pointing at a legend item does the same.
|
|
78
82
|
* Use it as is, or run `mozaic eject StackedBarChart` to copy this file into
|
|
79
83
|
* your project and change anything.
|
|
80
84
|
*/
|
package/package.json
CHANGED
|
@@ -5,6 +5,8 @@ of each series, with the series name and share written inside segments wide
|
|
|
5
5
|
enough to hold them. Optional: a reference line (e.g. at 50%), brackets above
|
|
6
6
|
the first bar that group neighbouring series ("pro-European", "pro-Russian"),
|
|
7
7
|
and a legend that lists groups of series instead of every series.
|
|
8
|
+
Pointing at (or tapping) a segment highlights its series in every bar and in
|
|
9
|
+
the default legend; pointing at a legend item does the same.
|
|
8
10
|
Use it as is, or run `mozaic eject StackedBarChart` to copy this file into
|
|
9
11
|
your project and change anything.
|
|
10
12
|
-->
|
|
@@ -17,6 +19,7 @@ your project and change anything.
|
|
|
17
19
|
import { Rects } from '../marks/index.js';
|
|
18
20
|
import { AxisX, AxisY, Legend } from '../guides/index.js';
|
|
19
21
|
import { RefLine } from '../annotations/index.js';
|
|
22
|
+
import { touchHold } from '../interactive/index.js';
|
|
20
23
|
import { stack } from '../transforms/index.js';
|
|
21
24
|
import { createLocale, type LocaleCode } from '../locale/index.js';
|
|
22
25
|
import { base, contrastRatio, type Medium, type Theme } from '../styles/index.js';
|
|
@@ -63,6 +66,8 @@ your project and change anything.
|
|
|
63
66
|
/** Share format (0–1); defaults to a locale percentage with one decimal. */
|
|
64
67
|
format?: (share: number) => string;
|
|
65
68
|
rowHeight?: number;
|
|
69
|
+
/** Highlight the series under the pointer. Off in print. */
|
|
70
|
+
interactive?: boolean;
|
|
66
71
|
/** Extra SVG drawn on top of the bars; receives the plot context. */
|
|
67
72
|
annotations?: Snippet<[PlotContext]>;
|
|
68
73
|
}
|
|
@@ -92,6 +97,7 @@ your project and change anything.
|
|
|
92
97
|
embed,
|
|
93
98
|
format,
|
|
94
99
|
rowHeight = 64,
|
|
100
|
+
interactive = true,
|
|
95
101
|
annotations
|
|
96
102
|
}: Props = $props();
|
|
97
103
|
|
|
@@ -111,6 +117,17 @@ your project and change anything.
|
|
|
111
117
|
const textOn = (fill: string) => (contrastRatio(fill, paper) >= contrastRatio(fill, ink) ? paper : ink);
|
|
112
118
|
const legendItems = $derived(legend ?? order.map((s) => ({ label: seriesLabel(s), color: colorOf(s) })));
|
|
113
119
|
|
|
120
|
+
// The highlighted series, shared by the bars and (when it lists the series) the legend.
|
|
121
|
+
let active = $state<string | null>(null);
|
|
122
|
+
const live = $derived(interactive && medium !== 'print');
|
|
123
|
+
const dimmed = (s: string) => active !== null && s !== active;
|
|
124
|
+
let bars = $state<SVGGElement>();
|
|
125
|
+
const hold = touchHold(
|
|
126
|
+
() => bars,
|
|
127
|
+
() => (active = null)
|
|
128
|
+
);
|
|
129
|
+
$effect(() => hold.destroy);
|
|
130
|
+
|
|
114
131
|
const rows = $derived([...new Set(data.map(row))]);
|
|
115
132
|
const single = $derived(rows.length === 1);
|
|
116
133
|
const x = scaleLinear().domain([0, 1]);
|
|
@@ -150,7 +167,19 @@ your project and change anything.
|
|
|
150
167
|
<Header>
|
|
151
168
|
{#if title}<Title>{title}</Title>{/if}
|
|
152
169
|
{#if subtitle}<Subtitle>{subtitle}</Subtitle>{/if}
|
|
153
|
-
{#if legendItems.length > 1}
|
|
170
|
+
{#if legendItems.length > 1}
|
|
171
|
+
{#if live && !legend}
|
|
172
|
+
<Legend
|
|
173
|
+
items={legendItems}
|
|
174
|
+
bind:active={
|
|
175
|
+
() => (active === null ? null : order.indexOf(active)),
|
|
176
|
+
(i) => (active = i === null || i === undefined ? null : (order[i] ?? null))
|
|
177
|
+
}
|
|
178
|
+
/>
|
|
179
|
+
{:else}
|
|
180
|
+
<Legend items={legendItems} />
|
|
181
|
+
{/if}
|
|
182
|
+
{/if}
|
|
154
183
|
</Header>
|
|
155
184
|
{/if}
|
|
156
185
|
|
|
@@ -159,25 +188,44 @@ your project and change anything.
|
|
|
159
188
|
<Svg>
|
|
160
189
|
<AxisX ticks={4} format={(v) => loc.format('.0%')(v)} />
|
|
161
190
|
{#if !single}<AxisY />{/if}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
{
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
191
|
+
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
192
|
+
<g
|
|
193
|
+
bind:this={bars}
|
|
194
|
+
class="mz-stacked-bars"
|
|
195
|
+
class:mz-stacked-live={live}
|
|
196
|
+
onpointerdown={live ? hold.down : undefined}
|
|
197
|
+
onpointercancel={live ? hold.cancel : undefined}
|
|
198
|
+
onpointerleave={live ? hold.leave : undefined}
|
|
199
|
+
>
|
|
200
|
+
<Rects
|
|
201
|
+
data={segments}
|
|
202
|
+
x={(s) => s.start}
|
|
203
|
+
x2={(s) => s.end}
|
|
204
|
+
y={(s) => s.key as string}
|
|
205
|
+
fill={(s) => colorOf(s.series)}
|
|
206
|
+
stroke={paper}
|
|
207
|
+
opacity={(s) => (dimmed(s.series) ? 0.25 : 1)}
|
|
208
|
+
title={(s) => `${single ? '' : `${s.key} · `}${seriesLabel(s.series)}: ${fmt(s.end - s.start)}`}
|
|
209
|
+
onpointerenter={live ? (s) => (active = s.series) : undefined}
|
|
210
|
+
/>
|
|
211
|
+
{#each segments as s (`${s.key}\u0000${s.series}`)}
|
|
212
|
+
{@const l = segmentLabel(plot, s)}
|
|
213
|
+
{#if l}
|
|
214
|
+
{@const cy = scaled(plot.y, s.key)}
|
|
215
|
+
<text
|
|
216
|
+
class="mz-stacked-label"
|
|
217
|
+
x={l.x}
|
|
218
|
+
y={cy}
|
|
219
|
+
fill={textOn(colorOf(s.series))}
|
|
220
|
+
opacity={dimmed(s.series) ? 0.25 : 1}
|
|
221
|
+
text-anchor="middle"
|
|
222
|
+
>
|
|
223
|
+
<tspan x={l.x} dy="-0.25em">{l.name}</tspan>
|
|
224
|
+
<tspan class="strong" x={l.x} dy="1.2em">{l.share}</tspan>
|
|
225
|
+
</text>
|
|
226
|
+
{/if}
|
|
227
|
+
{/each}
|
|
228
|
+
</g>
|
|
181
229
|
{#if reference !== undefined}
|
|
182
230
|
<RefLine x={reference} label={referenceLabel} />
|
|
183
231
|
{/if}
|
|
@@ -201,6 +249,16 @@ your project and change anything.
|
|
|
201
249
|
</Chart>
|
|
202
250
|
|
|
203
251
|
<style>
|
|
252
|
+
.mz-stacked-label {
|
|
253
|
+
/* Let the pointer reach the segment under the label. */
|
|
254
|
+
pointer-events: none;
|
|
255
|
+
}
|
|
256
|
+
.mz-stacked-live :global(rect),
|
|
257
|
+
.mz-stacked-live .mz-stacked-label {
|
|
258
|
+
transition:
|
|
259
|
+
fill-opacity 120ms,
|
|
260
|
+
opacity 120ms;
|
|
261
|
+
}
|
|
204
262
|
.mz-stacked-label,
|
|
205
263
|
.mz-stacked-bracket text {
|
|
206
264
|
font-family: var(--mz-font-body);
|