@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.
@@ -1,12 +1,14 @@
1
- import { useEffect, useMemo, useRef, useState } from 'octane';
2
- import { useMeasure } from '@octane-xplat/ui';
3
- import type { ChartProps } from './props';
4
- import { computeScene } from './core/scene';
5
- import type { ChartScene } from './core/scene';
6
- import { createScrub, hitLabel } from './core/hit';
7
- import type { SceneHit } from './core/hit';
1
+ /** @jsxImportSource @octane-xplat/jsx/dom */
2
+ import type { JSX } from 'octane/jsx-runtime'
3
+ import { useEffect, useMemo, useRef, useState } from 'octane'
4
+ import { useMeasure } from '@octane-xplat/ui'
5
+ import type { ChartProps } from './props'
6
+ import { computeScene } from './core/scene'
7
+ import type { ChartScene } from './core/scene'
8
+ import { createScrub, hitLabel } from './core/hit'
9
+ import type { SceneHit } from './core/hit'
8
10
 
9
- const TIP_ESTIMATE_CHAR = 6.2;
11
+ const TIP_ESTIMATE_CHAR = 6.2
10
12
 
11
13
  /** Chart — web leaf. The core's Mark[] serialize to one svg markup string,
12
14
  * inlined as `innerHTML` (Icon.web precedent — the same bytes feed
@@ -14,202 +16,258 @@ const TIP_ESTIMATE_CHAR = 6.2;
14
16
  * real absolutely-positioned elements in the overlay — never `<text>` in
15
17
  * the markup. Pointer handlers attach to the plot host so chart coords are
16
18
  * client-minus-rect, matching the markup's coordinate space. */
17
- export function Chart(props: ChartProps) @{
18
- const measure = useMeasure();
19
- const [cursor, setCursor] = useState<SceneHit | null>(null);
20
- const sceneRef = useRef<ChartScene | null>(null);
21
- const propsRef = useRef(props);
22
- propsRef.current = props;
23
- const hostRef = useRef<HTMLElement | null>(null);
24
-
25
- const bounds = measure.bounds;
26
- const w = props.width ?? bounds?.width ?? 0;
27
- const h =
28
- props.height ??
29
- (props.aspectRatio && w > 0
30
- ? w / props.aspectRatio
31
- : bounds?.height && bounds.height > 0
32
- ? bounds.height
33
- : w > 0
34
- ? w / 1.6
35
- : 0);
36
-
37
- const scene = useMemo(
38
- () => (w > 0 && h > 0 ? computeScene(propsRef.current, w, h) : null),
39
- [
40
- props.type,
41
- props.data,
42
- props.margin,
43
- props.stacked,
44
- props.innerRadiusRatio,
45
- props.xAxis,
46
- props.yAxis,
47
- w,
48
- h,
49
- ],
50
- );
51
-
52
- sceneRef.current = scene;
53
-
54
- const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair);
55
- useEffect(() => {
56
- const el = hostRef.current;
57
- if (!el || !interactive) {
58
- return;
59
- }
60
-
61
- const scrub = createScrub(
62
- () => sceneRef.current,
63
- () => propsRef.current,
64
- setCursor,
65
- );
66
-
67
- const at = (e: PointerEvent) => {
68
- const rect = el.getBoundingClientRect();
69
- return { x: e.clientX - rect.left, y: e.clientY - rect.top };
70
- };
71
-
72
- const down = (e: PointerEvent) => {
73
- el.setPointerCapture?.(e.pointerId);
74
- const p = at(e);
75
- scrub('down', p.x, p.y);
76
- };
77
-
78
- const move = (e: PointerEvent) => {
79
- const p = at(e);
80
- scrub(e.buttons !== 0 ? 'move' : 'hover', p.x, p.y);
81
- };
82
-
83
- const up = (e: PointerEvent) => {
84
- const p = at(e);
85
- scrub('up', p.x, p.y);
86
- };
87
-
88
- // `click` (incl. el.click()/AT activation) is the web 'tap' — onPress
89
- // lives on tap so a real click after pointerup doesn't double-fire.
90
- // Synthetic clicks carry no pointer position → tap the center.
91
- const click = (e: MouseEvent) => {
92
- const p =
93
- e.clientX === 0 && e.clientY === 0
94
- ? { x: el.getBoundingClientRect().width / 2, y: el.getBoundingClientRect().height / 2 }
95
- : at(e as PointerEvent);
96
-
97
- scrub('tap', p.x, p.y);
98
- };
99
-
100
- const end = () => scrub('cancel', 0, 0);
101
- const leave = () => scrub('leave', 0, 0);
102
- el.addEventListener('pointerdown', down);
103
- el.addEventListener('pointermove', move);
104
- el.addEventListener('pointerup', up);
105
- el.addEventListener('pointercancel', end);
106
- el.addEventListener('pointerleave', leave);
107
- el.addEventListener('click', click);
108
- return () => {
109
- el.removeEventListener('pointerdown', down);
110
- el.removeEventListener('pointermove', move);
111
- el.removeEventListener('pointerup', up);
112
- el.removeEventListener('pointercancel', end);
113
- el.removeEventListener('pointerleave', leave);
114
- el.removeEventListener('click', click);
115
- };
116
- }, [interactive]);
117
-
118
- const legend = props.legend === true ? 'bottom' : props.legend;
119
- const plot = scene?.plot;
120
- const dotColor =
121
- cursor && scene
122
- ? scene.pie
123
- ? scene.pie.slices.find((s) => s.series === cursor.series && s.index === cursor.index)?.color
124
- : scene.series[cursor.series]?.color
125
- : undefined;
126
-
127
- const tipText = cursor && scene ? hitLabel(scene, cursor) : null;
128
- const tipWidth = tipText ? tipText.length * TIP_ESTIMATE_CHAR + 14 : 0;
129
- const tipLeft =
130
- cursor && scene ? Math.min(Math.max(0, cursor.px - tipWidth / 2), Math.max(0, scene.width - tipWidth)) : 0;
131
-
132
- const tipTop = cursor ? cursor.py - 30 : 0;
133
-
134
- const legendRow = (items: { color: string; text: string }[], position: 'top' | 'bottom') => (
135
- <div style={{ display: 'flex', flexWrap: 'wrap', gap: 12, paddingTop: position === 'bottom' ? 4 : 0, paddingBottom: position === 'top' ? 4 : 0 }}>
136
- {items.map((item) => (
137
- <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4, fontSize: 11 }}>
138
- <span style={{ width: 10, height: 10, borderRadius: 2, backgroundColor: item.color, display: 'inline-block' }} />
139
- {item.text}
140
- </span>
141
- ))}
142
- </div>
143
- );
144
-
145
- <div
146
- id={props.id}
147
- className={props.className}
148
- style={{ display: 'flex', flexDirection: 'column', width: props.width, height: props.height, ...props.style }}
149
- role={props.accessibilityLabel ? 'img' : undefined}
150
- aria-label={props.accessibilityLabel}
151
- >
152
- {legend === 'top' && scene ? legendRow(scene.legend, 'top') : null}
153
- <div
154
- id={props.id ? props.id + '-overlay' : undefined}
155
- ref={(el: HTMLElement | null) => {
156
- hostRef.current = el;
157
- measure.ref(el);
158
- }}
159
- style={{
160
- position: 'relative',
161
- flexGrow: 1,
162
- minHeight: 0,
163
- width: props.width,
164
- height: props.height ?? (props.aspectRatio && w > 0 ? w / props.aspectRatio : undefined),
165
- }}
166
- >
167
- <div dangerouslySetInnerHTML={scene ? { __html: scene.markup } : undefined} />
168
- <div style={{ position: 'absolute', inset: 0 }}>
169
- {scene?.labels.map((l) => (
170
- <div
171
- style={{
172
- position: 'absolute',
173
- left: l.x,
174
- top: l.y,
175
- width: l.width,
176
- fontSize: 10,
177
- opacity: 0.7,
178
- textAlign: l.align,
179
- overflow: 'hidden',
180
- whiteSpace: 'nowrap',
181
- textOverflow: 'ellipsis',
182
- }}
183
- >
184
- {l.text}
185
- </div>
186
- ))}
187
- {cursor && props.crosshair && !scene?.pie && plot ? (
188
- <div style={{ position: 'absolute', left: cursor.px - 0.5, top: plot.y, width: 1, height: plot.height, backgroundColor: '#8a8a8a', pointerEvents: 'none' }} />
189
- ) : null}
190
- {cursor && (props.crosshair || props.tooltip) ? (
191
- <div style={{ position: 'absolute', left: cursor.px - 4, top: cursor.py - 4, width: 8, height: 8, borderRadius: 4, backgroundColor: dotColor ?? '#4e79a7', pointerEvents: 'none' }} />
192
- ) : null}
193
- {cursor && props.tooltip && tipText ? (
194
- <div
195
- style={{
196
- position: 'absolute',
197
- left: tipLeft,
198
- top: tipTop < 0 ? cursor.py + 12 : tipTop,
199
- backgroundColor: 'rgba(20,20,20,0.88)',
200
- color: '#fff',
201
- fontSize: 11,
202
- padding: '2px 6px',
203
- borderRadius: 4,
204
- whiteSpace: 'nowrap',
205
- pointerEvents: 'none',
206
- }}
207
- >
208
- {tipText}
209
- </div>
210
- ) : null}
211
- </div>
212
- </div>
213
- {legend === 'bottom' && scene ? legendRow(scene.legend, 'bottom') : null}
214
- </div>
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
+
25
+ propsRef.current = props
26
+
27
+ const hostRef = useRef<HTMLElement | null>(null)
28
+
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)
40
+
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
+ )
55
+
56
+ sceneRef.current = scene
57
+
58
+ const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair)
59
+
60
+ useEffect(() => {
61
+ const el = hostRef.current
62
+
63
+ if (!el || !interactive) {
64
+ return
65
+ }
66
+
67
+ const scrub = createScrub(
68
+ () => sceneRef.current,
69
+ () => propsRef.current,
70
+ setCursor,
71
+ )
72
+
73
+ const at = (e: PointerEvent) => {
74
+ const rect = el.getBoundingClientRect()
75
+
76
+ return { x: e.clientX - rect.left, y: e.clientY - rect.top }
77
+ }
78
+
79
+ const down = (e: PointerEvent) => {
80
+ el.setPointerCapture?.(e.pointerId)
81
+
82
+ const p = at(e)
83
+
84
+ scrub('down', p.x, p.y)
85
+ }
86
+
87
+ const move = (e: PointerEvent) => {
88
+ const p = at(e)
89
+
90
+ scrub(e.buttons !== 0 ? 'move' : 'hover', p.x, p.y)
91
+ }
92
+
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>
215
273
  }
@@ -0,0 +1,113 @@
1
+ import type { ScrubPhase } from './core/hit'
2
+
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
9
+ }
10
+
11
+ /** Raw renderer onPan event — top-left x/y and the NSPanGestureRecognizer
12
+ * state (1 began / 2 changed / 3 ended / anything else cancelled). */
13
+ export interface AppKitPanEvent {
14
+ x: number
15
+ y: number
16
+ deltaX: number
17
+ deltaY: number
18
+ state: number
19
+ }
20
+
21
+ const finite = (x: number, y: number) => Number.isFinite(x) && Number.isFinite(y)
22
+
23
+ /** Normalizes the AppKit pointer sources — pan recognizer deltas, tracking-
24
+ * area motion and the current mouse event — onto the shared scrub phases,
25
+ * mirroring the DOM listeners in Chart.web. */
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'),
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
35
+ } {
36
+ let panning = false
37
+
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
51
+
52
+ if (state === 1) {
53
+ if (!point) {
54
+ return
55
+ }
56
+
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
+ }
113
+ }