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