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.
@@ -0,0 +1,427 @@
1
+ <script module lang="ts">
2
+ export interface BoxGroup {
3
+ /** Group caption shown under its band and in the tooltip / accessible name. */
4
+ label: string
5
+ /** Raw values for this group; whiskers + quartiles are computed from these. */
6
+ values: number[]
7
+ /** Optional per-group colour; falls back to the cycling palette. */
8
+ color?: string
9
+ }
10
+ </script>
11
+
12
+ <script lang="ts">
13
+ import { scaleBand, scaleLinear } from 'd3-scale'
14
+ import { extent, quantile } from 'd3-array'
15
+ import { resolveComponentSize, type Size } from '../config.js'
16
+
17
+ interface Props {
18
+ /** One box per group. Each group has a `label`, `values`, and optional `color`. */
19
+ groups: BoxGroup[]
20
+ /** Overlay every value as a small jittered dot. Default true. */
21
+ showPoints?: boolean
22
+ /** Y axis caption (drawn rotated beside the axis). */
23
+ yLabel?: string
24
+ /** Force the y domain; defaults to the padded data extent across all values. */
25
+ yDomain?: [number, number]
26
+ /** Format a y value for ticks + tooltip. */
27
+ yFormat?: (y: number) => string
28
+ /** Intrinsic drawing height in px. Default 300. */
29
+ height?: number
30
+ /** Intrinsic drawing width in px. Default 420. */
31
+ width?: number
32
+ /** Stretch to the container width via the viewBox (width:100%). */
33
+ fluid?: boolean
34
+ /** Reveal a tooltip on hover/focus of a box. Default true. */
35
+ tooltip?: boolean
36
+ /** SR summary + accessible name; auto-generated when absent. */
37
+ summary?: string
38
+ /** Token size (sm|md|lg); inherits the global size when unset. */
39
+ size?: Size
40
+ }
41
+
42
+ let {
43
+ groups,
44
+ showPoints = true,
45
+ yLabel,
46
+ yDomain,
47
+ yFormat,
48
+ height = 300,
49
+ width = 420,
50
+ fluid = false,
51
+ tooltip = true,
52
+ summary,
53
+ size,
54
+ }: Props = $props()
55
+
56
+ const PALETTE = [
57
+ 'var(--ss-primary)',
58
+ 'var(--ss-blue)',
59
+ 'var(--ss-purple)',
60
+ 'var(--ss-cyan)',
61
+ 'var(--ss-yellow)',
62
+ 'var(--ss-lime)',
63
+ ]
64
+
65
+ const M = { top: 12, right: 16, bottom: 36, left: 48 }
66
+
67
+ // Keep only groups carrying at least one finite value.
68
+ const clean = $derived(
69
+ (groups ?? [])
70
+ .filter((g) => g && Array.isArray(g.values))
71
+ .map((g) => ({
72
+ label: g.label,
73
+ color: g.color,
74
+ values: g.values.filter((v) => Number.isFinite(v)),
75
+ }))
76
+ .filter((g) => g.values.length > 0),
77
+ )
78
+ const isEmpty = $derived(clean.length === 0)
79
+
80
+ function colorFor(g: { color?: string }, i: number): string {
81
+ return g.color ?? PALETTE[i % PALETTE.length]
82
+ }
83
+
84
+ const innerW = $derived(Math.max(0, width - M.left - M.right))
85
+ const innerH = $derived(Math.max(0, height - M.top - M.bottom))
86
+
87
+ const fmtY = $derived(yFormat ?? ((y: number) => String(y)))
88
+
89
+ /** Data extent padded by 8% each side; a flat axis is expanded to ±1. */
90
+ function paddedExtent(values: number[]): [number, number] {
91
+ const e = extent(values) as [number, number]
92
+ if (e[0] === undefined) return [0, 1]
93
+ if (e[0] === e[1]) return [e[0] - 1, e[1] + 1]
94
+ const pad = (e[1] - e[0]) * 0.08
95
+ return [e[0] - pad, e[1] + pad]
96
+ }
97
+
98
+ const allValues = $derived(clean.flatMap((g) => g.values))
99
+ const yDom = $derived(yDomain ?? paddedExtent(allValues))
100
+
101
+ const xScaleObj = $derived(
102
+ scaleBand<string>()
103
+ .domain(clean.map((g) => g.label))
104
+ .range([0, innerW])
105
+ .padding(0.3),
106
+ )
107
+ const yScaleObj = $derived(scaleLinear().domain(yDom).nice().range([innerH, 0]))
108
+
109
+ const yTicks = $derived(yScaleObj.ticks(5))
110
+
111
+ /** Deterministic horizontal offset for dot i within a band (no Math.random → SSR-safe). */
112
+ function jitter(i: number, bandW: number): number {
113
+ // Cheap integer hash → [0,1), mapped into the inner 70% of the band.
114
+ const h = (((Math.sin(i * 127.1) * 43758.5453) % 1) + 1) % 1
115
+ return (h - 0.5) * bandW * 0.7
116
+ }
117
+
118
+ // Five-number summary + Tukey whiskers per group.
119
+ const boxes = $derived.by(() => {
120
+ const bw = xScaleObj.bandwidth()
121
+ return clean.map((g, i) => {
122
+ const sorted = [...g.values].sort((a, b) => a - b)
123
+ const q1 = quantile(sorted, 0.25) as number
124
+ const median = quantile(sorted, 0.5) as number
125
+ const q3 = quantile(sorted, 0.75) as number
126
+ const iqr = q3 - q1
127
+ const loFence = q1 - 1.5 * iqr
128
+ const hiFence = q3 + 1.5 * iqr
129
+ const lo = sorted.find((v) => v >= loFence) ?? sorted[0]
130
+ const hi = [...sorted].reverse().find((v) => v <= hiFence) ?? sorted[sorted.length - 1]
131
+ const x = xScaleObj(g.label) ?? 0
132
+ const cx = x + bw / 2
133
+ const points = showPoints
134
+ ? g.values.map((v, j) => ({ cx: cx + jitter(j, bw), cy: yScaleObj(v) }))
135
+ : []
136
+ return {
137
+ label: g.label,
138
+ color: colorFor(g, i),
139
+ x,
140
+ cx,
141
+ w: bw,
142
+ q1,
143
+ median,
144
+ q3,
145
+ lo,
146
+ hi,
147
+ yQ1: yScaleObj(q1),
148
+ yMedian: yScaleObj(median),
149
+ yQ3: yScaleObj(q3),
150
+ yLo: yScaleObj(lo),
151
+ yHi: yScaleObj(hi),
152
+ points,
153
+ }
154
+ })
155
+ })
156
+
157
+ const autoSummary = $derived.by(() => {
158
+ if (isEmpty) return 'No data'
159
+ const names = clean.map((g) => g.label).join(', ')
160
+ return `Box plot with ${clean.length} groups (${names}).`
161
+ })
162
+ const srText = $derived(summary ?? autoSummary)
163
+
164
+ const resolvedSize = $derived(resolveComponentSize('Chart', size))
165
+
166
+ let active = $state<{
167
+ label: string
168
+ median: number
169
+ q1: number
170
+ q3: number
171
+ lo: number
172
+ hi: number
173
+ cx: number
174
+ cy: number
175
+ } | null>(null)
176
+ function show(b: (typeof boxes)[number]) {
177
+ if (tooltip)
178
+ active = {
179
+ label: b.label,
180
+ median: b.median,
181
+ q1: b.q1,
182
+ q3: b.q3,
183
+ lo: b.lo,
184
+ hi: b.hi,
185
+ cx: b.cx,
186
+ cy: b.yMedian,
187
+ }
188
+ }
189
+ function hide() {
190
+ active = null
191
+ }
192
+ function boxLabel(b: (typeof boxes)[number]): string {
193
+ return `${b.label}: median ${fmtY(b.median)} (Q1 ${fmtY(b.q1)}–Q3 ${fmtY(b.q3)})`
194
+ }
195
+ </script>
196
+
197
+ <div
198
+ class="ss-boxplot"
199
+ class:fluid
200
+ data-size-variant={resolvedSize}
201
+ role="group"
202
+ aria-label={srText}
203
+ >
204
+ {#if isEmpty}
205
+ <span class="empty" aria-hidden="true">—</span>
206
+ <span class="sr-only">No data</span>
207
+ {:else}
208
+ <div class="plot">
209
+ <svg
210
+ class="canvas"
211
+ viewBox="0 0 {width} {height}"
212
+ width={fluid ? undefined : width}
213
+ height={fluid ? undefined : height}
214
+ preserveAspectRatio="xMidYMid meet"
215
+ role="presentation"
216
+ >
217
+ <g transform="translate({M.left},{M.top})">
218
+ <!-- gridlines + y axis -->
219
+ {#each yTicks as t}
220
+ <line class="grid" x1="0" x2={innerW} y1={yScaleObj(t)} y2={yScaleObj(t)} />
221
+ <text class="tick y" x="-6" y={yScaleObj(t)} dy="0.32em" text-anchor="end"
222
+ >{fmtY(t)}</text
223
+ >
224
+ {/each}
225
+ <line class="axis" x1="0" x2="0" y1="0" y2={innerH} />
226
+ <line class="axis" x1="0" x2={innerW} y1={innerH} y2={innerH} />
227
+
228
+ <!-- boxes -->
229
+ {#each boxes as b}
230
+ <!-- whisker centre line + caps -->
231
+ <line class="whisker" x1={b.cx} x2={b.cx} y1={b.yLo} y2={b.yHi} />
232
+ <line class="whisker" x1={b.cx - b.w / 4} x2={b.cx + b.w / 4} y1={b.yLo} y2={b.yLo} />
233
+ <line class="whisker" x1={b.cx - b.w / 4} x2={b.cx + b.w / 4} y1={b.yHi} y2={b.yHi} />
234
+
235
+ <!-- box (q1..q3) -->
236
+ <rect
237
+ class="box"
238
+ x={b.x}
239
+ y={b.yQ3}
240
+ width={b.w}
241
+ height={Math.max(0, b.yQ1 - b.yQ3)}
242
+ style="fill:{b.color};stroke:{b.color}"
243
+ tabindex="0"
244
+ role="button"
245
+ aria-label={boxLabel(b)}
246
+ onfocus={() => show(b)}
247
+ onblur={hide}
248
+ onmouseenter={() => show(b)}
249
+ onmouseleave={hide}
250
+ />
251
+
252
+ <!-- median -->
253
+ <line
254
+ class="median"
255
+ x1={b.x}
256
+ x2={b.x + b.w}
257
+ y1={b.yMedian}
258
+ y2={b.yMedian}
259
+ style="stroke:{b.color}"
260
+ />
261
+
262
+ <!-- point overlay (decorative) -->
263
+ {#if showPoints}
264
+ {#each b.points as p}
265
+ <circle
266
+ class="point"
267
+ cx={p.cx}
268
+ cy={p.cy}
269
+ r="2.5"
270
+ style="fill:{b.color}"
271
+ aria-hidden="true"
272
+ />
273
+ {/each}
274
+ {/if}
275
+ {/each}
276
+
277
+ <!-- x axis labels -->
278
+ {#each boxes as b}
279
+ <text class="tick x" x={b.cx} y={innerH + 16} text-anchor="middle">{b.label}</text>
280
+ {/each}
281
+
282
+ <!-- y axis caption -->
283
+ {#if yLabel}
284
+ <text
285
+ class="axis-label y"
286
+ transform="translate({-M.left + 12},{innerH / 2}) rotate(-90)"
287
+ text-anchor="middle">{yLabel}</text
288
+ >
289
+ {/if}
290
+ </g>
291
+ </svg>
292
+
293
+ {#if tooltip && active}
294
+ <div
295
+ class="tooltip"
296
+ role="status"
297
+ style="left:{((active.cx + M.left) / width) * 100}%;top:{((active.cy + M.top) / height) *
298
+ 100}%"
299
+ >
300
+ <span class="t-label">{active.label}</span>
301
+ <span class="t-val">med {fmtY(active.median)}</span>
302
+ <span class="t-val">Q1 {fmtY(active.q1)} · Q3 {fmtY(active.q3)}</span>
303
+ <span class="t-val">min {fmtY(active.lo)} · max {fmtY(active.hi)}</span>
304
+ </div>
305
+ {/if}
306
+ </div>
307
+ {/if}
308
+ </div>
309
+
310
+ <style>.ss-boxplot {
311
+ display: inline-flex;
312
+ flex-direction: column;
313
+ gap: var(--ss-gap-sm);
314
+ font-family: var(--ss-font-body);
315
+ color: var(--ss-fg);
316
+ max-width: 100%;
317
+ }
318
+ .ss-boxplot.fluid {
319
+ display: flex;
320
+ width: 100%;
321
+ }
322
+ .ss-boxplot .plot {
323
+ position: relative;
324
+ width: 100%;
325
+ }
326
+ .ss-boxplot .canvas {
327
+ display: block;
328
+ max-width: 100%;
329
+ height: auto;
330
+ overflow: visible;
331
+ }
332
+ .ss-boxplot.fluid .canvas {
333
+ width: 100%;
334
+ }
335
+ .ss-boxplot .empty {
336
+ align-self: center;
337
+ margin: auto;
338
+ padding: var(--ss-gap-sm);
339
+ color: var(--ss-fg-muted);
340
+ font-family: var(--ss-font-mono);
341
+ line-height: 1;
342
+ min-height: var(--ss-chart-h, 240px);
343
+ display: flex;
344
+ align-items: center;
345
+ }
346
+ .ss-boxplot .sr-only {
347
+ position: absolute;
348
+ width: 1px;
349
+ height: 1px;
350
+ padding: 0;
351
+ margin: -1px;
352
+ overflow: hidden;
353
+ clip: rect(0, 0, 0, 0);
354
+ white-space: nowrap;
355
+ border: 0;
356
+ }
357
+ .ss-boxplot .grid {
358
+ stroke: var(--ss-line);
359
+ stroke-width: 1;
360
+ opacity: 0.5;
361
+ shape-rendering: crispEdges;
362
+ }
363
+ .ss-boxplot .axis {
364
+ stroke: var(--ss-line);
365
+ stroke-width: 1;
366
+ shape-rendering: crispEdges;
367
+ }
368
+ .ss-boxplot .tick {
369
+ font-family: var(--ss-font-mono);
370
+ font-size: var(--ss-ui-xs);
371
+ fill: var(--ss-fg-muted);
372
+ }
373
+ .ss-boxplot .axis-label {
374
+ font-family: var(--ss-font-mono);
375
+ font-size: var(--ss-ui-xs);
376
+ fill: var(--ss-fg-muted);
377
+ }
378
+ .ss-boxplot .whisker {
379
+ stroke: var(--ss-line-strong);
380
+ stroke-width: 1;
381
+ shape-rendering: crispEdges;
382
+ }
383
+ .ss-boxplot .box {
384
+ cursor: pointer;
385
+ fill-opacity: 0.18;
386
+ stroke-width: 1.5;
387
+ transition: fill-opacity var(--ss-dur-fast) var(--ss-ease);
388
+ }
389
+ .ss-boxplot .box:hover {
390
+ fill-opacity: 0.3;
391
+ }
392
+ .ss-boxplot .box:focus-visible {
393
+ outline: none;
394
+ stroke: var(--ss-fg);
395
+ stroke-width: 2;
396
+ }
397
+ .ss-boxplot .median {
398
+ stroke-width: 2;
399
+ shape-rendering: crispEdges;
400
+ }
401
+ .ss-boxplot .point {
402
+ fill-opacity: 0.6;
403
+ pointer-events: none;
404
+ }
405
+ .ss-boxplot .tooltip {
406
+ position: absolute;
407
+ transform: translate(-50%, calc(-100% - 8px));
408
+ pointer-events: none;
409
+ background: var(--ss-bg-elev);
410
+ border: 1px solid var(--ss-line-strong);
411
+ padding: var(--ss-gap-sm);
412
+ font-size: var(--ss-ui-xs);
413
+ line-height: 1.3;
414
+ white-space: nowrap;
415
+ display: flex;
416
+ flex-direction: column;
417
+ gap: 2px;
418
+ z-index: 2;
419
+ }
420
+ .ss-boxplot .tooltip .t-label {
421
+ font-family: var(--ss-font-mono);
422
+ color: var(--ss-fg);
423
+ }
424
+ .ss-boxplot .tooltip .t-val {
425
+ font-family: var(--ss-font-mono);
426
+ color: var(--ss-fg-muted);
427
+ }</style>
@@ -0,0 +1,36 @@
1
+ export interface BoxGroup {
2
+ /** Group caption shown under its band and in the tooltip / accessible name. */
3
+ label: string;
4
+ /** Raw values for this group; whiskers + quartiles are computed from these. */
5
+ values: number[];
6
+ /** Optional per-group colour; falls back to the cycling palette. */
7
+ color?: string;
8
+ }
9
+ import { type Size } from '../config.js';
10
+ interface Props {
11
+ /** One box per group. Each group has a `label`, `values`, and optional `color`. */
12
+ groups: BoxGroup[];
13
+ /** Overlay every value as a small jittered dot. Default true. */
14
+ showPoints?: boolean;
15
+ /** Y axis caption (drawn rotated beside the axis). */
16
+ yLabel?: string;
17
+ /** Force the y domain; defaults to the padded data extent across all values. */
18
+ yDomain?: [number, number];
19
+ /** Format a y value for ticks + tooltip. */
20
+ yFormat?: (y: number) => string;
21
+ /** Intrinsic drawing height in px. Default 300. */
22
+ height?: number;
23
+ /** Intrinsic drawing width in px. Default 420. */
24
+ width?: number;
25
+ /** Stretch to the container width via the viewBox (width:100%). */
26
+ fluid?: boolean;
27
+ /** Reveal a tooltip on hover/focus of a box. Default true. */
28
+ tooltip?: boolean;
29
+ /** SR summary + accessible name; auto-generated when absent. */
30
+ summary?: string;
31
+ /** Token size (sm|md|lg); inherits the global size when unset. */
32
+ size?: Size;
33
+ }
34
+ declare const BoxPlot: import("svelte").Component<Props, {}, "">;
35
+ type BoxPlot = ReturnType<typeof BoxPlot>;
36
+ export default BoxPlot;