@rokkit/chart 1.0.0-next.150 → 1.0.0-next.155

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.
Files changed (90) hide show
  1. package/dist/PlotState.svelte.d.ts +31 -3
  2. package/dist/crossfilter/createCrossFilter.svelte.d.ts +13 -15
  3. package/dist/index.d.ts +6 -1
  4. package/dist/lib/brewing/BoxBrewer.svelte.d.ts +3 -5
  5. package/dist/lib/brewing/QuartileBrewer.svelte.d.ts +9 -0
  6. package/dist/lib/brewing/ViolinBrewer.svelte.d.ts +3 -4
  7. package/dist/lib/brewing/brewer.svelte.d.ts +5 -36
  8. package/dist/lib/brewing/colors.d.ts +10 -1
  9. package/dist/lib/brewing/marks/points.d.ts +17 -2
  10. package/dist/lib/brewing/stats.d.ts +5 -13
  11. package/dist/lib/chart.d.ts +5 -7
  12. package/dist/lib/keyboard-nav.d.ts +15 -0
  13. package/dist/lib/plot/preset.d.ts +1 -1
  14. package/dist/lib/preset.d.ts +30 -0
  15. package/package.json +2 -1
  16. package/src/AnimatedPlot.svelte +375 -206
  17. package/src/Chart.svelte +81 -87
  18. package/src/ChartProvider.svelte +10 -0
  19. package/src/FacetPlot/Panel.svelte +30 -16
  20. package/src/FacetPlot.svelte +100 -76
  21. package/src/Plot/Area.svelte +26 -19
  22. package/src/Plot/Axis.svelte +81 -59
  23. package/src/Plot/Bar.svelte +47 -89
  24. package/src/Plot/Grid.svelte +23 -19
  25. package/src/Plot/Legend.svelte +213 -147
  26. package/src/Plot/Line.svelte +31 -21
  27. package/src/Plot/Point.svelte +35 -22
  28. package/src/Plot/Root.svelte +46 -91
  29. package/src/Plot/Timeline.svelte +82 -82
  30. package/src/Plot/Tooltip.svelte +68 -62
  31. package/src/Plot.svelte +290 -182
  32. package/src/PlotState.svelte.js +339 -267
  33. package/src/Sparkline.svelte +95 -56
  34. package/src/charts/AreaChart.svelte +22 -20
  35. package/src/charts/BarChart.svelte +23 -21
  36. package/src/charts/BoxPlot.svelte +15 -15
  37. package/src/charts/BubbleChart.svelte +17 -17
  38. package/src/charts/LineChart.svelte +20 -20
  39. package/src/charts/PieChart.svelte +30 -20
  40. package/src/charts/ScatterPlot.svelte +20 -19
  41. package/src/charts/ViolinPlot.svelte +15 -15
  42. package/src/crossfilter/CrossFilter.svelte +33 -29
  43. package/src/crossfilter/FilterBar.svelte +17 -25
  44. package/src/crossfilter/FilterHistogram.svelte +290 -0
  45. package/src/crossfilter/FilterSlider.svelte +69 -65
  46. package/src/crossfilter/createCrossFilter.svelte.js +100 -89
  47. package/src/geoms/Arc.svelte +114 -69
  48. package/src/geoms/Area.svelte +67 -39
  49. package/src/geoms/Bar.svelte +184 -126
  50. package/src/geoms/Box.svelte +102 -90
  51. package/src/geoms/LabelPill.svelte +11 -11
  52. package/src/geoms/Line.svelte +110 -87
  53. package/src/geoms/Point.svelte +132 -87
  54. package/src/geoms/Violin.svelte +45 -33
  55. package/src/geoms/lib/areas.js +122 -99
  56. package/src/geoms/lib/bars.js +195 -144
  57. package/src/index.js +21 -14
  58. package/src/lib/brewing/BoxBrewer.svelte.js +8 -50
  59. package/src/lib/brewing/CartesianBrewer.svelte.js +12 -7
  60. package/src/lib/brewing/PieBrewer.svelte.js +5 -5
  61. package/src/lib/brewing/QuartileBrewer.svelte.js +51 -0
  62. package/src/lib/brewing/ViolinBrewer.svelte.js +8 -49
  63. package/src/lib/brewing/brewer.svelte.js +249 -201
  64. package/src/lib/brewing/colors.js +34 -5
  65. package/src/lib/brewing/marks/arcs.js +28 -28
  66. package/src/lib/brewing/marks/areas.js +54 -41
  67. package/src/lib/brewing/marks/bars.js +34 -34
  68. package/src/lib/brewing/marks/boxes.js +51 -51
  69. package/src/lib/brewing/marks/lines.js +37 -30
  70. package/src/lib/brewing/marks/points.js +74 -26
  71. package/src/lib/brewing/marks/violins.js +57 -57
  72. package/src/lib/brewing/patterns.js +25 -11
  73. package/src/lib/brewing/scales.js +20 -20
  74. package/src/lib/brewing/stats.js +40 -28
  75. package/src/lib/brewing/symbols.js +1 -1
  76. package/src/lib/chart.js +12 -4
  77. package/src/lib/keyboard-nav.js +37 -0
  78. package/src/lib/plot/crossfilter.js +5 -5
  79. package/src/lib/plot/facet.js +30 -30
  80. package/src/lib/plot/frames.js +30 -29
  81. package/src/lib/plot/helpers.js +4 -4
  82. package/src/lib/plot/preset.js +48 -34
  83. package/src/lib/plot/scales.js +64 -39
  84. package/src/lib/plot/stat.js +47 -47
  85. package/src/lib/preset.js +41 -0
  86. package/src/patterns/DefinePatterns.svelte +24 -24
  87. package/src/patterns/PatternDef.svelte +1 -1
  88. package/src/patterns/patterns.js +328 -176
  89. package/src/patterns/scale.js +61 -32
  90. package/src/spec/chart-spec.js +64 -21
@@ -1,214 +1,383 @@
1
1
  <script>
2
- import { onMount, onDestroy } from 'svelte'
3
- import { extractFrames, completeFrames, computeStaticDomains } from './lib/plot/frames.js'
4
- import { applyGeomStat } from './lib/plot/stat.js'
5
- import Timeline from './Plot/Timeline.svelte'
6
- import PlotChart from './Plot.svelte'
7
-
8
- /**
9
- * @type {{
10
- * data: Object[],
11
- * animate: { by: string, duration?: number, loop?: boolean },
12
- * x?: string,
13
- * y?: string,
14
- * color?: string,
15
- * geoms?: import('./lib/plot/types.js').GeomSpec[],
16
- * helpers?: import('./lib/plot/types.js').PlotHelpers,
17
- * width?: number,
18
- * height?: number,
19
- * mode?: 'light' | 'dark',
20
- * grid?: boolean,
21
- * legend?: boolean,
22
- * children?: import('svelte').Snippet
23
- * }}
24
- */
25
- let {
26
- data = [],
27
- animate,
28
- x,
29
- y,
30
- color,
31
- geoms = [],
32
- helpers = {},
33
- width = 600,
34
- height = 400,
35
- mode = 'light',
36
- grid = true,
37
- legend = false,
38
- children
39
- } = $props()
40
-
41
- // Pre-aggregate and complete frames when any geom has a non-identity stat:
42
- // 1. applyGeomStat: aggregate data by (x, color?, by) → one row per combination
43
- // 2. completeFrames: alignBy(by) ensures all frame values appear for every (x, color?)
44
- // group, filling missing rows with y=0 so bars animate smoothly
45
- // Geoms are returned with stat: 'identity' so PlotChart renders the pre-aggregated values as-is.
46
- const prepared = $derived.by(() => {
47
- const firstNonIdentity = geoms.find((g) => g.stat && g.stat !== 'identity')
48
- if (!firstNonIdentity) return { data, geoms }
49
-
50
- const aggChannels = { y }
51
- if (x) aggChannels.x = x
52
- if (color) aggChannels.color = color
53
- aggChannels.frame = animate.by // 'frame' is not a value channel, so it becomes a group-by
54
-
55
- const aggregated = applyGeomStat(
56
- data,
57
- { stat: firstNonIdentity.stat, channels: aggChannels },
58
- helpers
59
- )
60
- const completeData = completeFrames(aggregated, { x, y, color }, animate.by)
61
-
62
- return { data: completeData, geoms: geoms.map((g) => ({ ...g, stat: 'identity' })) }
63
- })
64
-
65
- // Extract frames and compute stable domains from the full prepared dataset
66
- const rawFrames = $derived(extractFrames(prepared.data, animate.by))
67
- const frameKeys = $derived([...rawFrames.keys()])
68
-
69
- const channels = $derived({ x, y, color })
70
- const staticDomains = $derived(
71
- x && y ? computeStaticDomains(prepared.data, channels) : { xDomain: undefined, yDomain: undefined }
72
- )
73
-
74
- // Playback state
75
- let currentIndex = $state(0)
76
- let playing = $state(false)
77
- let speed = $state(1)
78
-
79
- // Current frame data already complete (all x/color combos present)
80
- const currentFrameData = $derived.by(() => {
81
- const key = frameKeys[currentIndex]
82
- return rawFrames.get(key) ?? []
83
- })
84
-
85
- // Reduced motion preference
86
- let prefersReducedMotion = $state(false)
87
- onMount(() => {
88
- if (typeof window.matchMedia !== 'function') return
89
- const mq = window.matchMedia('(prefers-reduced-motion: reduce)')
90
- prefersReducedMotion = mq.matches
91
- const handler = (e) => { prefersReducedMotion = e.matches }
92
- mq.addEventListener('change', handler)
93
- return () => mq.removeEventListener('change', handler)
94
- })
95
-
96
- // Animation loop
97
- const baseDuration = $derived(animate.duration ?? 800)
98
- const msPerFrame = $derived(Math.round(baseDuration / speed))
99
- let lastTime = 0
100
- let rafId = 0
101
-
102
- function tick(time) {
103
- if (!playing) return
104
- if (time - lastTime >= msPerFrame) {
105
- lastTime = time
106
- currentIndex = currentIndex + 1
107
- if (currentIndex >= frameKeys.length) {
108
- if (animate.loop ?? false) {
109
- currentIndex = 0
110
- } else {
111
- playing = false
112
- return
113
- }
114
- }
115
- }
116
- rafId = requestAnimationFrame(tick)
117
- }
118
-
119
- $effect(() => {
120
- // Reading msPerFrame here makes it a tracked dependency — effect re-runs on speed changes,
121
- // which resets lastTime=0 to re-anchor frame pacing.
122
- const _ms = msPerFrame
123
- if (playing && !prefersReducedMotion) {
124
- lastTime = 0
125
- rafId = requestAnimationFrame(tick)
126
- } else {
127
- cancelAnimationFrame(rafId)
128
- }
129
- return () => cancelAnimationFrame(rafId)
130
- })
131
-
132
- // Reduced motion: step frames on interval instead
133
- let reducedInterval = 0
134
- $effect(() => {
135
- if (!playing || !prefersReducedMotion) {
136
- clearInterval(reducedInterval)
137
- return
138
- }
139
- reducedInterval = setInterval(() => {
140
- currentIndex = currentIndex + 1
141
- if (currentIndex >= frameKeys.length) {
142
- if (animate.loop ?? false) currentIndex = 0
143
- else { playing = false; clearInterval(reducedInterval) }
144
- }
145
- }, msPerFrame)
146
- return () => clearInterval(reducedInterval)
147
- })
148
-
149
- onDestroy(() => {
150
- cancelAnimationFrame(rafId)
151
- clearInterval(reducedInterval)
152
- })
153
-
154
- $effect(() => {
155
- const len = frameKeys.length
156
- if (currentIndex >= len && len > 0) {
157
- currentIndex = len - 1
158
- playing = false
159
- } else if (len === 0) {
160
- currentIndex = 0
161
- playing = false
162
- }
163
- })
164
-
165
- function handlePlay() { playing = true }
166
- function handlePause() { playing = false }
167
- function handleScrub(index) {
168
- playing = false
169
- currentIndex = index
170
- }
171
- function handleSpeed(s) { speed = s }
172
-
173
- // Build spec for the current frame, with static domain overrides
174
- const frameSpec = $derived({
175
- data: currentFrameData,
176
- x, y, color,
177
- geoms: prepared.geoms,
178
- xDomain: staticDomains.xDomain,
179
- yDomain: staticDomains.yDomain
180
- })
2
+ import { onMount, onDestroy } from 'svelte'
3
+ import { tweened } from 'svelte/motion'
4
+ import { sineInOut } from 'svelte/easing'
5
+ import { extractFrames, completeFrames, computeStaticDomains } from './lib/plot/frames.js'
6
+ import { applyGeomStat } from './lib/plot/stat.js'
7
+ import Timeline from './Plot/Timeline.svelte'
8
+ import PlotChart from './Plot.svelte'
9
+
10
+ /**
11
+ * @type {{
12
+ * data: Object[],
13
+ * animate: { by: string, duration?: number, loop?: boolean },
14
+ * x?: string,
15
+ * y?: string,
16
+ * color?: string,
17
+ * fill?: string,
18
+ * pattern?: string,
19
+ * symbol?: string,
20
+ * geom?: string,
21
+ * stat?: string,
22
+ * geoms?: import('./lib/plot/types.js').GeomSpec[],
23
+ * helpers?: import('./lib/plot/types.js').PlotHelpers,
24
+ * width?: number,
25
+ * height?: number,
26
+ * mode?: 'light' | 'dark',
27
+ * grid?: boolean,
28
+ * legend?: boolean,
29
+ * tween?: boolean,
30
+ * sorted?: boolean,
31
+ * dynamicDomain?: boolean,
32
+ * label?: boolean | string | ((data: Record<string, unknown>) => string),
33
+ * children?: import('svelte').Snippet
34
+ * }}
35
+ */
36
+ let {
37
+ data = [],
38
+ animate,
39
+ x,
40
+ y,
41
+ color,
42
+ fill = undefined,
43
+ pattern = undefined,
44
+ symbol = undefined,
45
+ geom = 'bar',
46
+ stat = 'identity',
47
+ geoms = [],
48
+ helpers = {},
49
+ width = 600,
50
+ height = 400,
51
+ mode = 'light',
52
+ grid = true,
53
+ legend = false,
54
+ tween = true,
55
+ sorted = false,
56
+ dynamicDomain = false,
57
+ label = false,
58
+ children
59
+ } = $props()
60
+
61
+ // Effective geom list: explicit array takes precedence; otherwise build from shorthand props
62
+ const effectiveGeoms = $derived(
63
+ geoms.length > 0
64
+ ? geoms
65
+ : [
66
+ {
67
+ type: geom,
68
+ stat,
69
+ ...(fill !== undefined && { fill }),
70
+ ...(pattern !== undefined && { pattern }),
71
+ ...(symbol !== undefined && { symbol })
72
+ }
73
+ ]
74
+ )
75
+
76
+ // Pre-aggregate and complete frames when any geom has a non-identity stat
77
+ const prepared = $derived.by(() => {
78
+ const firstNonIdentity = effectiveGeoms.find((g) => g.stat && g.stat !== 'identity')
79
+ if (!firstNonIdentity) return { data, geoms: effectiveGeoms }
80
+
81
+ const aggChannels = { y }
82
+ if (x) aggChannels.x = x
83
+ if (color) aggChannels.color = color
84
+ aggChannels.frame = animate.by
85
+
86
+ const aggregated = applyGeomStat(
87
+ data,
88
+ { stat: firstNonIdentity.stat, channels: aggChannels },
89
+ helpers
90
+ )
91
+ const completeData = completeFrames(aggregated, { x, y, color }, animate.by)
92
+ return { data: completeData, geoms: effectiveGeoms.map((g) => ({ ...g, stat: 'identity' })) }
93
+ })
94
+
95
+ // Extract frames and compute stable domains from the full prepared dataset
96
+ const rawFrames = $derived(extractFrames(prepared.data, animate.by))
97
+ const frameKeys = $derived([...rawFrames.keys()])
98
+
99
+ const channels = $derived({ x, y, color })
100
+ const staticDomains = $derived(
101
+ x && y
102
+ ? computeStaticDomains(prepared.data, channels)
103
+ : { xDomain: undefined, yDomain: undefined }
104
+ )
105
+
106
+ // Tweened x-domain for dynamic axis animation (opt-in via dynamicDomain prop)
107
+ const xDomainTween = tweened([0, 1], { duration: 0 })
108
+ let xDomainInitialized = false
109
+
110
+ // Playback state
111
+ let currentIndex = $state(0)
112
+ let playing = $state(false)
113
+ let speed = $state(1)
114
+
115
+ // Current frame data — already complete (all x/color combos present)
116
+ const currentFrameData = $derived.by(() => {
117
+ const key = frameKeys[currentIndex]
118
+ return rawFrames.get(key) ?? []
119
+ })
120
+
121
+ // Reduced motion preference
122
+ let prefersReducedMotion = $state(false)
123
+ onMount(() => {
124
+ if (typeof window.matchMedia !== 'function') return
125
+ const mq = window.matchMedia('(prefers-reduced-motion: reduce)')
126
+ prefersReducedMotion = mq.matches
127
+ const handler = (e) => { prefersReducedMotion = e.matches }
128
+ mq.addEventListener('change', handler)
129
+ return () => mq.removeEventListener('change', handler)
130
+ })
131
+
132
+ // Animation timing
133
+ const baseDuration = $derived(animate.duration ?? 800)
134
+ const msPerFrame = $derived(Math.round(baseDuration / speed))
135
+
136
+ // rAF-based frame timer — advances currentIndex every msPerFrame ms.
137
+ // Separate from the tween: the tween handles visual interpolation only.
138
+ let lastTime = 0
139
+ let rafId = 0
140
+
141
+ function advanceFrame() {
142
+ currentIndex = currentIndex + 1
143
+ if (currentIndex >= frameKeys.length) {
144
+ if (animate.loop ?? false) currentIndex = 0
145
+ else playing = false
146
+ }
147
+ }
148
+
149
+ function tick(time) {
150
+ if (!playing) return
151
+ if (time - lastTime >= msPerFrame) {
152
+ lastTime = time
153
+ advanceFrame()
154
+ if (!playing) return
155
+ }
156
+ rafId = requestAnimationFrame(tick)
157
+ }
158
+
159
+ $effect(() => {
160
+ // msPerFrame is a tracked dependency: re-runs on speed change, resetting lastTime=0
161
+ const _ms = msPerFrame
162
+ if (playing && !prefersReducedMotion) {
163
+ lastTime = 0
164
+ rafId = requestAnimationFrame(tick)
165
+ } else {
166
+ cancelAnimationFrame(rafId)
167
+ }
168
+ return () => cancelAnimationFrame(rafId)
169
+ })
170
+
171
+ // Reduced motion: step frames on interval instead of rAF
172
+ let reducedInterval = 0
173
+ $effect(() => {
174
+ if (!playing || !prefersReducedMotion) {
175
+ clearInterval(reducedInterval)
176
+ return
177
+ }
178
+ reducedInterval = setInterval(() => {
179
+ currentIndex = currentIndex + 1
180
+ if (currentIndex >= frameKeys.length) {
181
+ if (animate.loop ?? false) currentIndex = 0
182
+ else { playing = false; clearInterval(reducedInterval) }
183
+ }
184
+ }, msPerFrame)
185
+ return () => clearInterval(reducedInterval)
186
+ })
187
+
188
+ onDestroy(() => {
189
+ cancelAnimationFrame(rafId)
190
+ clearInterval(reducedInterval)
191
+ })
192
+
193
+ $effect(() => {
194
+ const len = frameKeys.length
195
+ if (currentIndex >= len && len > 0) {
196
+ currentIndex = len - 1
197
+ playing = false
198
+ } else if (len === 0) {
199
+ currentIndex = 0
200
+ playing = false
201
+ }
202
+ })
203
+
204
+ function handlePlay() { playing = true }
205
+ function handlePause() { playing = false }
206
+ function handleScrub(index) { playing = false; currentIndex = index }
207
+ function handleSpeed(s) { speed = s }
208
+
209
+ // Detect horizontal bar chart race: sorted=true AND y is a categorical string field
210
+ const isHorizontalRace = $derived.by(() => {
211
+ if (!sorted || !prepared.data.length) return false
212
+ const sample = prepared.data[0]
213
+ return y && typeof sample[y] === 'string'
214
+ })
215
+
216
+ // Number of unique entities (y categories) for horizontal race yDomain
217
+ const entityCount = $derived.by(() => {
218
+ if (!isHorizontalRace) return 0
219
+ return new Set(prepared.data.map((d) => d[y])).size
220
+ })
221
+
222
+ // Tweened display data — smoothly interpolates values between frames.
223
+ // Tween duration is 1.1× the frame interval so tweens always overlap:
224
+ // when the next frame fires, the previous tween is ~91% complete.
225
+ // displayTween.set() starts from the current in-flight value → seamless continuous motion.
226
+ const displayTween = tweened([], { duration: 0 })
227
+
228
+ $effect(() => {
229
+ const raw = currentFrameData
230
+ const xField = x
231
+ const yField = y
232
+
233
+ // Build display target for this frame
234
+ let target
235
+ if (isHorizontalRace) {
236
+ const ranked = raw.slice().sort((a, b) => Number(b[xField]) - Number(a[xField]))
237
+ const n = ranked.length
238
+ target = ranked.map((row, i) => ({
239
+ ...row,
240
+ _entity: row[yField],
241
+ _rank: n - 1 - i
242
+ }))
243
+ } else if (sorted) {
244
+ target = raw.slice().sort((a, b) => Number(b[yField]) - Number(a[yField]))
245
+ } else {
246
+ target = raw
247
+ }
248
+
249
+ // Tween duration slightly longer than frame interval → guaranteed overlap, no pause
250
+ const tweenDuration = Math.round(msPerFrame * 1.1)
251
+
252
+ if (!tween || prefersReducedMotion) {
253
+ displayTween.set(target, { duration: 0 })
254
+ } else if (isHorizontalRace) {
255
+ displayTween.set(target, {
256
+ duration: tweenDuration,
257
+ easing: sineInOut,
258
+ interpolate: (a, b) => {
259
+ const aMap = new Map(a.map((r) => [r._entity, r]))
260
+ return (t) =>
261
+ b.map((r) => {
262
+ const p = aMap.get(r._entity) ?? r
263
+ return {
264
+ ...r,
265
+ [xField]: Number(p[xField] ?? 0) * (1 - t) + Number(r[xField]) * t,
266
+ _rank: Number(p._rank ?? r._rank) * (1 - t) + Number(r._rank) * t
267
+ }
268
+ })
269
+ }
270
+ })
271
+ } else if (sorted) {
272
+ displayTween.set(target, {
273
+ duration: tweenDuration,
274
+ easing: sineInOut,
275
+ interpolate: (a, b) => {
276
+ const aMap = new Map(a.map((r) => [r[xField], r]))
277
+ return (t) =>
278
+ b.map((r) => {
279
+ const p = aMap.get(r[xField]) ?? r
280
+ return { ...r, [yField]: Number(p[yField] ?? 0) * (1 - t) + Number(r[yField]) * t }
281
+ })
282
+ }
283
+ })
284
+ } else {
285
+ // Unsorted: if y is categorical (string), key by y-value and lerp x (bar width).
286
+ // This keeps bars at stable band positions while widths animate smoothly.
287
+ const yCategorical = yField && raw.length > 0 && typeof raw[0][yField] === 'string'
288
+ if (yCategorical) {
289
+ displayTween.set(target, {
290
+ duration: tweenDuration,
291
+ easing: sineInOut,
292
+ interpolate: (a, b) => {
293
+ const aMap = new Map(a.map((r) => [r[yField], r]))
294
+ return (t) =>
295
+ b.map((r) => {
296
+ const p = aMap.get(r[yField]) ?? r
297
+ return { ...r, [xField]: Number(p[xField] ?? 0) * (1 - t) + Number(r[xField]) * t }
298
+ })
299
+ }
300
+ })
301
+ } else {
302
+ displayTween.set(target, {
303
+ duration: tweenDuration,
304
+ easing: sineInOut,
305
+ interpolate: (a, b) => (t) =>
306
+ b.map((r, i) => ({
307
+ ...r,
308
+ [yField]: Number(a[i]?.[yField] ?? 0) * (1 - t) + Number(r[yField]) * t
309
+ }))
310
+ })
311
+ }
312
+ }
313
+ })
314
+
315
+ // Tween the x-domain per frame for dynamic axis animation (opt-in).
316
+ // First update is instant to avoid a jarring jump when toggled.
317
+ $effect(() => {
318
+ if (!dynamicDomain || !x || !currentFrameData.length) {
319
+ if (!dynamicDomain) xDomainInitialized = false
320
+ return
321
+ }
322
+ const vals = currentFrameData.map((d) => Number(d[x])).filter((v) => !isNaN(v))
323
+ if (vals.length === 0) return
324
+ const max = Math.max(...vals)
325
+ xDomainTween.set([0, max], {
326
+ duration: xDomainInitialized ? Math.round(msPerFrame * 1.1) : 0,
327
+ easing: sineInOut
328
+ })
329
+ xDomainInitialized = true
330
+ })
331
+
332
+ // For horizontal race: inject orientation, label, and labelInside into each geom
333
+ const raceGeoms = $derived(
334
+ prepared.geoms.map((g) => ({
335
+ ...g,
336
+ ...(label && { label: '_entity' }),
337
+ options: {
338
+ ...(g.options ?? {}),
339
+ orientation: 'horizontal',
340
+ ...(label && { labelInside: true })
341
+ }
342
+ }))
343
+ )
344
+
345
+ const xDomainForFrame = $derived(
346
+ isHorizontalRace && dynamicDomain ? $xDomainTween : staticDomains.xDomain
347
+ )
348
+ const frameSpec = $derived({
349
+ data: $displayTween,
350
+ x,
351
+ y: isHorizontalRace ? '_rank' : y,
352
+ color,
353
+ geoms: isHorizontalRace ? raceGeoms : prepared.geoms,
354
+ xDomain: xDomainForFrame,
355
+ yDomain: isHorizontalRace ? [0, entityCount - 1] : staticDomains.yDomain,
356
+ orientation: isHorizontalRace ? 'horizontal' : undefined
357
+ })
181
358
  </script>
182
359
 
183
360
  <div data-plot-animated>
184
- <PlotChart
185
- spec={frameSpec}
186
- {helpers}
187
- {width}
188
- {height}
189
- {mode}
190
- {grid}
191
- {legend}
192
- >
193
- {@render children?.()}
194
- </PlotChart>
195
-
196
- <Timeline
197
- {frameKeys}
198
- {currentIndex}
199
- {playing}
200
- {speed}
201
- onplay={handlePlay}
202
- onpause={handlePause}
203
- onscrub={handleScrub}
204
- onspeed={handleSpeed}
205
- />
361
+ <PlotChart spec={frameSpec} {helpers} {width} {height} {mode} {grid} {legend}>
362
+ {@render children?.()}
363
+ </PlotChart>
364
+
365
+ <Timeline
366
+ {frameKeys}
367
+ {currentIndex}
368
+ {playing}
369
+ {speed}
370
+ onplay={handlePlay}
371
+ onpause={handlePause}
372
+ onscrub={handleScrub}
373
+ onspeed={handleSpeed}
374
+ />
206
375
  </div>
207
376
 
208
377
  <style>
209
- [data-plot-animated] {
210
- display: flex;
211
- flex-direction: column;
212
- width: 100%;
213
- }
378
+ [data-plot-animated] {
379
+ display: flex;
380
+ flex-direction: column;
381
+ width: 100%;
382
+ }
214
383
  </style>