@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/Chart.tsrx CHANGED
@@ -1,26 +1,26 @@
1
- /** @jsxImportSource @nativescript-community/octane */
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';
11
-
12
- const EMPTY_SVG = '<svg xmlns="http://www.w3.org/2000/svg"/>';
13
- const TIP_ESTIMATE_CHAR = 6.2;
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
+
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
- try {
19
- return new Color(value ?? fallback);
20
- } catch {
21
- return new Color(fallback);
22
- }
23
- };
18
+ try {
19
+ return new Color(value ?? fallback)
20
+ } catch {
21
+ return new Color(fallback)
22
+ }
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,125 +29,260 @@ 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;
38
-
39
- const bounds = measure.bounds;
40
- const w = props.width ?? bounds?.width ?? 0;
41
- const h =
42
- props.height ??
43
- (props.aspectRatio && w > 0
44
- ? w / props.aspectRatio
45
- : bounds?.height && bounds.height > 0
46
- ? bounds.height
47
- : w > 0
48
- ? w / 1.6
49
- : 0);
50
-
51
- const scene = useMemo(
52
- () => (w > 0 && h > 0 ? computeScene(propsRef.current, w, h) : null),
53
- [
54
- props.type,
55
- props.data,
56
- props.margin,
57
- props.stacked,
58
- props.innerRadiusRatio,
59
- props.xAxis,
60
- props.yAxis,
61
- w,
62
- h,
63
- ],
64
- );
65
-
66
- sceneRef.current = scene;
67
-
68
- const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair);
69
- // Stable handler identity — the driver's prop apply subscribes `onTouch`
70
- // per identity change, so the closure reads scene/props through refs.
71
- const onTouch = useMemo(() => {
72
- const scrub = createScrub(
73
- () => sceneRef.current,
74
- () => propsRef.current,
75
- setCursor,
76
- );
77
-
78
- return (e: any) => {
79
- scrub(e.action, e.getX(), e.getY());
80
- };
81
- }, []);
82
-
83
- // `tap` is the completed-press phase — NS's tap recognizer (and the probe
84
- // harness's tap dispatch) reach onPress without a touch-sequence guess.
85
- const onTap = useMemo(() => {
86
- const scrub = createScrub(
87
- () => sceneRef.current,
88
- () => propsRef.current,
89
- setCursor,
90
- );
91
-
92
- return (e: any) => {
93
- // Probe harness taps dispatch with a synthetic event lacking
94
- // getX/getY — fall back to the view center there.
95
- const size = e.object?.getActualSize?.();
96
- scrub('tap', e.getX?.() ?? (size ? size.width / 2 : 0), e.getY?.() ?? (size ? size.height / 2 : 0));
97
- };
98
- }, []);
99
-
100
- const legend = props.legend === true ? 'bottom' : props.legend;
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;
107
-
108
- <masonview id={props.id} className={props.className} style={{display: "flex", ...({ flexDirection: 'column', ...props.style })}} accessible={props.accessibilityLabel != null} accessibilityRole={AccessibilityRole.Image} accessibilityLabel={props.accessibilityLabel}>
109
- @if (legend === 'top' && scene) {
110
- <masonview style={mergeStyle({flexDirection: "row", flexWrap: "wrap"}, {display: "flex", ...({ paddingBottom: 4 })})}>
111
- {scene.legend.map((item) => (
112
- <masonview style={mergeStyle({flexDirection: "row", alignItems: "center"}, {display: "flex", ...({ marginRight: 12 })})}>
113
- <contentview style={{ width: 10, height: 10, borderRadius: 2, backgroundColor: safeColor(item.color, 'transparent'), marginRight: 4 }} />
114
- <label text={item.text} style={{ fontSize: 11 }} />
115
- </masonview>
116
- ))}
117
- </masonview>
118
- }
119
- <masonview ref={measure.ref} style={{display: "grid", ...({
120
- width: props.width,
121
- height: props.height ?? (props.aspectRatio && w > 0 ? w / props.aspectRatio : undefined),
122
- flexGrow: 1,
123
- })}}>
124
- <svgview src={scene?.markup ?? EMPTY_SVG} stretch="aspectFit" style={{ width: w, height: h }} />
125
- <masonview id={props.id ? props.id + '-overlay' : undefined} style={{ width: w, height: h }} isUserInteractionEnabled={interactive} onTouch={interactive ? onTouch : undefined} onTap={interactive ? onTap : undefined}>
126
- {scene?.labels.map((l) => (
127
- <label width={l.width} text={l.text} textAlignment={l.align === 'center' ? 'center' : l.align === 'end' ? 'right' : 'left'} style={{ fontSize: 10, opacity: 0.7 , position: 'absolute', left: l.x, top: l.y}} />
128
- ))}
129
- @if (cursor && props.crosshair && !scene?.pie && plot) {
130
- <contentview width={1} height={plot.height} backgroundColor={new Color('#8a8a8a')} style={{position: 'absolute', left: cursor.px - 0.5, top: plot.y}} />
131
- }
132
- @if (cursor && (props.crosshair || props.tooltip)) {
133
- <contentview width={8} height={8} borderRadius={4} backgroundColor={safeColor(dotColor, '#4e79a7')} style={{position: 'absolute', left: cursor.px - 4, top: cursor.py - 4}} />
134
- }
135
- @if (cursor && props.tooltip && tipText) {
136
- <masonview style={{display: "flex", position: 'absolute', left: tipLeft, top: tipTop < 0 ? cursor.py + 12 : tipTop, paddingTop: 2, paddingBottom: 2, paddingLeft: 6, paddingRight: 6}}>
137
- <label text={tipText} color={new Color('#ffffff')} style={{ fontSize: 11 }} />
138
- </masonview>
139
- }
140
- </masonview>
141
- </masonview>
142
- @if (legend === 'bottom' && scene) {
143
- <masonview style={mergeStyle({flexDirection: "row", flexWrap: "wrap"}, {display: "flex", ...({ paddingTop: 4 })})}>
144
- {scene.legend.map((item) => (
145
- <masonview style={mergeStyle({flexDirection: "row", alignItems: "center"}, {display: "flex", ...({ marginRight: 12 })})}>
146
- <contentview style={{ width: 10, height: 10, borderRadius: 2, backgroundColor: safeColor(item.color, 'transparent'), marginRight: 4 }} />
147
- <label text={item.text} style={{ fontSize: 11 }} />
148
- </masonview>
149
- ))}
150
- </masonview>
151
- }
152
- </masonview>
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
+
38
+ propsRef.current = props
39
+
40
+ const bounds = measure.bounds
41
+ const w = props.width ?? bounds?.width ?? 0
42
+ const h =
43
+ props.height ??
44
+ (props.aspectRatio && w > 0
45
+ ? w / props.aspectRatio
46
+ : bounds?.height && bounds.height > 0
47
+ ? bounds.height
48
+ : w > 0
49
+ ? w / 1.6
50
+ : 0)
51
+
52
+ const scene = useMemo(
53
+ () => (w > 0 && h > 0 ? computeScene(propsRef.current, w, h) : null),
54
+ [
55
+ props.type,
56
+ props.data,
57
+ props.margin,
58
+ props.stacked,
59
+ props.innerRadiusRatio,
60
+ props.xAxis,
61
+ props.yAxis,
62
+ w,
63
+ h,
64
+ ],
65
+ )
66
+
67
+ sceneRef.current = scene
68
+
69
+ const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair)
70
+ // Stable handler identity — the driver's prop apply subscribes `onTouch`
71
+ // per identity change, so the closure reads scene/props through refs.
72
+ const onTouch = useMemo(() => {
73
+ const scrub = createScrub(
74
+ () => sceneRef.current,
75
+ () => propsRef.current,
76
+ setCursor,
77
+ )
78
+
79
+ return (e: any) => {
80
+ scrub(e.action, e.getX(), e.getY())
81
+ }
82
+ }, [])
83
+
84
+ // `tap` is the completed-press phase — NS's tap recognizer (and the probe
85
+ // harness's tap dispatch) reach onPress without a touch-sequence guess.
86
+ const onTap = useMemo(() => {
87
+ const scrub = createScrub(
88
+ () => sceneRef.current,
89
+ () => propsRef.current,
90
+ setCursor,
91
+ )
92
+
93
+ return (e: any) => {
94
+ // Probe harness taps dispatch with a synthetic event lacking
95
+ // getX/getY — fall back to the view center there.
96
+ const size = e.object?.getActualSize?.()
97
+
98
+ scrub(
99
+ 'tap',
100
+ e.getX?.() ?? (size ? size.width / 2 : 0),
101
+ e.getY?.() ?? (size ? size.height / 2 : 0),
102
+ )
103
+ }
104
+ }, [])
105
+
106
+ const legend = props.legend === true ? 'bottom' : props.legend
107
+ const plot = scene?.plot
108
+ const dotColor =
109
+ cursor && scene
110
+ ? scene.pie
111
+ ? scene.pie.slices.find((s) => s.series === cursor.series && s.index === cursor.index)
112
+ ?.color
113
+ : scene.series[cursor.series]?.color
114
+ : undefined
115
+
116
+ const tipText = cursor && scene ? hitLabel(scene, cursor) : null
117
+ const tipWidth = tipText ? tipText.length * TIP_ESTIMATE_CHAR + 14 : 0
118
+ const tipLeft =
119
+ cursor && scene
120
+ ? Math.min(Math.max(0, cursor.px - tipWidth / 2), Math.max(0, scene.width - tipWidth))
121
+ : 0
122
+
123
+ const tipTop = cursor ? cursor.py - 30 : 0
124
+
125
+ <masonview
126
+ id={props.id}
127
+ className={props.className}
128
+ style={{
129
+ display: 'flex',
130
+ flexDirection: 'column',
131
+ ...props.style,
132
+ }}
133
+ accessible={props.accessibilityLabel != null}
134
+ accessibilityRole={AccessibilityRole.Image}
135
+ accessibilityLabel={props.accessibilityLabel}
136
+ >
137
+ @if (legend === 'top' && scene) {
138
+ <masonview
139
+ style={mergeStyle(
140
+ { flexDirection: 'row', flexWrap: 'wrap' },
141
+ {
142
+ display: 'flex',
143
+ paddingBottom: 4,
144
+ },
145
+ )}
146
+ >
147
+ {scene.legend.map((item) => (
148
+ <masonview
149
+ style={mergeStyle(
150
+ { flexDirection: 'row', alignItems: 'center' },
151
+ {
152
+ display: 'flex',
153
+ marginRight: 12,
154
+ },
155
+ )}
156
+ >
157
+ <contentview
158
+ style={{
159
+ width: 10,
160
+ height: 10,
161
+ borderRadius: 2,
162
+ backgroundColor: safeColor(item.color, 'transparent'),
163
+ marginRight: 4,
164
+ }}
165
+ />
166
+ <label text={item.text} style={{ fontSize: 11 }} />
167
+ </masonview>
168
+ ))}
169
+ </masonview>
170
+ }
171
+ <masonview
172
+ ref={measure.ref}
173
+ style={{
174
+ display: 'grid',
175
+ width: props.width,
176
+ height: props.height ?? (props.aspectRatio && w > 0 ? w / props.aspectRatio : undefined),
177
+ flexGrow: 1,
178
+ }}
179
+ >
180
+ <svgview
181
+ src={scene?.markup ?? EMPTY_SVG}
182
+ stretch="aspectFit"
183
+ style={{ width: w, height: h }}
184
+ />
185
+ <masonview
186
+ id={props.id ? props.id + '-overlay' : undefined}
187
+ ref={(v: View | null) => {
188
+ if (v) {
189
+ v.isUserInteractionEnabled = interactive
190
+ }
191
+ }}
192
+ style={{ width: w, height: h }}
193
+ onTouch={interactive ? onTouch : undefined}
194
+ onTap={interactive ? onTap : undefined}
195
+ >
196
+ {scene?.labels.map((l) => (
197
+ <label
198
+ text={l.text}
199
+ style={{
200
+ width: l.width,
201
+ textAlign: l.align === 'center' ? 'center' : l.align === 'end' ? 'right' : 'left',
202
+ fontSize: 10,
203
+ opacity: 0.7,
204
+ position: 'absolute',
205
+ left: l.x,
206
+ top: l.y,
207
+ }}
208
+ />
209
+ ))}
210
+ @if (cursor && props.crosshair && !scene?.pie && plot) {
211
+ <contentview
212
+ style={{
213
+ width: 1,
214
+ height: plot.height,
215
+ backgroundColor: new Color('#8a8a8a'),
216
+ position: 'absolute',
217
+ left: cursor.px - 0.5,
218
+ top: plot.y,
219
+ }}
220
+ />
221
+ }
222
+ @if (cursor && (props.crosshair || props.tooltip)) {
223
+ <contentview
224
+ style={{
225
+ width: 8,
226
+ height: 8,
227
+ borderRadius: 4,
228
+ backgroundColor: safeColor(dotColor, '#4e79a7'),
229
+ position: 'absolute',
230
+ left: cursor.px - 4,
231
+ top: cursor.py - 4,
232
+ }}
233
+ />
234
+ }
235
+ @if (cursor && props.tooltip && tipText) {
236
+ <masonview
237
+ style={{
238
+ display: 'flex',
239
+ position: 'absolute',
240
+ left: tipLeft,
241
+ top: tipTop < 0 ? cursor.py + 12 : tipTop,
242
+ paddingTop: 2,
243
+ paddingBottom: 2,
244
+ paddingLeft: 6,
245
+ paddingRight: 6,
246
+ }}
247
+ >
248
+ <label text={tipText} style={{ color: new Color('#ffffff'), fontSize: 11 }} />
249
+ </masonview>
250
+ }
251
+ </masonview>
252
+ </masonview>
253
+ @if (legend === 'bottom' && scene) {
254
+ <masonview
255
+ style={mergeStyle(
256
+ { flexDirection: 'row', flexWrap: 'wrap' },
257
+ {
258
+ display: 'flex',
259
+ paddingTop: 4,
260
+ },
261
+ )}
262
+ >
263
+ {scene.legend.map((item) => (
264
+ <masonview
265
+ style={mergeStyle(
266
+ { flexDirection: 'row', alignItems: 'center' },
267
+ {
268
+ display: 'flex',
269
+ marginRight: 12,
270
+ },
271
+ )}
272
+ >
273
+ <contentview
274
+ style={{
275
+ width: 10,
276
+ height: 10,
277
+ borderRadius: 2,
278
+ backgroundColor: safeColor(item.color, 'transparent'),
279
+ marginRight: 4,
280
+ }}
281
+ />
282
+ <label text={item.text} style={{ fontSize: 11 }} />
283
+ </masonview>
284
+ ))}
285
+ </masonview>
286
+ }
287
+ </masonview>
153
288
  }