@ruckc/truss-chart 1.42.0 → 1.44.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/BarChart.svelte +13 -5
- package/dist/BarChart.svelte.d.ts +3 -1
- package/dist/ChartFrame.svelte +3 -0
- package/dist/DonutChart.svelte +12 -4
- package/dist/DonutChart.svelte.d.ts +3 -0
- package/dist/Histogram.svelte +15 -6
- package/dist/Histogram.svelte.d.ts +3 -0
- package/dist/LineChart.svelte +23 -4
- package/dist/LineChart.svelte.d.ts +3 -1
- package/dist/ScatterChart.svelte +13 -5
- package/dist/ScatterChart.svelte.d.ts +3 -1
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +23 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.js +7 -0
- package/package.json +1 -1
package/dist/BarChart.svelte
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
import ChartFrame from './ChartFrame.svelte';
|
|
3
3
|
import { watchFill } from './fill.js';
|
|
4
4
|
import Tooltip from './Tooltip.svelte';
|
|
5
|
-
import type { Series } from './types.js';
|
|
6
|
-
import { fmtAxis, fmtValue, linear, str, niceTicks, seriesColor, MAX_SERIES } from './util.js';
|
|
5
|
+
import type { ChartSelection, Series } from './types.js';
|
|
6
|
+
import { activate, fmtAxis, fmtValue, linear, str, niceTicks, seriesColor, MAX_SERIES } from './util.js';
|
|
7
7
|
|
|
8
8
|
let {
|
|
9
9
|
categories,
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
showValues = false,
|
|
17
17
|
reference,
|
|
18
18
|
details,
|
|
19
|
-
yFormat: yFormatProp
|
|
19
|
+
yFormat: yFormatProp,
|
|
20
|
+
onselect
|
|
20
21
|
}: {
|
|
21
22
|
categories: string[];
|
|
22
23
|
/** Up to 8 series (fixed-order palette). Negative values are allowed everywhere, including stacked bars. */
|
|
@@ -41,6 +42,8 @@
|
|
|
41
42
|
details?: string[];
|
|
42
43
|
/** Formats values for the value-axis ticks, labels, tooltip and table. Default: compact ticks (1.2K), plain numbers elsewhere. */
|
|
43
44
|
yFormat?: (n: number) => string;
|
|
45
|
+
/** Called with the bar's `category`, `series`, category `index` and raw `value` when a bar is clicked or activated with Enter / Space (bars are focusable). Also makes the table view's value cells buttons. Sets a pointer cursor on the bars. */
|
|
46
|
+
onselect?: (d: ChartSelection) => void;
|
|
44
47
|
} = $props();
|
|
45
48
|
|
|
46
49
|
type Ref = { value: number; label?: string };
|
|
@@ -116,6 +119,8 @@
|
|
|
116
119
|
const barLabel = (cat: string, name: string, v: number, share: number) => `${cat}, ${name}: ${str(yFormat(v))}${percent ? ` (${fmtValue(share)}%)` : ''}`;
|
|
117
120
|
const tipName = (t: Tip) => `${t.name}${t.share != null ? ` · ${fmtValue(t.share)}%` : ''}`;
|
|
118
121
|
|
|
122
|
+
const pick = (c: number, name: string, v: number) => onselect?.({ index: c, category: categories[c], series: name, value: v });
|
|
123
|
+
|
|
119
124
|
const GAP = 1; // each side of a shared edge yields a 2px surface gap
|
|
120
125
|
const labelWidth = $derived(Math.min(168, Math.max(48, Math.max(0, ...categories.map((c) => c.length)) * 6.4 + 14)));
|
|
121
126
|
const trunc = (s: string, px: number) => (s.length * 6.4 > px ? `${s.slice(0, Math.max(1, Math.floor(px / 6.4) - 1))}…` : s);
|
|
@@ -184,12 +189,14 @@
|
|
|
184
189
|
{#if d}
|
|
185
190
|
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
|
|
186
191
|
<path
|
|
187
|
-
class="bar" fill={ser.color} role=
|
|
192
|
+
class="bar" class:act={!!onselect} fill={ser.color} role={onselect ? 'button' : 'img'} tabindex="0" data-cat={categories[s.c]} data-series={ser.name}
|
|
188
193
|
aria-label={barLabel(categories[s.c], ser.name, s.v, Math.abs(s.hi - s.lo))}
|
|
189
194
|
{d}
|
|
190
195
|
onpointermove={() => (tip = tipFor(s, ser, tx, ty))}
|
|
191
196
|
onfocus={() => (tip = tipFor(s, ser, tx, ty))}
|
|
192
197
|
onblur={() => (tip = null)}
|
|
198
|
+
onclick={onselect && (() => pick(s.c, ser.name, s.v))}
|
|
199
|
+
onkeydown={onselect && activate(() => pick(s.c, ser.name, s.v))}
|
|
193
200
|
/>
|
|
194
201
|
{/if}
|
|
195
202
|
{/each}
|
|
@@ -235,7 +242,7 @@
|
|
|
235
242
|
<thead><tr><th>Category</th>{#each all as s}<th>{s.name}</th>{/each}{#if details}<th>Details</th>{/if}</tr></thead>
|
|
236
243
|
<tbody>
|
|
237
244
|
{#each categories as c, i}
|
|
238
|
-
<tr><td>{c}</td>{#each all as s}<td>{s.values[i] == null
|
|
245
|
+
<tr><td>{c}</td>{#each all as s}<td>{#if s.values[i] == null}—{:else if onselect}<button type="button" class="act" onclick={() => pick(i, s.name, s.values[i]!)}>{yFormat(s.values[i]!)}</button>{:else}{yFormat(s.values[i]!)}{/if}</td>{/each}{#if details}<td>{details[i] ?? ''}</td>{/if}</tr>
|
|
239
246
|
{/each}
|
|
240
247
|
</tbody>
|
|
241
248
|
{#if refs.length}
|
|
@@ -262,6 +269,7 @@
|
|
|
262
269
|
.tick { fill: var(--tr-on-surface-variant); font-size: var(--tr-text-xs); font-variant-numeric: tabular-nums; }
|
|
263
270
|
.val { fill: var(--tr-on-surface-variant); font-size: var(--tr-text-xs); font-weight: 600; font-variant-numeric: tabular-nums; }
|
|
264
271
|
.bar { outline: none; transition: filter var(--tr-dur-fast) var(--tr-ease); }
|
|
272
|
+
.bar.act { cursor: pointer; }
|
|
265
273
|
.bar:hover, .bar:focus-visible { filter: brightness(1.12) saturate(1.1); }
|
|
266
274
|
.bar:focus-visible { stroke: var(--tr-on-surface); stroke-width: 2; }
|
|
267
275
|
.ref { stroke: var(--tr-on-surface); stroke-width: 1.5; stroke-dasharray: 5 4; pointer-events: none; }
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Series } from './types.js';
|
|
1
|
+
import type { ChartSelection, Series } from './types.js';
|
|
2
2
|
type Ref = {
|
|
3
3
|
value: number;
|
|
4
4
|
label?: string;
|
|
@@ -27,6 +27,8 @@ type $$ComponentProps = {
|
|
|
27
27
|
details?: string[];
|
|
28
28
|
/** Formats values for the value-axis ticks, labels, tooltip and table. Default: compact ticks (1.2K), plain numbers elsewhere. */
|
|
29
29
|
yFormat?: (n: number) => string;
|
|
30
|
+
/** Called with the bar's `category`, `series`, category `index` and raw `value` when a bar is clicked or activated with Enter / Space (bars are focusable). Also makes the table view's value cells buttons. Sets a pointer cursor on the bars. */
|
|
31
|
+
onselect?: (d: ChartSelection) => void;
|
|
30
32
|
};
|
|
31
33
|
declare const BarChart: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
32
34
|
type BarChart = ReturnType<typeof BarChart>;
|
package/dist/ChartFrame.svelte
CHANGED
|
@@ -69,5 +69,8 @@
|
|
|
69
69
|
.tbl :global(table) { width: 100%; border-collapse: collapse; font-size: var(--tr-text-sm); font-variant-numeric: tabular-nums; }
|
|
70
70
|
.tbl :global(th), .tbl :global(td) { padding: 4px 8px; text-align: right; border-bottom: 1px solid var(--tr-outline-variant); }
|
|
71
71
|
.tbl :global(th:first-child), .tbl :global(td:first-child) { text-align: left; }
|
|
72
|
+
.tbl :global(button.act) { padding: 0; border: 0; border-radius: var(--tr-radius-sm); background: none; color: inherit; font: inherit; font-variant-numeric: inherit; cursor: pointer; text-decoration: underline dotted; text-underline-offset: 3px; }
|
|
73
|
+
.tbl :global(button.act:hover) { color: var(--tr-primary); }
|
|
74
|
+
.tbl :global(button.act:focus-visible) { outline: none; box-shadow: var(--tr-focus-ring); }
|
|
72
75
|
.tbl :global(th) { position: sticky; top: 0; background: var(--tr-surface-container); color: var(--tr-on-surface-variant); }
|
|
73
76
|
</style>
|
package/dist/DonutChart.svelte
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import ChartFrame from './ChartFrame.svelte';
|
|
3
3
|
import { watchFill } from './fill.js';
|
|
4
|
-
import {
|
|
4
|
+
import type { ChartSelection } from './types.js';
|
|
5
|
+
import { activate, fmtValue, seriesColor, str, MAX_SERIES } from './util.js';
|
|
5
6
|
|
|
6
7
|
let {
|
|
7
8
|
slices,
|
|
@@ -10,7 +11,8 @@
|
|
|
10
11
|
size = 200,
|
|
11
12
|
height,
|
|
12
13
|
format = fmtValue,
|
|
13
|
-
centerLabel = 'Total'
|
|
14
|
+
centerLabel = 'Total',
|
|
15
|
+
onselect
|
|
14
16
|
}: {
|
|
15
17
|
slices: { name: string; value: number }[];
|
|
16
18
|
title?: string;
|
|
@@ -26,6 +28,8 @@
|
|
|
26
28
|
height?: number | 'fill';
|
|
27
29
|
format?: (n: number) => string;
|
|
28
30
|
centerLabel?: string;
|
|
31
|
+
/** Called with the slice (`category` = its name, `index` into `slices`, `value`) when it is clicked or activated with Enter / Space (slices are focusable). Also makes the table view's slice names buttons. Sets a pointer cursor on the slices. */
|
|
32
|
+
onselect?: (d: ChartSelection) => void;
|
|
29
33
|
} = $props();
|
|
30
34
|
|
|
31
35
|
let hidden = $state<string[]>([]);
|
|
@@ -72,6 +76,7 @@
|
|
|
72
76
|
return { ...s, d };
|
|
73
77
|
});
|
|
74
78
|
};
|
|
79
|
+
const pick = (s: { name: string; value: number; i: number }) => onselect?.({ index: s.i, category: s.name, value: s.value });
|
|
75
80
|
const focus = $derived(vis.find((s) => s.name === active));
|
|
76
81
|
/** Share of the visible total; 0 (not NaN) when everything visible is 0. */
|
|
77
82
|
const pct = (v: number) => (total ? Math.round((v / total) * 100) : 0);
|
|
@@ -89,10 +94,12 @@
|
|
|
89
94
|
{#each arcs as a (a.name)}
|
|
90
95
|
<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
|
|
91
96
|
<path
|
|
92
|
-
role=
|
|
97
|
+
role={onselect ? 'button' : 'img'} d={a.d} fill={a.color} fill-rule="evenodd" class="slice" class:act={!!onselect} data-dim={active != null && active !== a.name} tabindex="0"
|
|
93
98
|
aria-label={sliceLabel(a)}
|
|
94
99
|
onpointerenter={() => (active = a.name)} onpointerleave={() => (active = null)}
|
|
95
100
|
onfocus={() => (active = a.name)} onblur={() => (active = null)}
|
|
101
|
+
onclick={onselect && (() => pick(a))}
|
|
102
|
+
onkeydown={onselect && activate(() => pick(a))}
|
|
96
103
|
/>
|
|
97
104
|
{/each}
|
|
98
105
|
<text class="big" x={size / 2} y={size / 2 - 2} text-anchor="middle">{format(focus?.value ?? total)}</text>
|
|
@@ -107,7 +114,7 @@
|
|
|
107
114
|
<table>
|
|
108
115
|
<thead><tr><th>Slice</th><th>Value</th><th>Share</th></tr></thead>
|
|
109
116
|
<tbody>
|
|
110
|
-
{#each base as s}<tr><td>{s.name}</td><td>{format(s.value)}</td><td>{`${Math.round((s.value / (base.reduce((a, q) => a + q.value, 0) || 1)) * 100)}%`}</td></tr>{/each}
|
|
117
|
+
{#each base as s}<tr><td>{#if onselect}<button type="button" class="act" onclick={() => pick(s)}>{s.name}</button>{:else}{s.name}{/if}</td><td>{format(s.value)}</td><td>{`${Math.round((s.value / (base.reduce((a, q) => a + q.value, 0) || 1)) * 100)}%`}</td></tr>{/each}
|
|
111
118
|
</tbody>
|
|
112
119
|
</table>
|
|
113
120
|
{/snippet}
|
|
@@ -125,6 +132,7 @@
|
|
|
125
132
|
.wrap { display: flex; justify-content: center; padding: 8px 0; }
|
|
126
133
|
/* 2px surface gap between adjacent fills */
|
|
127
134
|
.slice { stroke: var(--tr-surface-container-low); stroke-width: 2; outline: none; transition: opacity var(--tr-dur-fast) var(--tr-ease); }
|
|
135
|
+
.slice.act { cursor: pointer; }
|
|
128
136
|
.slice[data-dim='true'] { opacity: 0.4; }
|
|
129
137
|
.slice:focus-visible { stroke: var(--tr-on-surface); }
|
|
130
138
|
.big { fill: var(--tr-on-surface); font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ChartSelection } from './types.js';
|
|
1
2
|
type $$ComponentProps = {
|
|
2
3
|
slices: {
|
|
3
4
|
name: string;
|
|
@@ -16,6 +17,8 @@ type $$ComponentProps = {
|
|
|
16
17
|
height?: number | 'fill';
|
|
17
18
|
format?: (n: number) => string;
|
|
18
19
|
centerLabel?: string;
|
|
20
|
+
/** Called with the slice (`category` = its name, `index` into `slices`, `value`) when it is clicked or activated with Enter / Space (slices are focusable). Also makes the table view's slice names buttons. Sets a pointer cursor on the slices. */
|
|
21
|
+
onselect?: (d: ChartSelection) => void;
|
|
19
22
|
};
|
|
20
23
|
declare const DonutChart: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
21
24
|
type DonutChart = ReturnType<typeof DonutChart>;
|
package/dist/Histogram.svelte
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
import ChartFrame from './ChartFrame.svelte';
|
|
4
4
|
import { watchFill } from './fill.js';
|
|
5
5
|
import Tooltip from './Tooltip.svelte';
|
|
6
|
-
import {
|
|
6
|
+
import type { ChartSelection } from './types.js';
|
|
7
|
+
import { activate, barPath, fmtAxis, fmtValue, linear, niceTicks, seriesColor, str } from './util.js';
|
|
7
8
|
|
|
8
9
|
let {
|
|
9
10
|
values,
|
|
@@ -15,7 +16,8 @@
|
|
|
15
16
|
color = seriesColor(0),
|
|
16
17
|
xFormat = fmtValue,
|
|
17
18
|
range = $bindable(null),
|
|
18
|
-
onbrush
|
|
19
|
+
onbrush,
|
|
20
|
+
onselect
|
|
19
21
|
}: {
|
|
20
22
|
values: number[];
|
|
21
23
|
/** Approximate bin count, or 'auto' (Sturges). Edges snap to nice numbers. */
|
|
@@ -34,6 +36,8 @@
|
|
|
34
36
|
/** Brushed selection [from, to] on the value axis (snaps to bin edges). Drag to select, click to clear. */
|
|
35
37
|
range?: [number, number] | null;
|
|
36
38
|
onbrush?: (range: [number, number] | null) => void;
|
|
39
|
+
/** Called with the bin (`index`, `x0` / `x1` = its range, `x` = `x0`, `value` = its count) when a bin is clicked (a click without drag, which still clears the brush) or Enter / Space is pressed on the bin reached with the arrow keys (Enter still brushes it too). Also makes the table view's ranges buttons. Sets a pointer cursor on the plot. */
|
|
40
|
+
onselect?: (d: ChartSelection) => void;
|
|
37
41
|
} = $props();
|
|
38
42
|
|
|
39
43
|
let hover = $state<number | null>(null);
|
|
@@ -65,6 +69,7 @@
|
|
|
65
69
|
range = r;
|
|
66
70
|
onbrush?.(r);
|
|
67
71
|
}
|
|
72
|
+
const pick = (i: number) => onselect?.({ index: i, x: edges[i], x0: edges[i], x1: edges[i + 1], value: counts[i] });
|
|
68
73
|
// built in script: interpolating in markup would add unreachable `?? ''` branches
|
|
69
74
|
const share = (c: number) => (total ? Math.round((c / total) * 100) : 0);
|
|
70
75
|
const span = (i: number) => `${str(xFormat(edges[i]))} – ${str(xFormat(edges[i + 1]))}`;
|
|
@@ -102,7 +107,8 @@
|
|
|
102
107
|
onkeydown={(e) => {
|
|
103
108
|
if (e.key === 'ArrowRight') hover = Math.min(nb - 1, (hover ?? -1) + 1);
|
|
104
109
|
else if (e.key === 'ArrowLeft') hover = Math.max(0, (hover ?? nb) - 1);
|
|
105
|
-
else if (e.key === '
|
|
110
|
+
else if (e.key === ' ' && onselect && hover != null) pick(hover);
|
|
111
|
+
else if (e.key === 'Enter' && hover != null) { if (onselect) pick(hover); set(e.shiftKey && range ? [Math.min(range[0], edges[hover]), Math.max(range[1], edges[hover + 1])] : [edges[hover], edges[hover + 1]]); }
|
|
106
112
|
else if (e.key === 'Escape') set(null);
|
|
107
113
|
else return;
|
|
108
114
|
e.preventDefault();
|
|
@@ -129,7 +135,7 @@
|
|
|
129
135
|
<!-- hit layer: hover for tooltip, drag to brush -->
|
|
130
136
|
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
131
137
|
<rect
|
|
132
|
-
class="hit" x={m.left} y={m.top} width={w - m.left - m.right} height={height - m.top - m.bottom}
|
|
138
|
+
class="hit" class:act={!!onselect} x={m.left} y={m.top} width={w - m.left - m.right} height={height - m.top - m.bottom}
|
|
133
139
|
onpointermove={(e) => {
|
|
134
140
|
const px = e.clientX - e.currentTarget.getBoundingClientRect().left + m.left;
|
|
135
141
|
hover = Math.min(nb - 1, Math.max(0, Math.floor((toVal(px) - grid.min) / step)));
|
|
@@ -148,7 +154,9 @@
|
|
|
148
154
|
drag = null;
|
|
149
155
|
if (!d || cancelled) return;
|
|
150
156
|
const lo = Math.min(d.a, d.b), hi = Math.max(d.a, d.b);
|
|
151
|
-
|
|
157
|
+
const click = Math.abs(X(hi) - X(lo)) < 4;
|
|
158
|
+
if (click) pick(Math.min(nb - 1, Math.max(0, Math.floor((d.a - grid.min) / step))));
|
|
159
|
+
set(click ? null : [snapLo(lo), snapHi(hi)]);
|
|
152
160
|
}
|
|
153
161
|
});
|
|
154
162
|
if (stop) drag = { a: v, b: v };
|
|
@@ -169,7 +177,7 @@
|
|
|
169
177
|
<thead><tr><th>Range</th><th>Count</th><th>Share</th></tr></thead>
|
|
170
178
|
<tbody>
|
|
171
179
|
{#each counts as c, i}
|
|
172
|
-
<tr><td>{span(i)}</td><td>{c}</td><td>{`${share(c)}%`}</td></tr>
|
|
180
|
+
<tr><td>{#if onselect}<button type="button" class="act" onclick={() => pick(i)}>{span(i)}</button>{:else}{span(i)}{/if}</td><td>{c}</td><td>{`${share(c)}%`}</td></tr>
|
|
173
181
|
{/each}
|
|
174
182
|
</tbody>
|
|
175
183
|
</table>
|
|
@@ -195,6 +203,7 @@
|
|
|
195
203
|
.brush { fill: var(--tr-primary); fill-opacity: 0.1; pointer-events: none; }
|
|
196
204
|
.edge { stroke: var(--tr-primary); stroke-width: 1.5; pointer-events: none; }
|
|
197
205
|
.hit { fill: transparent; cursor: crosshair; touch-action: none; }
|
|
206
|
+
.hit.act { cursor: pointer; }
|
|
198
207
|
.head { color: var(--tr-on-surface-variant); margin-bottom: 4px; }
|
|
199
208
|
.row { display: flex; align-items: baseline; gap: 8px; }
|
|
200
209
|
.row b { color: var(--tr-on-surface); font-variant-numeric: tabular-nums; }
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ChartSelection } from './types.js';
|
|
1
2
|
type $$ComponentProps = {
|
|
2
3
|
values: number[];
|
|
3
4
|
/** Approximate bin count, or 'auto' (Sturges). Edges snap to nice numbers. */
|
|
@@ -16,6 +17,8 @@ type $$ComponentProps = {
|
|
|
16
17
|
/** Brushed selection [from, to] on the value axis (snaps to bin edges). Drag to select, click to clear. */
|
|
17
18
|
range?: [number, number] | null;
|
|
18
19
|
onbrush?: (range: [number, number] | null) => void;
|
|
20
|
+
/** Called with the bin (`index`, `x0` / `x1` = its range, `x` = `x0`, `value` = its count) when a bin is clicked (a click without drag, which still clears the brush) or Enter / Space is pressed on the bin reached with the arrow keys (Enter still brushes it too). Also makes the table view's ranges buttons. Sets a pointer cursor on the plot. */
|
|
21
|
+
onselect?: (d: ChartSelection) => void;
|
|
19
22
|
};
|
|
20
23
|
declare const Histogram: import("svelte").Component<$$ComponentProps, {}, "range">;
|
|
21
24
|
type Histogram = ReturnType<typeof Histogram>;
|
package/dist/LineChart.svelte
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import ChartFrame from './ChartFrame.svelte';
|
|
3
3
|
import Tooltip from './Tooltip.svelte';
|
|
4
4
|
import { watchFill } from './fill.js';
|
|
5
|
-
import type { Series, Band, ReferenceLine } from './types.js';
|
|
5
|
+
import type { Series, Band, ReferenceLine, ChartSelection } from './types.js';
|
|
6
6
|
import { fmtAxis, fmtValue, fmtX, isPos, linear, logLinear, logTicks, niceTicks, seriesColor, str, spreadLabels, labelGap, labelsFit, isDisplaced, estimateLabelWidth, MAX_SERIES } from './util.js';
|
|
7
7
|
|
|
8
8
|
let {
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
band,
|
|
21
21
|
referenceLines = [],
|
|
22
22
|
xScale = 'linear',
|
|
23
|
-
yScale: yScaleKind = 'linear'
|
|
23
|
+
yScale: yScaleKind = 'linear',
|
|
24
|
+
onselect
|
|
24
25
|
}: {
|
|
25
26
|
/** Shared x positions: numbers or Dates. */
|
|
26
27
|
x: (number | Date)[];
|
|
@@ -71,6 +72,8 @@
|
|
|
71
72
|
* minimum. `stacked` is ignored (series draw independently).
|
|
72
73
|
*/
|
|
73
74
|
yScale?: 'linear' | 'log';
|
|
75
|
+
/** Called with the nearest `x` / `index`, the `series` and its raw `value` when the plot is clicked (the series nearest the pointer) or Enter / Space is pressed on a position reached with the arrow keys (the first visible series with a value). Also makes the table view's value cells buttons. Sets a pointer cursor on the plot. */
|
|
76
|
+
onselect?: (d: ChartSelection) => void;
|
|
74
77
|
} = $props();
|
|
75
78
|
|
|
76
79
|
const yFormat = $derived(yFormatProp ?? fmtValue);
|
|
@@ -151,6 +154,11 @@
|
|
|
151
154
|
document.fonts.ready.then(measure);
|
|
152
155
|
return () => (live = false);
|
|
153
156
|
});
|
|
157
|
+
/** Reports position `i` (into the kept x) for the series called `name`. */
|
|
158
|
+
const choose = (i: number, name: string) => {
|
|
159
|
+
const v = all.find((s) => s.name === name)?.values[i];
|
|
160
|
+
if (v != null) onselect?.({ index: ix[i], x: x[ix[i]], series: name, value: v });
|
|
161
|
+
};
|
|
154
162
|
const items = $derived(all.map((s) => ({ name: s.name, color: s.color, hidden: hidden.includes(s.name) })));
|
|
155
163
|
const toggle = (n: string) => (hidden = hidden.includes(n) ? hidden.filter((h) => h !== n) : [...hidden, n]);
|
|
156
164
|
|
|
@@ -196,7 +204,7 @@
|
|
|
196
204
|
|
|
197
205
|
<!-- svelte-ignore a11y_no_noninteractive_tabindex, a11y_no_noninteractive_element_interactions -->
|
|
198
206
|
<svg
|
|
199
|
-
bind:this={svgEl} width={w} {height} role="group" tabindex="0"
|
|
207
|
+
bind:this={svgEl} width={w} {height} role="group" tabindex="0" class:act={!!onselect}
|
|
200
208
|
aria-label="{title ?? 'Line chart'}. Use left and right arrow keys to read values.{refsText && ` Reference lines: ${refsText}.`}"
|
|
201
209
|
onpointermove={(e) => {
|
|
202
210
|
const px = e.clientX - e.currentTarget.getBoundingClientRect().left;
|
|
@@ -204,12 +212,22 @@
|
|
|
204
212
|
for (let i = 1; i < xs.length; i++) if (Math.abs(X(xs[i]) - px) < Math.abs(X(xs[best]) - px)) best = i;
|
|
205
213
|
hover = best;
|
|
206
214
|
}}
|
|
215
|
+
onclick={onselect && ((e) => {
|
|
216
|
+
const r = e.currentTarget.getBoundingClientRect();
|
|
217
|
+
const px = e.clientX - r.left, py = e.clientY - r.top;
|
|
218
|
+
let best = 0;
|
|
219
|
+
for (let i = 1; i < xs.length; i++) if (Math.abs(X(xs[i]) - px) < Math.abs(X(xs[best]) - px)) best = i;
|
|
220
|
+
let k = -1;
|
|
221
|
+
vis.forEach((s, si) => { if (bands[si][best] != null && (k < 0 || Math.abs(Y(bands[si][best]!.hi) - py) < Math.abs(Y(bands[k][best]!.hi) - py))) k = si; });
|
|
222
|
+
if (k >= 0) choose(best, vis[k].name);
|
|
223
|
+
})}
|
|
207
224
|
onpointerleave={() => (hover = null)}
|
|
208
225
|
onblur={() => (hover = null)}
|
|
209
226
|
onkeydown={(e) => {
|
|
210
227
|
if (e.key === 'ArrowRight') hover = Math.min(xs.length - 1, (hover ?? -1) + 1);
|
|
211
228
|
else if (e.key === 'ArrowLeft') hover = Math.max(0, (hover ?? xs.length) - 1);
|
|
212
229
|
else if (e.key === 'Escape') hover = null;
|
|
230
|
+
else if ((e.key === 'Enter' || e.key === ' ') && onselect && hover != null) { const s = vis.find((q) => q.values[hover!] != null); if (s) choose(hover, s.name); }
|
|
213
231
|
else return;
|
|
214
232
|
e.preventDefault();
|
|
215
233
|
}}
|
|
@@ -282,7 +300,7 @@
|
|
|
282
300
|
<thead><tr><th>{isDate ? 'Date' : 'X'}</th>{#each all as s}<th>{s.name}</th>{/each}{#if bandData}<th>{bandLabel}</th>{/if}</tr></thead>
|
|
283
301
|
<tbody>
|
|
284
302
|
{#each xs as v, i}
|
|
285
|
-
<tr><td>{fx(v)}</td>{#each all as s}<td>{cellText(s, i)}</td>{/each}{#if bandData}<td>{bandAt(i) ? rangeText(bandAt(i)!) : '—'}</td>{/if}</tr>
|
|
303
|
+
<tr><td>{fx(v)}</td>{#each all as s}<td>{#if onselect && s.values[i] != null}<button type="button" class="act" onclick={() => choose(i, s.name)}>{cellText(s, i)}</button>{:else}{cellText(s, i)}{/if}</td>{/each}{#if bandData}<td>{bandAt(i) ? rangeText(bandAt(i)!) : '—'}</td>{/if}</tr>
|
|
286
304
|
{/each}
|
|
287
305
|
</tbody>
|
|
288
306
|
{#if refs.length}
|
|
@@ -303,6 +321,7 @@
|
|
|
303
321
|
|
|
304
322
|
<style>
|
|
305
323
|
svg { display: block; outline: none; overflow: visible; }
|
|
324
|
+
svg.act { cursor: pointer; }
|
|
306
325
|
svg:focus-visible { border-radius: var(--tr-radius-sm); box-shadow: var(--tr-focus-ring); }
|
|
307
326
|
.grid { stroke: var(--tr-outline-variant); stroke-opacity: 0.6; stroke-width: 1; }
|
|
308
327
|
.tick { fill: var(--tr-on-surface-variant); font-size: var(--tr-text-xs); font-variant-numeric: tabular-nums; }
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Series, Band, ReferenceLine } from './types.js';
|
|
1
|
+
import type { Series, Band, ReferenceLine, ChartSelection } from './types.js';
|
|
2
2
|
type $$ComponentProps = {
|
|
3
3
|
/** Shared x positions: numbers or Dates. */
|
|
4
4
|
x: (number | Date)[];
|
|
@@ -49,6 +49,8 @@ type $$ComponentProps = {
|
|
|
49
49
|
* minimum. `stacked` is ignored (series draw independently).
|
|
50
50
|
*/
|
|
51
51
|
yScale?: 'linear' | 'log';
|
|
52
|
+
/** Called with the nearest `x` / `index`, the `series` and its raw `value` when the plot is clicked (the series nearest the pointer) or Enter / Space is pressed on a position reached with the arrow keys (the first visible series with a value). Also makes the table view's value cells buttons. Sets a pointer cursor on the plot. */
|
|
53
|
+
onselect?: (d: ChartSelection) => void;
|
|
52
54
|
};
|
|
53
55
|
declare const LineChart: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
54
56
|
type LineChart = ReturnType<typeof LineChart>;
|
package/dist/ScatterChart.svelte
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import ChartFrame from './ChartFrame.svelte';
|
|
3
3
|
import { watchFill } from './fill.js';
|
|
4
4
|
import Tooltip from './Tooltip.svelte';
|
|
5
|
-
import type { ScatterPoint } from './types.js';
|
|
5
|
+
import type { ChartSelection, ScatterPoint } from './types.js';
|
|
6
6
|
import { fmtAxis, fmtValue, isPos, linear, logLinear, logTicks, niceTicks, seriesColor, MAX_SERIES } from './util.js';
|
|
7
7
|
|
|
8
8
|
let {
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
yFormat,
|
|
19
19
|
sizeFormat = fmtValue,
|
|
20
20
|
xScale: xScaleKind = 'linear',
|
|
21
|
-
yScale: yScaleKind = 'linear'
|
|
21
|
+
yScale: yScaleKind = 'linear',
|
|
22
|
+
onselect
|
|
22
23
|
}: {
|
|
23
24
|
/** `size` encodes a third value as marker diameter; `group` picks the (fixed-order, max 8) palette colour and legend entry. */
|
|
24
25
|
points: ScatterPoint[];
|
|
@@ -45,6 +46,8 @@
|
|
|
45
46
|
xScale?: 'linear' | 'log';
|
|
46
47
|
/** `'log'` plots y on a base-10 log axis with ticks at powers of 10. Points with y <= 0 are dropped (not drawn, not in the tooltip or table). */
|
|
47
48
|
yScale?: 'linear' | 'log';
|
|
49
|
+
/** Called with the point (`index` into `points`, `x`, `y`, `value` = y, `group`, `label`) when it is clicked (the nearest point within 28px) or Enter / Space is pressed on the point reached with the arrow keys. Also makes the table view's rows buttons. Sets a pointer cursor on the plot. */
|
|
50
|
+
onselect?: (d: ChartSelection) => void;
|
|
48
51
|
} = $props();
|
|
49
52
|
|
|
50
53
|
const xLog = $derived(xScaleKind === 'log');
|
|
@@ -89,6 +92,8 @@
|
|
|
89
92
|
const readout = (p: (typeof vis)[number]) =>
|
|
90
93
|
`${p.label ?? 'Point'}${p.group ? `, ${p.group}` : ''}: ${xLabel ?? 'x'} ${fx(p.x)}, ${yLabel ?? 'y'} ${fy(p.y)}${p.size != null ? `, ${sizeLabel.toLowerCase()} ${sizeFormat(p.size)}` : ''}`;
|
|
91
94
|
|
|
95
|
+
const pick = (p: ScatterPoint, index: number) => onselect?.({ index, x: p.x, y: p.y, value: p.y, group: p.group, label: p.label });
|
|
96
|
+
|
|
92
97
|
// height='fill': plot = container height minus the frame's own chrome (title, legend, key, padding)
|
|
93
98
|
let wrap = $state<HTMLElement>();
|
|
94
99
|
let fillH = $state(280);
|
|
@@ -110,7 +115,7 @@
|
|
|
110
115
|
|
|
111
116
|
<!-- svelte-ignore a11y_no_noninteractive_tabindex, a11y_no_noninteractive_element_interactions -->
|
|
112
117
|
<svg
|
|
113
|
-
width={w} {height} role="group" tabindex="0"
|
|
118
|
+
width={w} {height} role="group" tabindex="0" class:act={!!onselect}
|
|
114
119
|
aria-label="{title ?? 'Scatter chart'}, {vis.length} points. Use arrow keys to move between points."
|
|
115
120
|
onpointermove={(e) => {
|
|
116
121
|
const r = e.currentTarget.getBoundingClientRect();
|
|
@@ -119,6 +124,7 @@
|
|
|
119
124
|
vis.forEach((p, i) => { const d = (X(p.x) - px) ** 2 + (Y(p.y) - py) ** 2; if (d < bd) { bd = d; best = i; } });
|
|
120
125
|
active = best < 0 ? null : best;
|
|
121
126
|
}}
|
|
127
|
+
onclick={onselect && (() => { if (cur) pick(cur, cur.idx); })}
|
|
122
128
|
onpointerleave={() => (active = null)}
|
|
123
129
|
onblur={() => (active = null)}
|
|
124
130
|
onkeydown={(e) => {
|
|
@@ -129,6 +135,7 @@
|
|
|
129
135
|
else if (e.key === 'Home') active = 0;
|
|
130
136
|
else if (e.key === 'End') active = n - 1;
|
|
131
137
|
else if (e.key === 'Escape') active = null;
|
|
138
|
+
else if ((e.key === 'Enter' || e.key === ' ') && onselect && cur) pick(cur, cur.idx);
|
|
132
139
|
else return;
|
|
133
140
|
e.preventDefault();
|
|
134
141
|
}}
|
|
@@ -166,8 +173,8 @@
|
|
|
166
173
|
<table>
|
|
167
174
|
<thead><tr><th>Point</th>{#if groups.length}<th>Group</th>{/if}<th>{xLabel ?? 'X'}</th><th>{yLabel ?? 'Y'}</th>{#if hasSize}<th>{sizeLabel}</th>{/if}</tr></thead>
|
|
168
175
|
<tbody>
|
|
169
|
-
{#each points.filter(placeable) as p}
|
|
170
|
-
<tr><td>{p.label ?? '—'}</td>{#if groups.length}<td>{p.group ?? '—'}</td>{/if}<td>{fx(p.x)}</td><td>{fy(p.y)}</td>{#if hasSize}<td>{p.size == null ? '—' : sizeFormat(p.size)}</td>{/if}</tr>
|
|
176
|
+
{#each points.map((p, i) => ({ p, i })).filter((r) => placeable(r.p)) as { p, i }}
|
|
177
|
+
<tr><td>{#if onselect}<button type="button" class="act" onclick={() => pick(p, i)}>{p.label ?? '—'}</button>{:else}{p.label ?? '—'}{/if}</td>{#if groups.length}<td>{p.group ?? '—'}</td>{/if}<td>{fx(p.x)}</td><td>{fy(p.y)}</td>{#if hasSize}<td>{p.size == null ? '—' : sizeFormat(p.size)}</td>{/if}</tr>
|
|
171
178
|
{/each}
|
|
172
179
|
</tbody>
|
|
173
180
|
</table>
|
|
@@ -184,6 +191,7 @@
|
|
|
184
191
|
<style>
|
|
185
192
|
.fillwrap { height: 100%; min-height: 0; overflow: hidden; }
|
|
186
193
|
svg { display: block; outline: none; overflow: visible; }
|
|
194
|
+
svg.act { cursor: pointer; }
|
|
187
195
|
svg:focus-visible { border-radius: var(--tr-radius-sm); box-shadow: var(--tr-focus-ring); }
|
|
188
196
|
.grid { stroke: var(--tr-outline-variant); stroke-opacity: 0.6; stroke-width: 1; }
|
|
189
197
|
.tick { fill: var(--tr-on-surface-variant); font-size: var(--tr-text-xs); font-variant-numeric: tabular-nums; }
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ScatterPoint } from './types.js';
|
|
1
|
+
import type { ChartSelection, ScatterPoint } from './types.js';
|
|
2
2
|
type $$ComponentProps = {
|
|
3
3
|
/** `size` encodes a third value as marker diameter; `group` picks the (fixed-order, max 8) palette colour and legend entry. */
|
|
4
4
|
points: ScatterPoint[];
|
|
@@ -25,6 +25,8 @@ type $$ComponentProps = {
|
|
|
25
25
|
xScale?: 'linear' | 'log';
|
|
26
26
|
/** `'log'` plots y on a base-10 log axis with ticks at powers of 10. Points with y <= 0 are dropped (not drawn, not in the tooltip or table). */
|
|
27
27
|
yScale?: 'linear' | 'log';
|
|
28
|
+
/** Called with the point (`index` into `points`, `x`, `y`, `value` = y, `group`, `label`) when it is clicked (the nearest point within 28px) or Enter / Space is pressed on the point reached with the arrow keys. Also makes the table view's rows buttons. Sets a pointer cursor on the plot. */
|
|
29
|
+
onselect?: (d: ChartSelection) => void;
|
|
28
30
|
};
|
|
29
31
|
declare const ScatterChart: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
30
32
|
type ScatterChart = ReturnType<typeof ScatterChart>;
|
package/dist/index.d.ts
CHANGED
|
@@ -9,5 +9,5 @@ export { default as StatTile } from './StatTile.svelte';
|
|
|
9
9
|
export { default as ColorScale } from './ColorScale.svelte';
|
|
10
10
|
export { seriesColor, niceTicks, linear, gradientFrom } from './util.js';
|
|
11
11
|
export type { ScaleKind } from './util.js';
|
|
12
|
-
export type { Series, ScatterPoint, Band, ReferenceLine } from './types.js';
|
|
12
|
+
export type { Series, ScatterPoint, Band, ReferenceLine, ChartSelection } from './types.js';
|
|
13
13
|
export type { DensityPoint, DensityHighlight, DensityHover } from './density.js';
|
package/dist/types.d.ts
CHANGED
|
@@ -32,3 +32,26 @@ export type ReferenceLine = {
|
|
|
32
32
|
label?: string;
|
|
33
33
|
axis?: 'y';
|
|
34
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* What `onselect` reports when a bar, point, slice, histogram bin or line position is clicked, or activated with
|
|
37
|
+
* Enter / Space from the keyboard focus. `index` and `value` are always set; the rest depends on the chart:
|
|
38
|
+
* - `BarChart`: `category` and `series` of the bar, `index` = the category's index, `value` = the raw value.
|
|
39
|
+
* - `LineChart`: `x` (a Date when `x` holds Dates) and `index` of the nearest x, `series` = the series whose point
|
|
40
|
+
* is nearest the pointer (the first visible series with a value, from the keyboard), `value` = its raw value.
|
|
41
|
+
* - `ScatterChart`: `index` into `points`, `x`, `y` (also `value`), `group` and `label` of the point.
|
|
42
|
+
* - `DonutChart`: `category` = the slice name, `index` into `slices` (the folded "Other" slice reports the index
|
|
43
|
+
* it takes in the chart, 7), `value` = the slice value.
|
|
44
|
+
* - `Histogram`: `index` of the bin, `x0` / `x1` = its range, `x` = `x0`, `value` = its count.
|
|
45
|
+
*/
|
|
46
|
+
export type ChartSelection = {
|
|
47
|
+
index: number;
|
|
48
|
+
value: number;
|
|
49
|
+
series?: string;
|
|
50
|
+
category?: string;
|
|
51
|
+
x?: number | Date;
|
|
52
|
+
y?: number;
|
|
53
|
+
group?: string;
|
|
54
|
+
label?: string;
|
|
55
|
+
x0?: number;
|
|
56
|
+
x1?: number;
|
|
57
|
+
};
|
package/dist/util.d.ts
CHANGED
|
@@ -40,3 +40,5 @@ export declare const isDisplaced: (y: number, placed: number, threshold: number)
|
|
|
40
40
|
export declare const estimateLabelWidth: (name: string, fontPx?: number) => number;
|
|
41
41
|
/** Bar with a rounded data-end, square against the baseline. `y0` is the baseline, `y1` the data end. */
|
|
42
42
|
export declare function barPath(x: number, y0: number, y1: number, w: number, r: number, round?: boolean): string;
|
|
43
|
+
/** A keydown handler that runs `fn` on Enter or Space (and stops the page scrolling on Space). */
|
|
44
|
+
export declare const activate: (fn: () => void) => (e: KeyboardEvent) => void;
|
package/dist/util.js
CHANGED
|
@@ -94,3 +94,10 @@ export function barPath(x, y0, y1, w, r, round = true) {
|
|
|
94
94
|
? `M${x},${y0}V${top + rr}Q${x},${top} ${x + rr},${top}H${x + w - rr}Q${x + w},${top} ${x + w},${top + rr}V${y0}Z`
|
|
95
95
|
: `M${x},${y0}V${top + h - rr}Q${x},${top + h} ${x + rr},${top + h}H${x + w - rr}Q${x + w},${top + h} ${x + w},${top + h - rr}V${y0}Z`;
|
|
96
96
|
}
|
|
97
|
+
/** A keydown handler that runs `fn` on Enter or Space (and stops the page scrolling on Space). */
|
|
98
|
+
export const activate = (fn) => (e) => {
|
|
99
|
+
if (e.key !== 'Enter' && e.key !== ' ')
|
|
100
|
+
return;
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
fn();
|
|
103
|
+
};
|