@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.
@@ -17,247 +17,257 @@ const TIP_ESTIMATE_CHAR = 6.2
17
17
  * the markup. Pointer handlers attach to the plot host so chart coords are
18
18
  * client-minus-rect, matching the markup's coordinate space. */
19
19
  export function Chart(props: ChartProps): JSX.Element @{
20
- const measure = useMeasure()
21
- const [cursor, setCursor] = useState<SceneHit | null>(null)
22
- const sceneRef = useRef<ChartScene | null>(null)
23
- const propsRef = useRef(props)
24
- propsRef.current = props
25
- const hostRef = useRef<HTMLElement | null>(null)
20
+ const measure = useMeasure()
21
+ const [cursor, setCursor] = useState<SceneHit | null>(null)
22
+ const sceneRef = useRef<ChartScene | null>(null)
23
+ const propsRef = useRef(props)
26
24
 
27
- const bounds = measure.bounds
28
- const w = props.width ?? bounds?.width ?? 0
29
- const h =
30
- props.height ??
31
- (props.aspectRatio && w > 0
32
- ? w / props.aspectRatio
33
- : bounds?.height && bounds.height > 0
34
- ? bounds.height
35
- : w > 0
36
- ? w / 1.6
37
- : 0)
25
+ propsRef.current = props
38
26
 
39
- const scene = useMemo(
40
- () => (w > 0 && h > 0 ? computeScene(propsRef.current, w, h) : null),
41
- [
42
- props.type,
43
- props.data,
44
- props.margin,
45
- props.stacked,
46
- props.innerRadiusRatio,
47
- props.xAxis,
48
- props.yAxis,
49
- w,
50
- h,
51
- ],
52
- )
27
+ const hostRef = useRef<HTMLElement | null>(null)
53
28
 
54
- sceneRef.current = scene
29
+ const bounds = measure.bounds
30
+ const w = props.width ?? bounds?.width ?? 0
31
+ const h =
32
+ props.height ??
33
+ (props.aspectRatio && w > 0
34
+ ? w / props.aspectRatio
35
+ : bounds?.height && bounds.height > 0
36
+ ? bounds.height
37
+ : w > 0
38
+ ? w / 1.6
39
+ : 0)
55
40
 
56
- const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair)
57
- useEffect(() => {
58
- const el = hostRef.current
59
- if (!el || !interactive) {
60
- return
61
- }
41
+ const scene = useMemo(
42
+ () => (w > 0 && h > 0 ? computeScene(propsRef.current, w, h) : null),
43
+ [
44
+ props.type,
45
+ props.data,
46
+ props.margin,
47
+ props.stacked,
48
+ props.innerRadiusRatio,
49
+ props.xAxis,
50
+ props.yAxis,
51
+ w,
52
+ h,
53
+ ],
54
+ )
62
55
 
63
- const scrub = createScrub(
64
- () => sceneRef.current,
65
- () => propsRef.current,
66
- setCursor,
67
- )
56
+ sceneRef.current = scene
68
57
 
69
- const at = (e: PointerEvent) => {
70
- const rect = el.getBoundingClientRect()
71
- return { x: e.clientX - rect.left, y: e.clientY - rect.top }
72
- }
58
+ const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair)
73
59
 
74
- const down = (e: PointerEvent) => {
75
- el.setPointerCapture?.(e.pointerId)
76
- const p = at(e)
77
- scrub('down', p.x, p.y)
78
- }
60
+ useEffect(() => {
61
+ const el = hostRef.current
79
62
 
80
- const move = (e: PointerEvent) => {
81
- const p = at(e)
82
- scrub(e.buttons !== 0 ? 'move' : 'hover', p.x, p.y)
83
- }
63
+ if (!el || !interactive) {
64
+ return
65
+ }
84
66
 
85
- const up = (e: PointerEvent) => {
86
- const p = at(e)
87
- scrub('up', p.x, p.y)
88
- }
67
+ const scrub = createScrub(
68
+ () => sceneRef.current,
69
+ () => propsRef.current,
70
+ setCursor,
71
+ )
89
72
 
90
- // `click` (incl. el.click()/AT activation) is the web 'tap' — onPress
91
- // lives on tap so a real click after pointerup doesn't double-fire.
92
- // Synthetic clicks carry no pointer position → tap the center.
93
- const click = (e: MouseEvent) => {
94
- const p =
95
- e.clientX === 0 && e.clientY === 0
96
- ? { x: el.getBoundingClientRect().width / 2, y: el.getBoundingClientRect().height / 2 }
97
- : at(e as PointerEvent)
73
+ const at = (e: PointerEvent) => {
74
+ const rect = el.getBoundingClientRect()
98
75
 
99
- scrub('tap', p.x, p.y)
100
- }
76
+ return { x: e.clientX - rect.left, y: e.clientY - rect.top }
77
+ }
101
78
 
102
- const end = () => scrub('cancel', 0, 0)
103
- const leave = () => scrub('leave', 0, 0)
104
- el.addEventListener('pointerdown', down)
105
- el.addEventListener('pointermove', move)
106
- el.addEventListener('pointerup', up)
107
- el.addEventListener('pointercancel', end)
108
- el.addEventListener('pointerleave', leave)
109
- el.addEventListener('click', click)
110
- return () => {
111
- el.removeEventListener('pointerdown', down)
112
- el.removeEventListener('pointermove', move)
113
- el.removeEventListener('pointerup', up)
114
- el.removeEventListener('pointercancel', end)
115
- el.removeEventListener('pointerleave', leave)
116
- el.removeEventListener('click', click)
117
- }
118
- }, [interactive])
79
+ const down = (e: PointerEvent) => {
80
+ el.setPointerCapture?.(e.pointerId)
119
81
 
120
- const legend = props.legend === true ? 'bottom' : props.legend
121
- const plot = scene?.plot
122
- const dotColor =
123
- cursor && scene
124
- ? scene.pie
125
- ? scene.pie.slices.find((s) => s.series === cursor.series && s.index === cursor.index)
126
- ?.color
127
- : scene.series[cursor.series]?.color
128
- : undefined
82
+ const p = at(e)
129
83
 
130
- const tipText = cursor && scene ? hitLabel(scene, cursor) : null
131
- const tipWidth = tipText ? tipText.length * TIP_ESTIMATE_CHAR + 14 : 0
132
- const tipLeft =
133
- cursor && scene
134
- ? Math.min(Math.max(0, cursor.px - tipWidth / 2), Math.max(0, scene.width - tipWidth))
135
- : 0
84
+ scrub('down', p.x, p.y)
85
+ }
136
86
 
137
- const tipTop = cursor ? cursor.py - 30 : 0
87
+ const move = (e: PointerEvent) => {
88
+ const p = at(e)
138
89
 
139
- const legendRow = (items: { color: string; text: string }[], position: 'top' | 'bottom') => (
140
- <div
141
- style={{
142
- display: 'flex',
143
- flexWrap: 'wrap',
144
- gap: 12,
145
- paddingTop: position === 'bottom' ? 4 : 0,
146
- paddingBottom: position === 'top' ? 4 : 0,
147
- }}
148
- >
149
- {items.map((item) => (
150
- <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4, fontSize: 11 }}>
151
- <span
152
- style={{
153
- width: 10,
154
- height: 10,
155
- borderRadius: 2,
156
- backgroundColor: item.color,
157
- display: 'inline-block',
158
- }}
159
- />
160
- {item.text}
161
- </span>
162
- ))}
163
- </div>
164
- )
90
+ scrub(e.buttons !== 0 ? 'move' : 'hover', p.x, p.y)
91
+ }
165
92
 
166
- <div
167
- id={props.id}
168
- className={props.className}
169
- style={{
170
- display: 'flex',
171
- flexDirection: 'column',
172
- width: props.width,
173
- height: props.height,
174
- ...props.style,
175
- }}
176
- role={props.accessibilityLabel ? 'img' : undefined}
177
- aria-label={props.accessibilityLabel}
178
- >
179
- {legend === 'top' && scene ? legendRow(scene.legend, 'top') : null}
180
- <div
181
- id={props.id ? props.id + '-overlay' : undefined}
182
- ref={(el: HTMLElement | null) => {
183
- hostRef.current = el
184
- measure.ref(el)
185
- }}
186
- style={{
187
- position: 'relative',
188
- flexGrow: 1,
189
- minHeight: 0,
190
- width: props.width,
191
- height: props.height ?? (props.aspectRatio && w > 0 ? w / props.aspectRatio : undefined),
192
- }}
193
- >
194
- <div dangerouslySetInnerHTML={scene ? { __html: scene.markup } : undefined} />
195
- <div style={{ position: 'absolute', inset: 0 }}>
196
- {scene?.labels.map((l) => (
197
- <div
198
- style={{
199
- position: 'absolute',
200
- left: l.x,
201
- top: l.y,
202
- width: l.width,
203
- fontSize: 10,
204
- opacity: 0.7,
205
- textAlign: l.align,
206
- overflow: 'hidden',
207
- whiteSpace: 'nowrap',
208
- textOverflow: 'ellipsis',
209
- }}
210
- >
211
- {l.text}
212
- </div>
213
- ))}
214
- {cursor && props.crosshair && !scene?.pie && plot ? (
215
- <div
216
- style={{
217
- position: 'absolute',
218
- left: cursor.px - 0.5,
219
- top: plot.y,
220
- width: 1,
221
- height: plot.height,
222
- backgroundColor: '#8a8a8a',
223
- pointerEvents: 'none',
224
- }}
225
- />
226
- ) : null}
227
- {cursor && (props.crosshair || props.tooltip) ? (
228
- <div
229
- style={{
230
- position: 'absolute',
231
- left: cursor.px - 4,
232
- top: cursor.py - 4,
233
- width: 8,
234
- height: 8,
235
- borderRadius: 4,
236
- backgroundColor: dotColor ?? '#4e79a7',
237
- pointerEvents: 'none',
238
- }}
239
- />
240
- ) : null}
241
- {cursor && props.tooltip && tipText ? (
242
- <div
243
- style={{
244
- position: 'absolute',
245
- left: tipLeft,
246
- top: tipTop < 0 ? cursor.py + 12 : tipTop,
247
- backgroundColor: 'rgba(20,20,20,0.88)',
248
- color: '#fff',
249
- fontSize: 11,
250
- padding: '2px 6px',
251
- borderRadius: 4,
252
- whiteSpace: 'nowrap',
253
- pointerEvents: 'none',
254
- }}
255
- >
256
- {tipText}
257
- </div>
258
- ) : null}
259
- </div>
260
- </div>
261
- {legend === 'bottom' && scene ? legendRow(scene.legend, 'bottom') : null}
262
- </div>
93
+ const up = (e: PointerEvent) => {
94
+ const p = at(e)
95
+
96
+ scrub('up', p.x, p.y)
97
+ }
98
+
99
+ // `click` (incl. el.click()/AT activation) is the web 'tap' — onPress
100
+ // lives on tap so a real click after pointerup doesn't double-fire.
101
+ // Synthetic clicks carry no pointer position → tap the center.
102
+ const click = (e: MouseEvent) => {
103
+ const p =
104
+ e.clientX === 0 && e.clientY === 0
105
+ ? { x: el.getBoundingClientRect().width / 2, y: el.getBoundingClientRect().height / 2 }
106
+ : at(e as PointerEvent)
107
+
108
+ scrub('tap', p.x, p.y)
109
+ }
110
+
111
+ const end = () => scrub('cancel', 0, 0)
112
+ const leave = () => scrub('leave', 0, 0)
113
+
114
+ el.addEventListener('pointerdown', down)
115
+ el.addEventListener('pointermove', move)
116
+ el.addEventListener('pointerup', up)
117
+ el.addEventListener('pointercancel', end)
118
+ el.addEventListener('pointerleave', leave)
119
+ el.addEventListener('click', click)
120
+ return () => {
121
+ el.removeEventListener('pointerdown', down)
122
+ el.removeEventListener('pointermove', move)
123
+ el.removeEventListener('pointerup', up)
124
+ el.removeEventListener('pointercancel', end)
125
+ el.removeEventListener('pointerleave', leave)
126
+ el.removeEventListener('click', click)
127
+ }
128
+ }, [interactive])
129
+
130
+ const legend = props.legend === true ? 'bottom' : props.legend
131
+ const plot = scene?.plot
132
+ const dotColor =
133
+ cursor && scene
134
+ ? scene.pie
135
+ ? scene.pie.slices.find((s) => s.series === cursor.series && s.index === cursor.index)
136
+ ?.color
137
+ : scene.series[cursor.series]?.color
138
+ : undefined
139
+
140
+ const tipText = cursor && scene ? hitLabel(scene, cursor) : null
141
+ const tipWidth = tipText ? tipText.length * TIP_ESTIMATE_CHAR + 14 : 0
142
+ const tipLeft =
143
+ cursor && scene
144
+ ? Math.min(Math.max(0, cursor.px - tipWidth / 2), Math.max(0, scene.width - tipWidth))
145
+ : 0
146
+
147
+ const tipTop = cursor ? cursor.py - 30 : 0
148
+
149
+ const legendRow = (items: { color: string; text: string }[], position: 'top' | 'bottom') => (
150
+ <div
151
+ style={{
152
+ display: 'flex',
153
+ flexWrap: 'wrap',
154
+ gap: 12,
155
+ paddingTop: position === 'bottom' ? 4 : 0,
156
+ paddingBottom: position === 'top' ? 4 : 0,
157
+ }}
158
+ >
159
+ {items.map((item) => (
160
+ <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4, fontSize: 11 }}>
161
+ <span
162
+ style={{
163
+ width: 10,
164
+ height: 10,
165
+ borderRadius: 2,
166
+ backgroundColor: item.color,
167
+ display: 'inline-block',
168
+ }}
169
+ />
170
+ {item.text}
171
+ </span>
172
+ ))}
173
+ </div>
174
+ )
175
+
176
+ <div
177
+ id={props.id}
178
+ className={props.className}
179
+ style={{
180
+ display: 'flex',
181
+ flexDirection: 'column',
182
+ width: props.width,
183
+ height: props.height,
184
+ ...props.style,
185
+ }}
186
+ role={props.accessibilityLabel ? 'img' : undefined}
187
+ aria-label={props.accessibilityLabel}
188
+ >
189
+ {legend === 'top' && scene ? legendRow(scene.legend, 'top') : null}
190
+ <div
191
+ id={props.id ? props.id + '-overlay' : undefined}
192
+ ref={(el: HTMLElement | null) => {
193
+ hostRef.current = el
194
+ measure.ref(el)
195
+ }}
196
+ style={{
197
+ position: 'relative',
198
+ flexGrow: 1,
199
+ minHeight: 0,
200
+ width: props.width,
201
+ height: props.height ?? (props.aspectRatio && w > 0 ? w / props.aspectRatio : undefined),
202
+ }}
203
+ >
204
+ <div dangerouslySetInnerHTML={scene ? { __html: scene.markup } : undefined} />
205
+ <div style={{ position: 'absolute', inset: 0 }}>
206
+ {scene?.labels.map((l) => (
207
+ <div
208
+ style={{
209
+ position: 'absolute',
210
+ left: l.x,
211
+ top: l.y,
212
+ width: l.width,
213
+ fontSize: 10,
214
+ opacity: 0.7,
215
+ textAlign: l.align,
216
+ overflow: 'hidden',
217
+ whiteSpace: 'nowrap',
218
+ textOverflow: 'ellipsis',
219
+ }}
220
+ >
221
+ {l.text}
222
+ </div>
223
+ ))}
224
+ {cursor && props.crosshair && !scene?.pie && plot ? (
225
+ <div
226
+ style={{
227
+ position: 'absolute',
228
+ left: cursor.px - 0.5,
229
+ top: plot.y,
230
+ width: 1,
231
+ height: plot.height,
232
+ backgroundColor: '#8a8a8a',
233
+ pointerEvents: 'none',
234
+ }}
235
+ />
236
+ ) : null}
237
+ {cursor && (props.crosshair || props.tooltip) ? (
238
+ <div
239
+ style={{
240
+ position: 'absolute',
241
+ left: cursor.px - 4,
242
+ top: cursor.py - 4,
243
+ width: 8,
244
+ height: 8,
245
+ borderRadius: 4,
246
+ backgroundColor: dotColor ?? '#4e79a7',
247
+ pointerEvents: 'none',
248
+ }}
249
+ />
250
+ ) : null}
251
+ {cursor && props.tooltip && tipText ? (
252
+ <div
253
+ style={{
254
+ position: 'absolute',
255
+ left: tipLeft,
256
+ top: tipTop < 0 ? cursor.py + 12 : tipTop,
257
+ backgroundColor: 'rgba(20,20,20,0.88)',
258
+ color: '#fff',
259
+ fontSize: 11,
260
+ padding: '2px 6px',
261
+ borderRadius: 4,
262
+ whiteSpace: 'nowrap',
263
+ pointerEvents: 'none',
264
+ }}
265
+ >
266
+ {tipText}
267
+ </div>
268
+ ) : null}
269
+ </div>
270
+ </div>
271
+ {legend === 'bottom' && scene ? legendRow(scene.legend, 'bottom') : null}
272
+ </div>
263
273
  }
@@ -1,21 +1,21 @@
1
1
  import type { ScrubPhase } from './core/hit'
2
2
 
3
3
  export interface AppKitChartPointerBridge {
4
- observePointerMotion?(
5
- view: unknown,
6
- handlers: { move(x: number, y: number): void; exit(): void },
7
- ): () => void
8
- locatePointer?(view: unknown): { x: number; y: number } | null
4
+ observePointerMotion?(
5
+ view: unknown,
6
+ handlers: { move(x: number, y: number): void; exit(): void },
7
+ ): () => void
8
+ locatePointer?(view: unknown): { x: number; y: number } | null
9
9
  }
10
10
 
11
11
  /** Raw renderer onPan event — top-left x/y and the NSPanGestureRecognizer
12
12
  * state (1 began / 2 changed / 3 ended / anything else cancelled). */
13
13
  export interface AppKitPanEvent {
14
- x: number
15
- y: number
16
- deltaX: number
17
- deltaY: number
18
- state: number
14
+ x: number
15
+ y: number
16
+ deltaX: number
17
+ deltaY: number
18
+ state: number
19
19
  }
20
20
 
21
21
  const finite = (x: number, y: number) => Number.isFinite(x) && Number.isFinite(y)
@@ -24,75 +24,90 @@ const finite = (x: number, y: number) => Number.isFinite(x) && Number.isFinite(y
24
24
  * area motion and the current mouse event — onto the shared scrub phases,
25
25
  * mirroring the DOM listeners in Chart.web. */
26
26
  export function createAppKitChartPointer(
27
- scrub: (phase: ScrubPhase, x: number, y: number) => void,
28
- size: () => { width: number; height: number },
29
- bridge: () => AppKitChartPointerBridge | undefined = () =>
30
- Reflect.get(globalThis, '__xplatAppKit'),
27
+ scrub: (phase: ScrubPhase, x: number, y: number) => void,
28
+ size: () => { width: number; height: number },
29
+ bridge: () => AppKitChartPointerBridge | undefined = () =>
30
+ Reflect.get(globalThis, '__xplatAppKit'),
31
31
  ): {
32
- pan(event: { deltaX?: unknown; deltaY?: unknown; state?: unknown; x?: number; y?: number }): void
33
- tap(view: unknown): void
34
- observe(view: unknown): () => void
32
+ pan(event: { deltaX?: unknown; deltaY?: unknown; state?: unknown; x?: number; y?: number }): void
33
+ tap(view: unknown): void
34
+ observe(view: unknown): () => void
35
35
  } {
36
- let panning = false
37
- return {
38
- pan(event: { deltaX?: unknown; deltaY?: unknown; state?: unknown; x?: number; y?: number }): void {
39
- // The AppKit driver emits a superset payload carrying x/y; the shared
40
- // PanGestureEvent type only declares deltaX/deltaY/state.
41
- const panEvent = event as AppKitPanEvent
42
- const state = Number(panEvent.state)
43
- const point = finite(panEvent.x, panEvent.y)
44
- ? { x: panEvent.x, y: panEvent.y }
45
- : null
46
- if (state === 1) {
47
- if (!point) {
48
- return
49
- }
36
+ let panning = false
50
37
 
51
- // x/y includes the current delta — subtract it for the press start.
52
- panning = true
53
- scrub('down', point.x - Number(panEvent.deltaX ?? 0), point.y - Number(panEvent.deltaY ?? 0))
54
- scrub('move', point.x, point.y)
55
- } else if (state === 2) {
56
- if (panning && point) {
57
- scrub('move', point.x, point.y)
58
- }
59
- } else if (panning) {
60
- // `cancel` clears without coordinates, matching web's scrub('cancel', 0, 0).
61
- const ended = state === 3 ? point : null
62
- scrub(ended ? 'up' : 'cancel', ended?.x ?? 0, ended?.y ?? 0)
63
- panning = false
64
- }
65
- },
66
- tap(view: unknown): void {
67
- const point = bridge()?.locatePointer?.(view)
68
- if (point && finite(point.x, point.y)) {
69
- scrub('tap', point.x, point.y)
70
- return
71
- }
38
+ return {
39
+ pan(event: {
40
+ deltaX?: unknown
41
+ deltaY?: unknown
42
+ state?: unknown
43
+ x?: number
44
+ y?: number
45
+ }): void {
46
+ // The AppKit driver emits a superset payload carrying x/y; the shared
47
+ // PanGestureEvent type only declares deltaX/deltaY/state.
48
+ const panEvent = event as AppKitPanEvent
49
+ const state = Number(panEvent.state)
50
+ const point = finite(panEvent.x, panEvent.y) ? { x: panEvent.x, y: panEvent.y } : null
72
51
 
73
- const { width, height } = size()
74
- scrub('tap', width / 2, height / 2)
75
- },
76
- observe(view: unknown): () => void {
77
- const observePointerMotion = bridge()?.observePointerMotion
78
- if (!view || !observePointerMotion) {
79
- return () => {}
80
- }
52
+ if (state === 1) {
53
+ if (!point) {
54
+ return
55
+ }
81
56
 
82
- // An AppKit exit mid-drag must not reset the scrub — the pan
83
- // recognizer keeps delivering while pressed outside the view.
84
- return observePointerMotion(view, {
85
- move(x, y): void {
86
- if (!panning) {
87
- scrub('hover', x, y)
88
- }
89
- },
90
- exit(): void {
91
- if (!panning) {
92
- scrub('leave', 0, 0)
93
- }
94
- },
95
- })
96
- },
97
- }
57
+ // x/y includes the current delta — subtract it for the press start.
58
+ panning = true
59
+ scrub(
60
+ 'down',
61
+ point.x - Number(panEvent.deltaX ?? 0),
62
+ point.y - Number(panEvent.deltaY ?? 0),
63
+ )
64
+
65
+ scrub('move', point.x, point.y)
66
+ } else if (state === 2) {
67
+ if (panning && point) {
68
+ scrub('move', point.x, point.y)
69
+ }
70
+ } else if (panning) {
71
+ // `cancel` clears without coordinates, matching web's scrub('cancel', 0, 0).
72
+ const ended = state === 3 ? point : null
73
+
74
+ scrub(ended ? 'up' : 'cancel', ended?.x ?? 0, ended?.y ?? 0)
75
+ panning = false
76
+ }
77
+ },
78
+ tap(view: unknown): void {
79
+ const point = bridge()?.locatePointer?.(view)
80
+
81
+ if (point && finite(point.x, point.y)) {
82
+ scrub('tap', point.x, point.y)
83
+ return
84
+ }
85
+
86
+ const { width, height } = size()
87
+
88
+ scrub('tap', width / 2, height / 2)
89
+ },
90
+ observe(view: unknown): () => void {
91
+ const observePointerMotion = bridge()?.observePointerMotion
92
+
93
+ if (!view || !observePointerMotion) {
94
+ return () => {}
95
+ }
96
+
97
+ // An AppKit exit mid-drag must not reset the scrub — the pan
98
+ // recognizer keeps delivering while pressed outside the view.
99
+ return observePointerMotion(view, {
100
+ move(x, y): void {
101
+ if (!panning) {
102
+ scrub('hover', x, y)
103
+ }
104
+ },
105
+ exit(): void {
106
+ if (!panning) {
107
+ scrub('leave', 0, 0)
108
+ }
109
+ },
110
+ })
111
+ },
112
+ }
98
113
  }