@samizdata/mozaic 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/annotations/RefLine.svelte +41 -6
- package/dist/annotations/RefLine.svelte.d.ts +9 -1
- package/dist/guides/AxisY.svelte +25 -2
- package/dist/guides/AxisY.svelte.d.ts +4 -0
- package/dist/interactive/Tooltip.svelte +52 -2
- package/dist/interactive/Tooltip.svelte.d.ts +16 -1
- package/dist/marks/Waffle.svelte +10 -2
- package/dist/marks/Waffle.svelte.d.ts +5 -1
- package/package.json +1 -1
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
<!--
|
|
2
2
|
@component
|
|
3
3
|
A labelled reference line at an x value (vertical) or y value (horizontal):
|
|
4
|
-
a target, an average, an event date.
|
|
4
|
+
a target, an average, an event date. Newlines split the label. For event
|
|
5
|
+
markers, `dotted` draws a line of dots and a negative `labelY` puts the label
|
|
6
|
+
in the top padding, above the data, with the line running up to it.
|
|
5
7
|
-->
|
|
6
8
|
<script lang="ts">
|
|
7
9
|
import { usePlot } from '../core/context.js';
|
|
@@ -15,7 +17,13 @@ a target, an average, an event date.
|
|
|
15
17
|
label?: string;
|
|
16
18
|
/** Where the label sits along the line. */
|
|
17
19
|
labelPosition?: 'start' | 'end';
|
|
20
|
+
/** Vertical lines: which side of the line the label is on (`start`: right, `end`: left). */
|
|
21
|
+
anchor?: 'start' | 'end';
|
|
22
|
+
/** Vertical lines with the label at the end: label top in px from the top of the plot. Negative values sit in the padding above it. */
|
|
23
|
+
labelY?: number;
|
|
18
24
|
dash?: string;
|
|
25
|
+
/** A line of round dots instead of dashes. */
|
|
26
|
+
dotted?: boolean;
|
|
19
27
|
stroke?: string;
|
|
20
28
|
class?: string;
|
|
21
29
|
}
|
|
@@ -25,7 +33,10 @@ a target, an average, an event date.
|
|
|
25
33
|
y,
|
|
26
34
|
label,
|
|
27
35
|
labelPosition = 'end',
|
|
36
|
+
anchor = 'start',
|
|
37
|
+
labelY = 0,
|
|
28
38
|
dash = '4 3',
|
|
39
|
+
dotted = false,
|
|
29
40
|
stroke = 'var(--mz-color-text)',
|
|
30
41
|
class: className = ''
|
|
31
42
|
}: Props = $props();
|
|
@@ -33,15 +44,31 @@ a target, an average, an event date.
|
|
|
33
44
|
const plot = usePlot();
|
|
34
45
|
const px = $derived(x === undefined ? undefined : scaled(plot.x, x));
|
|
35
46
|
const py = $derived(y === undefined ? undefined : scaled(plot.y, y));
|
|
47
|
+
const lines = $derived(label ? label.split('\n') : []);
|
|
48
|
+
const top = $derived(labelPosition === 'end' ? Math.min(0, labelY) : 0);
|
|
49
|
+
const dx = $derived(anchor === 'start' ? 4 : -4);
|
|
50
|
+
// Labels that end at a point (above a horizontal line, at the foot of a vertical one) start this far up.
|
|
51
|
+
const lift = $derived(`-${(lines.length - 1) * 1.15}em`);
|
|
36
52
|
</script>
|
|
37
53
|
|
|
38
54
|
<g class="mz-ref-line {className}">
|
|
39
55
|
{#if px !== undefined}
|
|
40
|
-
<line
|
|
56
|
+
<line
|
|
57
|
+
x1={px}
|
|
58
|
+
x2={px}
|
|
59
|
+
y1={top}
|
|
60
|
+
y2={plot.height}
|
|
61
|
+
style:stroke
|
|
62
|
+
stroke-dasharray={dotted ? '0 3' : dash}
|
|
63
|
+
stroke-linecap={dotted ? 'round' : undefined}
|
|
64
|
+
class:dotted
|
|
65
|
+
/>
|
|
41
66
|
{#if label}
|
|
42
|
-
<text x={px +
|
|
43
|
-
|
|
44
|
-
|
|
67
|
+
<text x={px + dx} y={labelPosition === 'end' ? labelY : plot.height - 4} text-anchor={anchor}>
|
|
68
|
+
{#each lines as line, i (i)}
|
|
69
|
+
<tspan x={px + dx} dy={i === 0 ? (labelPosition === 'end' ? '0.9em' : lift) : '1.15em'}>{line}</tspan>
|
|
70
|
+
{/each}
|
|
71
|
+
</text>
|
|
45
72
|
{/if}
|
|
46
73
|
{/if}
|
|
47
74
|
{#if py !== undefined}
|
|
@@ -50,8 +77,12 @@ a target, an average, an event date.
|
|
|
50
77
|
<text
|
|
51
78
|
x={labelPosition === 'end' ? plot.width : 0}
|
|
52
79
|
y={py - 4}
|
|
53
|
-
text-anchor={labelPosition === 'end' ? 'end' : 'start'}
|
|
80
|
+
text-anchor={labelPosition === 'end' ? 'end' : 'start'}
|
|
54
81
|
>
|
|
82
|
+
{#each lines as line, i (i)}
|
|
83
|
+
<tspan x={labelPosition === 'end' ? plot.width : 0} dy={i === 0 ? lift : '1.15em'}>{line}</tspan>
|
|
84
|
+
{/each}
|
|
85
|
+
</text>
|
|
55
86
|
{/if}
|
|
56
87
|
{/if}
|
|
57
88
|
</g>
|
|
@@ -61,6 +92,10 @@ a target, an average, an event date.
|
|
|
61
92
|
stroke-width: 1;
|
|
62
93
|
shape-rendering: crispEdges;
|
|
63
94
|
}
|
|
95
|
+
line.dotted {
|
|
96
|
+
stroke-width: 1.5;
|
|
97
|
+
shape-rendering: auto;
|
|
98
|
+
}
|
|
64
99
|
text {
|
|
65
100
|
fill: var(--mz-color-text);
|
|
66
101
|
font-size: var(--mz-size-axis);
|
|
@@ -6,13 +6,21 @@ interface Props {
|
|
|
6
6
|
label?: string;
|
|
7
7
|
/** Where the label sits along the line. */
|
|
8
8
|
labelPosition?: 'start' | 'end';
|
|
9
|
+
/** Vertical lines: which side of the line the label is on (`start`: right, `end`: left). */
|
|
10
|
+
anchor?: 'start' | 'end';
|
|
11
|
+
/** Vertical lines with the label at the end: label top in px from the top of the plot. Negative values sit in the padding above it. */
|
|
12
|
+
labelY?: number;
|
|
9
13
|
dash?: string;
|
|
14
|
+
/** A line of round dots instead of dashes. */
|
|
15
|
+
dotted?: boolean;
|
|
10
16
|
stroke?: string;
|
|
11
17
|
class?: string;
|
|
12
18
|
}
|
|
13
19
|
/**
|
|
14
20
|
* A labelled reference line at an x value (vertical) or y value (horizontal):
|
|
15
|
-
* a target, an average, an event date.
|
|
21
|
+
* a target, an average, an event date. Newlines split the label. For event
|
|
22
|
+
* markers, `dotted` draws a line of dots and a negative `labelY` puts the label
|
|
23
|
+
* in the top padding, above the data, with the line running up to it.
|
|
16
24
|
*/
|
|
17
25
|
declare const RefLine: import("svelte").Component<Props, {}, "">;
|
|
18
26
|
type RefLine = ReturnType<typeof RefLine>;
|
package/dist/guides/AxisY.svelte
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
@component
|
|
3
3
|
Vertical axis for the plot's y scale. With `inside`, labels sit just above
|
|
4
4
|
the gridlines inside the plot area, which saves left padding on small screens.
|
|
5
|
+
`unit` names the unit once, after the top tick label ("50 lei/m³"); on a
|
|
6
|
+
left axis it runs into the plot, so it needs no extra padding.
|
|
5
7
|
-->
|
|
6
8
|
<script lang="ts">
|
|
7
9
|
import { useChart, usePlot } from '../core/context.js';
|
|
@@ -20,6 +22,8 @@ the gridlines inside the plot area, which saves left padding on small screens.
|
|
|
20
22
|
baseline?: boolean;
|
|
21
23
|
/** Axis title, drawn above the axis. */
|
|
22
24
|
label?: string;
|
|
25
|
+
/** Unit written after the top tick label, e.g. "lei/m³" or "%". */
|
|
26
|
+
unit?: string;
|
|
23
27
|
class?: string;
|
|
24
28
|
}
|
|
25
29
|
|
|
@@ -36,6 +40,7 @@ the gridlines inside the plot area, which saves left padding on small screens.
|
|
|
36
40
|
tickPadding: tickPaddingInput,
|
|
37
41
|
baseline: baselineInput,
|
|
38
42
|
label,
|
|
43
|
+
unit,
|
|
39
44
|
class: className = ''
|
|
40
45
|
}: Props = $props();
|
|
41
46
|
|
|
@@ -47,17 +52,29 @@ the gridlines inside the plot area, which saves left padding on small screens.
|
|
|
47
52
|
const fmt = $derived(format ?? defaultTickFormat(values, chart.locale));
|
|
48
53
|
const dir = $derived(position === 'left' ? -1 : 1);
|
|
49
54
|
const anchor = $derived(inside ? (position === 'left' ? 'start' : 'end') : position === 'left' ? 'end' : 'start');
|
|
55
|
+
// The tick nearest the top of the plot carries the unit.
|
|
56
|
+
const top = $derived(
|
|
57
|
+
unit
|
|
58
|
+
? values.reduce((a, b) => ((scaled(plot.y, b) ?? Infinity) < (scaled(plot.y, a) ?? Infinity) ? b : a), values[0])
|
|
59
|
+
: undefined
|
|
60
|
+
);
|
|
61
|
+
// Outside a left axis, the unit starts where the numbers end and runs into the plot.
|
|
62
|
+
const unitApart = $derived(position === 'left' && !inside);
|
|
50
63
|
</script>
|
|
51
64
|
|
|
52
65
|
<g class="mz-axis mz-axis-y {className}" transform="translate({position === 'left' ? 0 : plot.width},0)">
|
|
53
66
|
{#if baseline}<line class="mz-axis-line" y2={plot.height} />{/if}
|
|
54
67
|
{#each values as value, i (value)}
|
|
68
|
+
{@const withUnit = unit && value === top && !unitApart ? `${fmt(value, i)} ${unit}` : fmt(value, i)}
|
|
55
69
|
<g class="mz-tick" transform="translate(0,{scaled(plot.y, value)})">
|
|
56
70
|
{#if tickSize > 0 && !inside}<line x2={dir * tickSize} />{/if}
|
|
57
71
|
{#if inside}
|
|
58
|
-
<text x={0} dy="-0.35em" text-anchor={anchor}>{
|
|
72
|
+
<text x={0} dy="-0.35em" text-anchor={anchor}>{withUnit}</text>
|
|
59
73
|
{:else}
|
|
60
|
-
<text x={dir * (tickSize + tickPadding)} dy="0.32em" text-anchor={anchor}>{
|
|
74
|
+
<text x={dir * (tickSize + tickPadding)} dy="0.32em" text-anchor={anchor}>{withUnit}</text>
|
|
75
|
+
{/if}
|
|
76
|
+
{#if unit && value === top && unitApart}
|
|
77
|
+
<text class="mz-axis-unit" x={-(tickSize + tickPadding) + 4} dy="0.32em" text-anchor="start">{unit}</text>
|
|
61
78
|
{/if}
|
|
62
79
|
</g>
|
|
63
80
|
{/each}
|
|
@@ -71,6 +88,12 @@ the gridlines inside the plot area, which saves left padding on small screens.
|
|
|
71
88
|
fill: var(--mz-color-muted);
|
|
72
89
|
font-size: var(--mz-size-axis);
|
|
73
90
|
}
|
|
91
|
+
.mz-axis-unit {
|
|
92
|
+
paint-order: stroke;
|
|
93
|
+
stroke: var(--mz-color-bg);
|
|
94
|
+
stroke-width: 3px;
|
|
95
|
+
stroke-linejoin: round;
|
|
96
|
+
}
|
|
74
97
|
.mz-axis line {
|
|
75
98
|
stroke: var(--mz-color-axis);
|
|
76
99
|
shape-rendering: crispEdges;
|
|
@@ -10,11 +10,15 @@ interface Props {
|
|
|
10
10
|
baseline?: boolean;
|
|
11
11
|
/** Axis title, drawn above the axis. */
|
|
12
12
|
label?: string;
|
|
13
|
+
/** Unit written after the top tick label, e.g. "lei/m³" or "%". */
|
|
14
|
+
unit?: string;
|
|
13
15
|
class?: string;
|
|
14
16
|
}
|
|
15
17
|
/**
|
|
16
18
|
* Vertical axis for the plot's y scale. With `inside`, labels sit just above
|
|
17
19
|
* the gridlines inside the plot area, which saves left padding on small screens.
|
|
20
|
+
* `unit` names the unit once, after the top tick label ("50 lei/m³"); on a
|
|
21
|
+
* left axis it runs into the plot, so it needs no extra padding.
|
|
18
22
|
*/
|
|
19
23
|
declare const AxisY: import("svelte").Component<Props, {}, "">;
|
|
20
24
|
type AxisY = ReturnType<typeof AxisY>;
|
|
@@ -2,14 +2,30 @@
|
|
|
2
2
|
@component
|
|
3
3
|
A tooltip card at a pixel position in the plot area (inside `<Html>`).
|
|
4
4
|
Flips to the left of the point in the right part of the plot, and changes side or
|
|
5
|
-
slides over when needed to stay inside the chart.
|
|
5
|
+
slides over when needed to stay inside the chart. Pass `title` and `items` for the
|
|
6
|
+
usual card: a heading, then one row per series with its colour, name and value,
|
|
7
|
+
values aligned in a column. `children` follow the rows.
|
|
6
8
|
-->
|
|
7
9
|
<script lang="ts">
|
|
8
10
|
import type { Snippet } from 'svelte';
|
|
9
11
|
import { usePlot } from '../core/context.js';
|
|
10
12
|
import { tooltipLeft } from './tooltip-position.js';
|
|
11
13
|
|
|
14
|
+
interface Item {
|
|
15
|
+
label: string;
|
|
16
|
+
/** Already formatted. */
|
|
17
|
+
value: string;
|
|
18
|
+
/** Series colour, shown as a swatch before the label. */
|
|
19
|
+
color?: string;
|
|
20
|
+
/** Swatch shape: a line for line charts, a square otherwise. */
|
|
21
|
+
shape?: 'line' | 'square';
|
|
22
|
+
}
|
|
23
|
+
|
|
12
24
|
interface Props {
|
|
25
|
+
/** Heading of the card, e.g. the date or category. */
|
|
26
|
+
title?: string;
|
|
27
|
+
/** One row per series: swatch, label and value. */
|
|
28
|
+
items?: Item[];
|
|
13
29
|
/** Plot-area pixel position. */
|
|
14
30
|
x: number;
|
|
15
31
|
y: number;
|
|
@@ -20,7 +36,7 @@ slides over when needed to stay inside the chart.
|
|
|
20
36
|
children?: Snippet;
|
|
21
37
|
}
|
|
22
38
|
|
|
23
|
-
let { x, y, visible = true, offset = 12, class: className = '', children }: Props = $props();
|
|
39
|
+
let { title, items = [], x, y, visible = true, offset = 12, class: className = '', children }: Props = $props();
|
|
24
40
|
|
|
25
41
|
const plot = usePlot();
|
|
26
42
|
const flip = $derived(x > plot.width * 0.6);
|
|
@@ -43,6 +59,17 @@ slides over when needed to stay inside the chart.
|
|
|
43
59
|
: `translate(${offset}px, -50%)`}
|
|
44
60
|
bind:offsetWidth={tooltipWidth}
|
|
45
61
|
>
|
|
62
|
+
{#if title}<strong class="mz-tooltip-title">{title}</strong>{/if}
|
|
63
|
+
{#if items.length}
|
|
64
|
+
<div class="mz-tooltip-items">
|
|
65
|
+
{#each items as item, i (i)}
|
|
66
|
+
<span class="mz-tooltip-swatch {item.shape ?? 'square'}" style:background={item.color ?? 'transparent'}
|
|
67
|
+
></span>
|
|
68
|
+
<span class="mz-tooltip-label">{item.label}</span>
|
|
69
|
+
<strong class="mz-tooltip-value">{item.value}</strong>
|
|
70
|
+
{/each}
|
|
71
|
+
</div>
|
|
72
|
+
{/if}
|
|
46
73
|
{@render children?.()}
|
|
47
74
|
</div>
|
|
48
75
|
{/if}
|
|
@@ -63,4 +90,27 @@ slides over when needed to stay inside the chart.
|
|
|
63
90
|
color: var(--mz-color-text);
|
|
64
91
|
pointer-events: none;
|
|
65
92
|
}
|
|
93
|
+
.mz-tooltip-title {
|
|
94
|
+
display: block;
|
|
95
|
+
margin-bottom: 0.2rem;
|
|
96
|
+
}
|
|
97
|
+
.mz-tooltip-items {
|
|
98
|
+
display: grid;
|
|
99
|
+
grid-template-columns: auto 1fr auto;
|
|
100
|
+
align-items: center;
|
|
101
|
+
column-gap: 0.4rem;
|
|
102
|
+
row-gap: 0.15rem;
|
|
103
|
+
}
|
|
104
|
+
.mz-tooltip-swatch {
|
|
105
|
+
width: 0.7rem;
|
|
106
|
+
height: 0.7rem;
|
|
107
|
+
}
|
|
108
|
+
.mz-tooltip-swatch.line {
|
|
109
|
+
height: 3px;
|
|
110
|
+
}
|
|
111
|
+
.mz-tooltip-value {
|
|
112
|
+
text-align: right;
|
|
113
|
+
font-variant-numeric: tabular-nums;
|
|
114
|
+
white-space: nowrap;
|
|
115
|
+
}
|
|
66
116
|
</style>
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
|
+
interface Item {
|
|
3
|
+
label: string;
|
|
4
|
+
/** Already formatted. */
|
|
5
|
+
value: string;
|
|
6
|
+
/** Series colour, shown as a swatch before the label. */
|
|
7
|
+
color?: string;
|
|
8
|
+
/** Swatch shape: a line for line charts, a square otherwise. */
|
|
9
|
+
shape?: 'line' | 'square';
|
|
10
|
+
}
|
|
2
11
|
interface Props {
|
|
12
|
+
/** Heading of the card, e.g. the date or category. */
|
|
13
|
+
title?: string;
|
|
14
|
+
/** One row per series: swatch, label and value. */
|
|
15
|
+
items?: Item[];
|
|
3
16
|
/** Plot-area pixel position. */
|
|
4
17
|
x: number;
|
|
5
18
|
y: number;
|
|
@@ -12,7 +25,9 @@ interface Props {
|
|
|
12
25
|
/**
|
|
13
26
|
* A tooltip card at a pixel position in the plot area (inside `<Html>`).
|
|
14
27
|
* Flips to the left of the point in the right part of the plot, and changes side or
|
|
15
|
-
* slides over when needed to stay inside the chart.
|
|
28
|
+
* slides over when needed to stay inside the chart. Pass `title` and `items` for the
|
|
29
|
+
* usual card: a heading, then one row per series with its colour, name and value,
|
|
30
|
+
* values aligned in a column. `children` follow the rows.
|
|
16
31
|
*/
|
|
17
32
|
declare const Tooltip: import("svelte").Component<Props, {}, "">;
|
|
18
33
|
type Tooltip = ReturnType<typeof Tooltip>;
|
package/dist/marks/Waffle.svelte
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
<!--
|
|
2
2
|
@component
|
|
3
3
|
A waffle chart: a grid of squares, one per unit, coloured by category in
|
|
4
|
-
data order. Good for parts of a whole ("37 of 100 people").
|
|
4
|
+
data order. Good for parts of a whole ("37 of 100 people"). With
|
|
5
|
+
`origin="bottom"` the squares fill from the bottom row up, so the first
|
|
6
|
+
category sits at the base and waffles of different totals share a baseline.
|
|
5
7
|
-->
|
|
6
8
|
<script lang="ts" generics="T">
|
|
7
9
|
import { useChart, usePlot } from '../core/context.js';
|
|
@@ -17,6 +19,8 @@ data order. Good for parts of a whole ("37 of 100 people").
|
|
|
17
19
|
emptyFill?: string;
|
|
18
20
|
/** Gap between squares in px. */
|
|
19
21
|
gap?: number;
|
|
22
|
+
/** Row the squares start filling from: the top (default) or the bottom of the plot. */
|
|
23
|
+
origin?: 'top' | 'bottom';
|
|
20
24
|
title?: (d: T) => string;
|
|
21
25
|
class?: string;
|
|
22
26
|
}
|
|
@@ -29,6 +33,7 @@ data order. Good for parts of a whole ("37 of 100 people").
|
|
|
29
33
|
total,
|
|
30
34
|
emptyFill = 'var(--mz-color-grid)',
|
|
31
35
|
gap = 2,
|
|
36
|
+
origin = 'top',
|
|
32
37
|
title,
|
|
33
38
|
class: className = ''
|
|
34
39
|
}: Props = $props();
|
|
@@ -55,7 +60,10 @@ data order. Good for parts of a whole ("37 of 100 people").
|
|
|
55
60
|
{#each squares as sq}
|
|
56
61
|
<rect
|
|
57
62
|
x={(sq.i % columns) * cell + gap / 2}
|
|
58
|
-
y={
|
|
63
|
+
y={(origin === 'bottom'
|
|
64
|
+
? plot.height - (Math.floor(sq.i / columns) + 1) * cell
|
|
65
|
+
: Math.floor(sq.i / columns) * cell) +
|
|
66
|
+
gap / 2}
|
|
59
67
|
width={Math.max(0, cell - gap)}
|
|
60
68
|
height={Math.max(0, cell - gap)}
|
|
61
69
|
fill={sq.d === undefined
|
|
@@ -10,6 +10,8 @@ declare function $$render<T>(): {
|
|
|
10
10
|
emptyFill?: string;
|
|
11
11
|
/** Gap between squares in px. */
|
|
12
12
|
gap?: number;
|
|
13
|
+
/** Row the squares start filling from: the top (default) or the bottom of the plot. */
|
|
14
|
+
origin?: "top" | "bottom";
|
|
13
15
|
title?: (d: T) => string;
|
|
14
16
|
class?: string;
|
|
15
17
|
};
|
|
@@ -34,7 +36,9 @@ interface $$IsomorphicComponent {
|
|
|
34
36
|
}
|
|
35
37
|
/**
|
|
36
38
|
* A waffle chart: a grid of squares, one per unit, coloured by category in
|
|
37
|
-
* data order. Good for parts of a whole ("37 of 100 people").
|
|
39
|
+
* data order. Good for parts of a whole ("37 of 100 people"). With
|
|
40
|
+
* `origin="bottom"` the squares fill from the bottom row up, so the first
|
|
41
|
+
* category sits at the base and waffles of different totals share a baseline.
|
|
38
42
|
*/
|
|
39
43
|
declare const Waffle: $$IsomorphicComponent;
|
|
40
44
|
type Waffle<T> = InstanceType<typeof Waffle<T>>;
|