@octane-xplat/charts 0.0.0-nightly.g14544fe
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/LICENSE +21 -0
- package/package.json +94 -0
- package/src/Chart.macos.tsrx +119 -0
- package/src/Chart.tsrx +153 -0
- package/src/Chart.web.tsrx +215 -0
- package/src/core/axes.ts +211 -0
- package/src/core/hit.ts +216 -0
- package/src/core/marks.ts +382 -0
- package/src/core/scales.ts +172 -0
- package/src/core/scene.ts +145 -0
- package/src/core/svg.ts +84 -0
- package/src/index.macos.ts +12 -0
- package/src/index.ts +12 -0
- package/src/index.web.ts +12 -0
- package/src/props.ts +90 -0
- package/src/style-merge.ts +61 -0
- package/src/svg-image.macos.ts +34 -0
- package/types/index.d.ts +17 -0
- package/types/props.d.ts +83 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alec Larson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@octane-xplat/charts",
|
|
3
|
+
"version": "0.0.0-nightly.g14544fe",
|
|
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.0.0-nightly.g14544fe",
|
|
59
|
+
"@octane-xplat/ui": "0.0.0-nightly.g14544fe",
|
|
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.11.0",
|
|
68
|
+
"tsrx-typegen": "0.3.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
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/** @jsxImportSource @octane-xplat/macos-renderer */
|
|
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';
|
|
11
|
+
|
|
12
|
+
const EMPTY_SVG = '<svg xmlns="http://www.w3.org/2000/svg"/>';
|
|
13
|
+
const TIP_ESTIMATE_CHAR = 6.2;
|
|
14
|
+
|
|
15
|
+
/** 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;
|
|
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
|
+
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
|
+
}, []);
|
|
62
|
+
|
|
63
|
+
const markup = scene?.markup ?? EMPTY_SVG;
|
|
64
|
+
const imageSource = useMemo(() => appKitImageSource(markup), [markup]);
|
|
65
|
+
|
|
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;
|
|
73
|
+
|
|
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>
|
|
119
|
+
}
|
package/src/Chart.tsrx
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/** @jsxImportSource @nativescript-community/octane */
|
|
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;
|
|
14
|
+
|
|
15
|
+
// Color throws on unparseable input — user-controlled palette strings
|
|
16
|
+
// degrade to the fallback like a browser dropping a bad CSS value.
|
|
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
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Chart — NativeScript leaf. The core's Mark[] serialize to one svg markup
|
|
26
|
+
* string fed to `<svgview>` (vendored ui-svg → SVGKit/androidsvg — its
|
|
27
|
+
* registration arrives transitively through `@octane-xplat/ui`'s native
|
|
28
|
+
* entry). Axis labels, legend, tooltip, and crosshair are real views in an
|
|
29
|
+
* `absolutelayout` overlay — never `<text>` in the markup. Touch input on
|
|
30
|
+
* the overlay reports view-relative dips (`e.getX()/getY()`), matching the
|
|
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>
|
|
153
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
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';
|
|
8
|
+
|
|
9
|
+
const TIP_ESTIMATE_CHAR = 6.2;
|
|
10
|
+
|
|
11
|
+
/** Chart — web leaf. The core's Mark[] serialize to one svg markup string,
|
|
12
|
+
* inlined as `innerHTML` (Icon.web precedent — the same bytes feed
|
|
13
|
+
* `<svgview>` on native). Axis labels, legend, tooltip, and crosshair are
|
|
14
|
+
* real absolutely-positioned elements in the overlay — never `<text>` in
|
|
15
|
+
* the markup. Pointer handlers attach to the plot host so chart coords are
|
|
16
|
+
* 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>
|
|
215
|
+
}
|