dssoca 0.10.0 → 0.11.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/components/BoxPlot.svelte +427 -0
- package/dist/components/BoxPlot.svelte.d.ts +36 -0
- package/dist/components/BumpChart.svelte +351 -0
- package/dist/components/BumpChart.svelte.d.ts +32 -0
- package/dist/components/Heatmap.svelte +333 -0
- package/dist/components/Heatmap.svelte.d.ts +30 -0
- package/dist/components/ScatterPlot.svelte +401 -0
- package/dist/components/ScatterPlot.svelte.d.ts +55 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/package.json +1 -1
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
export interface BumpSeries {
|
|
3
|
+
/** Competitor label shown at the last node and in the tooltip / accessible name. */
|
|
4
|
+
label: string
|
|
5
|
+
/** 1-based finishing ranks; `ranks[i]` is the rank at stage `i` (length must match `stages`). */
|
|
6
|
+
ranks: number[]
|
|
7
|
+
/** Optional per-series colour; falls back to the cycling palette. */
|
|
8
|
+
color?: string
|
|
9
|
+
}
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<script lang="ts">
|
|
13
|
+
import { scalePoint, scaleLinear } from 'd3-scale'
|
|
14
|
+
import { line as shapeLine } from 'd3-shape'
|
|
15
|
+
import { max } from 'd3-array'
|
|
16
|
+
import { resolveComponentSize, type Size } from '../config.js'
|
|
17
|
+
|
|
18
|
+
interface Props {
|
|
19
|
+
/** One series per competitor; each carries a `label`, `ranks` and optional `color`. */
|
|
20
|
+
series: BumpSeries[]
|
|
21
|
+
/** Ordered x-axis category labels (e.g. ['G1','G2','G3','G4']). */
|
|
22
|
+
stages: string[]
|
|
23
|
+
/** Draw each series label at its last node (direct labelling). Default true. */
|
|
24
|
+
showLabels?: boolean
|
|
25
|
+
/** Intrinsic drawing height in px. Default 280. */
|
|
26
|
+
height?: number
|
|
27
|
+
/** Intrinsic drawing width in px. Default 480. */
|
|
28
|
+
width?: number
|
|
29
|
+
/** Stretch to the container width via the viewBox (width:100%). */
|
|
30
|
+
fluid?: boolean
|
|
31
|
+
/** Reveal a tooltip on hover/focus of a node. Default true. */
|
|
32
|
+
tooltip?: boolean
|
|
33
|
+
/** SR summary + accessible name; auto-generated when absent. */
|
|
34
|
+
summary?: string
|
|
35
|
+
/** Token size (sm|md|lg); inherits the global size when unset. */
|
|
36
|
+
size?: Size
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let {
|
|
40
|
+
series,
|
|
41
|
+
stages,
|
|
42
|
+
showLabels = true,
|
|
43
|
+
height = 280,
|
|
44
|
+
width = 480,
|
|
45
|
+
fluid = false,
|
|
46
|
+
tooltip = true,
|
|
47
|
+
summary,
|
|
48
|
+
size,
|
|
49
|
+
}: Props = $props()
|
|
50
|
+
|
|
51
|
+
const PALETTE = [
|
|
52
|
+
'var(--ss-primary)',
|
|
53
|
+
'var(--ss-blue)',
|
|
54
|
+
'var(--ss-purple)',
|
|
55
|
+
'var(--ss-cyan)',
|
|
56
|
+
'var(--ss-yellow)',
|
|
57
|
+
'var(--ss-lime)',
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
const M = { top: 12, right: 56, bottom: 28, left: 32 }
|
|
61
|
+
|
|
62
|
+
const cleanSeries = $derived((series ?? []).filter((s) => s && Array.isArray(s.ranks)))
|
|
63
|
+
const cleanStages = $derived(stages ?? [])
|
|
64
|
+
const isEmpty = $derived(cleanSeries.length === 0 || cleanStages.length === 0)
|
|
65
|
+
|
|
66
|
+
function colorFor(s: BumpSeries, i: number): string {
|
|
67
|
+
return s.color ?? PALETTE[i % PALETTE.length]
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const innerW = $derived(Math.max(0, width - M.left - M.right))
|
|
71
|
+
const innerH = $derived(Math.max(0, height - M.top - M.bottom))
|
|
72
|
+
|
|
73
|
+
// Highest rank present across all series; default to the competitor count.
|
|
74
|
+
const maxRank = $derived(
|
|
75
|
+
max(cleanSeries.flatMap((s) => s.ranks.filter((r) => Number.isFinite(r)))) ??
|
|
76
|
+
cleanSeries.length,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
// x over stages; y maps rank 1 → top (0) and maxRank → bottom (innerH).
|
|
80
|
+
const xScaleObj = $derived(
|
|
81
|
+
scalePoint<string>().domain(cleanStages).range([0, innerW]).padding(0.5),
|
|
82
|
+
)
|
|
83
|
+
const yScaleObj = $derived(scaleLinear().domain([1, maxRank]).range([0, innerH]))
|
|
84
|
+
|
|
85
|
+
// Integer rank ticks 1..maxRank.
|
|
86
|
+
const rankTicks = $derived(
|
|
87
|
+
Array.from({ length: Math.max(1, Math.round(maxRank)) }, (_, i) => i + 1),
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
// One node per (series, stage) where the rank is finite.
|
|
91
|
+
const seriesNodes = $derived(
|
|
92
|
+
cleanSeries.map((s, i) => {
|
|
93
|
+
const color = colorFor(s, i)
|
|
94
|
+
const nodes = cleanStages
|
|
95
|
+
.map((stage, j) => {
|
|
96
|
+
const rank = s.ranks[j]
|
|
97
|
+
if (!Number.isFinite(rank)) return null
|
|
98
|
+
return {
|
|
99
|
+
cx: xScaleObj(stage) ?? 0,
|
|
100
|
+
cy: yScaleObj(rank),
|
|
101
|
+
stage,
|
|
102
|
+
rank,
|
|
103
|
+
label: s.label,
|
|
104
|
+
color,
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
.filter((n): n is NonNullable<typeof n> => n !== null)
|
|
108
|
+
return { label: s.label, color, nodes }
|
|
109
|
+
}),
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
const lineGen = $derived(
|
|
113
|
+
shapeLine<{ cx: number; cy: number }>()
|
|
114
|
+
.x((n) => n.cx)
|
|
115
|
+
.y((n) => n.cy),
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
const linePaths = $derived(
|
|
119
|
+
seriesNodes.map((s) => ({
|
|
120
|
+
d: lineGen(s.nodes) ?? '',
|
|
121
|
+
color: s.color,
|
|
122
|
+
label: s.label,
|
|
123
|
+
})),
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
// Last node of each series for direct labelling.
|
|
127
|
+
const endLabels = $derived.by(() => {
|
|
128
|
+
if (!showLabels) return [] as { x: number; y: number; color: string; label: string }[]
|
|
129
|
+
return seriesNodes
|
|
130
|
+
.filter((s) => s.nodes.length > 0)
|
|
131
|
+
.map((s) => {
|
|
132
|
+
const last = s.nodes[s.nodes.length - 1]
|
|
133
|
+
return { x: last.cx + 8, y: last.cy, color: s.color, label: s.label }
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
const autoSummary = $derived.by(() => {
|
|
138
|
+
if (isEmpty) return 'No data'
|
|
139
|
+
const stageNames = cleanStages.join(', ')
|
|
140
|
+
return `Bump chart with ${cleanSeries.length} series over ${cleanStages.length} stages (${stageNames}).`
|
|
141
|
+
})
|
|
142
|
+
const srText = $derived(summary ?? autoSummary)
|
|
143
|
+
|
|
144
|
+
const resolvedSize = $derived(resolveComponentSize('Chart', size))
|
|
145
|
+
|
|
146
|
+
let active = $state<{
|
|
147
|
+
label: string
|
|
148
|
+
stage: string
|
|
149
|
+
rank: number
|
|
150
|
+
cx: number
|
|
151
|
+
cy: number
|
|
152
|
+
} | null>(null)
|
|
153
|
+
function show(label: string, stage: string, rank: number, cx: number, cy: number) {
|
|
154
|
+
if (tooltip) active = { label, stage, rank, cx, cy }
|
|
155
|
+
}
|
|
156
|
+
function hide() {
|
|
157
|
+
active = null
|
|
158
|
+
}
|
|
159
|
+
function nodeLabel(label: string, stage: string, rank: number): string {
|
|
160
|
+
return `${label}: ${stage}, rank ${rank}`
|
|
161
|
+
}
|
|
162
|
+
</script>
|
|
163
|
+
|
|
164
|
+
<div class="ss-bump" class:fluid data-size-variant={resolvedSize} role="group" aria-label={srText}>
|
|
165
|
+
{#if isEmpty}
|
|
166
|
+
<span class="empty" aria-hidden="true">—</span>
|
|
167
|
+
<span class="sr-only">No data</span>
|
|
168
|
+
{:else}
|
|
169
|
+
<div class="plot">
|
|
170
|
+
<svg
|
|
171
|
+
class="canvas"
|
|
172
|
+
viewBox="0 0 {width} {height}"
|
|
173
|
+
width={fluid ? undefined : width}
|
|
174
|
+
height={fluid ? undefined : height}
|
|
175
|
+
preserveAspectRatio="xMidYMid meet"
|
|
176
|
+
role="presentation"
|
|
177
|
+
>
|
|
178
|
+
<g transform="translate({M.left},{M.top})">
|
|
179
|
+
<!-- gridlines + y axis (rank ticks) -->
|
|
180
|
+
{#each rankTicks as t}
|
|
181
|
+
<line class="grid" x1="0" x2={innerW} y1={yScaleObj(t)} y2={yScaleObj(t)} />
|
|
182
|
+
<text class="tick y" x="-6" y={yScaleObj(t)} dy="0.32em" text-anchor="end">{t}</text>
|
|
183
|
+
{/each}
|
|
184
|
+
<line class="axis" x1="0" x2="0" y1="0" y2={innerH} />
|
|
185
|
+
<line class="axis" x1="0" x2={innerW} y1={innerH} y2={innerH} />
|
|
186
|
+
|
|
187
|
+
<!-- x axis (stage labels) -->
|
|
188
|
+
{#each cleanStages as stage}
|
|
189
|
+
<text class="tick x" x={xScaleObj(stage)} y={innerH + 16} text-anchor="middle"
|
|
190
|
+
>{stage}</text
|
|
191
|
+
>
|
|
192
|
+
{/each}
|
|
193
|
+
|
|
194
|
+
<!-- connecting lines -->
|
|
195
|
+
{#each linePaths as p}
|
|
196
|
+
<path class="line" d={p.d} style="stroke:{p.color}" />
|
|
197
|
+
{/each}
|
|
198
|
+
|
|
199
|
+
<!-- nodes -->
|
|
200
|
+
{#each seriesNodes as s}
|
|
201
|
+
{#each s.nodes as n}
|
|
202
|
+
<circle
|
|
203
|
+
class="node"
|
|
204
|
+
cx={n.cx}
|
|
205
|
+
cy={n.cy}
|
|
206
|
+
r="5"
|
|
207
|
+
style="fill:{n.color}"
|
|
208
|
+
tabindex="0"
|
|
209
|
+
role="button"
|
|
210
|
+
aria-label={nodeLabel(n.label, n.stage, n.rank)}
|
|
211
|
+
onfocus={() => show(n.label, n.stage, n.rank, n.cx, n.cy)}
|
|
212
|
+
onblur={hide}
|
|
213
|
+
onmouseenter={() => show(n.label, n.stage, n.rank, n.cx, n.cy)}
|
|
214
|
+
onmouseleave={hide}
|
|
215
|
+
/>
|
|
216
|
+
{/each}
|
|
217
|
+
{/each}
|
|
218
|
+
|
|
219
|
+
<!-- direct series labels -->
|
|
220
|
+
{#each endLabels as l}
|
|
221
|
+
<text class="series-label" x={l.x} y={l.y} dy="0.32em" style="fill:{l.color}"
|
|
222
|
+
>{l.label}</text
|
|
223
|
+
>
|
|
224
|
+
{/each}
|
|
225
|
+
</g>
|
|
226
|
+
</svg>
|
|
227
|
+
|
|
228
|
+
{#if tooltip && active}
|
|
229
|
+
<div
|
|
230
|
+
class="tooltip"
|
|
231
|
+
role="status"
|
|
232
|
+
style="left:{((active.cx + M.left) / width) * 100}%;top:{((active.cy + M.top) / height) *
|
|
233
|
+
100}%"
|
|
234
|
+
>
|
|
235
|
+
<span class="t-label">{active.label}</span>
|
|
236
|
+
<span class="t-val">{active.stage} · rank {active.rank}</span>
|
|
237
|
+
</div>
|
|
238
|
+
{/if}
|
|
239
|
+
</div>
|
|
240
|
+
{/if}
|
|
241
|
+
</div>
|
|
242
|
+
|
|
243
|
+
<style>.ss-bump {
|
|
244
|
+
display: inline-flex;
|
|
245
|
+
flex-direction: column;
|
|
246
|
+
gap: var(--ss-gap-sm);
|
|
247
|
+
font-family: var(--ss-font-body);
|
|
248
|
+
color: var(--ss-fg);
|
|
249
|
+
max-width: 100%;
|
|
250
|
+
}
|
|
251
|
+
.ss-bump.fluid {
|
|
252
|
+
display: flex;
|
|
253
|
+
width: 100%;
|
|
254
|
+
}
|
|
255
|
+
.ss-bump .plot {
|
|
256
|
+
position: relative;
|
|
257
|
+
width: 100%;
|
|
258
|
+
}
|
|
259
|
+
.ss-bump .canvas {
|
|
260
|
+
display: block;
|
|
261
|
+
max-width: 100%;
|
|
262
|
+
height: auto;
|
|
263
|
+
overflow: visible;
|
|
264
|
+
}
|
|
265
|
+
.ss-bump.fluid .canvas {
|
|
266
|
+
width: 100%;
|
|
267
|
+
}
|
|
268
|
+
.ss-bump .empty {
|
|
269
|
+
align-self: center;
|
|
270
|
+
margin: auto;
|
|
271
|
+
padding: var(--ss-gap-sm);
|
|
272
|
+
color: var(--ss-fg-muted);
|
|
273
|
+
font-family: var(--ss-font-mono);
|
|
274
|
+
line-height: 1;
|
|
275
|
+
min-height: var(--ss-chart-h, 240px);
|
|
276
|
+
display: flex;
|
|
277
|
+
align-items: center;
|
|
278
|
+
}
|
|
279
|
+
.ss-bump .sr-only {
|
|
280
|
+
position: absolute;
|
|
281
|
+
width: 1px;
|
|
282
|
+
height: 1px;
|
|
283
|
+
padding: 0;
|
|
284
|
+
margin: -1px;
|
|
285
|
+
overflow: hidden;
|
|
286
|
+
clip: rect(0, 0, 0, 0);
|
|
287
|
+
white-space: nowrap;
|
|
288
|
+
border: 0;
|
|
289
|
+
}
|
|
290
|
+
.ss-bump .grid {
|
|
291
|
+
stroke: var(--ss-line);
|
|
292
|
+
stroke-width: 1;
|
|
293
|
+
opacity: 0.5;
|
|
294
|
+
shape-rendering: crispEdges;
|
|
295
|
+
}
|
|
296
|
+
.ss-bump .axis {
|
|
297
|
+
stroke: var(--ss-line);
|
|
298
|
+
stroke-width: 1;
|
|
299
|
+
shape-rendering: crispEdges;
|
|
300
|
+
}
|
|
301
|
+
.ss-bump .tick {
|
|
302
|
+
font-family: var(--ss-font-mono);
|
|
303
|
+
font-size: var(--ss-ui-xs);
|
|
304
|
+
fill: var(--ss-fg-muted);
|
|
305
|
+
}
|
|
306
|
+
.ss-bump .line {
|
|
307
|
+
fill: none;
|
|
308
|
+
stroke-width: 2;
|
|
309
|
+
stroke-linejoin: round;
|
|
310
|
+
stroke-linecap: round;
|
|
311
|
+
}
|
|
312
|
+
.ss-bump .node {
|
|
313
|
+
cursor: pointer;
|
|
314
|
+
transition: r var(--ss-dur-fast) var(--ss-ease);
|
|
315
|
+
}
|
|
316
|
+
.ss-bump .node:hover, .ss-bump .node:focus-visible {
|
|
317
|
+
outline: none;
|
|
318
|
+
r: 7;
|
|
319
|
+
}
|
|
320
|
+
.ss-bump .node:focus-visible {
|
|
321
|
+
stroke: var(--ss-fg);
|
|
322
|
+
stroke-width: 2;
|
|
323
|
+
}
|
|
324
|
+
.ss-bump .series-label {
|
|
325
|
+
font-family: var(--ss-font-mono);
|
|
326
|
+
font-size: var(--ss-ui-xs);
|
|
327
|
+
pointer-events: none;
|
|
328
|
+
}
|
|
329
|
+
.ss-bump .tooltip {
|
|
330
|
+
position: absolute;
|
|
331
|
+
transform: translate(-50%, calc(-100% - 8px));
|
|
332
|
+
pointer-events: none;
|
|
333
|
+
background: var(--ss-bg-elev);
|
|
334
|
+
border: 1px solid var(--ss-line-strong);
|
|
335
|
+
padding: var(--ss-gap-sm);
|
|
336
|
+
font-size: var(--ss-ui-xs);
|
|
337
|
+
line-height: 1.3;
|
|
338
|
+
white-space: nowrap;
|
|
339
|
+
display: flex;
|
|
340
|
+
flex-direction: column;
|
|
341
|
+
gap: 2px;
|
|
342
|
+
z-index: 2;
|
|
343
|
+
}
|
|
344
|
+
.ss-bump .tooltip .t-label {
|
|
345
|
+
font-family: var(--ss-font-mono);
|
|
346
|
+
color: var(--ss-fg);
|
|
347
|
+
}
|
|
348
|
+
.ss-bump .tooltip .t-val {
|
|
349
|
+
font-family: var(--ss-font-mono);
|
|
350
|
+
color: var(--ss-fg-muted);
|
|
351
|
+
}</style>
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export interface BumpSeries {
|
|
2
|
+
/** Competitor label shown at the last node and in the tooltip / accessible name. */
|
|
3
|
+
label: string;
|
|
4
|
+
/** 1-based finishing ranks; `ranks[i]` is the rank at stage `i` (length must match `stages`). */
|
|
5
|
+
ranks: number[];
|
|
6
|
+
/** Optional per-series colour; falls back to the cycling palette. */
|
|
7
|
+
color?: string;
|
|
8
|
+
}
|
|
9
|
+
import { type Size } from '../config.js';
|
|
10
|
+
interface Props {
|
|
11
|
+
/** One series per competitor; each carries a `label`, `ranks` and optional `color`. */
|
|
12
|
+
series: BumpSeries[];
|
|
13
|
+
/** Ordered x-axis category labels (e.g. ['G1','G2','G3','G4']). */
|
|
14
|
+
stages: string[];
|
|
15
|
+
/** Draw each series label at its last node (direct labelling). Default true. */
|
|
16
|
+
showLabels?: boolean;
|
|
17
|
+
/** Intrinsic drawing height in px. Default 280. */
|
|
18
|
+
height?: number;
|
|
19
|
+
/** Intrinsic drawing width in px. Default 480. */
|
|
20
|
+
width?: number;
|
|
21
|
+
/** Stretch to the container width via the viewBox (width:100%). */
|
|
22
|
+
fluid?: boolean;
|
|
23
|
+
/** Reveal a tooltip on hover/focus of a node. Default true. */
|
|
24
|
+
tooltip?: boolean;
|
|
25
|
+
/** SR summary + accessible name; auto-generated when absent. */
|
|
26
|
+
summary?: string;
|
|
27
|
+
/** Token size (sm|md|lg); inherits the global size when unset. */
|
|
28
|
+
size?: Size;
|
|
29
|
+
}
|
|
30
|
+
declare const BumpChart: import("svelte").Component<Props, {}, "">;
|
|
31
|
+
type BumpChart = ReturnType<typeof BumpChart>;
|
|
32
|
+
export default BumpChart;
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { scaleLinear } from 'd3-scale'
|
|
3
|
+
import { extent } from 'd3-array'
|
|
4
|
+
import { resolveComponentSize, type Size } from '../config.js'
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
/** Row labels, top→bottom. */
|
|
8
|
+
rows: string[]
|
|
9
|
+
/** Column labels, left→right. */
|
|
10
|
+
columns: string[]
|
|
11
|
+
/** values[r][c]; `null` renders a blank cell excluded from the colour scale (e.g. the diagonal). */
|
|
12
|
+
values: (number | null)[][]
|
|
13
|
+
/** Value range mapped to colour intensity; defaults to the [min, max] of all non-null values. */
|
|
14
|
+
domain?: [number, number]
|
|
15
|
+
/** Format a cell value for its text + tooltip. */
|
|
16
|
+
valueFormat?: (v: number) => string
|
|
17
|
+
/** Render the value text inside each cell. Default true. */
|
|
18
|
+
showValues?: boolean
|
|
19
|
+
/** X axis caption (drawn under the column headers). */
|
|
20
|
+
xLabel?: string
|
|
21
|
+
/** Y axis caption (drawn rotated beside the row headers). */
|
|
22
|
+
yLabel?: string
|
|
23
|
+
/** Square cell size in px. Default 48. */
|
|
24
|
+
cellSize?: number
|
|
25
|
+
/** Reveal a tooltip on hover/focus of a cell. Default true. */
|
|
26
|
+
tooltip?: boolean
|
|
27
|
+
/** SR summary + accessible name; auto-generated when absent. */
|
|
28
|
+
summary?: string
|
|
29
|
+
/** Token size (sm|md|lg); inherits the global size when unset. */
|
|
30
|
+
size?: Size
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let {
|
|
34
|
+
rows,
|
|
35
|
+
columns,
|
|
36
|
+
values,
|
|
37
|
+
domain,
|
|
38
|
+
valueFormat,
|
|
39
|
+
showValues = true,
|
|
40
|
+
xLabel,
|
|
41
|
+
yLabel,
|
|
42
|
+
cellSize = 48,
|
|
43
|
+
tooltip = true,
|
|
44
|
+
summary,
|
|
45
|
+
size,
|
|
46
|
+
}: Props = $props()
|
|
47
|
+
|
|
48
|
+
const cleanRows = $derived(rows ?? [])
|
|
49
|
+
const cleanCols = $derived(columns ?? [])
|
|
50
|
+
const isEmpty = $derived(cleanRows.length === 0 || cleanCols.length === 0)
|
|
51
|
+
|
|
52
|
+
const fmt = $derived(valueFormat ?? ((v: number) => String(v)))
|
|
53
|
+
|
|
54
|
+
// Header gutters + optional axis-label room.
|
|
55
|
+
const headLeft = $derived(72 + (yLabel ? 20 : 0))
|
|
56
|
+
const headTop = 28
|
|
57
|
+
const labelBottom = $derived(xLabel ? 24 : 0)
|
|
58
|
+
|
|
59
|
+
const gridW = $derived(cleanCols.length * cellSize)
|
|
60
|
+
const gridH = $derived(cleanRows.length * cellSize)
|
|
61
|
+
const width = $derived(headLeft + gridW)
|
|
62
|
+
const height = $derived(headTop + gridH + labelBottom)
|
|
63
|
+
|
|
64
|
+
function valueAt(r: number, c: number): number | null {
|
|
65
|
+
const v = values?.[r]?.[c]
|
|
66
|
+
return typeof v === 'number' && Number.isFinite(v) ? v : null
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const allValues = $derived.by(() => {
|
|
70
|
+
const out: number[] = []
|
|
71
|
+
for (let r = 0; r < cleanRows.length; r++) {
|
|
72
|
+
for (let c = 0; c < cleanCols.length; c++) {
|
|
73
|
+
const v = valueAt(r, c)
|
|
74
|
+
if (v !== null) out.push(v)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return out
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const dom = $derived.by<[number, number]>(() => {
|
|
81
|
+
if (domain) return domain
|
|
82
|
+
const e = extent(allValues) as [number, number]
|
|
83
|
+
if (e[0] === undefined) return [0, 1]
|
|
84
|
+
if (e[0] === e[1]) return [e[0], e[1] + 1]
|
|
85
|
+
return e
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
// Opacity on the primary token encodes intensity.
|
|
89
|
+
const opacityScale = $derived(scaleLinear().domain(dom).range([0.12, 1]).clamp(true))
|
|
90
|
+
|
|
91
|
+
const cells = $derived.by(() =>
|
|
92
|
+
cleanRows.flatMap((row, r) =>
|
|
93
|
+
cleanCols.map((col, c) => {
|
|
94
|
+
const v = valueAt(r, c)
|
|
95
|
+
return {
|
|
96
|
+
r,
|
|
97
|
+
c,
|
|
98
|
+
row,
|
|
99
|
+
col,
|
|
100
|
+
x: headLeft + c * cellSize,
|
|
101
|
+
y: headTop + r * cellSize,
|
|
102
|
+
value: v,
|
|
103
|
+
opacity: v === null ? 0 : opacityScale(v),
|
|
104
|
+
}
|
|
105
|
+
}),
|
|
106
|
+
),
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
const autoSummary = $derived.by(() => {
|
|
110
|
+
if (isEmpty) return 'No data'
|
|
111
|
+
return `Heatmap, ${cleanRows.length} rows × ${cleanCols.length} columns.`
|
|
112
|
+
})
|
|
113
|
+
const srText = $derived(summary ?? autoSummary)
|
|
114
|
+
|
|
115
|
+
const resolvedSize = $derived(resolveComponentSize('Chart', size))
|
|
116
|
+
|
|
117
|
+
let active = $state<{ row: string; col: string; value: number; cx: number; cy: number } | null>(
|
|
118
|
+
null,
|
|
119
|
+
)
|
|
120
|
+
function show(row: string, col: string, value: number, cx: number, cy: number) {
|
|
121
|
+
if (tooltip) active = { row, col, value, cx, cy }
|
|
122
|
+
}
|
|
123
|
+
function hide() {
|
|
124
|
+
active = null
|
|
125
|
+
}
|
|
126
|
+
function cellLabel(row: string, col: string, value: number): string {
|
|
127
|
+
return `${row} vs ${col}: ${fmt(value)}`
|
|
128
|
+
}
|
|
129
|
+
</script>
|
|
130
|
+
|
|
131
|
+
<div class="ss-heatmap" data-size-variant={resolvedSize} role="group" aria-label={srText}>
|
|
132
|
+
{#if isEmpty}
|
|
133
|
+
<span class="empty" aria-hidden="true">—</span>
|
|
134
|
+
<span class="sr-only">No data</span>
|
|
135
|
+
{:else}
|
|
136
|
+
<div class="plot">
|
|
137
|
+
<svg
|
|
138
|
+
class="canvas"
|
|
139
|
+
viewBox="0 0 {width} {height}"
|
|
140
|
+
{width}
|
|
141
|
+
{height}
|
|
142
|
+
preserveAspectRatio="xMidYMid meet"
|
|
143
|
+
role="presentation"
|
|
144
|
+
>
|
|
145
|
+
<!-- column headers -->
|
|
146
|
+
{#each cleanCols as col, c}
|
|
147
|
+
<text
|
|
148
|
+
class="header col"
|
|
149
|
+
x={headLeft + c * cellSize + cellSize / 2}
|
|
150
|
+
y={headTop - 8}
|
|
151
|
+
text-anchor="middle">{col}</text
|
|
152
|
+
>
|
|
153
|
+
{/each}
|
|
154
|
+
|
|
155
|
+
<!-- row headers -->
|
|
156
|
+
{#each cleanRows as row, r}
|
|
157
|
+
<text
|
|
158
|
+
class="header row"
|
|
159
|
+
x={headLeft - 8}
|
|
160
|
+
y={headTop + r * cellSize + cellSize / 2}
|
|
161
|
+
dy="0.32em"
|
|
162
|
+
text-anchor="end">{row}</text
|
|
163
|
+
>
|
|
164
|
+
{/each}
|
|
165
|
+
|
|
166
|
+
<!-- cells -->
|
|
167
|
+
{#each cells as cell}
|
|
168
|
+
{#if cell.value === null}
|
|
169
|
+
<rect
|
|
170
|
+
class="cell cell-empty"
|
|
171
|
+
x={cell.x}
|
|
172
|
+
y={cell.y}
|
|
173
|
+
width={cellSize}
|
|
174
|
+
height={cellSize}
|
|
175
|
+
aria-hidden="true"
|
|
176
|
+
/>
|
|
177
|
+
{:else}
|
|
178
|
+
<rect
|
|
179
|
+
class="cell"
|
|
180
|
+
x={cell.x}
|
|
181
|
+
y={cell.y}
|
|
182
|
+
width={cellSize}
|
|
183
|
+
height={cellSize}
|
|
184
|
+
fill="var(--ss-primary)"
|
|
185
|
+
fill-opacity={cell.opacity}
|
|
186
|
+
tabindex="0"
|
|
187
|
+
role="button"
|
|
188
|
+
aria-label={cellLabel(cell.row, cell.col, cell.value)}
|
|
189
|
+
onfocus={() => show(cell.row, cell.col, cell.value!, cell.x + cellSize / 2, cell.y)}
|
|
190
|
+
onblur={hide}
|
|
191
|
+
onmouseenter={() =>
|
|
192
|
+
show(cell.row, cell.col, cell.value!, cell.x + cellSize / 2, cell.y)}
|
|
193
|
+
onmouseleave={hide}
|
|
194
|
+
/>
|
|
195
|
+
{#if showValues}
|
|
196
|
+
<text
|
|
197
|
+
class="cell-text"
|
|
198
|
+
x={cell.x + cellSize / 2}
|
|
199
|
+
y={cell.y + cellSize / 2}
|
|
200
|
+
dy="0.32em"
|
|
201
|
+
text-anchor="middle">{fmt(cell.value)}</text
|
|
202
|
+
>
|
|
203
|
+
{/if}
|
|
204
|
+
{/if}
|
|
205
|
+
{/each}
|
|
206
|
+
|
|
207
|
+
<!-- axis captions -->
|
|
208
|
+
{#if xLabel}
|
|
209
|
+
<text class="axis-label x" x={headLeft + gridW / 2} y={height - 6} text-anchor="middle"
|
|
210
|
+
>{xLabel}</text
|
|
211
|
+
>
|
|
212
|
+
{/if}
|
|
213
|
+
{#if yLabel}
|
|
214
|
+
<text
|
|
215
|
+
class="axis-label y"
|
|
216
|
+
transform="translate(12,{headTop + gridH / 2}) rotate(-90)"
|
|
217
|
+
text-anchor="middle">{yLabel}</text
|
|
218
|
+
>
|
|
219
|
+
{/if}
|
|
220
|
+
</svg>
|
|
221
|
+
|
|
222
|
+
{#if tooltip && active}
|
|
223
|
+
<div
|
|
224
|
+
class="tooltip"
|
|
225
|
+
role="status"
|
|
226
|
+
style="left:{(active.cx / width) * 100}%;top:{(active.cy / height) * 100}%"
|
|
227
|
+
>
|
|
228
|
+
<span class="t-label">{active.row} · {active.col}</span>
|
|
229
|
+
<span class="t-val">{fmt(active.value)}</span>
|
|
230
|
+
</div>
|
|
231
|
+
{/if}
|
|
232
|
+
</div>
|
|
233
|
+
{/if}
|
|
234
|
+
</div>
|
|
235
|
+
|
|
236
|
+
<style>.ss-heatmap {
|
|
237
|
+
display: inline-flex;
|
|
238
|
+
flex-direction: column;
|
|
239
|
+
gap: var(--ss-gap-sm);
|
|
240
|
+
font-family: var(--ss-font-body);
|
|
241
|
+
color: var(--ss-fg);
|
|
242
|
+
max-width: 100%;
|
|
243
|
+
}
|
|
244
|
+
.ss-heatmap .plot {
|
|
245
|
+
position: relative;
|
|
246
|
+
width: 100%;
|
|
247
|
+
}
|
|
248
|
+
.ss-heatmap .canvas {
|
|
249
|
+
display: block;
|
|
250
|
+
max-width: 100%;
|
|
251
|
+
height: auto;
|
|
252
|
+
overflow: visible;
|
|
253
|
+
}
|
|
254
|
+
.ss-heatmap .empty {
|
|
255
|
+
align-self: center;
|
|
256
|
+
margin: auto;
|
|
257
|
+
padding: var(--ss-gap-sm);
|
|
258
|
+
color: var(--ss-fg-muted);
|
|
259
|
+
font-family: var(--ss-font-mono);
|
|
260
|
+
line-height: 1;
|
|
261
|
+
min-height: var(--ss-chart-h, 240px);
|
|
262
|
+
display: flex;
|
|
263
|
+
align-items: center;
|
|
264
|
+
}
|
|
265
|
+
.ss-heatmap .sr-only {
|
|
266
|
+
position: absolute;
|
|
267
|
+
width: 1px;
|
|
268
|
+
height: 1px;
|
|
269
|
+
padding: 0;
|
|
270
|
+
margin: -1px;
|
|
271
|
+
overflow: hidden;
|
|
272
|
+
clip: rect(0, 0, 0, 0);
|
|
273
|
+
white-space: nowrap;
|
|
274
|
+
border: 0;
|
|
275
|
+
}
|
|
276
|
+
.ss-heatmap .header {
|
|
277
|
+
font-family: var(--ss-font-mono);
|
|
278
|
+
font-size: var(--ss-ui-xs);
|
|
279
|
+
fill: var(--ss-fg-muted);
|
|
280
|
+
}
|
|
281
|
+
.ss-heatmap .axis-label {
|
|
282
|
+
font-family: var(--ss-font-mono);
|
|
283
|
+
font-size: var(--ss-ui-xs);
|
|
284
|
+
fill: var(--ss-fg-muted);
|
|
285
|
+
}
|
|
286
|
+
.ss-heatmap .cell {
|
|
287
|
+
stroke: var(--ss-line);
|
|
288
|
+
stroke-width: 1;
|
|
289
|
+
cursor: pointer;
|
|
290
|
+
shape-rendering: crispEdges;
|
|
291
|
+
transition: fill-opacity var(--ss-dur-fast) var(--ss-ease);
|
|
292
|
+
}
|
|
293
|
+
.ss-heatmap .cell:hover {
|
|
294
|
+
fill-opacity: 1;
|
|
295
|
+
}
|
|
296
|
+
.ss-heatmap .cell:focus-visible {
|
|
297
|
+
outline: none;
|
|
298
|
+
stroke: var(--ss-fg);
|
|
299
|
+
stroke-width: 2;
|
|
300
|
+
}
|
|
301
|
+
.ss-heatmap .cell-empty {
|
|
302
|
+
fill: none;
|
|
303
|
+
cursor: default;
|
|
304
|
+
}
|
|
305
|
+
.ss-heatmap .cell-text {
|
|
306
|
+
font-family: var(--ss-font-mono);
|
|
307
|
+
font-size: var(--ss-ui-xs);
|
|
308
|
+
fill: var(--ss-fg);
|
|
309
|
+
pointer-events: none;
|
|
310
|
+
}
|
|
311
|
+
.ss-heatmap .tooltip {
|
|
312
|
+
position: absolute;
|
|
313
|
+
transform: translate(-50%, calc(-100% - 8px));
|
|
314
|
+
pointer-events: none;
|
|
315
|
+
background: var(--ss-bg-elev);
|
|
316
|
+
border: 1px solid var(--ss-line-strong);
|
|
317
|
+
padding: var(--ss-gap-sm);
|
|
318
|
+
font-size: var(--ss-ui-xs);
|
|
319
|
+
line-height: 1.3;
|
|
320
|
+
white-space: nowrap;
|
|
321
|
+
display: flex;
|
|
322
|
+
flex-direction: column;
|
|
323
|
+
gap: 2px;
|
|
324
|
+
z-index: 2;
|
|
325
|
+
}
|
|
326
|
+
.ss-heatmap .tooltip .t-label {
|
|
327
|
+
font-family: var(--ss-font-mono);
|
|
328
|
+
color: var(--ss-fg);
|
|
329
|
+
}
|
|
330
|
+
.ss-heatmap .tooltip .t-val {
|
|
331
|
+
font-family: var(--ss-font-mono);
|
|
332
|
+
color: var(--ss-fg-muted);
|
|
333
|
+
}</style>
|