@octane-xplat/charts 0.19.0 → 0.20.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/README.md +2 -102
- package/package.json +14 -14
- package/src/Chart.macos.tsrx +248 -246
- package/src/Chart.tsrx +249 -247
- package/src/Chart.web.tsrx +236 -226
- package/src/appkit-pointer.macos.ts +91 -76
- package/src/core/axes.ts +175 -168
- package/src/core/hit.ts +197 -177
- package/src/core/marks.ts +336 -319
- package/src/core/scales.ts +128 -115
- package/src/core/scene.ts +123 -120
- package/src/core/svg.ts +52 -50
- package/src/index.macos.ts +7 -7
- package/src/index.ts +7 -7
- package/src/index.web.ts +7 -7
- package/src/props.ts +83 -83
- package/src/style-merge.ts +40 -37
- package/src/svg-image.macos.ts +21 -20
- package/types/generated/.tsrx-typegen-manifest.json +1 -7
- package/types/generated/Chart.macos.d.ts +0 -7
- package/types/generated/Chart.web.d.ts +0 -10
- package/types/generated/appkit-pointer.macos.d.ts +0 -37
- package/types/generated/index.macos.d.ts +0 -2
- package/types/generated/index.web.d.ts +0 -2
- package/types/generated/svg-image.macos.d.ts +0 -3
package/src/core/marks.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { scaleBand } from 'd3-scale'
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
arc as arcPath,
|
|
4
|
+
area as areaPath,
|
|
5
|
+
curveMonotoneX,
|
|
6
|
+
line as linePath,
|
|
7
|
+
pie as pieLayout,
|
|
8
8
|
} from 'd3-shape'
|
|
9
9
|
|
|
10
10
|
import type { ChartDatum, ChartType } from '../props'
|
|
@@ -15,368 +15,385 @@ import { categoryKey, xCenter } from './scales'
|
|
|
15
15
|
/** Presentation attributes — the androidsvg ∩ SVGKit subset only (fills,
|
|
16
16
|
* strokes, opacity). No filters, no text, no radial gradients. */
|
|
17
17
|
export interface Paint {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
fill?: string
|
|
19
|
+
fillOpacity?: number
|
|
20
|
+
stroke?: string
|
|
21
|
+
strokeOpacity?: number
|
|
22
|
+
strokeWidth?: number
|
|
23
|
+
strokeLinecap?: 'butt' | 'round' | 'square'
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
/** Datum reference — lets the overlay and hit-test map marks back to data. */
|
|
27
27
|
interface DatumRef {
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
series?: number
|
|
29
|
+
index?: number
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
export type Mark = DatumRef &
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
Paint & { transform?: string } & (
|
|
34
|
+
| { kind: 'path'; d: string }
|
|
35
|
+
| { kind: 'rect'; x: number; y: number; width: number; height: number }
|
|
36
|
+
| { kind: 'circle'; cx: number; cy: number; r: number }
|
|
37
|
+
| { kind: 'line'; x1: number; y1: number; x2: number; y2: number }
|
|
38
|
+
)
|
|
39
39
|
|
|
40
40
|
/** One hit-testable datum position in chart space. */
|
|
41
41
|
export interface ScenePoint {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
42
|
+
series: number
|
|
43
|
+
index: number
|
|
44
|
+
/** Snap-to-x group — String(datum.x). */
|
|
45
|
+
key: string
|
|
46
|
+
cx: number
|
|
47
|
+
cy: number
|
|
48
|
+
x: number | string | Date
|
|
49
|
+
y: number
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
export interface PieSlice {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
53
|
+
series: number
|
|
54
|
+
index: number
|
|
55
|
+
label: string
|
|
56
|
+
value: number
|
|
57
|
+
color: string
|
|
58
|
+
startAngle: number
|
|
59
|
+
endAngle: number
|
|
60
|
+
/** Slice centroid relative to the pie center — the tooltip anchor. */
|
|
61
|
+
cx: number
|
|
62
|
+
cy: number
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export interface PieScene {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
/** Pie center in chart space (plot offset applied by scene.ts). */
|
|
67
|
+
cx: number
|
|
68
|
+
cy: number
|
|
69
|
+
rOuter: number
|
|
70
|
+
rInner: number
|
|
71
|
+
slices: PieSlice[]
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
export interface ChartLayout {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
plot: PlotBox
|
|
76
|
+
xKind: XScaleKind
|
|
77
|
+
x: XScale
|
|
78
|
+
y: YScale
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
export interface BuiltMarks {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
marks: Mark[]
|
|
83
|
+
points: ScenePoint[]
|
|
84
|
+
pie?: PieScene
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
export function buildMarks(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
88
|
+
type: ChartType,
|
|
89
|
+
layout: ChartLayout,
|
|
90
|
+
series: ResolvedSeries[],
|
|
91
|
+
options: { stacked?: boolean; innerRadiusRatio?: number; palette: string[] },
|
|
92
92
|
): BuiltMarks {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
93
|
+
switch (type) {
|
|
94
|
+
case 'line':
|
|
95
|
+
return lineMarks(layout, series)
|
|
96
|
+
case 'area':
|
|
97
|
+
return areaMarks(layout, series)
|
|
98
|
+
case 'scatter':
|
|
99
|
+
return scatterMarks(layout, series)
|
|
100
|
+
case 'bar':
|
|
101
|
+
return options.stacked ? stackedBarMarks(layout, series) : groupedBarMarks(layout, series)
|
|
102
|
+
case 'pie':
|
|
103
|
+
return pieMarks(layout, series, options)
|
|
104
|
+
}
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
function lineMarks(layout: ChartLayout, series: ResolvedSeries[]): BuiltMarks {
|
|
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
|
-
|
|
108
|
+
const { xKind, x, y } = layout
|
|
109
|
+
const gen = linePath<ChartDatum>()
|
|
110
|
+
.defined((d) => Number.isFinite(d.y) && Number.isFinite(xCenter(xKind, x, d.x)))
|
|
111
|
+
.x((d) => xCenter(xKind, x, d.x))
|
|
112
|
+
.y((d) => y(d.y))
|
|
113
|
+
.curve(curveMonotoneX)
|
|
114
|
+
|
|
115
|
+
const marks: Mark[] = []
|
|
116
|
+
const points: ScenePoint[] = []
|
|
117
|
+
|
|
118
|
+
series.forEach((s, si) => {
|
|
119
|
+
marks.push({
|
|
120
|
+
kind: 'path',
|
|
121
|
+
d: gen(s.values) ?? '',
|
|
122
|
+
fill: 'none',
|
|
123
|
+
stroke: s.color,
|
|
124
|
+
strokeWidth: 2,
|
|
125
|
+
strokeLinecap: 'round',
|
|
126
|
+
series: si,
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
s.values.forEach((d, i) => {
|
|
130
|
+
const cx = xCenter(xKind, x, d.x)
|
|
131
|
+
const cy = y(d.y)
|
|
132
|
+
|
|
133
|
+
if (!Number.isFinite(cx) || !Number.isFinite(cy)) {
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
points.push({ series: si, index: i, key: categoryKey(d.x), cx, cy, x: d.x, y: d.y })
|
|
138
|
+
})
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
return { marks, points }
|
|
140
142
|
}
|
|
141
143
|
|
|
142
144
|
function areaMarks(layout: ChartLayout, series: ResolvedSeries[]): BuiltMarks {
|
|
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
|
-
|
|
145
|
+
const { xKind, x, y } = layout
|
|
146
|
+
const zero = y(Math.max(y.domain()[0], Math.min(0, y.domain()[1])))
|
|
147
|
+
const defined = (d: ChartDatum) => Number.isFinite(d.y) && Number.isFinite(xCenter(xKind, x, d.x))
|
|
148
|
+
|
|
149
|
+
const gen = areaPath<ChartDatum>()
|
|
150
|
+
.defined(defined)
|
|
151
|
+
.x((d) => xCenter(xKind, x, d.x))
|
|
152
|
+
.y0(zero)
|
|
153
|
+
.y1((d) => y(d.y))
|
|
154
|
+
.curve(curveMonotoneX)
|
|
155
|
+
|
|
156
|
+
const stroke = linePath<ChartDatum>()
|
|
157
|
+
.defined(defined)
|
|
158
|
+
.x((d) => xCenter(xKind, x, d.x))
|
|
159
|
+
.y((d) => y(d.y))
|
|
160
|
+
.curve(curveMonotoneX)
|
|
161
|
+
|
|
162
|
+
const marks: Mark[] = []
|
|
163
|
+
const points: ScenePoint[] = []
|
|
164
|
+
|
|
165
|
+
series.forEach((s, si) => {
|
|
166
|
+
marks.push({
|
|
167
|
+
kind: 'path',
|
|
168
|
+
d: gen(s.values) ?? '',
|
|
169
|
+
fill: s.color,
|
|
170
|
+
fillOpacity: 0.25,
|
|
171
|
+
series: si,
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
marks.push({
|
|
175
|
+
kind: 'path',
|
|
176
|
+
d: stroke(s.values) ?? '',
|
|
177
|
+
fill: 'none',
|
|
178
|
+
stroke: s.color,
|
|
179
|
+
strokeWidth: 2,
|
|
180
|
+
series: si,
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
s.values.forEach((d, i) => {
|
|
184
|
+
const cx = xCenter(xKind, x, d.x)
|
|
185
|
+
const cy = y(d.y)
|
|
186
|
+
|
|
187
|
+
if (!Number.isFinite(cx) || !Number.isFinite(cy)) {
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
points.push({ series: si, index: i, key: categoryKey(d.x), cx, cy, x: d.x, y: d.y })
|
|
192
|
+
})
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
return { marks, points }
|
|
192
196
|
}
|
|
193
197
|
|
|
194
198
|
function scatterMarks(layout: ChartLayout, series: ResolvedSeries[]): BuiltMarks {
|
|
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
|
-
|
|
199
|
+
const { xKind, x, y } = layout
|
|
200
|
+
const marks: Mark[] = []
|
|
201
|
+
const points: ScenePoint[] = []
|
|
202
|
+
|
|
203
|
+
series.forEach((s, si) => {
|
|
204
|
+
s.values.forEach((d, i) => {
|
|
205
|
+
const cx = xCenter(xKind, x, d.x)
|
|
206
|
+
const cy = y(d.y)
|
|
207
|
+
|
|
208
|
+
if (!Number.isFinite(cx) || !Number.isFinite(cy)) {
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
marks.push({
|
|
213
|
+
kind: 'circle',
|
|
214
|
+
cx,
|
|
215
|
+
cy,
|
|
216
|
+
r: 3.5,
|
|
217
|
+
fill: d.color ?? s.color,
|
|
218
|
+
fillOpacity: 0.85,
|
|
219
|
+
series: si,
|
|
220
|
+
index: i,
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
points.push({ series: si, index: i, key: categoryKey(d.x), cx, cy, x: d.x, y: d.y })
|
|
224
|
+
})
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
return { marks, points }
|
|
222
228
|
}
|
|
223
229
|
|
|
224
230
|
function groupedBarMarks(layout: ChartLayout, series: ResolvedSeries[]): BuiltMarks {
|
|
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
|
-
|
|
231
|
+
const { x, y } = layout
|
|
232
|
+
const outer = x as ScaleBand<string>
|
|
233
|
+
const inner = scaleBand<string>()
|
|
234
|
+
.domain(series.map((_, i) => String(i)))
|
|
235
|
+
.range([0, outer.bandwidth()])
|
|
236
|
+
.padding(0.08)
|
|
237
|
+
|
|
238
|
+
const marks: Mark[] = []
|
|
239
|
+
const points: ScenePoint[] = []
|
|
240
|
+
|
|
241
|
+
series.forEach((s, si) => {
|
|
242
|
+
const offset = inner(String(si)) ?? 0
|
|
243
|
+
const width = inner.bandwidth()
|
|
244
|
+
|
|
245
|
+
s.values.forEach((d, i) => {
|
|
246
|
+
const band = outer(String(d.x))
|
|
247
|
+
|
|
248
|
+
if (band === undefined || !Number.isFinite(d.y)) {
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const top = y(Math.max(0, d.y))
|
|
253
|
+
const bottom = y(Math.min(0, d.y))
|
|
254
|
+
|
|
255
|
+
marks.push({
|
|
256
|
+
kind: 'rect',
|
|
257
|
+
x: band + offset,
|
|
258
|
+
y: top,
|
|
259
|
+
width,
|
|
260
|
+
height: Math.max(0, bottom - top),
|
|
261
|
+
fill: d.color ?? s.color,
|
|
262
|
+
series: si,
|
|
263
|
+
index: i,
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
points.push({
|
|
267
|
+
series: si,
|
|
268
|
+
index: i,
|
|
269
|
+
key: categoryKey(d.x),
|
|
270
|
+
cx: band + offset + width / 2,
|
|
271
|
+
cy: y(d.y),
|
|
272
|
+
x: d.x,
|
|
273
|
+
y: d.y,
|
|
274
|
+
})
|
|
275
|
+
})
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
return { marks, points }
|
|
269
279
|
}
|
|
270
280
|
|
|
271
281
|
/** Stacked bars accumulate per category — positive values stack up from the
|
|
272
282
|
* baseline, negatives down, so mixed-sign data stays correct. */
|
|
273
283
|
function stackedBarMarks(layout: ChartLayout, series: ResolvedSeries[]): BuiltMarks {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
284
|
+
const { x, y } = layout
|
|
285
|
+
const outer = x as ScaleBand<string>
|
|
286
|
+
const posBase = new Map<string, number>()
|
|
287
|
+
const negBase = new Map<string, number>()
|
|
288
|
+
|
|
289
|
+
const marks: Mark[] = []
|
|
290
|
+
const points: ScenePoint[] = []
|
|
291
|
+
|
|
292
|
+
series.forEach((s, si) => {
|
|
293
|
+
s.values.forEach((d, i) => {
|
|
294
|
+
const cat = String(d.x)
|
|
295
|
+
const band = outer(cat)
|
|
296
|
+
|
|
297
|
+
if (band === undefined || !Number.isFinite(d.y)) {
|
|
298
|
+
return
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const v0 = d.y >= 0 ? (posBase.get(cat) ?? 0) : (negBase.get(cat) ?? 0)
|
|
302
|
+
const v1 = v0 + d.y
|
|
303
|
+
|
|
304
|
+
if (d.y >= 0) {
|
|
305
|
+
posBase.set(cat, v1)
|
|
306
|
+
} else {
|
|
307
|
+
negBase.set(cat, v1)
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const top = y(Math.max(v0, v1))
|
|
311
|
+
const bottom = y(Math.min(v0, v1))
|
|
312
|
+
|
|
313
|
+
marks.push({
|
|
314
|
+
kind: 'rect',
|
|
315
|
+
x: band,
|
|
316
|
+
y: top,
|
|
317
|
+
width: outer.bandwidth(),
|
|
318
|
+
height: Math.max(0, bottom - top),
|
|
319
|
+
fill: d.color ?? s.color,
|
|
320
|
+
series: si,
|
|
321
|
+
index: i,
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
points.push({
|
|
325
|
+
series: si,
|
|
326
|
+
index: i,
|
|
327
|
+
key: cat,
|
|
328
|
+
cx: band + outer.bandwidth() / 2,
|
|
329
|
+
cy: y(v1),
|
|
330
|
+
x: d.x,
|
|
331
|
+
y: d.y,
|
|
332
|
+
})
|
|
333
|
+
})
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
return { marks, points }
|
|
323
337
|
}
|
|
324
338
|
|
|
325
339
|
/** Pie consumes every datum across series — each {x: label, y: value} is a
|
|
326
340
|
* slice; slice colors are categorical (palette by order) with datum.color
|
|
327
341
|
* overrides. A series color is ignored on pie — it would be monochrome. */
|
|
328
342
|
function pieMarks(
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
343
|
+
layout: ChartLayout,
|
|
344
|
+
series: ResolvedSeries[],
|
|
345
|
+
options: { innerRadiusRatio?: number; palette: string[] },
|
|
332
346
|
): BuiltMarks {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
347
|
+
const { plot } = layout
|
|
348
|
+
const flat: { d: ChartDatum; si: number; i: number }[] = []
|
|
349
|
+
|
|
350
|
+
series.forEach((s, si) => s.values.forEach((d, i) => flat.push({ d, si, i })))
|
|
351
|
+
|
|
352
|
+
// Marks are plot-space; d3 arcs generate around (0,0), so each slice path
|
|
353
|
+
// carries its own translate to the pie center and scene.ts folds the plot
|
|
354
|
+
// offset into the published pie center.
|
|
355
|
+
const cx = plot.width / 2
|
|
356
|
+
const cy = plot.height / 2
|
|
357
|
+
const rOuter = Math.max(0, Math.min(plot.width, plot.height) / 2)
|
|
358
|
+
const ratio = options.innerRadiusRatio ?? 0
|
|
359
|
+
const rInner = rOuter * Math.min(Math.max(ratio, 0), 0.95)
|
|
360
|
+
|
|
361
|
+
const arcs = pieLayout<{ d: ChartDatum; si: number; i: number }>()
|
|
362
|
+
.value((v) => Math.max(0, v.d.y))
|
|
363
|
+
.sort(null)(flat)
|
|
364
|
+
|
|
365
|
+
const gen = arcPath<any, any>().innerRadius(rInner).outerRadius(rOuter)
|
|
366
|
+
|
|
367
|
+
const marks: Mark[] = []
|
|
368
|
+
const slices: PieSlice[] = []
|
|
369
|
+
|
|
370
|
+
arcs.forEach((a, order) => {
|
|
371
|
+
// Pie slices are categorical — palette by slice order; a series color on
|
|
372
|
+
// pie would monochrome every slice. datum.color overrides.
|
|
373
|
+
const color = a.data.d.color ?? options.palette[order % options.palette.length]
|
|
374
|
+
const centroid = gen.centroid(a)
|
|
375
|
+
|
|
376
|
+
marks.push({
|
|
377
|
+
kind: 'path',
|
|
378
|
+
d: gen(a) ?? '',
|
|
379
|
+
transform: `translate(${cx} ${cy})`,
|
|
380
|
+
fill: color,
|
|
381
|
+
series: a.data.si,
|
|
382
|
+
index: a.data.i,
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
slices.push({
|
|
386
|
+
series: a.data.si,
|
|
387
|
+
index: a.data.i,
|
|
388
|
+
label: String(a.data.d.x),
|
|
389
|
+
value: a.data.d.y,
|
|
390
|
+
color,
|
|
391
|
+
startAngle: a.startAngle,
|
|
392
|
+
endAngle: a.endAngle,
|
|
393
|
+
cx: centroid[0],
|
|
394
|
+
cy: centroid[1],
|
|
395
|
+
})
|
|
396
|
+
})
|
|
397
|
+
|
|
398
|
+
return { marks, points: [], pie: { cx, cy, rOuter, rInner, slices } }
|
|
382
399
|
}
|