@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/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @octane-xplat/charts
2
+
3
+ [Documentation](https://octane-xplat.goddardai.org/package-charts).
4
+
5
+ [API reference](https://octane-xplat.goddardai.org/api-charts).
package/package.json CHANGED
@@ -1,94 +1,94 @@
1
1
  {
2
- "name": "@octane-xplat/charts",
3
- "version": "0.18.0",
4
- "description": "Cross-platform SVG charts for Octane xplat — a DOM-free d3 core with web, iOS/Android, and macOS AppKit leaves",
5
- "keywords": [
6
- "android",
7
- "charts",
8
- "cross-platform",
9
- "d3",
10
- "graph",
11
- "ios",
12
- "macos",
13
- "nativescript",
14
- "octane",
15
- "svg",
16
- "visualization",
17
- "xplat"
18
- ],
19
- "homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/charts",
20
- "license": "MIT",
21
- "repository": {
22
- "type": "git",
23
- "url": "https://github.com/octane-xplat/octane-xplat",
24
- "directory": "packages/charts"
25
- },
26
- "files": [
27
- "types",
28
- "src",
29
- "!src/**/*.test.*",
30
- "!src/**/*.spec.*"
31
- ],
32
- "type": "module",
33
- "exports": {
34
- ".": {
35
- "macos": {
36
- "types": "./types/index.d.ts",
37
- "default": "./src/index.macos.ts"
38
- },
39
- "types": "./types/index.d.ts",
40
- "web": "./src/index.web.ts",
41
- "native": "./src/index.ts",
42
- "default": "./src/index.ts"
43
- }
44
- },
45
- "publishConfig": {
46
- "access": "public"
47
- },
48
- "dependencies": {
49
- "d3-array": "3.2.4",
50
- "d3-format": "3.1.2",
51
- "d3-scale": "4.0.2",
52
- "d3-shape": "3.2.0",
53
- "d3-time-format": "4.1.0"
54
- },
55
- "devDependencies": {
56
- "@nativescript-community/octane": "0.2.5",
57
- "@nativescript/core": "9.1.3",
58
- "@octane-xplat/macos-renderer": "0.18.0",
59
- "@octane-xplat/ui": "0.18.0",
60
- "@octanejs/vite-plugin": "0.1.61",
61
- "@tsrx/typescript-plugin": "0.4.11",
62
- "@types/d3-array": "3.2.2",
63
- "@types/d3-format": "3.0.4",
64
- "@types/d3-scale": "4.0.9",
65
- "@types/d3-shape": "3.2.0",
66
- "@types/d3-time-format": "4.0.3",
67
- "octane": "0.14.0",
68
- "tsrx-typegen": "0.4.0",
69
- "typescript": "5.9.3",
70
- "vite": "8.3.0",
71
- "vitest": "4.1.11"
72
- },
73
- "peerDependencies": {
74
- "@nativescript-community/octane": ">=0.2.1 <1",
75
- "@nativescript/core": ">=9.1.0 <10",
76
- "@octane-xplat/ui": ">=0.6.0 <1",
77
- "octane": ">=0.6.3 <1"
78
- },
79
- "peerDependenciesMeta": {
80
- "@nativescript-community/octane": {
81
- "optional": true
82
- },
83
- "@nativescript/core": {
84
- "optional": true
85
- }
86
- },
87
- "scripts": {
88
- "typegen": "tsc -p tsconfig.types.json",
89
- "typecheck": "node ../../scripts/typecheck.ts",
90
- "test": "vitest run",
91
- "test:packed": "node tests/packed-consumer.mjs",
92
- "pack:check": "tsrx-typegen --pack-check"
93
- }
94
- }
2
+ "name": "@octane-xplat/charts",
3
+ "version": "0.20.0",
4
+ "description": "Cross-platform SVG charts for Octane xplat — a DOM-free d3 core with web, iOS/Android, and macOS AppKit leaves",
5
+ "keywords": [
6
+ "android",
7
+ "charts",
8
+ "cross-platform",
9
+ "d3",
10
+ "graph",
11
+ "ios",
12
+ "macos",
13
+ "nativescript",
14
+ "octane",
15
+ "svg",
16
+ "visualization",
17
+ "xplat"
18
+ ],
19
+ "homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/charts",
20
+ "license": "MIT",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/octane-xplat/octane-xplat",
24
+ "directory": "packages/charts"
25
+ },
26
+ "files": [
27
+ "types/generated",
28
+ "src",
29
+ "!src/**/*.test.*",
30
+ "!src/**/*.spec.*"
31
+ ],
32
+ "type": "module",
33
+ "exports": {
34
+ ".": {
35
+ "types": "./types/generated/index.d.ts",
36
+ "macos": "./src/index.macos.ts",
37
+ "web": "./src/index.web.ts",
38
+ "native": "./src/index.ts",
39
+ "default": "./src/index.ts"
40
+ }
41
+ },
42
+ "publishConfig": {
43
+ "access": "public"
44
+ },
45
+ "scripts": {
46
+ "typegen": "tsrx-typegen --target charts",
47
+ "typecheck": "node ../../scripts/typecheck.ts",
48
+ "test": "vitest run",
49
+ "prepack": "bun run typegen && tsrx-typegen --pack-check",
50
+ "test:packed": "node tests/packed-consumer.mjs",
51
+ "pack:check": "tsrx-typegen --pack-check"
52
+ },
53
+ "dependencies": {
54
+ "@octane-xplat/jsx": "0.20.0",
55
+ "d3-array": "3.2.5",
56
+ "d3-format": "3.1.3",
57
+ "d3-scale": "4.0.3",
58
+ "d3-shape": "3.2.1",
59
+ "d3-time-format": "4.1.1"
60
+ },
61
+ "devDependencies": {
62
+ "@nativescript-community/octane": "0.2.5",
63
+ "@nativescript/core": "9.1.3",
64
+ "@octane-xplat/macos-appkit": "0.20.0",
65
+ "@octane-xplat/ui": "0.20.0",
66
+ "@octanejs/vite-plugin": "0.5.0",
67
+ "@types/d3-array": "3.2.2",
68
+ "@types/d3-format": "3.0.4",
69
+ "@types/d3-scale": "4.0.9",
70
+ "@types/d3-shape": "3.2.0",
71
+ "@types/d3-time-format": "4.0.3",
72
+ "octane": "0.16.3",
73
+ "tsrx-typegen": "0.6.1",
74
+ "typescript": "7.1.0-beta",
75
+ "vite": "8.3.6",
76
+ "vitest": "4.1.11",
77
+ "@tsrx/content-mapper": "0.1.2"
78
+ },
79
+ "peerDependencies": {
80
+ "@nativescript-community/octane": ">=0.2.1 <1",
81
+ "@nativescript/core": ">=9.1.0 <10",
82
+ "@octane-xplat/ui": ">=0.6.0 <1",
83
+ "octane": ">=0.6.3 <1"
84
+ },
85
+ "peerDependenciesMeta": {
86
+ "@nativescript-community/octane": {
87
+ "optional": true
88
+ },
89
+ "@nativescript/core": {
90
+ "optional": true
91
+ }
92
+ },
93
+ "types": "./types/generated/index.d.ts"
94
+ }
@@ -1,119 +1,281 @@
1
- /** @jsxImportSource @octane-xplat/macos-renderer */
1
+ /** @jsxImportSource @octane-xplat/jsx */
2
2
  import { mergeStyle } from './style-merge'
3
- import { useMemo, useRef, useState } from 'octane';
4
- import { appKitImageSource } from './svg-image.macos';
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 { useCallback, useEffect, useMemo, useRef, useState } from 'octane'
4
+ import { appKitImageSource } from './svg-image.macos'
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
+ import { createAppKitChartPointer } from './appkit-pointer.macos'
11
12
 
12
- const EMPTY_SVG = '<svg xmlns="http://www.w3.org/2000/svg"/>';
13
- const TIP_ESTIMATE_CHAR = 6.2;
13
+ const EMPTY_SVG = '<svg xmlns="http://www.w3.org/2000/svg"/>'
14
+ const TIP_ESTIMATE_CHAR = 6.2
14
15
 
15
16
  /** AppKit leaf: one NSImage plus native label and cursor overlays.
16
- * Click recognizers currently supply no coordinates; taps use the scene center.
17
- * AppKit does not deliver onTouch, so touch scrubbing is unavailable. */
18
- export function Chart(props: ChartProps) @{
19
- const measure = useMeasure();
20
- const [cursor, setCursor] = useState<SceneHit | null>(null);
21
- const sceneRef = useRef<ChartScene | null>(null);
22
- const propsRef = useRef(props);
23
- propsRef.current = props;
17
+ * Hover/drag/click coordinates come from renderer tracking areas, pan
18
+ * recognizer location and the current mouse event; native delivery is
19
+ * unqualified. AppKit does not deliver onTouch, so touch scrubbing is
20
+ * unavailable. */
21
+ export function Chart(props: ChartProps): unknown @{
22
+ const measure = useMeasure()
23
+ const [cursor, setCursor] = useState<SceneHit | null>(null)
24
+ const sceneRef = useRef<ChartScene | null>(null)
25
+ const propsRef = useRef(props)
24
26
 
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);
27
+ propsRef.current = props
36
28
 
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
- );
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)
51
40
 
52
- sceneRef.current = scene;
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
+ )
53
55
 
54
- const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair);
55
- const onTap = useMemo(() => {
56
- const scrub = createScrub(() => sceneRef.current, () => propsRef.current, setCursor);
57
- return (e?: any) => {
58
- const scene = sceneRef.current;
59
- scrub('tap', e?.getX?.() ?? (scene?.width ?? 0) / 2, e?.getY?.() ?? (scene?.height ?? 0) / 2);
60
- };
61
- }, []);
56
+ sceneRef.current = scene
62
57
 
63
- const markup = scene?.markup ?? EMPTY_SVG;
64
- const imageSource = useMemo(() => appKitImageSource(markup), [markup]);
58
+ const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair)
59
+ const pointer = useMemo(
60
+ () =>
61
+ createAppKitChartPointer(
62
+ createScrub(
63
+ () => sceneRef.current,
64
+ () => propsRef.current,
65
+ setCursor,
66
+ ),
67
+ () => ({
68
+ width: sceneRef.current?.width ?? 0,
69
+ height: sceneRef.current?.height ?? 0,
70
+ }),
71
+ ),
72
+ [],
73
+ )
65
74
 
66
- const legend = props.legend === true ? 'bottom' : props.legend;
67
- const plot = scene?.plot;
68
- 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;
69
- const tipText = cursor && scene ? hitLabel(scene, cursor) : null;
70
- const tipWidth = tipText ? tipText.length * TIP_ESTIMATE_CHAR + 14 : 0;
71
- const tipLeft = cursor && scene ? Math.min(Math.max(0, cursor.px - tipWidth / 2), Math.max(0, scene.width - tipWidth)) : 0;
72
- const tipTop = cursor ? cursor.py - 30 : 0;
75
+ const [tapView, setTapView] = useState<any>(null)
76
+ const bindTapView = useCallback((view: any) => setTapView(view), [])
73
77
 
74
- <masonview id={props.id} className={props.className} style={{display: "flex", ...({ flexDirection: 'column', ...props.style })}} accessible={props.accessibilityLabel != null} accessibilityRole={'image'} accessibilityLabel={props.accessibilityLabel}>
75
- @if (legend === 'top' && scene) {
76
- <masonview style={mergeStyle({flexDirection: "row", flexWrap: "wrap"}, {display: "flex", ...({ paddingBottom: 4 })})}>
77
- {scene.legend.map((item) => (
78
- <masonview style={mergeStyle({flexDirection: "row", alignItems: "center"}, {display: "flex", ...({ marginRight: 12 })})}>
79
- <masonview style={{display: "flex", ...({ width: 10, height: 10, borderRadius: 2, backgroundColor: item.color, marginRight: 4 })}}/>
80
- <label text={item.text} style={{ fontSize: 11 }} />
81
- </masonview>
82
- ))}
83
- </masonview>
84
- }
85
- <masonview ref={measure.ref} style={{display: "grid", ...({
86
- width: props.width,
87
- height: props.height ?? (props.aspectRatio && w > 0 ? w / props.aspectRatio : undefined),
88
- flexGrow: 1,
89
- })}}>
90
- <image id={props.id ? props.id + '-image' : undefined} src={imageSource} style={{ width: w, height: h }} accessibilityLabel="" />
91
- <masonview id={props.id ? props.id + '-tap' : undefined} style={{display: "flex", ...({ width: w, height: h })}} onTap={interactive ? onTap : undefined}>
92
- <masonview id={props.id ? props.id + '-overlay' : undefined} style={{ width: w, height: h }}>
93
- {scene?.labels.map((l) => (
94
- <label text={l.text} {...{ left: l.x, top: l.y }} style={{ width: l.width, fontSize: 10, opacity: 0.7, textAlign: l.align === 'center' ? 'center' : l.align === 'end' ? 'right' : 'left' , position: 'absolute'}} />
95
- ))}
96
- @if (cursor && props.crosshair && !scene?.pie && plot) {
97
- <masonview style={{display: "flex", ...({ width: 1, height: plot.height, backgroundColor: '#8a8a8a' , position: 'absolute', left: cursor.px - 0.5, top: plot.y})}}/>
98
- }
99
- @if (cursor && (props.crosshair || props.tooltip)) {
100
- <masonview style={{display: "flex", ...({ width: 8, height: 8, borderRadius: 4, backgroundColor: dotColor ?? '#4e79a7' , position: 'absolute', left: cursor.px - 4, top: cursor.py - 4})}}/>
101
- }
102
- @if (cursor && props.tooltip && tipText) {
103
- <label id={props.id ? props.id + '-tooltip' : undefined} text={tipText} {...{ left: tipLeft, top: tipTop < 0 ? cursor.py + 12 : tipTop }} style={{ width: tipWidth, height: 24, backgroundColor: 'rgba(20,20,20,0.88)', borderRadius: 4, color: '#ffffff', fontSize: 11, textAlign: 'center' , position: 'absolute'}} />
104
- }
105
- </masonview>
106
- </masonview>
107
- </masonview>
108
- @if (legend === 'bottom' && scene) {
109
- <masonview style={mergeStyle({flexDirection: "row", flexWrap: "wrap"}, {display: "flex", ...({ paddingTop: 4 })})}>
110
- {scene.legend.map((item) => (
111
- <masonview style={mergeStyle({flexDirection: "row", alignItems: "center"}, {display: "flex", ...({ marginRight: 12 })})}>
112
- <masonview style={{display: "flex", ...({ width: 10, height: 10, borderRadius: 2, backgroundColor: item.color, marginRight: 4 })}}/>
113
- <label text={item.text} style={{ fontSize: 11 }} />
114
- </masonview>
115
- ))}
116
- </masonview>
117
- }
118
- </masonview>
78
+ useEffect(() => {
79
+ if (!interactive || !tapView) {
80
+ return
81
+ }
82
+
83
+ return pointer.observe(tapView)
84
+ }, [interactive, tapView, pointer])
85
+
86
+ const markup = scene?.markup ?? EMPTY_SVG
87
+ const imageSource = useMemo(() => appKitImageSource(markup), [markup])
88
+
89
+ const legend = props.legend === true ? 'bottom' : props.legend
90
+ const plot = scene?.plot
91
+ const dotColor =
92
+ cursor && scene
93
+ ? scene.pie
94
+ ? scene.pie.slices.find((s) => s.series === cursor.series && s.index === cursor.index)
95
+ ?.color
96
+ : scene.series[cursor.series]?.color
97
+ : undefined
98
+
99
+ const tipText = cursor && scene ? hitLabel(scene, cursor) : null
100
+ const tipWidth = tipText ? tipText.length * TIP_ESTIMATE_CHAR + 14 : 0
101
+ const tipLeft =
102
+ cursor && scene
103
+ ? Math.min(Math.max(0, cursor.px - tipWidth / 2), Math.max(0, scene.width - tipWidth))
104
+ : 0
105
+
106
+ const tipTop = cursor ? cursor.py - 30 : 0
107
+
108
+ <masonview
109
+ id={props.id}
110
+ className={props.className}
111
+ style={{
112
+ display: 'flex',
113
+ flexDirection: 'column',
114
+ ...props.style,
115
+ }}
116
+ accessible={props.accessibilityLabel != null}
117
+ accessibilityRole={'image'}
118
+ accessibilityLabel={props.accessibilityLabel}
119
+ >
120
+ @if (legend === 'top' && scene) {
121
+ <masonview
122
+ style={mergeStyle(
123
+ { flexDirection: 'row', flexWrap: 'wrap' },
124
+ {
125
+ display: 'flex',
126
+ paddingBottom: 4,
127
+ },
128
+ )}
129
+ >
130
+ {scene.legend.map((item) => (
131
+ <masonview
132
+ style={mergeStyle(
133
+ { flexDirection: 'row', alignItems: 'center' },
134
+ {
135
+ display: 'flex',
136
+ marginRight: 12,
137
+ },
138
+ )}
139
+ >
140
+ <masonview
141
+ style={{
142
+ display: 'flex',
143
+ width: 10,
144
+ height: 10,
145
+ borderRadius: 2,
146
+ backgroundColor: item.color,
147
+ marginRight: 4,
148
+ }}
149
+ />
150
+ <label text={item.text} style={{ fontSize: 11 }} />
151
+ </masonview>
152
+ ))}
153
+ </masonview>
154
+ }
155
+ <masonview
156
+ ref={measure.ref}
157
+ style={{
158
+ display: 'grid',
159
+ width: props.width,
160
+ height: props.height ?? (props.aspectRatio && w > 0 ? w / props.aspectRatio : undefined),
161
+ flexGrow: 1,
162
+ }}
163
+ >
164
+ <image
165
+ id={props.id ? props.id + '-image' : undefined}
166
+ src={imageSource}
167
+ style={{ width: w, height: h }}
168
+ accessibilityLabel=""
169
+ />
170
+ <masonview
171
+ id={props.id ? props.id + '-tap' : undefined}
172
+ ref={bindTapView}
173
+ style={{
174
+ display: 'flex',
175
+ width: w,
176
+ height: h,
177
+ }}
178
+ onPan={interactive ? pointer.pan : undefined}
179
+ onTap={interactive ? () => pointer.tap(tapView) : undefined}
180
+ >
181
+ <masonview
182
+ id={props.id ? props.id + '-overlay' : undefined}
183
+ style={{ width: w, height: h }}
184
+ >
185
+ {scene?.labels.map((l) => (
186
+ <label
187
+ text={l.text}
188
+ {...{ left: l.x, top: l.y }}
189
+ style={{
190
+ width: l.width,
191
+ fontSize: 10,
192
+ opacity: 0.7,
193
+ textAlign: l.align === 'center' ? 'center' : l.align === 'end' ? 'right' : 'left',
194
+ position: 'absolute',
195
+ }}
196
+ />
197
+ ))}
198
+ @if (cursor && props.crosshair && !scene?.pie && plot) {
199
+ <masonview
200
+ style={{
201
+ display: 'flex',
202
+ width: 1,
203
+ height: plot.height,
204
+ backgroundColor: '#8a8a8a',
205
+ position: 'absolute',
206
+ left: cursor.px - 0.5,
207
+ top: plot.y,
208
+ }}
209
+ />
210
+ }
211
+ @if (cursor && (props.crosshair || props.tooltip)) {
212
+ <masonview
213
+ style={{
214
+ display: 'flex',
215
+ width: 8,
216
+ height: 8,
217
+ borderRadius: 4,
218
+ backgroundColor: dotColor ?? '#4e79a7',
219
+ position: 'absolute',
220
+ left: cursor.px - 4,
221
+ top: cursor.py - 4,
222
+ }}
223
+ />
224
+ }
225
+ @if (cursor && props.tooltip && tipText) {
226
+ <label
227
+ id={props.id ? props.id + '-tooltip' : undefined}
228
+ text={tipText}
229
+ {...{ left: tipLeft, top: tipTop < 0 ? cursor.py + 12 : tipTop }}
230
+ style={{
231
+ width: tipWidth,
232
+ height: 24,
233
+ backgroundColor: 'rgba(20,20,20,0.88)',
234
+ borderRadius: 4,
235
+ color: '#ffffff',
236
+ fontSize: 11,
237
+ textAlign: 'center',
238
+ position: 'absolute',
239
+ }}
240
+ />
241
+ }
242
+ </masonview>
243
+ </masonview>
244
+ </masonview>
245
+ @if (legend === 'bottom' && scene) {
246
+ <masonview
247
+ style={mergeStyle(
248
+ { flexDirection: 'row', flexWrap: 'wrap' },
249
+ {
250
+ display: 'flex',
251
+ paddingTop: 4,
252
+ },
253
+ )}
254
+ >
255
+ {scene.legend.map((item) => (
256
+ <masonview
257
+ style={mergeStyle(
258
+ { flexDirection: 'row', alignItems: 'center' },
259
+ {
260
+ display: 'flex',
261
+ marginRight: 12,
262
+ },
263
+ )}
264
+ >
265
+ <masonview
266
+ style={{
267
+ display: 'flex',
268
+ width: 10,
269
+ height: 10,
270
+ borderRadius: 2,
271
+ backgroundColor: item.color,
272
+ marginRight: 4,
273
+ }}
274
+ />
275
+ <label text={item.text} style={{ fontSize: 11 }} />
276
+ </masonview>
277
+ ))}
278
+ </masonview>
279
+ }
280
+ </masonview>
119
281
  }