@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.
- package/dist/PlotState.svelte.d.ts +31 -3
- package/dist/crossfilter/createCrossFilter.svelte.d.ts +13 -15
- package/dist/index.d.ts +6 -1
- package/dist/lib/brewing/BoxBrewer.svelte.d.ts +3 -5
- package/dist/lib/brewing/QuartileBrewer.svelte.d.ts +9 -0
- package/dist/lib/brewing/ViolinBrewer.svelte.d.ts +3 -4
- package/dist/lib/brewing/brewer.svelte.d.ts +5 -36
- package/dist/lib/brewing/colors.d.ts +10 -1
- package/dist/lib/brewing/marks/points.d.ts +17 -2
- package/dist/lib/brewing/stats.d.ts +5 -13
- package/dist/lib/chart.d.ts +5 -7
- package/dist/lib/keyboard-nav.d.ts +15 -0
- package/dist/lib/plot/preset.d.ts +1 -1
- package/dist/lib/preset.d.ts +30 -0
- package/package.json +2 -1
- package/src/AnimatedPlot.svelte +375 -206
- package/src/Chart.svelte +81 -87
- package/src/ChartProvider.svelte +10 -0
- package/src/FacetPlot/Panel.svelte +30 -16
- package/src/FacetPlot.svelte +100 -76
- package/src/Plot/Area.svelte +26 -19
- package/src/Plot/Axis.svelte +81 -59
- package/src/Plot/Bar.svelte +47 -89
- package/src/Plot/Grid.svelte +23 -19
- package/src/Plot/Legend.svelte +213 -147
- package/src/Plot/Line.svelte +31 -21
- package/src/Plot/Point.svelte +35 -22
- package/src/Plot/Root.svelte +46 -91
- package/src/Plot/Timeline.svelte +82 -82
- package/src/Plot/Tooltip.svelte +68 -62
- package/src/Plot.svelte +290 -182
- package/src/PlotState.svelte.js +339 -267
- package/src/Sparkline.svelte +95 -56
- package/src/charts/AreaChart.svelte +22 -20
- package/src/charts/BarChart.svelte +23 -21
- package/src/charts/BoxPlot.svelte +15 -15
- package/src/charts/BubbleChart.svelte +17 -17
- package/src/charts/LineChart.svelte +20 -20
- package/src/charts/PieChart.svelte +30 -20
- package/src/charts/ScatterPlot.svelte +20 -19
- package/src/charts/ViolinPlot.svelte +15 -15
- package/src/crossfilter/CrossFilter.svelte +33 -29
- package/src/crossfilter/FilterBar.svelte +17 -25
- package/src/crossfilter/FilterHistogram.svelte +290 -0
- package/src/crossfilter/FilterSlider.svelte +69 -65
- package/src/crossfilter/createCrossFilter.svelte.js +100 -89
- package/src/geoms/Arc.svelte +114 -69
- package/src/geoms/Area.svelte +67 -39
- package/src/geoms/Bar.svelte +184 -126
- package/src/geoms/Box.svelte +102 -90
- package/src/geoms/LabelPill.svelte +11 -11
- package/src/geoms/Line.svelte +110 -87
- package/src/geoms/Point.svelte +132 -87
- package/src/geoms/Violin.svelte +45 -33
- package/src/geoms/lib/areas.js +122 -99
- package/src/geoms/lib/bars.js +195 -144
- package/src/index.js +21 -14
- package/src/lib/brewing/BoxBrewer.svelte.js +8 -50
- package/src/lib/brewing/CartesianBrewer.svelte.js +12 -7
- package/src/lib/brewing/PieBrewer.svelte.js +5 -5
- package/src/lib/brewing/QuartileBrewer.svelte.js +51 -0
- package/src/lib/brewing/ViolinBrewer.svelte.js +8 -49
- package/src/lib/brewing/brewer.svelte.js +249 -201
- package/src/lib/brewing/colors.js +34 -5
- package/src/lib/brewing/marks/arcs.js +28 -28
- package/src/lib/brewing/marks/areas.js +54 -41
- package/src/lib/brewing/marks/bars.js +34 -34
- package/src/lib/brewing/marks/boxes.js +51 -51
- package/src/lib/brewing/marks/lines.js +37 -30
- package/src/lib/brewing/marks/points.js +74 -26
- package/src/lib/brewing/marks/violins.js +57 -57
- package/src/lib/brewing/patterns.js +25 -11
- package/src/lib/brewing/scales.js +20 -20
- package/src/lib/brewing/stats.js +40 -28
- package/src/lib/brewing/symbols.js +1 -1
- package/src/lib/chart.js +12 -4
- package/src/lib/keyboard-nav.js +37 -0
- package/src/lib/plot/crossfilter.js +5 -5
- package/src/lib/plot/facet.js +30 -30
- package/src/lib/plot/frames.js +30 -29
- package/src/lib/plot/helpers.js +4 -4
- package/src/lib/plot/preset.js +48 -34
- package/src/lib/plot/scales.js +64 -39
- package/src/lib/plot/stat.js +47 -47
- package/src/lib/preset.js +41 -0
- package/src/patterns/DefinePatterns.svelte +24 -24
- package/src/patterns/PatternDef.svelte +1 -1
- package/src/patterns/patterns.js +328 -176
- package/src/patterns/scale.js +61 -32
- package/src/spec/chart-spec.js +64 -21
package/src/AnimatedPlot.svelte
CHANGED
|
@@ -1,214 +1,383 @@
|
|
|
1
1
|
<script>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
378
|
+
[data-plot-animated] {
|
|
379
|
+
display: flex;
|
|
380
|
+
flex-direction: column;
|
|
381
|
+
width: 100%;
|
|
382
|
+
}
|
|
214
383
|
</style>
|