@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/hit.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { defaultFormat } from './axes'
|
|
|
3
3
|
import type { ChartScene } from './scene'
|
|
4
4
|
|
|
5
5
|
export interface SceneHit extends ChartHit {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
/** Datum anchor in chart space — tooltip/crosshair position. */
|
|
7
|
+
px: number
|
|
8
|
+
py: number
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
/** Touch slop — how far outside the plot a pointer may sit and still hit.
|
|
@@ -17,121 +17,132 @@ export const HIT_SLOP = 24
|
|
|
17
17
|
* scatter gates on pointer distance instead of always snapping. Pie hits
|
|
18
18
|
* resolve by ring + angle. Coordinates outside the plot (+slop) miss. */
|
|
19
19
|
export function hitTest(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
scene: ChartScene,
|
|
21
|
+
x: number,
|
|
22
|
+
y: number,
|
|
23
|
+
slop: number = HIT_SLOP,
|
|
24
24
|
): SceneHit | null {
|
|
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
|
-
|
|
25
|
+
if (scene.pie) {
|
|
26
|
+
return pieHit(scene, x, y, slop)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const { plot, points } = scene
|
|
30
|
+
|
|
31
|
+
if (!points.length) {
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (
|
|
36
|
+
x < plot.x - slop ||
|
|
37
|
+
x > plot.x + plot.width + slop ||
|
|
38
|
+
y < plot.y - slop ||
|
|
39
|
+
y > plot.y + plot.height + slop
|
|
40
|
+
) {
|
|
41
|
+
return null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (scene.type === 'scatter') {
|
|
45
|
+
// Scatter is a point cloud, not a snapped axis — pick the datum
|
|
46
|
+
// nearest the pointer in 2D and miss when nothing is actually near.
|
|
47
|
+
let best: SceneHit | null = null
|
|
48
|
+
let bestDist = Infinity
|
|
49
|
+
|
|
50
|
+
for (const p of points) {
|
|
51
|
+
const d = Math.hypot(p.cx - x, p.cy - y)
|
|
52
|
+
|
|
53
|
+
if (d < bestDist) {
|
|
54
|
+
bestDist = d
|
|
55
|
+
best = { series: p.series, index: p.index, x: p.x, y: p.y, px: p.cx, py: p.cy }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return best !== null && bestDist <= slop ? best : null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const centers = new Map<string, { total: number; count: number }>()
|
|
63
|
+
|
|
64
|
+
for (const p of points) {
|
|
65
|
+
const cur = centers.get(p.key)
|
|
66
|
+
|
|
67
|
+
if (cur === undefined) {
|
|
68
|
+
centers.set(p.key, { total: p.cx, count: 1 })
|
|
69
|
+
} else {
|
|
70
|
+
cur.total += p.cx
|
|
71
|
+
cur.count += 1
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let key: string | null = null
|
|
76
|
+
let keyDist = Infinity
|
|
77
|
+
|
|
78
|
+
for (const [k, sum] of centers) {
|
|
79
|
+
const d = Math.abs(sum.total / sum.count - x)
|
|
80
|
+
|
|
81
|
+
if (d < keyDist) {
|
|
82
|
+
keyDist = d
|
|
83
|
+
key = k
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (key === null) {
|
|
88
|
+
return null
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let best: SceneHit | null = null
|
|
92
|
+
let bestDist = Infinity
|
|
93
|
+
|
|
94
|
+
for (const p of points) {
|
|
95
|
+
if (p.key !== key) {
|
|
96
|
+
continue
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const d = Math.hypot(p.cx - x, p.cy - y)
|
|
100
|
+
|
|
101
|
+
if (d < bestDist) {
|
|
102
|
+
bestDist = d
|
|
103
|
+
best = { series: p.series, index: p.index, x: p.x, y: p.y, px: p.cx, py: p.cy }
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return best
|
|
99
108
|
}
|
|
100
109
|
|
|
101
110
|
function pieHit(scene: ChartScene, x: number, y: number, slop: number): SceneHit | null {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
111
|
+
const pie = scene.pie!
|
|
112
|
+
const dx = x - pie.cx
|
|
113
|
+
const dy = y - pie.cy
|
|
114
|
+
const r = Math.hypot(dx, dy)
|
|
115
|
+
|
|
116
|
+
if (r < pie.rInner - slop || r > pie.rOuter + slop) {
|
|
117
|
+
return null
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// d3 pie angles run clockwise from 12 o'clock in y-down space:
|
|
121
|
+
// θ = atan2(dx, −dy) normalized into [0, 2π).
|
|
122
|
+
const angle = (Math.atan2(dx, -dy) + Math.PI * 2) % (Math.PI * 2)
|
|
123
|
+
const slice = pie.slices.find((s) => angle >= s.startAngle && angle < s.endAngle)
|
|
124
|
+
|
|
125
|
+
if (!slice) {
|
|
126
|
+
return null
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
series: slice.series,
|
|
131
|
+
index: slice.index,
|
|
132
|
+
x: slice.label,
|
|
133
|
+
y: slice.value,
|
|
134
|
+
px: slice.cx,
|
|
135
|
+
py: slice.cy,
|
|
136
|
+
}
|
|
126
137
|
}
|
|
127
138
|
|
|
128
139
|
export type ScrubPhase = 'down' | 'move' | 'up' | 'cancel' | 'hover' | 'leave' | 'tap'
|
|
129
140
|
|
|
130
141
|
export interface ScrubProps {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
142
|
+
tooltip?: boolean
|
|
143
|
+
crosshair?: boolean
|
|
144
|
+
onPress?: (hit: ChartHit) => void
|
|
145
|
+
onScrub?: (hit: ChartHit | null) => void
|
|
135
146
|
}
|
|
136
147
|
|
|
137
148
|
/** Shared gesture → cursor state machine. Both leaves normalize their pointer
|
|
@@ -143,79 +154,88 @@ export interface ScrubProps {
|
|
|
143
154
|
* `cancel`/`leave` clear. `onPress` lives only on `tap` so NS's tap
|
|
144
155
|
* recognizer and the web click both reach it exactly once. */
|
|
145
156
|
export function createScrub(
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
157
|
+
getScene: () => ChartScene | null,
|
|
158
|
+
getProps: () => ScrubProps,
|
|
159
|
+
setCursor: (hit: SceneHit | null) => void,
|
|
149
160
|
): (phase: ScrubPhase, x: number, y: number) => void {
|
|
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
|
-
|
|
161
|
+
let downAt: { x: number; y: number; t: number } | null = null
|
|
162
|
+
|
|
163
|
+
return (phase, x, y) => {
|
|
164
|
+
const scene = getScene()
|
|
165
|
+
const props = getProps()
|
|
166
|
+
const showCursor = props.tooltip === true || props.crosshair === true
|
|
167
|
+
|
|
168
|
+
switch (phase) {
|
|
169
|
+
case 'down': {
|
|
170
|
+
downAt = { x, y, t: Date.now() }
|
|
171
|
+
|
|
172
|
+
const hit = scene && showCursor ? hitTest(scene, x, y) : null
|
|
173
|
+
|
|
174
|
+
setCursor(hit)
|
|
175
|
+
if (hit) {
|
|
176
|
+
props.onScrub?.(hit)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
break
|
|
180
|
+
}
|
|
181
|
+
case 'move': {
|
|
182
|
+
if (!downAt) {
|
|
183
|
+
break
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const hit = scene && showCursor ? hitTest(scene, x, y) : null
|
|
187
|
+
|
|
188
|
+
setCursor(hit)
|
|
189
|
+
props.onScrub?.(hit)
|
|
190
|
+
break
|
|
191
|
+
}
|
|
192
|
+
case 'hover': {
|
|
193
|
+
if (downAt) {
|
|
194
|
+
break
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
setCursor(scene && showCursor ? hitTest(scene, x, y) : null)
|
|
198
|
+
break
|
|
199
|
+
}
|
|
200
|
+
case 'up': {
|
|
201
|
+
downAt = null
|
|
202
|
+
setCursor(scene ? hitTest(scene, x, y) : null)
|
|
203
|
+
props.onScrub?.(null)
|
|
204
|
+
break
|
|
205
|
+
}
|
|
206
|
+
case 'tap': {
|
|
207
|
+
downAt = null
|
|
208
|
+
|
|
209
|
+
const hit = scene ? hitTest(scene, x, y) : null
|
|
210
|
+
|
|
211
|
+
setCursor(hit)
|
|
212
|
+
if (hit) {
|
|
213
|
+
props.onPress?.(hit)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
break
|
|
217
|
+
}
|
|
218
|
+
case 'cancel':
|
|
219
|
+
case 'leave': {
|
|
220
|
+
downAt = null
|
|
221
|
+
setCursor(null)
|
|
222
|
+
props.onScrub?.(null)
|
|
223
|
+
break
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
209
227
|
}
|
|
210
228
|
|
|
211
229
|
/** Tooltip body for a hit — series name + formatted y for cartesian charts,
|
|
212
230
|
* slice label + value for pie. */
|
|
213
231
|
export function hitLabel(scene: ChartScene, hit: ChartHit): string {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
return slice ? `${slice.label}: ${defaultFormat(slice.value)}` : defaultFormat(hit.y)
|
|
217
|
-
}
|
|
232
|
+
if (scene.pie) {
|
|
233
|
+
const slice = scene.pie.slices.find((s) => s.series === hit.series && s.index === hit.index)
|
|
218
234
|
|
|
219
|
-
|
|
220
|
-
|
|
235
|
+
return slice ? `${slice.label}: ${defaultFormat(slice.value)}` : defaultFormat(hit.y)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const name = scene.series[hit.series]?.name ?? String(hit.series)
|
|
239
|
+
|
|
240
|
+
return `${name}: ${defaultFormat(hit.y)}`
|
|
221
241
|
}
|