@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/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
- /** Datum anchor in chart space — tooltip/crosshair position. */
7
- px: number
8
- py: number
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(scene: ChartScene, x: number, y: number, slop = HIT_SLOP): SceneHit | null {
20
- if (scene.pie) {
21
- return pieHit(scene, x, y, slop)
22
- }
23
-
24
- const { plot, points } = scene
25
- if (!points.length) {
26
- return null
27
- }
28
-
29
- if (
30
- x < plot.x - slop ||
31
- x > plot.x + plot.width + slop ||
32
- y < plot.y - slop ||
33
- y > plot.y + plot.height + slop
34
- ) {
35
- return null
36
- }
37
-
38
- if (scene.type === 'scatter') {
39
- // Scatter is a point cloud, not a snapped axis — pick the datum
40
- // nearest the pointer in 2D and miss when nothing is actually near.
41
- let best: SceneHit | null = null
42
- let bestDist = Infinity
43
- for (const p of points) {
44
- const d = Math.hypot(p.cx - x, p.cy - y)
45
- if (d < bestDist) {
46
- bestDist = d
47
- best = { series: p.series, index: p.index, x: p.x, y: p.y, px: p.cx, py: p.cy }
48
- }
49
- }
50
-
51
- return best !== null && bestDist <= slop ? best : null
52
- }
53
-
54
- const centers = new Map<string, { total: number; count: number }>()
55
- for (const p of points) {
56
- const cur = centers.get(p.key)
57
- if (cur === undefined) {
58
- centers.set(p.key, { total: p.cx, count: 1 })
59
- } else {
60
- cur.total += p.cx
61
- cur.count += 1
62
- }
63
- }
64
-
65
- let key: string | null = null
66
- let keyDist = Infinity
67
- for (const [k, sum] of centers) {
68
- const d = Math.abs(sum.total / sum.count - x)
69
- if (d < keyDist) {
70
- keyDist = d
71
- key = k
72
- }
73
- }
74
-
75
- if (key === null) {
76
- return null
77
- }
78
-
79
- let best: SceneHit | null = null
80
- let bestDist = Infinity
81
- for (const p of points) {
82
- if (p.key !== key) {
83
- continue
84
- }
85
-
86
- const d = Math.hypot(p.cx - x, p.cy - y)
87
- if (d < bestDist) {
88
- bestDist = d
89
- best = { series: p.series, index: p.index, x: p.x, y: p.y, px: p.cx, py: p.cy }
90
- }
91
- }
92
-
93
- return best
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
- const pie = scene.pie!
98
- const dx = x - pie.cx
99
- const dy = y - pie.cy
100
- const r = Math.hypot(dx, dy)
101
- if (r < pie.rInner - slop || r > pie.rOuter + slop) {
102
- return null
103
- }
104
-
105
- // d3 pie angles run clockwise from 12 o'clock in y-down space:
106
- // θ = atan2(dx, −dy) normalized into [0, 2π).
107
- const angle = (Math.atan2(dx, -dy) + Math.PI * 2) % (Math.PI * 2)
108
- const slice = pie.slices.find((s) => angle >= s.startAngle && angle < s.endAngle)
109
- if (!slice) {
110
- return null
111
- }
112
-
113
- return {
114
- series: slice.series,
115
- index: slice.index,
116
- x: slice.label,
117
- y: slice.value,
118
- px: slice.cx,
119
- py: slice.cy,
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
- tooltip?: boolean
127
- crosshair?: boolean
128
- onPress?: (hit: ChartHit) => void
129
- onScrub?: (hit: ChartHit | null) => void
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
- getScene: () => ChartScene | null,
142
- getProps: () => ScrubProps,
143
- setCursor: (hit: SceneHit | null) => void,
157
+ getScene: () => ChartScene | null,
158
+ getProps: () => ScrubProps,
159
+ setCursor: (hit: SceneHit | null) => void,
144
160
  ): (phase: ScrubPhase, x: number, y: number) => void {
145
- let downAt: { x: number; y: number; t: number } | null = null
146
- return (phase, x, y) => {
147
- const scene = getScene()
148
- const props = getProps()
149
- const showCursor = props.tooltip === true || props.crosshair === true
150
- switch (phase) {
151
- case 'down': {
152
- downAt = { x, y, t: Date.now() }
153
- const hit = scene && showCursor ? hitTest(scene, x, y) : null
154
- setCursor(hit)
155
- if (hit) {
156
- props.onScrub?.(hit)
157
- }
158
-
159
- break
160
- }
161
- case 'move': {
162
- if (!downAt) {
163
- break
164
- }
165
-
166
- const hit = scene && showCursor ? hitTest(scene, x, y) : null
167
- setCursor(hit)
168
- props.onScrub?.(hit)
169
- break
170
- }
171
- case 'hover': {
172
- if (downAt) {
173
- break
174
- }
175
-
176
- setCursor(scene && showCursor ? hitTest(scene, x, y) : null)
177
- break
178
- }
179
- case 'up': {
180
- downAt = null
181
- setCursor(scene ? hitTest(scene, x, y) : null)
182
- props.onScrub?.(null)
183
- break
184
- }
185
- case 'tap': {
186
- downAt = null
187
- const hit = scene ? hitTest(scene, x, y) : null
188
- setCursor(hit)
189
- if (hit) {
190
- props.onPress?.(hit)
191
- }
192
-
193
- break
194
- }
195
- case 'cancel':
196
- case 'leave': {
197
- downAt = null
198
- setCursor(null)
199
- props.onScrub?.(null)
200
- break
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
- if (scene.pie) {
210
- const slice = scene.pie.slices.find((s) => s.series === hit.series && s.index === hit.index)
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
- const name = scene.series[hit.series]?.name ?? String(hit.series)
215
- return `${name}: ${defaultFormat(hit.y)}`
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
  }