@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/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.
|
|
@@ -16,117 +16,133 @@ export const HIT_SLOP = 24
|
|
|
16
16
|
* snap to the nearest x group, then pick the closest point inside it —
|
|
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
|
-
export function hitTest(
|
|
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
|
-
|
|
19
|
+
export function hitTest(
|
|
20
|
+
scene: ChartScene,
|
|
21
|
+
x: number,
|
|
22
|
+
y: number,
|
|
23
|
+
slop: number = HIT_SLOP,
|
|
24
|
+
): SceneHit | null {
|
|
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
|
|
94
108
|
}
|
|
95
109
|
|
|
96
110
|
function pieHit(scene: ChartScene, x: number, y: number, slop: number): SceneHit | null {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
+
}
|
|
121
137
|
}
|
|
122
138
|
|
|
123
139
|
export type ScrubPhase = 'down' | 'move' | 'up' | 'cancel' | 'hover' | 'leave' | 'tap'
|
|
124
140
|
|
|
125
141
|
export interface ScrubProps {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
142
|
+
tooltip?: boolean
|
|
143
|
+
crosshair?: boolean
|
|
144
|
+
onPress?: (hit: ChartHit) => void
|
|
145
|
+
onScrub?: (hit: ChartHit | null) => void
|
|
130
146
|
}
|
|
131
147
|
|
|
132
148
|
/** Shared gesture → cursor state machine. Both leaves normalize their pointer
|
|
@@ -138,79 +154,88 @@ export interface ScrubProps {
|
|
|
138
154
|
* `cancel`/`leave` clear. `onPress` lives only on `tap` so NS's tap
|
|
139
155
|
* recognizer and the web click both reach it exactly once. */
|
|
140
156
|
export function createScrub(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
157
|
+
getScene: () => ChartScene | null,
|
|
158
|
+
getProps: () => ScrubProps,
|
|
159
|
+
setCursor: (hit: SceneHit | null) => void,
|
|
144
160
|
): (phase: ScrubPhase, x: number, y: number) => void {
|
|
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
|
-
|
|
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
|
+
}
|
|
204
227
|
}
|
|
205
228
|
|
|
206
229
|
/** Tooltip body for a hit — series name + formatted y for cartesian charts,
|
|
207
230
|
* slice label + value for pie. */
|
|
208
231
|
export function hitLabel(scene: ChartScene, hit: ChartHit): string {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
return slice ? `${slice.label}: ${defaultFormat(slice.value)}` : defaultFormat(hit.y)
|
|
212
|
-
}
|
|
232
|
+
if (scene.pie) {
|
|
233
|
+
const slice = scene.pie.slices.find((s) => s.series === hit.series && s.index === hit.index)
|
|
213
234
|
|
|
214
|
-
|
|
215
|
-
|
|
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)}`
|
|
216
241
|
}
|