@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/Chart.tsrx CHANGED
@@ -15,11 +15,11 @@ const TIP_ESTIMATE_CHAR = 6.2
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
- }
18
+ try {
19
+ return new Color(value ?? fallback)
20
+ } catch {
21
+ return new Color(fallback)
22
+ }
23
23
  }
24
24
 
25
25
  /** Chart — NativeScript leaf. The core's Mark[] serialize to one svg markup
@@ -30,257 +30,259 @@ const safeColor = (value: string | undefined, fallback: string): Color => {
30
30
  * the overlay reports view-relative dips (`e.getX()/getY()`), matching the
31
31
  * markup's coordinate space. */
32
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
33
+ const measure = useMeasure()
34
+ const [cursor, setCursor] = useState<SceneHit | null>(null)
35
+ const sceneRef = useRef<ChartScene | null>(null)
36
+ const propsRef = useRef(props)
38
37
 
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)
38
+ propsRef.current = props
50
39
 
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
- )
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)
65
51
 
66
- sceneRef.current = scene
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
+ )
67
66
 
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
- )
67
+ sceneRef.current = scene
77
68
 
78
- return (e: any) => {
79
- scrub(e.action, e.getX(), e.getY())
80
- }
81
- }, [])
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
+ )
82
78
 
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
- )
79
+ return (e: any) => {
80
+ scrub(e.action, e.getX(), e.getY())
81
+ }
82
+ }, [])
91
83
 
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(
97
- 'tap',
98
- e.getX?.() ?? (size ? size.width / 2 : 0),
99
- e.getY?.() ?? (size ? size.height / 2 : 0),
100
- )
101
- }
102
- }, [])
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
+ )
103
92
 
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
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?.()
113
97
 
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
98
+ scrub(
99
+ 'tap',
100
+ e.getX?.() ?? (size ? size.width / 2 : 0),
101
+ e.getY?.() ?? (size ? size.height / 2 : 0),
102
+ )
103
+ }
104
+ }, [])
120
105
 
121
- const tipTop = cursor ? cursor.py - 30 : 0
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
122
115
 
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
- >
135
- @if (legend === 'top' && scene) {
136
- <masonview
137
- style={mergeStyle(
138
- { flexDirection: 'row', flexWrap: 'wrap' },
139
- {
140
- display: 'flex',
141
- paddingBottom: 4,
142
- },
143
- )}
144
- >
145
- {scene.legend.map((item) => (
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
- />
164
- <label text={item.text} style={{ fontSize: 11 }} />
165
- </masonview>
166
- ))}
167
- </masonview>
168
- }
169
- <masonview
170
- ref={measure.ref}
171
- style={{
172
- display: 'grid',
173
- width: props.width,
174
- height: props.height ?? (props.aspectRatio && w > 0 ? w / props.aspectRatio : undefined),
175
- flexGrow: 1,
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
- >
194
- {scene?.labels.map((l) => (
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
- />
207
- ))}
208
- @if (cursor && props.crosshair && !scene?.pie && plot) {
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
- />
219
- }
220
- @if (cursor && (props.crosshair || props.tooltip)) {
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
- />
232
- }
233
- @if (cursor && props.tooltip && tipText) {
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 }} />
247
- </masonview>
248
- }
249
- </masonview>
250
- </masonview>
251
- @if (legend === 'bottom' && scene) {
252
- <masonview
253
- style={mergeStyle(
254
- { flexDirection: 'row', flexWrap: 'wrap' },
255
- {
256
- display: 'flex',
257
- paddingTop: 4,
258
- },
259
- )}
260
- >
261
- {scene.legend.map((item) => (
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
- />
280
- <label text={item.text} style={{ fontSize: 11 }} />
281
- </masonview>
282
- ))}
283
- </masonview>
284
- }
285
- </masonview>
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>
286
288
  }