@octane-xplat/charts 0.17.1 → 0.19.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 +105 -0
- package/package.json +93 -93
- package/src/Chart.macos.tsrx +217 -57
- package/src/Chart.tsrx +191 -58
- package/src/Chart.web.tsrx +124 -76
- package/src/appkit-pointer.macos.ts +98 -0
- package/src/core/hit.ts +6 -1
- package/src/core/scene.ts +1 -1
- package/src/props.ts +27 -0
- package/src/style-merge.ts +3 -8
- package/types/generated/.tsrx-typegen-manifest.json +21 -0
- package/types/generated/Chart.d.ts +9 -0
- package/types/generated/Chart.macos.d.ts +7 -0
- package/types/generated/Chart.web.d.ts +10 -0
- package/types/generated/appkit-pointer.macos.d.ts +37 -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/generated/index.macos.d.ts +2 -0
- package/types/generated/index.web.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/types/generated/svg-image.macos.d.ts +3 -0
- package/LICENSE +0 -21
- package/types/index.d.ts +0 -17
package/src/Chart.tsrx
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
/** @jsxImportSource @
|
|
1
|
+
/** @jsxImportSource @octane-xplat/jsx */
|
|
2
2
|
import { mergeStyle } from './style-merge'
|
|
3
|
-
import { useMemo, useRef, useState } from 'octane'
|
|
4
|
-
import { AccessibilityRole, Color } from '@nativescript/core'
|
|
5
|
-
import { useMeasure } from '@octane-xplat/ui'
|
|
6
|
-
import type { ChartProps } from './props'
|
|
7
|
-
import { computeScene } from './core/scene'
|
|
8
|
-
import type { ChartScene } from './core/scene'
|
|
9
|
-
import { createScrub, hitLabel } from './core/hit'
|
|
10
|
-
import type { SceneHit } from './core/hit'
|
|
3
|
+
import { useMemo, useRef, useState } from 'octane'
|
|
4
|
+
import { AccessibilityRole, Color, type View } from '@nativescript/core'
|
|
5
|
+
import { useMeasure } from '@octane-xplat/ui'
|
|
6
|
+
import type { ChartProps } from './props'
|
|
7
|
+
import { computeScene } from './core/scene'
|
|
8
|
+
import type { ChartScene } from './core/scene'
|
|
9
|
+
import { createScrub, hitLabel } from './core/hit'
|
|
10
|
+
import type { SceneHit } from './core/hit'
|
|
11
11
|
|
|
12
|
-
const EMPTY_SVG = '<svg xmlns="http://www.w3.org/2000/svg"/>'
|
|
13
|
-
const TIP_ESTIMATE_CHAR = 6.2
|
|
12
|
+
const EMPTY_SVG = '<svg xmlns="http://www.w3.org/2000/svg"/>'
|
|
13
|
+
const TIP_ESTIMATE_CHAR = 6.2
|
|
14
14
|
|
|
15
15
|
// Color throws on unparseable input — user-controlled palette strings
|
|
16
16
|
// degrade to the fallback like a browser dropping a bad CSS value.
|
|
17
17
|
const safeColor = (value: string | undefined, fallback: string): Color => {
|
|
18
18
|
try {
|
|
19
|
-
return new Color(value ?? fallback)
|
|
19
|
+
return new Color(value ?? fallback)
|
|
20
20
|
} catch {
|
|
21
|
-
return new Color(fallback)
|
|
21
|
+
return new Color(fallback)
|
|
22
22
|
}
|
|
23
|
-
}
|
|
23
|
+
}
|
|
24
24
|
|
|
25
25
|
/** Chart — NativeScript leaf. The core's Mark[] serialize to one svg markup
|
|
26
26
|
* string fed to `<svgview>` (vendored ui-svg → SVGKit/androidsvg — its
|
|
@@ -29,15 +29,15 @@ const safeColor = (value: string | undefined, fallback: string): Color => {
|
|
|
29
29
|
* `absolutelayout` overlay — never `<text>` in the markup. Touch input on
|
|
30
30
|
* the overlay reports view-relative dips (`e.getX()/getY()`), matching the
|
|
31
31
|
* markup's coordinate space. */
|
|
32
|
-
export function Chart(props: ChartProps) @{
|
|
33
|
-
const measure = useMeasure()
|
|
34
|
-
const [cursor, setCursor] = useState<SceneHit | null>(null)
|
|
35
|
-
const sceneRef = useRef<ChartScene | null>(null)
|
|
36
|
-
const propsRef = useRef(props)
|
|
37
|
-
propsRef.current = props
|
|
32
|
+
export function Chart(props: ChartProps): unknown @{
|
|
33
|
+
const measure = useMeasure()
|
|
34
|
+
const [cursor, setCursor] = useState<SceneHit | null>(null)
|
|
35
|
+
const sceneRef = useRef<ChartScene | null>(null)
|
|
36
|
+
const propsRef = useRef(props)
|
|
37
|
+
propsRef.current = props
|
|
38
38
|
|
|
39
|
-
const bounds = measure.bounds
|
|
40
|
-
const w = props.width ?? bounds?.width ?? 0
|
|
39
|
+
const bounds = measure.bounds
|
|
40
|
+
const w = props.width ?? bounds?.width ?? 0
|
|
41
41
|
const h =
|
|
42
42
|
props.height ??
|
|
43
43
|
(props.aspectRatio && w > 0
|
|
@@ -46,7 +46,7 @@ export function Chart(props: ChartProps) @{
|
|
|
46
46
|
? bounds.height
|
|
47
47
|
: w > 0
|
|
48
48
|
? w / 1.6
|
|
49
|
-
: 0)
|
|
49
|
+
: 0)
|
|
50
50
|
|
|
51
51
|
const scene = useMemo(
|
|
52
52
|
() => (w > 0 && h > 0 ? computeScene(propsRef.current, w, h) : null),
|
|
@@ -61,11 +61,11 @@ export function Chart(props: ChartProps) @{
|
|
|
61
61
|
w,
|
|
62
62
|
h,
|
|
63
63
|
],
|
|
64
|
-
)
|
|
64
|
+
)
|
|
65
65
|
|
|
66
|
-
sceneRef.current = scene
|
|
66
|
+
sceneRef.current = scene
|
|
67
67
|
|
|
68
|
-
const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair)
|
|
68
|
+
const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair)
|
|
69
69
|
// Stable handler identity — the driver's prop apply subscribes `onTouch`
|
|
70
70
|
// per identity change, so the closure reads scene/props through refs.
|
|
71
71
|
const onTouch = useMemo(() => {
|
|
@@ -73,12 +73,12 @@ export function Chart(props: ChartProps) @{
|
|
|
73
73
|
() => sceneRef.current,
|
|
74
74
|
() => propsRef.current,
|
|
75
75
|
setCursor,
|
|
76
|
-
)
|
|
76
|
+
)
|
|
77
77
|
|
|
78
78
|
return (e: any) => {
|
|
79
|
-
scrub(e.action, e.getX(), e.getY())
|
|
80
|
-
}
|
|
81
|
-
}, [])
|
|
79
|
+
scrub(e.action, e.getX(), e.getY())
|
|
80
|
+
}
|
|
81
|
+
}, [])
|
|
82
82
|
|
|
83
83
|
// `tap` is the completed-press phase — NS's tap recognizer (and the probe
|
|
84
84
|
// harness's tap dispatch) reach onPress without a touch-sequence guess.
|
|
@@ -87,63 +87,196 @@ export function Chart(props: ChartProps) @{
|
|
|
87
87
|
() => sceneRef.current,
|
|
88
88
|
() => propsRef.current,
|
|
89
89
|
setCursor,
|
|
90
|
-
)
|
|
90
|
+
)
|
|
91
91
|
|
|
92
92
|
return (e: any) => {
|
|
93
93
|
// Probe harness taps dispatch with a synthetic event lacking
|
|
94
94
|
// getX/getY — fall back to the view center there.
|
|
95
|
-
const size = e.object?.getActualSize?.()
|
|
96
|
-
scrub(
|
|
97
|
-
|
|
98
|
-
|
|
95
|
+
const size = e.object?.getActualSize?.()
|
|
96
|
+
scrub(
|
|
97
|
+
'tap',
|
|
98
|
+
e.getX?.() ?? (size ? size.width / 2 : 0),
|
|
99
|
+
e.getY?.() ?? (size ? size.height / 2 : 0),
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
}, [])
|
|
103
|
+
|
|
104
|
+
const legend = props.legend === true ? 'bottom' : props.legend
|
|
105
|
+
const plot = scene?.plot
|
|
106
|
+
const dotColor =
|
|
107
|
+
cursor && scene
|
|
108
|
+
? scene.pie
|
|
109
|
+
? scene.pie.slices.find((s) => s.series === cursor.series && s.index === cursor.index)
|
|
110
|
+
?.color
|
|
111
|
+
: scene.series[cursor.series]?.color
|
|
112
|
+
: undefined
|
|
113
|
+
|
|
114
|
+
const tipText = cursor && scene ? hitLabel(scene, cursor) : null
|
|
115
|
+
const tipWidth = tipText ? tipText.length * TIP_ESTIMATE_CHAR + 14 : 0
|
|
116
|
+
const tipLeft =
|
|
117
|
+
cursor && scene
|
|
118
|
+
? Math.min(Math.max(0, cursor.px - tipWidth / 2), Math.max(0, scene.width - tipWidth))
|
|
119
|
+
: 0
|
|
99
120
|
|
|
100
|
-
const
|
|
101
|
-
const plot = scene?.plot;
|
|
102
|
-
const dotColor = cursor && scene ? (scene.pie ? scene.pie.slices.find((s) => s.series === cursor.series && s.index === cursor.index)?.color : scene.series[cursor.series]?.color) : undefined;
|
|
103
|
-
const tipText = cursor && scene ? hitLabel(scene, cursor) : null;
|
|
104
|
-
const tipWidth = tipText ? tipText.length * TIP_ESTIMATE_CHAR + 14 : 0;
|
|
105
|
-
const tipLeft = cursor && scene ? Math.min(Math.max(0, cursor.px - tipWidth / 2), Math.max(0, scene.width - tipWidth)) : 0;
|
|
106
|
-
const tipTop = cursor ? cursor.py - 30 : 0;
|
|
121
|
+
const tipTop = cursor ? cursor.py - 30 : 0
|
|
107
122
|
|
|
108
|
-
<masonview
|
|
123
|
+
<masonview
|
|
124
|
+
id={props.id}
|
|
125
|
+
className={props.className}
|
|
126
|
+
style={{
|
|
127
|
+
display: 'flex',
|
|
128
|
+
flexDirection: 'column',
|
|
129
|
+
...props.style,
|
|
130
|
+
}}
|
|
131
|
+
accessible={props.accessibilityLabel != null}
|
|
132
|
+
accessibilityRole={AccessibilityRole.Image}
|
|
133
|
+
accessibilityLabel={props.accessibilityLabel}
|
|
134
|
+
>
|
|
109
135
|
@if (legend === 'top' && scene) {
|
|
110
|
-
<masonview
|
|
136
|
+
<masonview
|
|
137
|
+
style={mergeStyle(
|
|
138
|
+
{ flexDirection: 'row', flexWrap: 'wrap' },
|
|
139
|
+
{
|
|
140
|
+
display: 'flex',
|
|
141
|
+
paddingBottom: 4,
|
|
142
|
+
},
|
|
143
|
+
)}
|
|
144
|
+
>
|
|
111
145
|
{scene.legend.map((item) => (
|
|
112
|
-
<masonview
|
|
113
|
-
|
|
146
|
+
<masonview
|
|
147
|
+
style={mergeStyle(
|
|
148
|
+
{ flexDirection: 'row', alignItems: 'center' },
|
|
149
|
+
{
|
|
150
|
+
display: 'flex',
|
|
151
|
+
marginRight: 12,
|
|
152
|
+
},
|
|
153
|
+
)}
|
|
154
|
+
>
|
|
155
|
+
<contentview
|
|
156
|
+
style={{
|
|
157
|
+
width: 10,
|
|
158
|
+
height: 10,
|
|
159
|
+
borderRadius: 2,
|
|
160
|
+
backgroundColor: safeColor(item.color, 'transparent'),
|
|
161
|
+
marginRight: 4,
|
|
162
|
+
}}
|
|
163
|
+
/>
|
|
114
164
|
<label text={item.text} style={{ fontSize: 11 }} />
|
|
115
165
|
</masonview>
|
|
116
166
|
))}
|
|
117
167
|
</masonview>
|
|
118
168
|
}
|
|
119
|
-
<masonview
|
|
169
|
+
<masonview
|
|
170
|
+
ref={measure.ref}
|
|
171
|
+
style={{
|
|
172
|
+
display: 'grid',
|
|
120
173
|
width: props.width,
|
|
121
174
|
height: props.height ?? (props.aspectRatio && w > 0 ? w / props.aspectRatio : undefined),
|
|
122
175
|
flexGrow: 1,
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
<
|
|
176
|
+
}}
|
|
177
|
+
>
|
|
178
|
+
<svgview
|
|
179
|
+
src={scene?.markup ?? EMPTY_SVG}
|
|
180
|
+
stretch="aspectFit"
|
|
181
|
+
style={{ width: w, height: h }}
|
|
182
|
+
/>
|
|
183
|
+
<masonview
|
|
184
|
+
id={props.id ? props.id + '-overlay' : undefined}
|
|
185
|
+
ref={(v: View | null) => {
|
|
186
|
+
if (v) {
|
|
187
|
+
v.isUserInteractionEnabled = interactive
|
|
188
|
+
}
|
|
189
|
+
}}
|
|
190
|
+
style={{ width: w, height: h }}
|
|
191
|
+
onTouch={interactive ? onTouch : undefined}
|
|
192
|
+
onTap={interactive ? onTap : undefined}
|
|
193
|
+
>
|
|
126
194
|
{scene?.labels.map((l) => (
|
|
127
|
-
<label
|
|
195
|
+
<label
|
|
196
|
+
text={l.text}
|
|
197
|
+
style={{
|
|
198
|
+
width: l.width,
|
|
199
|
+
textAlign: l.align === 'center' ? 'center' : l.align === 'end' ? 'right' : 'left',
|
|
200
|
+
fontSize: 10,
|
|
201
|
+
opacity: 0.7,
|
|
202
|
+
position: 'absolute',
|
|
203
|
+
left: l.x,
|
|
204
|
+
top: l.y,
|
|
205
|
+
}}
|
|
206
|
+
/>
|
|
128
207
|
))}
|
|
129
208
|
@if (cursor && props.crosshair && !scene?.pie && plot) {
|
|
130
|
-
<contentview
|
|
209
|
+
<contentview
|
|
210
|
+
style={{
|
|
211
|
+
width: 1,
|
|
212
|
+
height: plot.height,
|
|
213
|
+
backgroundColor: new Color('#8a8a8a'),
|
|
214
|
+
position: 'absolute',
|
|
215
|
+
left: cursor.px - 0.5,
|
|
216
|
+
top: plot.y,
|
|
217
|
+
}}
|
|
218
|
+
/>
|
|
131
219
|
}
|
|
132
220
|
@if (cursor && (props.crosshair || props.tooltip)) {
|
|
133
|
-
<contentview
|
|
221
|
+
<contentview
|
|
222
|
+
style={{
|
|
223
|
+
width: 8,
|
|
224
|
+
height: 8,
|
|
225
|
+
borderRadius: 4,
|
|
226
|
+
backgroundColor: safeColor(dotColor, '#4e79a7'),
|
|
227
|
+
position: 'absolute',
|
|
228
|
+
left: cursor.px - 4,
|
|
229
|
+
top: cursor.py - 4,
|
|
230
|
+
}}
|
|
231
|
+
/>
|
|
134
232
|
}
|
|
135
233
|
@if (cursor && props.tooltip && tipText) {
|
|
136
|
-
<masonview
|
|
137
|
-
|
|
234
|
+
<masonview
|
|
235
|
+
style={{
|
|
236
|
+
display: 'flex',
|
|
237
|
+
position: 'absolute',
|
|
238
|
+
left: tipLeft,
|
|
239
|
+
top: tipTop < 0 ? cursor.py + 12 : tipTop,
|
|
240
|
+
paddingTop: 2,
|
|
241
|
+
paddingBottom: 2,
|
|
242
|
+
paddingLeft: 6,
|
|
243
|
+
paddingRight: 6,
|
|
244
|
+
}}
|
|
245
|
+
>
|
|
246
|
+
<label text={tipText} style={{ color: new Color('#ffffff'), fontSize: 11 }} />
|
|
138
247
|
</masonview>
|
|
139
248
|
}
|
|
140
249
|
</masonview>
|
|
141
250
|
</masonview>
|
|
142
251
|
@if (legend === 'bottom' && scene) {
|
|
143
|
-
<masonview
|
|
252
|
+
<masonview
|
|
253
|
+
style={mergeStyle(
|
|
254
|
+
{ flexDirection: 'row', flexWrap: 'wrap' },
|
|
255
|
+
{
|
|
256
|
+
display: 'flex',
|
|
257
|
+
paddingTop: 4,
|
|
258
|
+
},
|
|
259
|
+
)}
|
|
260
|
+
>
|
|
144
261
|
{scene.legend.map((item) => (
|
|
145
|
-
<masonview
|
|
146
|
-
|
|
262
|
+
<masonview
|
|
263
|
+
style={mergeStyle(
|
|
264
|
+
{ flexDirection: 'row', alignItems: 'center' },
|
|
265
|
+
{
|
|
266
|
+
display: 'flex',
|
|
267
|
+
marginRight: 12,
|
|
268
|
+
},
|
|
269
|
+
)}
|
|
270
|
+
>
|
|
271
|
+
<contentview
|
|
272
|
+
style={{
|
|
273
|
+
width: 10,
|
|
274
|
+
height: 10,
|
|
275
|
+
borderRadius: 2,
|
|
276
|
+
backgroundColor: safeColor(item.color, 'transparent'),
|
|
277
|
+
marginRight: 4,
|
|
278
|
+
}}
|
|
279
|
+
/>
|
|
147
280
|
<label text={item.text} style={{ fontSize: 11 }} />
|
|
148
281
|
</masonview>
|
|
149
282
|
))}
|
package/src/Chart.web.tsrx
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import type {
|
|
6
|
-
import {
|
|
7
|
-
import type {
|
|
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,16 +16,16 @@ 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
|
|
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)
|
|
26
|
+
|
|
27
|
+
const bounds = measure.bounds
|
|
28
|
+
const w = props.width ?? bounds?.width ?? 0
|
|
27
29
|
const h =
|
|
28
30
|
props.height ??
|
|
29
31
|
(props.aspectRatio && w > 0
|
|
@@ -32,7 +34,7 @@ export function Chart(props: ChartProps) @{
|
|
|
32
34
|
? bounds.height
|
|
33
35
|
: w > 0
|
|
34
36
|
? w / 1.6
|
|
35
|
-
: 0)
|
|
37
|
+
: 0)
|
|
36
38
|
|
|
37
39
|
const scene = useMemo(
|
|
38
40
|
() => (w > 0 && h > 0 ? computeScene(propsRef.current, w, h) : null),
|
|
@@ -47,43 +49,43 @@ export function Chart(props: ChartProps) @{
|
|
|
47
49
|
w,
|
|
48
50
|
h,
|
|
49
51
|
],
|
|
50
|
-
)
|
|
52
|
+
)
|
|
51
53
|
|
|
52
|
-
sceneRef.current = scene
|
|
54
|
+
sceneRef.current = scene
|
|
53
55
|
|
|
54
|
-
const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair)
|
|
56
|
+
const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair)
|
|
55
57
|
useEffect(() => {
|
|
56
|
-
const el = hostRef.current
|
|
58
|
+
const el = hostRef.current
|
|
57
59
|
if (!el || !interactive) {
|
|
58
|
-
return
|
|
60
|
+
return
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
const scrub = createScrub(
|
|
62
64
|
() => sceneRef.current,
|
|
63
65
|
() => propsRef.current,
|
|
64
66
|
setCursor,
|
|
65
|
-
)
|
|
67
|
+
)
|
|
66
68
|
|
|
67
69
|
const at = (e: PointerEvent) => {
|
|
68
|
-
const rect = el.getBoundingClientRect()
|
|
69
|
-
return { x: e.clientX - rect.left, y: e.clientY - rect.top }
|
|
70
|
-
}
|
|
70
|
+
const rect = el.getBoundingClientRect()
|
|
71
|
+
return { x: e.clientX - rect.left, y: e.clientY - rect.top }
|
|
72
|
+
}
|
|
71
73
|
|
|
72
74
|
const down = (e: PointerEvent) => {
|
|
73
|
-
el.setPointerCapture?.(e.pointerId)
|
|
74
|
-
const p = at(e)
|
|
75
|
-
scrub('down', p.x, p.y)
|
|
76
|
-
}
|
|
75
|
+
el.setPointerCapture?.(e.pointerId)
|
|
76
|
+
const p = at(e)
|
|
77
|
+
scrub('down', p.x, p.y)
|
|
78
|
+
}
|
|
77
79
|
|
|
78
80
|
const move = (e: PointerEvent) => {
|
|
79
|
-
const p = at(e)
|
|
80
|
-
scrub(e.buttons !== 0 ? 'move' : 'hover', p.x, p.y)
|
|
81
|
-
}
|
|
81
|
+
const p = at(e)
|
|
82
|
+
scrub(e.buttons !== 0 ? 'move' : 'hover', p.x, p.y)
|
|
83
|
+
}
|
|
82
84
|
|
|
83
85
|
const up = (e: PointerEvent) => {
|
|
84
|
-
const p = at(e)
|
|
85
|
-
scrub('up', p.x, p.y)
|
|
86
|
-
}
|
|
86
|
+
const p = at(e)
|
|
87
|
+
scrub('up', p.x, p.y)
|
|
88
|
+
}
|
|
87
89
|
|
|
88
90
|
// `click` (incl. el.click()/AT activation) is the web 'tap' — onPress
|
|
89
91
|
// lives on tap so a real click after pointerup doesn't double-fire.
|
|
@@ -92,60 +94,85 @@ export function Chart(props: ChartProps) @{
|
|
|
92
94
|
const p =
|
|
93
95
|
e.clientX === 0 && e.clientY === 0
|
|
94
96
|
? { 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)
|
|
97
|
+
: at(e as PointerEvent)
|
|
98
|
+
|
|
99
|
+
scrub('tap', p.x, p.y)
|
|
100
|
+
}
|
|
101
|
+
|
|
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)
|
|
108
110
|
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
|
|
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])
|
|
119
|
+
|
|
120
|
+
const legend = props.legend === true ? 'bottom' : props.legend
|
|
121
|
+
const plot = scene?.plot
|
|
120
122
|
const dotColor =
|
|
121
123
|
cursor && scene
|
|
122
124
|
? scene.pie
|
|
123
|
-
? scene.pie.slices.find((s) => s.series === cursor.series && s.index === cursor.index)
|
|
125
|
+
? scene.pie.slices.find((s) => s.series === cursor.series && s.index === cursor.index)
|
|
126
|
+
?.color
|
|
124
127
|
: scene.series[cursor.series]?.color
|
|
125
|
-
: undefined
|
|
128
|
+
: undefined
|
|
126
129
|
|
|
127
|
-
const tipText = cursor && scene ? hitLabel(scene, cursor) : null
|
|
128
|
-
const tipWidth = tipText ? tipText.length * TIP_ESTIMATE_CHAR + 14 : 0
|
|
130
|
+
const tipText = cursor && scene ? hitLabel(scene, cursor) : null
|
|
131
|
+
const tipWidth = tipText ? tipText.length * TIP_ESTIMATE_CHAR + 14 : 0
|
|
129
132
|
const tipLeft =
|
|
130
|
-
cursor && scene
|
|
133
|
+
cursor && scene
|
|
134
|
+
? Math.min(Math.max(0, cursor.px - tipWidth / 2), Math.max(0, scene.width - tipWidth))
|
|
135
|
+
: 0
|
|
131
136
|
|
|
132
|
-
const tipTop = cursor ? cursor.py - 30 : 0
|
|
137
|
+
const tipTop = cursor ? cursor.py - 30 : 0
|
|
133
138
|
|
|
134
139
|
const legendRow = (items: { color: string; text: string }[], position: 'top' | 'bottom') => (
|
|
135
|
-
<div
|
|
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
|
+
>
|
|
136
149
|
{items.map((item) => (
|
|
137
150
|
<span style={{ display: 'inline-flex', alignItems: 'center', gap: 4, fontSize: 11 }}>
|
|
138
|
-
<span
|
|
151
|
+
<span
|
|
152
|
+
style={{
|
|
153
|
+
width: 10,
|
|
154
|
+
height: 10,
|
|
155
|
+
borderRadius: 2,
|
|
156
|
+
backgroundColor: item.color,
|
|
157
|
+
display: 'inline-block',
|
|
158
|
+
}}
|
|
159
|
+
/>
|
|
139
160
|
{item.text}
|
|
140
161
|
</span>
|
|
141
162
|
))}
|
|
142
163
|
</div>
|
|
143
|
-
)
|
|
164
|
+
)
|
|
144
165
|
|
|
145
166
|
<div
|
|
146
167
|
id={props.id}
|
|
147
168
|
className={props.className}
|
|
148
|
-
style={{
|
|
169
|
+
style={{
|
|
170
|
+
display: 'flex',
|
|
171
|
+
flexDirection: 'column',
|
|
172
|
+
width: props.width,
|
|
173
|
+
height: props.height,
|
|
174
|
+
...props.style,
|
|
175
|
+
}}
|
|
149
176
|
role={props.accessibilityLabel ? 'img' : undefined}
|
|
150
177
|
aria-label={props.accessibilityLabel}
|
|
151
178
|
>
|
|
@@ -153,8 +180,8 @@ export function Chart(props: ChartProps) @{
|
|
|
153
180
|
<div
|
|
154
181
|
id={props.id ? props.id + '-overlay' : undefined}
|
|
155
182
|
ref={(el: HTMLElement | null) => {
|
|
156
|
-
hostRef.current = el
|
|
157
|
-
measure.ref(el)
|
|
183
|
+
hostRef.current = el
|
|
184
|
+
measure.ref(el)
|
|
158
185
|
}}
|
|
159
186
|
style={{
|
|
160
187
|
position: 'relative',
|
|
@@ -185,10 +212,31 @@ export function Chart(props: ChartProps) @{
|
|
|
185
212
|
</div>
|
|
186
213
|
))}
|
|
187
214
|
{cursor && props.crosshair && !scene?.pie && plot ? (
|
|
188
|
-
<div
|
|
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
|
+
/>
|
|
189
226
|
) : null}
|
|
190
227
|
{cursor && (props.crosshair || props.tooltip) ? (
|
|
191
|
-
<div
|
|
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
|
+
/>
|
|
192
240
|
) : null}
|
|
193
241
|
{cursor && props.tooltip && tipText ? (
|
|
194
242
|
<div
|