@rokkit/chart 1.0.0-next.151 → 1.0.0-next.158
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 +26 -0
- 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/colors.d.ts +10 -1
- package/dist/lib/brewing/marks/points.d.ts +17 -2
- 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 -207
- package/src/Chart.svelte +81 -84
- 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 -174
- package/src/PlotState.svelte.js +338 -265
- 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 +94 -90
- 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 +101 -91
- package/src/geoms/LabelPill.svelte +11 -11
- package/src/geoms/Line.svelte +110 -86
- package/src/geoms/Point.svelte +130 -90
- package/src/geoms/Violin.svelte +51 -41
- 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 +11 -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 +242 -195
- 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 +17 -17
- package/src/lib/brewing/stats.js +37 -29
- package/src/lib/brewing/symbols.js +1 -1
- package/src/lib/chart.js +2 -1
- 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/README.md +3 -0
- package/src/patterns/patterns.js +328 -176
- package/src/patterns/scale.js +61 -32
- package/src/spec/chart-spec.js +64 -21
package/src/PlotState.svelte.js
CHANGED
|
@@ -1,277 +1,350 @@
|
|
|
1
1
|
import { untrack } from 'svelte'
|
|
2
2
|
import { SvelteMap, SvelteSet } from 'svelte/reactivity'
|
|
3
3
|
import { applyGeomStat } from './lib/plot/stat.js'
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
inferFieldType,
|
|
6
|
+
inferOrientation,
|
|
7
|
+
buildUnifiedXScale,
|
|
8
|
+
buildUnifiedYScale,
|
|
9
|
+
inferColorScaleType
|
|
10
|
+
} from './lib/plot/scales.js'
|
|
5
11
|
import { resolvePreset } from './lib/plot/preset.js'
|
|
6
12
|
import { resolveFormat, resolveTooltip, resolveGeom } from './lib/plot/helpers.js'
|
|
7
|
-
import {
|
|
13
|
+
import { defaultPreset } from './lib/preset.js'
|
|
14
|
+
import { distinct, assignColors, isLiteralColor } from './lib/brewing/colors.js'
|
|
8
15
|
import { assignPatterns } from './lib/brewing/patterns.js'
|
|
9
16
|
import { assignSymbols } from './lib/brewing/marks/points.js'
|
|
10
17
|
|
|
11
18
|
let nextId = 0
|
|
12
19
|
|
|
13
20
|
export class PlotState {
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
21
|
+
#data = $state([])
|
|
22
|
+
#rawData = $state([])
|
|
23
|
+
#channels = $state({})
|
|
24
|
+
#labels = $state({})
|
|
25
|
+
#helpers = $state({})
|
|
26
|
+
#presetName = $state(undefined)
|
|
27
|
+
#colorMidpoint = $state(undefined)
|
|
28
|
+
#colorSpec = $state(undefined)
|
|
29
|
+
#colorDomain = $state(undefined)
|
|
30
|
+
#xDomain = $state(undefined)
|
|
31
|
+
#yDomain = $state(undefined)
|
|
32
|
+
#width = $state(600)
|
|
33
|
+
#height = $state(400)
|
|
34
|
+
#margin = $state({ top: 20, right: 20, bottom: 40, left: 50 })
|
|
35
|
+
#marginOverride = $state(undefined)
|
|
36
|
+
|
|
37
|
+
#geoms = $state([])
|
|
38
|
+
#mode = $state('light')
|
|
39
|
+
#chartPreset = $state(defaultPreset)
|
|
40
|
+
#hovered = $state(null)
|
|
41
|
+
#orientationOverride = $state(undefined)
|
|
42
|
+
|
|
43
|
+
axisOrigin = $state([undefined, undefined])
|
|
44
|
+
|
|
45
|
+
#zoomTransform = $state(null)
|
|
46
|
+
|
|
47
|
+
#effectiveMargin = $derived(this.#marginOverride ?? this.#margin)
|
|
48
|
+
#innerWidth = $derived(this.#width - this.#effectiveMargin.left - this.#effectiveMargin.right)
|
|
49
|
+
#innerHeight = $derived(this.#height - this.#effectiveMargin.top - this.#effectiveMargin.bottom)
|
|
50
|
+
|
|
51
|
+
// Effective channels: prefer top-level channels; fall back to first geom's channels
|
|
52
|
+
// for the declarative API where no spec is provided.
|
|
53
|
+
#mergeGeomChannels(tc, geom) {
|
|
54
|
+
return {
|
|
55
|
+
x: tc.x ?? geom.channels?.x,
|
|
56
|
+
y: tc.y ?? geom.channels?.y,
|
|
57
|
+
color: tc.color ?? geom.channels?.color,
|
|
58
|
+
pattern: tc.pattern ?? geom.channels?.pattern,
|
|
59
|
+
symbol: tc.symbol ?? geom.channels?.symbol
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
#effectiveChannels = $derived.by(() => {
|
|
64
|
+
const tc = this.#channels
|
|
65
|
+
if (tc.x && tc.y) return tc
|
|
66
|
+
const firstGeom = this.#geoms[0]
|
|
67
|
+
if (!firstGeom) return tc
|
|
68
|
+
return this.#mergeGeomChannels(tc, firstGeom)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
#resolveXType(rawXType, yType) {
|
|
72
|
+
const hasBarGeom = this.#geoms.some((g) => g.type === 'bar')
|
|
73
|
+
return hasBarGeom && rawXType === 'continuous' && yType === 'continuous' ? 'band' : rawXType
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
orientation = $derived.by(() => {
|
|
77
|
+
if (this.#orientationOverride) return this.#orientationOverride
|
|
78
|
+
const xField = this.#effectiveChannels.x
|
|
79
|
+
const yField = this.#effectiveChannels.y
|
|
80
|
+
if (!xField || !yField) return 'none'
|
|
81
|
+
const rawXType = inferFieldType(this.#data, xField)
|
|
82
|
+
const yType = inferFieldType(this.#data, yField)
|
|
83
|
+
// Bar geoms treat numeric X as categorical (e.g. year on X → vertical bars).
|
|
84
|
+
return inferOrientation(this.#resolveXType(rawXType, yType), yType)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
colorScaleType = $derived.by(() => {
|
|
88
|
+
const field = this.#effectiveChannels.color
|
|
89
|
+
if (!field) return 'categorical'
|
|
90
|
+
return inferColorScaleType(this.#data, field, {
|
|
91
|
+
colorScale: this.#colorSpec,
|
|
92
|
+
colorMidpoint: this.#colorMidpoint
|
|
93
|
+
})
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
xScale = $derived.by(() => {
|
|
97
|
+
const field = this.#effectiveChannels.x
|
|
98
|
+
if (!field) return null
|
|
99
|
+
const datasets =
|
|
100
|
+
this.#geoms.length > 0 ? this.#geoms.map((g) => this.geomData(g.id)) : [this.#rawData]
|
|
101
|
+
const includeZero = this.orientation === 'horizontal'
|
|
102
|
+
// For vertical bar charts, force scaleBand even when X values are numeric (e.g. year).
|
|
103
|
+
// Horizontal bar charts keep X as a continuous value axis.
|
|
104
|
+
const hasBarGeom = this.#geoms.some((g) => g.type === 'bar')
|
|
105
|
+
const bandX = hasBarGeom && this.orientation !== 'horizontal'
|
|
106
|
+
const base = buildUnifiedXScale(datasets, field, this.#innerWidth, {
|
|
107
|
+
domain: this.#xDomain,
|
|
108
|
+
includeZero,
|
|
109
|
+
band: bandX
|
|
110
|
+
})
|
|
111
|
+
return this.#zoomTransform && typeof base?.bandwidth !== 'function'
|
|
112
|
+
? this.#zoomTransform.rescaleX(base)
|
|
113
|
+
: base
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
// For box/violin geoms, compute y domain from iqr_min/iqr_max instead of raw y values.
|
|
117
|
+
#resolveBoxDomain() {
|
|
118
|
+
const boxGeom = this.#geoms.find((g) => g.type === 'box' || g.type === 'violin')
|
|
119
|
+
if (!boxGeom) return null
|
|
120
|
+
const boxData = this.geomData(boxGeom.id)
|
|
121
|
+
const isValid = (v) => v !== null && v !== undefined && !isNaN(v)
|
|
122
|
+
const mins = boxData.map((d) => d.iqr_min).filter(isValid)
|
|
123
|
+
const maxs = boxData.map((d) => d.iqr_max).filter(isValid)
|
|
124
|
+
return mins.length > 0 && maxs.length > 0 ? [Math.min(...mins), Math.max(...maxs)] : null
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// For stacked bars, compute y domain from per-x column totals.
|
|
128
|
+
#resolveStackDomain(field) {
|
|
129
|
+
const stackGeom = this.#geoms.find((g) => g.options?.stack)
|
|
130
|
+
if (!stackGeom) return null
|
|
131
|
+
const xField = this.#effectiveChannels.x
|
|
132
|
+
const stackData = this.geomData(stackGeom.id)
|
|
133
|
+
if (!xField || stackData.length === 0) return null
|
|
134
|
+
// Mirror buildStackedBars/subBandFields: stack dimension is the first
|
|
135
|
+
// non-x field among [color, pattern]. Summing all raw rows (stat=identity)
|
|
136
|
+
// would overcount when multiple rows share the same (x, stack) key.
|
|
137
|
+
const colorField = isLiteralColor(this.#effectiveChannels.color)
|
|
138
|
+
? null
|
|
139
|
+
: this.#effectiveChannels.color
|
|
140
|
+
const patternField = this.#effectiveChannels.pattern
|
|
141
|
+
const stackField = [colorField, patternField].find((f) => f && f !== xField) ?? colorField
|
|
142
|
+
const lookup = new SvelteMap()
|
|
143
|
+
for (const d of stackData) {
|
|
144
|
+
const xVal = d[xField]
|
|
145
|
+
const cKey = stackField ? String(d[stackField]) : '_'
|
|
146
|
+
if (!lookup.has(xVal)) lookup.set(xVal, new SvelteMap())
|
|
147
|
+
lookup.get(xVal).set(cKey, Number(d[field]) || 0)
|
|
148
|
+
}
|
|
149
|
+
const totals = new SvelteMap()
|
|
150
|
+
for (const [xVal, colorMap] of lookup) {
|
|
151
|
+
totals.set(
|
|
152
|
+
xVal,
|
|
153
|
+
[...colorMap.values()].reduce((s, v) => s + v, 0)
|
|
154
|
+
)
|
|
155
|
+
}
|
|
156
|
+
return [0, Math.max(0, ...totals.values())]
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
yScale = $derived.by(() => {
|
|
160
|
+
const field = this.#effectiveChannels.y
|
|
161
|
+
if (!field) return null
|
|
162
|
+
const datasets =
|
|
163
|
+
this.#geoms.length > 0 ? this.#geoms.map((g) => this.geomData(g.id)) : [this.#rawData]
|
|
164
|
+
const includeZero = this.orientation === 'vertical'
|
|
165
|
+
const yDomain = this.#yDomain ?? this.#resolveBoxDomain() ?? this.#resolveStackDomain(field)
|
|
166
|
+
const base = buildUnifiedYScale(datasets, field, this.#innerHeight, { domain: yDomain, includeZero })
|
|
167
|
+
return this.#zoomTransform ? this.#zoomTransform.rescaleY(base) : base
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
// Colors: Map<colorKey, { fill, stroke }> for all distinct color field values.
|
|
171
|
+
// If the color channel is a CSS literal (e.g. '#4a90d9'), return a singleton map
|
|
172
|
+
// keyed by null so all marks pick it up via the fallback path.
|
|
173
|
+
// If a colorDomain is provided (e.g. from FacetPlot for cross-panel consistency),
|
|
174
|
+
// use it instead of deriving distinct values from the local panel data.
|
|
175
|
+
colors = $derived.by(() => {
|
|
176
|
+
const field = this.#effectiveChannels.color
|
|
177
|
+
if (isLiteralColor(field)) return new Map([[null, { fill: field, stroke: field }]])
|
|
178
|
+
const values = this.#colorDomain ?? distinct(this.#data, field)
|
|
179
|
+
// No color channel but data exists → use first preset color for single-series rendering.
|
|
180
|
+
// This prevents geoms from falling back to gray (#888) on charts with no fill channel.
|
|
181
|
+
if (values.length === 0 && this.#data.length > 0) return assignColors([null], this.#mode, this.#chartPreset)
|
|
182
|
+
return assignColors(values, this.#mode, this.#chartPreset)
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
// Patterns: Map<patternKey, patternName> — only populated when a pattern channel is set
|
|
186
|
+
// and the pattern field is categorical (continuous fields can't be discretely patterned).
|
|
187
|
+
patterns = $derived.by(() => {
|
|
188
|
+
const pf = this.#effectiveChannels.pattern
|
|
189
|
+
if (!pf) return new SvelteMap()
|
|
190
|
+
if (inferFieldType(this.#data, pf) === 'continuous') return new SvelteMap()
|
|
191
|
+
return assignPatterns(distinct(this.#data, pf))
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
// Symbols: Map<symbolKey, shapeName> — only populated when a symbol channel is set.
|
|
195
|
+
symbols = $derived.by(() => {
|
|
196
|
+
const sf = this.#effectiveChannels.symbol
|
|
197
|
+
if (!sf) return new SvelteMap()
|
|
198
|
+
return assignSymbols(distinct(this.#data, sf), this.#chartPreset)
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
// Expose effective channel fields for consumers (e.g. Legend).
|
|
202
|
+
// Returns null for literal CSS colors since they don't map to a data field.
|
|
203
|
+
colorField = $derived(
|
|
204
|
+
isLiteralColor(this.#effectiveChannels.color) ? null : this.#effectiveChannels.color
|
|
205
|
+
)
|
|
206
|
+
patternField = $derived(this.#effectiveChannels.pattern)
|
|
207
|
+
symbolField = $derived(this.#effectiveChannels.symbol)
|
|
208
|
+
|
|
209
|
+
// Set of geom types currently registered (used by Legend to pick swatch style)
|
|
210
|
+
geomTypes = $derived(new SvelteSet(this.#geoms.map((g) => g.type)))
|
|
211
|
+
|
|
212
|
+
xAxisY = $derived.by(() => {
|
|
213
|
+
if (!this.yScale || typeof this.yScale !== 'function') return this.#innerHeight
|
|
214
|
+
const crossVal = this.axisOrigin[1]
|
|
215
|
+
if (crossVal !== undefined) return this.yScale(crossVal)
|
|
216
|
+
const domain = this.yScale.domain?.()
|
|
217
|
+
if (!domain) return this.#innerHeight
|
|
218
|
+
// Auto quadrant: place x-axis at y=0 when domain spans zero
|
|
219
|
+
if (domain[0] <= 0 && domain[domain.length - 1] >= 0) return this.yScale(0)
|
|
220
|
+
return this.yScale(domain[0])
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
yAxisX = $derived.by(() => {
|
|
224
|
+
if (!this.xScale || typeof this.xScale !== 'function') return 0
|
|
225
|
+
const crossVal = this.axisOrigin[0]
|
|
226
|
+
if (crossVal !== undefined) return this.xScale(crossVal)
|
|
227
|
+
const domain = this.xScale.domain?.()
|
|
228
|
+
if (!domain || typeof this.xScale.bandwidth === 'function') return 0
|
|
229
|
+
// Auto quadrant: place y-axis at x=0 when domain spans zero
|
|
230
|
+
if (domain[0] <= 0 && domain[domain.length - 1] >= 0) return this.xScale(0)
|
|
231
|
+
return this.xScale(domain[0])
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
constructor(config = {}) {
|
|
235
|
+
this.#rawData = config.data ?? []
|
|
236
|
+
this.#data = config.data ?? []
|
|
237
|
+
this.#channels = config.channels ?? {}
|
|
238
|
+
this.#labels = config.labels ?? {}
|
|
239
|
+
this.#helpers = config.helpers ?? {}
|
|
240
|
+
this.#presetName = config.preset
|
|
241
|
+
this.#colorMidpoint = config.colorMidpoint
|
|
242
|
+
this.#colorSpec = config.colorScale
|
|
243
|
+
this.#colorDomain = config.colorDomain
|
|
244
|
+
this.#xDomain = config.xDomain
|
|
245
|
+
this.#yDomain = config.yDomain
|
|
246
|
+
this.#width = config.width ?? 600
|
|
247
|
+
this.#height = config.height ?? 400
|
|
248
|
+
this.#mode = config.mode ?? 'light'
|
|
249
|
+
this.#chartPreset = config.chartPreset ?? defaultPreset
|
|
250
|
+
this.#marginOverride = config.margin ?? undefined
|
|
251
|
+
this.#orientationOverride = config.orientation ?? undefined
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
update(config) {
|
|
255
|
+
if (config.data !== undefined) {
|
|
256
|
+
this.#rawData = config.data
|
|
257
|
+
this.#data = config.data
|
|
258
|
+
}
|
|
259
|
+
if (config.channels !== undefined) this.#channels = config.channels
|
|
260
|
+
if (config.labels !== undefined) this.#labels = config.labels
|
|
261
|
+
if (config.helpers !== undefined) this.#helpers = config.helpers
|
|
262
|
+
if (config.preset !== undefined) this.#presetName = config.preset
|
|
263
|
+
if (config.colorMidpoint !== undefined) this.#colorMidpoint = config.colorMidpoint
|
|
264
|
+
if (config.colorScale !== undefined) this.#colorSpec = config.colorScale
|
|
265
|
+
this.#colorDomain = config.colorDomain
|
|
266
|
+
this.#xDomain = config.xDomain
|
|
267
|
+
this.#yDomain = config.yDomain
|
|
268
|
+
if (config.width !== undefined) this.#width = config.width
|
|
269
|
+
if (config.height !== undefined) this.#height = config.height
|
|
270
|
+
if (config.mode !== undefined) this.#mode = config.mode
|
|
271
|
+
if (config.chartPreset !== undefined) this.#chartPreset = config.chartPreset
|
|
272
|
+
this.#marginOverride = config.margin ?? undefined
|
|
273
|
+
this.#orientationOverride = config.orientation ?? undefined
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
registerGeom(config) {
|
|
277
|
+
const id = `geom-${nextId++}`
|
|
278
|
+
this.#geoms = [...this.#geoms, { id, ...config }]
|
|
279
|
+
return id
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
updateGeom(id, config) {
|
|
283
|
+
// untrack the read of #geoms to avoid effect_update_depth_exceeded when
|
|
284
|
+
// called from a geom's $effect (which would otherwise track #geoms as a dependency)
|
|
285
|
+
this.#geoms = untrack(() => this.#geoms).map((g) => (g.id === id ? { ...g, ...config } : g))
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
unregisterGeom(id) {
|
|
289
|
+
this.#geoms = this.#geoms.filter((g) => g.id !== id)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
geomData(id) {
|
|
293
|
+
const geom = this.#geoms.find((g) => g.id === id)
|
|
294
|
+
if (!geom) return []
|
|
295
|
+
const stat = geom.stat ?? 'identity'
|
|
296
|
+
if (stat === 'identity') return this.#rawData
|
|
297
|
+
const mergedChannels = { ...this.#channels, ...geom.channels }
|
|
298
|
+
return applyGeomStat(this.#rawData, { stat, channels: mergedChannels }, this.#helpers)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
label(field) {
|
|
302
|
+
return this.#labels?.[field] ?? field
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
format(field) {
|
|
306
|
+
return resolveFormat(field, this.#helpers)
|
|
307
|
+
}
|
|
308
|
+
tooltip() {
|
|
309
|
+
return resolveTooltip(this.#helpers)
|
|
310
|
+
}
|
|
311
|
+
geomComponent(type) {
|
|
312
|
+
return resolveGeom(type, this.#helpers)
|
|
313
|
+
}
|
|
314
|
+
preset() {
|
|
315
|
+
return resolvePreset(this.#presetName, this.#helpers)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
get margin() {
|
|
319
|
+
return this.#effectiveMargin
|
|
320
|
+
}
|
|
321
|
+
get innerWidth() {
|
|
322
|
+
return this.#innerWidth
|
|
323
|
+
}
|
|
324
|
+
get innerHeight() {
|
|
325
|
+
return this.#innerHeight
|
|
326
|
+
}
|
|
327
|
+
get mode() {
|
|
328
|
+
return this.#mode
|
|
329
|
+
}
|
|
330
|
+
get chartPreset() {
|
|
331
|
+
return this.#chartPreset
|
|
332
|
+
}
|
|
333
|
+
get hovered() {
|
|
334
|
+
return this.#hovered
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
setHovered(data) {
|
|
338
|
+
this.#hovered = data
|
|
339
|
+
}
|
|
340
|
+
clearHovered() {
|
|
341
|
+
this.#hovered = null
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
applyZoom(transform) {
|
|
345
|
+
this.#zoomTransform = transform
|
|
346
|
+
}
|
|
347
|
+
resetZoom() {
|
|
348
|
+
this.#zoomTransform = null
|
|
349
|
+
}
|
|
277
350
|
}
|