@octane-xplat/charts 0.18.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 +5 -0
- package/package.json +93 -93
- package/src/Chart.macos.tsrx +269 -107
- package/src/Chart.tsrx +274 -139
- package/src/Chart.web.tsrx +264 -206
- package/src/appkit-pointer.macos.ts +113 -0
- package/src/core/axes.ts +175 -168
- package/src/core/hit.ts +199 -174
- package/src/core/marks.ts +336 -319
- package/src/core/scales.ts +128 -115
- package/src/core/scene.ts +124 -121
- 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 +90 -63
- package/src/style-merge.ts +43 -45
- package/src/svg-image.macos.ts +21 -20
- package/types/generated/.tsrx-typegen-manifest.json +15 -0
- package/types/generated/Chart.d.ts +9 -0
- package/types/generated/core/axes.d.ts +26 -0
- package/types/generated/core/hit.d.ts +34 -0
- package/types/generated/core/marks.d.ts +88 -0
- package/types/generated/core/scales.d.ts +37 -0
- package/types/generated/core/scene.d.ts +30 -0
- package/types/generated/core/svg.d.ts +5 -0
- package/types/generated/index.d.ts +2 -0
- package/types/{props.d.ts → generated/props.d.ts} +27 -0
- package/types/generated/style-merge.d.ts +8 -0
- package/LICENSE +0 -21
- package/types/index.d.ts +0 -17
package/src/core/scales.ts
CHANGED
|
@@ -6,167 +6,180 @@ import type { ChartType, SeriesSpec } from '../props'
|
|
|
6
6
|
export type XScaleKind = 'band' | 'point' | 'linear' | 'time'
|
|
7
7
|
|
|
8
8
|
export type XScale =
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
| ScaleBand<string>
|
|
10
|
+
| ScalePoint<string>
|
|
11
|
+
| ScaleLinear<number, number>
|
|
12
|
+
| ScaleTime<number, number>
|
|
13
13
|
|
|
14
14
|
export type YScale = ScaleLinear<number, number>
|
|
15
15
|
|
|
16
16
|
export interface PlotBox {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
x: number
|
|
18
|
+
y: number
|
|
19
|
+
width: number
|
|
20
|
+
height: number
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export interface ResolvedSeries {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
name: string
|
|
25
|
+
color: string
|
|
26
|
+
values: { x: number | string | Date; y: number; color?: string }[]
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
/** Categorical x values across all series, first-seen order — the band/point
|
|
30
30
|
* domain. */
|
|
31
31
|
export function categoryDomain(data: SeriesSpec[]): string[] {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
32
|
+
const seen = new Set<string>()
|
|
33
|
+
const out: string[] = []
|
|
34
|
+
|
|
35
|
+
for (const series of data) {
|
|
36
|
+
for (const d of series.values) {
|
|
37
|
+
const key = String(d.x)
|
|
38
|
+
|
|
39
|
+
if (!seen.has(key)) {
|
|
40
|
+
seen.add(key)
|
|
41
|
+
out.push(key)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return out
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
/** A categorical x lookup — band/point domains key on String(x). */
|
|
48
50
|
export function categoryKey(x: number | string | Date): string {
|
|
49
|
-
|
|
51
|
+
return String(x)
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
/** Pick the x scale family: bar is always band, other cartesian types infer
|
|
53
55
|
* from the first datum — Date → time, string → point, number → linear. */
|
|
54
56
|
export function inferXKind(type: ChartType, data: SeriesSpec[]): XScaleKind {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
if (type === 'bar') {
|
|
58
|
+
return 'band'
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const first = data[0]?.values[0]?.x
|
|
58
62
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
+
if (first instanceof Date) {
|
|
64
|
+
return 'time'
|
|
65
|
+
}
|
|
63
66
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
+
if (typeof first === 'string') {
|
|
68
|
+
return 'point'
|
|
69
|
+
}
|
|
67
70
|
|
|
68
|
-
|
|
71
|
+
return 'linear'
|
|
69
72
|
}
|
|
70
73
|
|
|
71
74
|
export function buildXScale(kind: XScaleKind, data: SeriesSpec[], width: number): XScale {
|
|
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
|
-
|
|
75
|
+
switch (kind) {
|
|
76
|
+
case 'band':
|
|
77
|
+
return scaleBand<string>()
|
|
78
|
+
.domain(categoryDomain(data))
|
|
79
|
+
.range([0, width])
|
|
80
|
+
.paddingInner(0.25)
|
|
81
|
+
.paddingOuter(0.15)
|
|
82
|
+
case 'point': {
|
|
83
|
+
const domain = categoryDomain(data)
|
|
84
|
+
|
|
85
|
+
return scalePoint<string>()
|
|
86
|
+
.domain(domain)
|
|
87
|
+
.range([0, width])
|
|
88
|
+
.padding(domain.length <= 1 ? 0 : 0.5)
|
|
89
|
+
}
|
|
90
|
+
case 'time': {
|
|
91
|
+
const values = data.flatMap((s) => s.values.map((d) => d.x as Date))
|
|
92
|
+
const [lo, hi] = extent(values)
|
|
93
|
+
|
|
94
|
+
if (lo === undefined || hi === undefined) {
|
|
95
|
+
const now = new Date()
|
|
96
|
+
|
|
97
|
+
return scaleTime().domain([now, now]).range([0, width])
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return scaleTime()
|
|
101
|
+
.domain(padDomain(+lo, +hi).map((v) => new Date(v)))
|
|
102
|
+
.range([0, width])
|
|
103
|
+
.nice()
|
|
104
|
+
}
|
|
105
|
+
case 'linear': {
|
|
106
|
+
const values = data.flatMap((s) => s.values.map((d) => Number(d.x)))
|
|
107
|
+
const [lo, hi] = extent(values)
|
|
108
|
+
|
|
109
|
+
return scaleLinear()
|
|
110
|
+
.domain(padDomain(lo ?? 0, hi ?? 1))
|
|
111
|
+
.range([0, width])
|
|
112
|
+
.nice()
|
|
113
|
+
}
|
|
114
|
+
}
|
|
108
115
|
}
|
|
109
116
|
|
|
110
117
|
/** Degenerate domains (one value) get a unit pad so the scale still maps
|
|
111
118
|
* somewhere sensible. */
|
|
112
119
|
function padDomain(lo: number, hi: number): [number, number] {
|
|
113
|
-
|
|
120
|
+
return lo === hi ? [lo - 0.5, hi + 0.5] : [lo, hi]
|
|
114
121
|
}
|
|
115
122
|
|
|
116
123
|
/** Y domain: bar/area pin the zero baseline; line/scatter take the data
|
|
117
124
|
* extent; stacked bars sum per category (positive stack only — negative
|
|
118
125
|
* values in stacked mode stack below zero independently). */
|
|
119
126
|
export function yDomain(data: SeriesSpec[], type: ChartType, stacked: boolean): [number, number] {
|
|
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
|
-
|
|
127
|
+
if (stacked && type === 'bar') {
|
|
128
|
+
const pos = new Map<string, number>()
|
|
129
|
+
const neg = new Map<string, number>()
|
|
130
|
+
|
|
131
|
+
for (const series of data) {
|
|
132
|
+
for (const d of series.values) {
|
|
133
|
+
if (!Number.isFinite(d.y)) {
|
|
134
|
+
continue
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const key = String(d.x)
|
|
138
|
+
|
|
139
|
+
if (d.y >= 0) {
|
|
140
|
+
pos.set(key, (pos.get(key) ?? 0) + d.y)
|
|
141
|
+
} else {
|
|
142
|
+
neg.set(key, (neg.get(key) ?? 0) + d.y)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const hi = Math.max(0, ...pos.values())
|
|
148
|
+
const lo = Math.min(0, ...neg.values())
|
|
149
|
+
|
|
150
|
+
return lo === hi ? [0, 1] : [lo, hi]
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const values = data.flatMap((s) => s.values.map((d) => d.y))
|
|
154
|
+
const [lo, hi] = extent(values)
|
|
155
|
+
const min = lo ?? 0
|
|
156
|
+
const max = hi ?? 1
|
|
157
|
+
|
|
158
|
+
if (type === 'bar' || type === 'area') {
|
|
159
|
+
const domain: [number, number] = [Math.min(0, min), Math.max(0, max)]
|
|
160
|
+
|
|
161
|
+
return domain[0] === domain[1] ? [0, 1] : domain
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return padDomain(min, max)
|
|
153
165
|
}
|
|
154
166
|
|
|
155
167
|
export function buildYScale(domain: [number, number], height: number): YScale {
|
|
156
|
-
|
|
168
|
+
return scaleLinear().domain(domain).range([height, 0]).nice()
|
|
157
169
|
}
|
|
158
170
|
|
|
159
171
|
/** Pixel center of an x value — band offsets by half the band, point and
|
|
160
172
|
* continuous scales map directly. */
|
|
161
173
|
export function xCenter(kind: XScaleKind, scale: XScale, value: number | string | Date): number {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
174
|
+
if (kind === 'band') {
|
|
175
|
+
const band = scale as ScaleBand<string>
|
|
176
|
+
|
|
177
|
+
return (band(String(value)) ?? NaN) + band.bandwidth() / 2
|
|
178
|
+
}
|
|
166
179
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
180
|
+
if (kind === 'point') {
|
|
181
|
+
return (scale as ScalePoint<string>)(String(value)) ?? NaN
|
|
182
|
+
}
|
|
170
183
|
|
|
171
|
-
|
|
184
|
+
return (scale as ScaleLinear<number, number> | ScaleTime<number, number>)(value as never)
|
|
172
185
|
}
|
package/src/core/scene.ts
CHANGED
|
@@ -9,137 +9,140 @@ import { svgDocument } from './svg'
|
|
|
9
9
|
|
|
10
10
|
/** Default categorical palette — Tableau 10, embedded so the leaf stays free
|
|
11
11
|
* of d3-scale-chromatic. */
|
|
12
|
-
export const PALETTE = [
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
export const PALETTE: string[] = [
|
|
13
|
+
'#4e79a7',
|
|
14
|
+
'#f28e2b',
|
|
15
|
+
'#e15759',
|
|
16
|
+
'#76b7b2',
|
|
17
|
+
'#59a14f',
|
|
18
|
+
'#edc948',
|
|
19
|
+
'#b07aa1',
|
|
20
|
+
'#ff9da7',
|
|
21
|
+
'#9c755f',
|
|
22
|
+
'#bab0ac',
|
|
23
23
|
]
|
|
24
24
|
|
|
25
25
|
export interface LegendItem {
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
color: string
|
|
27
|
+
text: string
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/** The fully-resolved chart: svg markup, overlay label boxes, legend items,
|
|
31
31
|
* and per-datum hit targets in chart space. `computeScene` is the core's one
|
|
32
32
|
* entry point — the leaves only render it. */
|
|
33
33
|
export interface ChartScene {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
type: ChartProps['type']
|
|
35
|
+
width: number
|
|
36
|
+
height: number
|
|
37
|
+
plot: PlotBox
|
|
38
|
+
markup: string
|
|
39
|
+
labels: LabelSpec[]
|
|
40
|
+
legend: LegendItem[]
|
|
41
|
+
series: { name: string; color: string }[]
|
|
42
|
+
points: ScenePoint[]
|
|
43
|
+
pie: PieScene | null
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
export function computeScene(props: ChartProps, width: number, height: number): ChartScene | null {
|
|
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
|
-
|
|
47
|
+
if (!(width > 0) || !(height > 0) || !props.data?.length) {
|
|
48
|
+
return null
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const pie = props.type === 'pie'
|
|
52
|
+
const xLabels = !pie && props.xAxis?.labels !== false
|
|
53
|
+
const yLabels = !pie && props.yAxis?.labels !== false
|
|
54
|
+
const margin = {
|
|
55
|
+
top: 8,
|
|
56
|
+
right: 10,
|
|
57
|
+
bottom: pie ? 8 : xLabels ? 26 : 10,
|
|
58
|
+
left: pie ? 8 : yLabels ? 46 : 10,
|
|
59
|
+
...props.margin,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const plot: PlotBox = {
|
|
63
|
+
x: margin.left,
|
|
64
|
+
y: margin.top,
|
|
65
|
+
width: Math.max(0, width - margin.left - margin.right),
|
|
66
|
+
height: Math.max(0, height - margin.top - margin.bottom),
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!(plot.width > 0) || !(plot.height > 0)) {
|
|
70
|
+
return null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const series = props.data.map((s, i) => ({
|
|
74
|
+
name: s.name,
|
|
75
|
+
color: s.color ?? PALETTE[i % PALETTE.length],
|
|
76
|
+
values: s.values,
|
|
77
|
+
}))
|
|
78
|
+
|
|
79
|
+
let marks: Mark[]
|
|
80
|
+
let points: ScenePoint[]
|
|
81
|
+
let pieScene: PieScene | null = null
|
|
82
|
+
let labels: LabelSpec[] = []
|
|
83
|
+
let grid: Mark[] = []
|
|
84
|
+
let frame: Mark[] = []
|
|
85
|
+
|
|
86
|
+
if (pie) {
|
|
87
|
+
const built = buildMarks(
|
|
88
|
+
'pie',
|
|
89
|
+
// Pie doesn't consume the cartesian scales — placeholders only.
|
|
90
|
+
{ plot, xKind: 'band', x: buildXScale('band', [], 0), y: buildYScale([0, 1], 1) },
|
|
91
|
+
series,
|
|
92
|
+
{ innerRadiusRatio: props.innerRadiusRatio, palette: PALETTE },
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
marks = built.marks
|
|
96
|
+
points = []
|
|
97
|
+
|
|
98
|
+
const center = { cx: built.pie!.cx + plot.x, cy: built.pie!.cy + plot.y }
|
|
99
|
+
|
|
100
|
+
pieScene = {
|
|
101
|
+
...built.pie!,
|
|
102
|
+
...center,
|
|
103
|
+
slices: built.pie!.slices.map((s) => ({ ...s, cx: s.cx + center.cx, cy: s.cy + center.cy })),
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
const xKind = inferXKind(props.type, props.data)
|
|
107
|
+
const layout = {
|
|
108
|
+
plot,
|
|
109
|
+
xKind,
|
|
110
|
+
x: buildXScale(xKind, props.data, plot.width),
|
|
111
|
+
y: buildYScale(yDomain(props.data, props.type, props.stacked === true), plot.height),
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const built = buildMarks(props.type, layout, series, {
|
|
115
|
+
stacked: props.stacked,
|
|
116
|
+
palette: PALETTE,
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
marks = built.marks
|
|
120
|
+
// Marks stay plot-space in the markup (the serializer wraps them in a
|
|
121
|
+
// translate group); hit targets publish chart space.
|
|
122
|
+
points = built.points.map((p) => ({ ...p, cx: p.cx + plot.x, cy: p.cy + plot.y }))
|
|
123
|
+
|
|
124
|
+
const xAxis = xAxisModel(xKind, layout.x, props.xAxis, plot)
|
|
125
|
+
const yAxis = yAxisModel(layout.y, props.yAxis, plot)
|
|
126
|
+
|
|
127
|
+
labels = [...xAxis.labels, ...yAxis.labels]
|
|
128
|
+
grid = [...xAxis.grid, ...yAxis.grid]
|
|
129
|
+
frame = [...xAxis.frame, ...yAxis.frame]
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const legend = pieScene
|
|
133
|
+
? pieScene.slices.map((s) => ({ color: s.color, text: s.label }))
|
|
134
|
+
: series.map((s) => ({ color: s.color, text: s.name }))
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
type: props.type,
|
|
138
|
+
width,
|
|
139
|
+
height,
|
|
140
|
+
plot,
|
|
141
|
+
markup: svgDocument(marks, grid, frame, plot, width, height),
|
|
142
|
+
labels,
|
|
143
|
+
legend,
|
|
144
|
+
series,
|
|
145
|
+
points,
|
|
146
|
+
pie: pieScene,
|
|
147
|
+
}
|
|
145
148
|
}
|