@ruckc/truss-chart 1.48.1 → 1.50.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.
@@ -1,5 +1,37 @@
1
+ <script module lang="ts">
2
+ import { FRAME_DEFAULTS, type ChartFrameLabels } from './ChartFrame.svelte';
3
+
4
+ export type DonutChartLabels = ChartFrameLabels & {
5
+ /** Name of the chart in its accessible name when there is no `title`. */
6
+ name: string;
7
+ /** Accessible name of the chart: its title (or `name`) and the hint that a table view exists. */
8
+ tableHint: (name: string) => string;
9
+ /** Text in the centre when `centerLabel` is not set (the total of the visible slices). */
10
+ total: string;
11
+ /** Name of the slice that folds the slices past the eighth. */
12
+ other: string;
13
+ /** Header of the slice-name column in the table view. */
14
+ slice: string;
15
+ /** Header of the value column in the table view. */
16
+ value: string;
17
+ /** Header of the percentage column in the table view. */
18
+ share: string;
19
+ };
20
+ const DEFAULTS: DonutChartLabels = {
21
+ ...FRAME_DEFAULTS,
22
+ name: 'Donut chart',
23
+ tableHint: (name) => `${name}. A table view is available.`,
24
+ total: 'Total',
25
+ other: 'Other',
26
+ slice: 'Slice',
27
+ value: 'Value',
28
+ share: 'Share'
29
+ };
30
+ </script>
31
+
1
32
  <script lang="ts">
2
33
  import ChartFrame from './ChartFrame.svelte';
34
+ import { useLabels } from './labels.js';
3
35
  import { watchFill } from './fill.js';
4
36
  import type { ChartSelection } from './types.js';
5
37
  import { activate, fmtValue, seriesColor, str, MAX_SERIES } from './util.js';
@@ -11,8 +43,9 @@
11
43
  size = 200,
12
44
  height,
13
45
  format = fmtValue,
14
- centerLabel = 'Total',
15
- onselect
46
+ centerLabel,
47
+ onselect,
48
+ labels
16
49
  }: {
17
50
  slices: { name: string; value: number }[];
18
51
  title?: string;
@@ -27,11 +60,16 @@
27
60
  */
28
61
  height?: number | 'fill';
29
62
  format?: (n: number) => string;
63
+ /** Text under the figure in the centre when no slice is hovered (default: "Total"). */
30
64
  centerLabel?: string;
31
65
  /** 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
66
  onselect?: (d: ChartSelection) => void;
67
+ /** Replaces the built-in texts (translation). Missing keys keep the defaults. */
68
+ labels?: Partial<DonutChartLabels>;
33
69
  } = $props();
34
70
 
71
+ const L = useLabels('DonutChart', DEFAULTS, () => labels);
72
+
35
73
  let hidden = $state<string[]>([]);
36
74
  let active = $state<string | null>(null);
37
75
 
@@ -41,7 +79,7 @@
41
79
  if (sorted.length <= MAX_SERIES) return sorted.map((s) => ({ ...s, color: seriesColor(s.i) }));
42
80
  const head = sorted.slice(0, MAX_SERIES - 1).map((s) => ({ ...s, color: seriesColor(s.i) }));
43
81
  const rest = sorted.slice(MAX_SERIES - 1).reduce((a, s) => a + s.value, 0);
44
- return [...head, { name: 'Other', value: rest, i: MAX_SERIES - 1, color: 'var(--tr-outline)' }];
82
+ return [...head, { name: L.other, value: rest, i: MAX_SERIES - 1, color: 'var(--tr-outline)' }];
45
83
  });
46
84
  const vis = $derived(base.filter((s) => !hidden.includes(s.name)));
47
85
  const total = $derived(vis.reduce((a, s) => a + s.value, 0));
@@ -85,12 +123,12 @@
85
123
  </script>
86
124
 
87
125
  {#snippet chart()}
88
- <ChartFrame {title} {subtitle} {items} ontoggle={toggle}>
126
+ <ChartFrame {title} {subtitle} {items} ontoggle={toggle} labels={{ chart: L.chart, table: L.table, legend: L.legend }}>
89
127
  {#snippet plot(w)}
90
128
  {@const size = diameter(w)}
91
129
  {@const arcs = arcsFor(size)}
92
130
  <div class="wrap">
93
- <svg width={size} height={size} role="img" aria-label="{title ?? 'Donut chart'}. A table view is available.">
131
+ <svg width={size} height={size} role="img" aria-label={L.tableHint(title ?? L.name)}>
94
132
  {#each arcs as a (a.name)}
95
133
  <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
96
134
  <path
@@ -104,7 +142,7 @@
104
142
  {/each}
105
143
  <text class="big" x={size / 2} y={size / 2 - 2} text-anchor="middle">{format(focus?.value ?? total)}</text>
106
144
  <text class="small" x={size / 2} y={size / 2 + 16} text-anchor="middle">
107
- {focus ? `${focus.name} · ${pct(focus.value)}%` : centerLabel}
145
+ {focus ? `${focus.name} · ${pct(focus.value)}%` : (centerLabel ?? L.total)}
108
146
  </text>
109
147
  </svg>
110
148
  </div>
@@ -112,7 +150,7 @@
112
150
 
113
151
  {#snippet table()}
114
152
  <table>
115
- <thead><tr><th>Slice</th><th>Value</th><th>Share</th></tr></thead>
153
+ <thead><tr><th>{L.slice}</th><th>{L.value}</th><th>{L.share}</th></tr></thead>
116
154
  <tbody>
117
155
  {#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}
118
156
  </tbody>
@@ -1,3 +1,20 @@
1
+ import { type ChartFrameLabels } from './ChartFrame.svelte';
2
+ export type DonutChartLabels = ChartFrameLabels & {
3
+ /** Name of the chart in its accessible name when there is no `title`. */
4
+ name: string;
5
+ /** Accessible name of the chart: its title (or `name`) and the hint that a table view exists. */
6
+ tableHint: (name: string) => string;
7
+ /** Text in the centre when `centerLabel` is not set (the total of the visible slices). */
8
+ total: string;
9
+ /** Name of the slice that folds the slices past the eighth. */
10
+ other: string;
11
+ /** Header of the slice-name column in the table view. */
12
+ slice: string;
13
+ /** Header of the value column in the table view. */
14
+ value: string;
15
+ /** Header of the percentage column in the table view. */
16
+ share: string;
17
+ };
1
18
  import type { ChartSelection } from './types.js';
2
19
  type $$ComponentProps = {
3
20
  slices: {
@@ -16,9 +33,12 @@ type $$ComponentProps = {
16
33
  */
17
34
  height?: number | 'fill';
18
35
  format?: (n: number) => string;
36
+ /** Text under the figure in the centre when no slice is hovered (default: "Total"). */
19
37
  centerLabel?: string;
20
38
  /** 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
39
  onselect?: (d: ChartSelection) => void;
40
+ /** Replaces the built-in texts (translation). Missing keys keep the defaults. */
41
+ labels?: Partial<DonutChartLabels>;
22
42
  };
23
43
  declare const DonutChart: import("svelte").Component<$$ComponentProps, {}, "">;
24
44
  type DonutChart = ReturnType<typeof DonutChart>;
@@ -1,6 +1,35 @@
1
+ <script module lang="ts">
2
+ import { FRAME_DEFAULTS, type ChartFrameLabels } from './ChartFrame.svelte';
3
+
4
+ export type HistogramLabels = ChartFrameLabels & {
5
+ /** Name of the chart in its accessible name when there is no `title`. */
6
+ name: string;
7
+ /** Accessible name of the chart: its title (or `name`) and the keyboard instructions. */
8
+ keys: (name: string) => string;
9
+ /** Tooltip line: the bin's share of all values, `share` a whole percentage. */
10
+ shareOf: (share: number, total: number) => string;
11
+ /** Header of the range column in the table view. */
12
+ range: string;
13
+ /** Header of the count column in the table view. */
14
+ count: string;
15
+ /** Header of the percentage column in the table view. */
16
+ share: string;
17
+ };
18
+ const DEFAULTS: HistogramLabels = {
19
+ ...FRAME_DEFAULTS,
20
+ name: 'Histogram',
21
+ keys: (name) => `${name}. Arrow keys move between bins, Enter selects a bin, Escape clears the selection.`,
22
+ shareOf: (share, total) => `${share}% of ${total}`,
23
+ range: 'Range',
24
+ count: 'Count',
25
+ share: 'Share'
26
+ };
27
+ </script>
28
+
1
29
  <script lang="ts">
2
30
  import { startDrag } from '@ruckc/truss-ui';
3
31
  import ChartFrame from './ChartFrame.svelte';
32
+ import { useLabels } from './labels.js';
4
33
  import { watchFill } from './fill.js';
5
34
  import Tooltip from './Tooltip.svelte';
6
35
  import type { ChartSelection } from './types.js';
@@ -17,7 +46,8 @@
17
46
  xFormat = fmtValue,
18
47
  range = $bindable(null),
19
48
  onbrush,
20
- onselect
49
+ onselect,
50
+ labels
21
51
  }: {
22
52
  values: number[];
23
53
  /** Approximate bin count, or 'auto' (Sturges). Edges snap to nice numbers. */
@@ -38,8 +68,12 @@
38
68
  onbrush?: (range: [number, number] | null) => void;
39
69
  /** 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
70
  onselect?: (d: ChartSelection) => void;
71
+ /** Replaces the built-in texts (translation). Missing keys keep the defaults. */
72
+ labels?: Partial<HistogramLabels>;
41
73
  } = $props();
42
74
 
75
+ const L = useLabels('Histogram', DEFAULTS, () => labels);
76
+
43
77
  let hover = $state<number | null>(null);
44
78
  let drag = $state<{ a: number; b: number } | null>(null);
45
79
  let stop: (() => void) | undefined; // the running brush, abandoned if the chart unmounts mid-drag
@@ -88,7 +122,7 @@
88
122
  </script>
89
123
 
90
124
  {#snippet chart()}
91
- <ChartFrame {title} {subtitle}>
125
+ <ChartFrame {title} {subtitle} labels={{ chart: L.chart, table: L.table, legend: L.legend }}>
92
126
  {#snippet plot(w)}
93
127
  {@const height = H}
94
128
  {@const m = { top: 12, right: 12, bottom: 28, left: 46 }}
@@ -101,7 +135,7 @@
101
135
  <!-- svelte-ignore a11y_no_noninteractive_tabindex, a11y_no_noninteractive_element_interactions -->
102
136
  <svg
103
137
  width={w} {height} role="group" tabindex="0"
104
- aria-label="{title ?? 'Histogram'}. Arrow keys move between bins, Enter selects a bin, Escape clears the selection."
138
+ aria-label={L.keys(title ?? L.name)}
105
139
  onpointerleave={() => (hover = null)}
106
140
  onblur={() => (hover = null)}
107
141
  onkeydown={(e) => {
@@ -167,14 +201,14 @@
167
201
  {#if hover != null && !drag}
168
202
  <Tooltip x={X(edges[hover] + step / 2)} y={Y(counts[hover]) - 4} width={w}>
169
203
  <div class="head">{span(hover)}</div>
170
- <div class="row"><b>{counts[hover]}</b><span class="name">{`${share(counts[hover])}% of ${total}`}</span></div>
204
+ <div class="row"><b>{counts[hover]}</b><span class="name">{L.shareOf(share(counts[hover]), total)}</span></div>
171
205
  </Tooltip>
172
206
  {/if}
173
207
  {/snippet}
174
208
 
175
209
  {#snippet table()}
176
210
  <table>
177
- <thead><tr><th>Range</th><th>Count</th><th>Share</th></tr></thead>
211
+ <thead><tr><th>{L.range}</th><th>{L.count}</th><th>{L.share}</th></tr></thead>
178
212
  <tbody>
179
213
  {#each counts as c, i}
180
214
  <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>
@@ -1,3 +1,18 @@
1
+ import { type ChartFrameLabels } from './ChartFrame.svelte';
2
+ export type HistogramLabels = ChartFrameLabels & {
3
+ /** Name of the chart in its accessible name when there is no `title`. */
4
+ name: string;
5
+ /** Accessible name of the chart: its title (or `name`) and the keyboard instructions. */
6
+ keys: (name: string) => string;
7
+ /** Tooltip line: the bin's share of all values, `share` a whole percentage. */
8
+ shareOf: (share: number, total: number) => string;
9
+ /** Header of the range column in the table view. */
10
+ range: string;
11
+ /** Header of the count column in the table view. */
12
+ count: string;
13
+ /** Header of the percentage column in the table view. */
14
+ share: string;
15
+ };
1
16
  import type { ChartSelection } from './types.js';
2
17
  type $$ComponentProps = {
3
18
  values: number[];
@@ -19,6 +34,8 @@ type $$ComponentProps = {
19
34
  onbrush?: (range: [number, number] | null) => void;
20
35
  /** 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
36
  onselect?: (d: ChartSelection) => void;
37
+ /** Replaces the built-in texts (translation). Missing keys keep the defaults. */
38
+ labels?: Partial<HistogramLabels>;
22
39
  };
23
40
  declare const Histogram: import("svelte").Component<$$ComponentProps, {}, "range">;
24
41
  type Histogram = ReturnType<typeof Histogram>;
@@ -1,5 +1,37 @@
1
+ <script module lang="ts">
2
+ import { FRAME_DEFAULTS, type ChartFrameLabels } from './ChartFrame.svelte';
3
+
4
+ export type LineChartLabels = ChartFrameLabels & {
5
+ /** Name of the chart in its accessible name when there is no `title`. */
6
+ name: string;
7
+ /** Accessible name of the chart: its title (or `name`), the keyboard hint, and `lines` (the reference lines, from `referenceLine`, joined by commas; empty when there are none). */
8
+ keys: (name: string, lines: string) => string;
9
+ /** One reference line in that accessible name: its label and the value it marks. */
10
+ referenceLine: (label: string, value: string) => string;
11
+ /** Name of a reference line that has no label (accessible name and table row header). */
12
+ reference: string;
13
+ /** Name of the shaded band when `band.label` is not set. */
14
+ band: string;
15
+ /** Header of the x column in the table view when x is a date. */
16
+ date: string;
17
+ /** Header of the x column in the table view when x is a number. */
18
+ x: string;
19
+ };
20
+ const DEFAULTS: LineChartLabels = {
21
+ ...FRAME_DEFAULTS,
22
+ name: 'Line chart',
23
+ keys: (name, lines) => `${name}. Use left and right arrow keys to read values.${lines && ` Reference lines: ${lines}.`}`,
24
+ referenceLine: (label, value) => `${label} at ${value}`,
25
+ reference: 'Reference',
26
+ band: 'Band',
27
+ date: 'Date',
28
+ x: 'X'
29
+ };
30
+ </script>
31
+
1
32
  <script lang="ts">
2
33
  import ChartFrame from './ChartFrame.svelte';
34
+ import { useLabels } from './labels.js';
3
35
  import { cssvars } from './cssvars.js';
4
36
  import Tooltip from './Tooltip.svelte';
5
37
  import { watchFill } from './fill.js';
@@ -22,7 +54,8 @@
22
54
  referenceLines = [],
23
55
  xScale = 'linear',
24
56
  yScale: yScaleKind = 'linear',
25
- onselect
57
+ onselect,
58
+ labels
26
59
  }: {
27
60
  /** Shared x positions: numbers or Dates. */
28
61
  x: (number | Date)[];
@@ -75,8 +108,12 @@
75
108
  yScale?: 'linear' | 'log';
76
109
  /** 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. */
77
110
  onselect?: (d: ChartSelection) => void;
111
+ /** Replaces the built-in texts (translation). Missing keys keep the defaults. */
112
+ labels?: Partial<LineChartLabels>;
78
113
  } = $props();
79
114
 
115
+ const L = useLabels('LineChart', DEFAULTS, () => labels);
116
+
80
117
  const yFormat = $derived(yFormatProp ?? fmtValue);
81
118
  const yTick = $derived(yFormatProp ?? fmtAxis);
82
119
 
@@ -98,11 +135,11 @@
98
135
  const bandData = $derived(band && !isStack ? { lo: pick(band.lo), hi: pick(band.hi) } : null);
99
136
  const bandAt = (i: number) => { const lo = bandData?.lo[i], hi = bandData?.hi[i]; return lo != null && hi != null ? { lo, hi } : null; };
100
137
  const bandColor = $derived(band?.color ?? all[0]?.color ?? 'var(--tr-outline)');
101
- const bandLabel = $derived(band?.label ?? 'Band');
138
+ const bandLabel = $derived(band?.label ?? L.band);
102
139
  const refs = $derived(referenceLines.filter((r) => !yLog || isPos(r.value)));
103
140
  const xSpan = $derived(Math.max(...xs) - Math.min(...xs));
104
141
  const fx = (v: number) => (xFormat ? xFormat(isDate ? new Date(v) : v) : fmtX(v, xSpan, isDate));
105
- const refsText = $derived(refs.map((r) => `${r.label ?? 'Reference'} at ${yFormat(r.value)}`).join(', '));
142
+ const refsText = $derived(refs.map((r) => L.referenceLine(r.label ?? L.reference, yFormat(r.value))).join(', '));
106
143
  const rangeText = (b: { lo: number; hi: number }) => `${yFormat(b.lo)} – ${yFormat(b.hi)}`;
107
144
 
108
145
  let wrap = $state<HTMLElement>();
@@ -186,7 +223,7 @@
186
223
  </script>
187
224
 
188
225
  {#snippet chart()}
189
- <ChartFrame {title} {subtitle} {items} ontoggle={toggle}>
226
+ <ChartFrame {title} {subtitle} {items} ontoggle={toggle} labels={{ chart: L.chart, table: L.table, legend: L.legend }}>
190
227
  {#snippet plot(w)}
191
228
  {@const height = H}
192
229
  {@const Y = (yLog ? logLinear : linear)(yDom.min, yDom.max, height - 26, 12)}
@@ -206,7 +243,7 @@
206
243
  <!-- svelte-ignore a11y_no_noninteractive_tabindex, a11y_no_noninteractive_element_interactions -->
207
244
  <svg
208
245
  bind:this={svgEl} width={w} {height} role="group" tabindex="0" class:act={!!onselect}
209
- aria-label="{title ?? 'Line chart'}. Use left and right arrow keys to read values.{refsText && ` Reference lines: ${refsText}.`}"
246
+ aria-label={L.keys(title ?? L.name, refsText)}
210
247
  onpointermove={(e) => {
211
248
  const px = e.clientX - e.currentTarget.getBoundingClientRect().left;
212
249
  let best = 0;
@@ -298,7 +335,7 @@
298
335
 
299
336
  {#snippet table()}
300
337
  <table>
301
- <thead><tr><th>{isDate ? 'Date' : 'X'}</th>{#each all as s}<th>{s.name}</th>{/each}{#if bandData}<th>{bandLabel}</th>{/if}</tr></thead>
338
+ <thead><tr><th>{isDate ? L.date : L.x}</th>{#each all as s}<th>{s.name}</th>{/each}{#if bandData}<th>{bandLabel}</th>{/if}</tr></thead>
302
339
  <tbody>
303
340
  {#each xs as v, i}
304
341
  <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>
@@ -306,7 +343,7 @@
306
343
  </tbody>
307
344
  {#if refs.length}
308
345
  <tfoot>
309
- {#each refs as r}<tr><th scope="row">{r.label ?? 'Reference'}</th><td colspan={all.length + (bandData ? 1 : 0)}>{yFormat(r.value)}</td></tr>{/each}
346
+ {#each refs as r}<tr><th scope="row">{r.label ?? L.reference}</th><td colspan={all.length + (bandData ? 1 : 0)}>{yFormat(r.value)}</td></tr>{/each}
310
347
  </tfoot>
311
348
  {/if}
312
349
  </table>
@@ -1,3 +1,20 @@
1
+ import { type ChartFrameLabels } from './ChartFrame.svelte';
2
+ export type LineChartLabels = ChartFrameLabels & {
3
+ /** Name of the chart in its accessible name when there is no `title`. */
4
+ name: string;
5
+ /** Accessible name of the chart: its title (or `name`), the keyboard hint, and `lines` (the reference lines, from `referenceLine`, joined by commas; empty when there are none). */
6
+ keys: (name: string, lines: string) => string;
7
+ /** One reference line in that accessible name: its label and the value it marks. */
8
+ referenceLine: (label: string, value: string) => string;
9
+ /** Name of a reference line that has no label (accessible name and table row header). */
10
+ reference: string;
11
+ /** Name of the shaded band when `band.label` is not set. */
12
+ band: string;
13
+ /** Header of the x column in the table view when x is a date. */
14
+ date: string;
15
+ /** Header of the x column in the table view when x is a number. */
16
+ x: string;
17
+ };
1
18
  import type { Series, Band, ReferenceLine, ChartSelection } from './types.js';
2
19
  type $$ComponentProps = {
3
20
  /** Shared x positions: numbers or Dates. */
@@ -51,6 +68,8 @@ type $$ComponentProps = {
51
68
  yScale?: 'linear' | 'log';
52
69
  /** 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
70
  onselect?: (d: ChartSelection) => void;
71
+ /** Replaces the built-in texts (translation). Missing keys keep the defaults. */
72
+ labels?: Partial<LineChartLabels>;
54
73
  };
55
74
  declare const LineChart: import("svelte").Component<$$ComponentProps, {}, "">;
56
75
  type LineChart = ReturnType<typeof LineChart>;
@@ -1,5 +1,43 @@
1
+ <script module lang="ts">
2
+ import { FRAME_DEFAULTS, type ChartFrameLabels } from './ChartFrame.svelte';
3
+
4
+ export type ScatterChartLabels = ChartFrameLabels & {
5
+ /** Name of the chart in its accessible name when there is no `title`. */
6
+ name: string;
7
+ /** Accessible name of the chart: its title (or `name`), how many points are visible, and the keyboard hint. */
8
+ keys: (name: string, count: number) => string;
9
+ /** Name of a point that has no `label`. */
10
+ point: string;
11
+ /** Header of the group column in the table view. */
12
+ group: string;
13
+ /** Name of the x axis in the readout and tooltip when there is no `xLabel`. */
14
+ x: string;
15
+ /** Name of the y axis in the readout and tooltip when there is no `yLabel`. */
16
+ y: string;
17
+ /** Header of the x column in the table view when there is no `xLabel`. */
18
+ xHeader: string;
19
+ /** Header of the y column in the table view when there is no `yLabel`. */
20
+ yHeader: string;
21
+ /** Name of the size dimension when there is no `sizeLabel`. */
22
+ size: string;
23
+ };
24
+ const DEFAULTS: ScatterChartLabels = {
25
+ ...FRAME_DEFAULTS,
26
+ name: 'Scatter chart',
27
+ keys: (name, count) => `${name}, ${count} points. Use arrow keys to move between points.`,
28
+ point: 'Point',
29
+ group: 'Group',
30
+ x: 'x',
31
+ y: 'y',
32
+ xHeader: 'X',
33
+ yHeader: 'Y',
34
+ size: 'Size'
35
+ };
36
+ </script>
37
+
1
38
  <script lang="ts">
2
39
  import ChartFrame from './ChartFrame.svelte';
40
+ import { useLabels } from './labels.js';
3
41
  import { watchFill } from './fill.js';
4
42
  import Tooltip from './Tooltip.svelte';
5
43
  import type { ChartSelection, ScatterPoint } from './types.js';
@@ -12,14 +50,15 @@
12
50
  height = 280,
13
51
  xLabel,
14
52
  yLabel,
15
- sizeLabel = 'Size',
53
+ sizeLabel: sizeLabelProp,
16
54
  sizeRange = [8, 24],
17
55
  xFormat,
18
56
  yFormat,
19
57
  sizeFormat = fmtValue,
20
58
  xScale: xScaleKind = 'linear',
21
59
  yScale: yScaleKind = 'linear',
22
- onselect
60
+ onselect,
61
+ labels
23
62
  }: {
24
63
  /** `size` encodes a third value as marker diameter; `group` picks the (fixed-order, max 8) palette colour and legend entry. */
25
64
  points: ScatterPoint[];
@@ -48,8 +87,13 @@
48
87
  yScale?: 'linear' | 'log';
49
88
  /** 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
89
  onselect?: (d: ChartSelection) => void;
90
+ /** Replaces the built-in texts (translation). Missing keys keep the defaults. */
91
+ labels?: Partial<ScatterChartLabels>;
51
92
  } = $props();
52
93
 
94
+ const L = useLabels('ScatterChart', DEFAULTS, () => labels);
95
+ const sizeLabel = $derived(sizeLabelProp ?? L.size);
96
+
53
97
  const xLog = $derived(xScaleKind === 'log');
54
98
  const yLog = $derived(yScaleKind === 'log');
55
99
  const xTick = $derived(xFormat ?? fmtAxis);
@@ -90,7 +134,7 @@
90
134
  const yAx = $derived(axis(vis.map((p) => p.y), yLog, 5));
91
135
  const cur = $derived(active != null ? (vis[active] ?? null) : null);
92
136
  const readout = (p: (typeof vis)[number]) =>
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)}` : ''}`;
137
+ `${p.label ?? L.point}${p.group ? `, ${p.group}` : ''}: ${xLabel ?? L.x} ${fx(p.x)}, ${yLabel ?? L.y} ${fy(p.y)}${p.size != null ? `, ${sizeLabel.toLowerCase()} ${sizeFormat(p.size)}` : ''}`;
94
138
 
95
139
  const pick = (p: ScatterPoint, index: number) => onselect?.({ index, x: p.x, y: p.y, value: p.y, group: p.group, label: p.label });
96
140
 
@@ -106,7 +150,7 @@
106
150
  </script>
107
151
 
108
152
  {#snippet chart()}
109
- <ChartFrame {title} {subtitle} {items} ontoggle={toggle}>
153
+ <ChartFrame {title} {subtitle} {items} ontoggle={toggle} labels={{ chart: L.chart, table: L.table, legend: L.legend }}>
110
154
  {#snippet plot(w)}
111
155
  {@const height = H}
112
156
  {@const m = { top: 14, right: 16, bottom: xLabel ? 44 : 28, left: yLabel ? 62 : 48 }}
@@ -116,7 +160,7 @@
116
160
  <!-- svelte-ignore a11y_no_noninteractive_tabindex, a11y_no_noninteractive_element_interactions -->
117
161
  <svg
118
162
  width={w} {height} role="group" tabindex="0" class:act={!!onselect}
119
- aria-label="{title ?? 'Scatter chart'}, {vis.length} points. Use arrow keys to move between points."
163
+ aria-label={L.keys(title ?? L.name, vis.length)}
120
164
  onpointermove={(e) => {
121
165
  const r = e.currentTarget.getBoundingClientRect();
122
166
  const px = e.clientX - r.left, py = e.clientY - r.top;
@@ -161,9 +205,9 @@
161
205
 
162
206
  {#if cur}
163
207
  <Tooltip x={X(cur.x)} y={Y(cur.y)} width={w}>
164
- <div class="head">{cur.label ?? 'Point'}{#if cur.group} · {cur.group}{/if}</div>
165
- <div class="row"><b>{fx(cur.x)}</b><span class="name">{xLabel ?? 'x'}</span></div>
166
- <div class="row"><b>{fy(cur.y)}</b><span class="name">{yLabel ?? 'y'}</span></div>
208
+ <div class="head">{cur.label ?? L.point}{#if cur.group} · {cur.group}{/if}</div>
209
+ <div class="row"><b>{fx(cur.x)}</b><span class="name">{xLabel ?? L.x}</span></div>
210
+ <div class="row"><b>{fy(cur.y)}</b><span class="name">{yLabel ?? L.y}</span></div>
167
211
  {#if cur.size != null}<div class="row"><b>{sizeFormat(cur.size)}</b><span class="name">{sizeLabel}</span></div>{/if}
168
212
  </Tooltip>
169
213
  {/if}
@@ -171,7 +215,7 @@
171
215
 
172
216
  {#snippet table()}
173
217
  <table>
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>
218
+ <thead><tr><th>{L.point}</th>{#if groups.length}<th>{L.group}</th>{/if}<th>{xLabel ?? L.xHeader}</th><th>{yLabel ?? L.yHeader}</th>{#if hasSize}<th>{sizeLabel}</th>{/if}</tr></thead>
175
219
  <tbody>
176
220
  {#each points.map((p, i) => ({ p, i })).filter((r) => placeable(r.p)) as { p, i }}
177
221
  <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>
@@ -1,3 +1,24 @@
1
+ import { type ChartFrameLabels } from './ChartFrame.svelte';
2
+ export type ScatterChartLabels = ChartFrameLabels & {
3
+ /** Name of the chart in its accessible name when there is no `title`. */
4
+ name: string;
5
+ /** Accessible name of the chart: its title (or `name`), how many points are visible, and the keyboard hint. */
6
+ keys: (name: string, count: number) => string;
7
+ /** Name of a point that has no `label`. */
8
+ point: string;
9
+ /** Header of the group column in the table view. */
10
+ group: string;
11
+ /** Name of the x axis in the readout and tooltip when there is no `xLabel`. */
12
+ x: string;
13
+ /** Name of the y axis in the readout and tooltip when there is no `yLabel`. */
14
+ y: string;
15
+ /** Header of the x column in the table view when there is no `xLabel`. */
16
+ xHeader: string;
17
+ /** Header of the y column in the table view when there is no `yLabel`. */
18
+ yHeader: string;
19
+ /** Name of the size dimension when there is no `sizeLabel`. */
20
+ size: string;
21
+ };
1
22
  import type { ChartSelection, ScatterPoint } from './types.js';
2
23
  type $$ComponentProps = {
3
24
  /** `size` encodes a third value as marker diameter; `group` picks the (fixed-order, max 8) palette colour and legend entry. */
@@ -27,6 +48,8 @@ type $$ComponentProps = {
27
48
  yScale?: 'linear' | 'log';
28
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. */
29
50
  onselect?: (d: ChartSelection) => void;
51
+ /** Replaces the built-in texts (translation). Missing keys keep the defaults. */
52
+ labels?: Partial<ScatterChartLabels>;
30
53
  };
31
54
  declare const ScatterChart: import("svelte").Component<$$ComponentProps, {}, "">;
32
55
  type ScatterChart = ReturnType<typeof ScatterChart>;
@@ -1,4 +1,27 @@
1
+ <script module lang="ts">
2
+ export type StatTileLabels = {
3
+ /** Start of the accessible name: the tile's label and its value. */
4
+ value: (label: string, value: string) => string;
5
+ /** Comparison text when `deltaLabel` is not set. */
6
+ vsPrevious: string;
7
+ /** Accessible text when the change is zero: `deltaLabel` is the comparison text. */
8
+ noChange: (deltaLabel: string) => string;
9
+ /** Accessible text for a change: its direction, its size in percent (a number, without the sign), the comparison text, and whether it is the good direction. */
10
+ change: (up: boolean, percent: string, deltaLabel: string, favourable: boolean) => string;
11
+ /** Accessible text for the trend line: how many values, the first and the last. */
12
+ trend: (count: number, from: string, to: string) => string;
13
+ };
14
+ const DEFAULTS: StatTileLabels = {
15
+ value: (label, value) => `${label}: ${value}.`,
16
+ vsPrevious: 'vs previous',
17
+ noChange: (deltaLabel) => `No change ${deltaLabel}.`,
18
+ change: (up, percent, deltaLabel, favourable) => `${up ? 'Up' : 'Down'} ${percent}% ${deltaLabel}, ${favourable ? 'favourable' : 'unfavourable'}.`,
19
+ trend: (count, from, to) => `Trend over ${count} points, from ${from} to ${to}.`
20
+ };
21
+ </script>
22
+
1
23
  <script lang="ts">
24
+ import { useLabels } from './labels.js';
2
25
  import Sparkline from './Sparkline.svelte';
3
26
  import { fmtValue } from './util.js';
4
27
 
@@ -7,10 +30,11 @@
7
30
  value,
8
31
  format,
9
32
  delta,
10
- deltaLabel = 'vs previous',
33
+ deltaLabel: deltaLabelProp,
11
34
  deltaGoodDirection = 'up',
12
35
  spark,
13
- size = 'md'
36
+ size = 'md',
37
+ labels
14
38
  }: {
15
39
  label: string;
16
40
  value: number | string;
@@ -18,6 +42,7 @@
18
42
  format?: (n: number) => string;
19
43
  /** Change vs the previous period, in percent (4.2 means +4.2%). */
20
44
  delta?: number;
45
+ /** Comparison text next to the change (default: "vs previous"). */
21
46
  deltaLabel?: string;
22
47
  /** Which direction of change is good: picks the success or danger colour. The arrow and signed text carry the meaning without colour. */
23
48
  deltaGoodDirection?: 'up' | 'down';
@@ -25,8 +50,13 @@
25
50
  spark?: number[];
26
51
  /** `'auto'` makes the tile a width-based size container: the value size and padding follow the tile's width (`cqi`, clamped to 20-56px), so it needs no fixed size in a grid cell; it ignores height so it never collapses. `'sm'` uses `--tr-text-xl` for the value and tighter padding. The value size can also be set from outside with the `--trc-stat-value-size` custom property (default 28px). */
27
52
  size?: 'sm' | 'md' | 'auto';
53
+ /** Replaces the built-in texts (translation). Missing keys keep the defaults. */
54
+ labels?: Partial<StatTileLabels>;
28
55
  } = $props();
29
56
 
57
+ const L = useLabels('StatTile', DEFAULTS, () => labels);
58
+ const deltaLabel = $derived(deltaLabelProp ?? L.vsPrevious);
59
+
30
60
  const shown = $derived(typeof value === 'number' ? (format ?? fmtValue)(value) : value);
31
61
  const dir = $derived(delta == null || delta === 0 ? 'flat' : delta > 0 ? 'up' : 'down');
32
62
  const tone = $derived(dir === 'flat' ? 'neutral' : dir === deltaGoodDirection ? 'good' : 'bad');
@@ -34,9 +64,13 @@
34
64
  // built in script: interpolating in markup would add an unreachable `?? ''` branch
35
65
  const deltaClass = $derived(`delta ${tone}`);
36
66
  const summary = $derived(
37
- `${label}: ${shown}.` +
38
- (delta == null ? '' : dir === 'flat' ? ` No change ${deltaLabel}.` : ` ${dir === 'up' ? 'Up' : 'Down'} ${fmtValue(Math.abs(delta))}% ${deltaLabel}, ${tone === 'good' ? 'favourable' : 'unfavourable'}.`) +
39
- (spark && spark.length > 1 ? ` Trend over ${spark.length} points, from ${fmtValue(spark[0])} to ${fmtValue(spark.at(-1)!)}.` : '')
67
+ [
68
+ L.value(label, String(shown)),
69
+ delta == null ? '' : dir === 'flat' ? L.noChange(deltaLabel) : L.change(dir === 'up', fmtValue(Math.abs(delta)), deltaLabel, tone === 'good'),
70
+ spark && spark.length > 1 ? L.trend(spark.length, fmtValue(spark[0]), fmtValue(spark.at(-1)!)) : ''
71
+ ]
72
+ .filter(Boolean)
73
+ .join(' ')
40
74
  );
41
75
  </script>
42
76