@octane-xplat/charts 0.0.1 → 0.11.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/LICENSE +21 -0
- package/dist/macos/Chart.macos.tsrx.js +351 -0
- package/dist/macos/core/axes.js +146 -0
- package/dist/macos/core/hit.js +131 -0
- package/dist/macos/core/marks.js +262 -0
- package/dist/macos/core/scales.js +99 -0
- package/dist/macos/core/scene.js +120 -0
- package/dist/macos/core/svg.js +40 -0
- package/dist/macos/index.js +2 -0
- package/dist/macos/octane-client-build.json +8 -0
- package/dist/macos/svg-image.macos.js +14 -0
- package/dist/native/Chart.tsrx.js +340 -0
- package/dist/native/core/axes.js +146 -0
- package/dist/native/core/hit.js +131 -0
- package/dist/native/core/marks.js +262 -0
- package/dist/native/core/scales.js +99 -0
- package/dist/native/core/scene.js +120 -0
- package/dist/native/core/svg.js +40 -0
- package/dist/native/index.js +2 -0
- package/dist/native/octane-client-build.json +8 -0
- package/dist/web/Chart.web.tsrx.js +488 -0
- package/dist/web/core/axes.js +146 -0
- package/dist/web/core/hit.js +131 -0
- package/dist/web/core/marks.js +262 -0
- package/dist/web/core/scales.js +99 -0
- package/dist/web/core/scene.js +120 -0
- package/dist/web/core/svg.js +40 -0
- package/dist/web/index.js +2 -0
- package/dist/web/octane-client-build.json +8 -0
- package/package.json +86 -3
- package/src/Chart.macos.tsrx +134 -0
- package/src/Chart.tsrx +174 -0
- package/src/Chart.web.tsrx +215 -0
- package/src/core/axes.ts +211 -0
- package/src/core/hit.ts +201 -0
- package/src/core/marks.ts +380 -0
- package/src/core/scales.ts +168 -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/svg-image.macos.ts +34 -0
- package/types/index.d.ts +17 -0
- package/types/props.d.ts +83 -0
- package/README.md +0 -4
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.
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import { appKitImageSource } from "./svg-image.macos.js";
|
|
2
|
+
import { computeScene } from "./core/scene.js";
|
|
3
|
+
import { createScrub, hitLabel } from "./core/hit.js";
|
|
4
|
+
import { defineUniversalComponent, hookSlots, universalIf, universalPlan, universalProps, universalValue, useMemo, useRef, useState, withSlot } from "@nativescript-community/octane";
|
|
5
|
+
import { useMeasure } from "@octane-xplat/ui";
|
|
6
|
+
//#region src/Chart.macos.tsrx
|
|
7
|
+
var _hs$ = /* @__PURE__ */ hookSlots(7);
|
|
8
|
+
var _h$0 = /* @__PURE__ */ Symbol(_hs$);
|
|
9
|
+
var _h$1 = /* @__PURE__ */ Symbol(_hs$ + 1);
|
|
10
|
+
var _h$2 = /* @__PURE__ */ Symbol(_hs$ + 2);
|
|
11
|
+
var _h$3 = /* @__PURE__ */ Symbol(_hs$ + 3);
|
|
12
|
+
var _h$4 = /* @__PURE__ */ Symbol(_hs$ + 4);
|
|
13
|
+
var _h$5 = /* @__PURE__ */ Symbol(_hs$ + 5);
|
|
14
|
+
var _h$6 = /* @__PURE__ */ Symbol(_hs$ + 6);
|
|
15
|
+
var __octaneUniversalPlan0 = universalPlan("macos", {
|
|
16
|
+
"kind": "host",
|
|
17
|
+
"type": "flexboxlayout",
|
|
18
|
+
"props": {
|
|
19
|
+
"flexDirection": "row",
|
|
20
|
+
"alignItems": "center"
|
|
21
|
+
},
|
|
22
|
+
"bindings": [["style", 0]],
|
|
23
|
+
"children": [{
|
|
24
|
+
"kind": "host",
|
|
25
|
+
"type": "flexboxlayout",
|
|
26
|
+
"bindings": [["style", 1]]
|
|
27
|
+
}, {
|
|
28
|
+
"kind": "host",
|
|
29
|
+
"type": "label",
|
|
30
|
+
"bindings": [["text", 2], ["style", 3]]
|
|
31
|
+
}]
|
|
32
|
+
});
|
|
33
|
+
var __octaneUniversalPlan1 = universalPlan("macos", {
|
|
34
|
+
"kind": "host",
|
|
35
|
+
"type": "flexboxlayout",
|
|
36
|
+
"props": {
|
|
37
|
+
"flexDirection": "row",
|
|
38
|
+
"flexWrap": "wrap"
|
|
39
|
+
},
|
|
40
|
+
"bindings": [["style", 0]],
|
|
41
|
+
"children": [{
|
|
42
|
+
"kind": "slot",
|
|
43
|
+
"slot": 1
|
|
44
|
+
}]
|
|
45
|
+
});
|
|
46
|
+
var __octaneUniversalPlan2 = universalPlan("macos", {
|
|
47
|
+
"kind": "host",
|
|
48
|
+
"type": "label",
|
|
49
|
+
"propsSlot": 0
|
|
50
|
+
});
|
|
51
|
+
var __octaneUniversalPlan3 = universalPlan("macos", {
|
|
52
|
+
"kind": "host",
|
|
53
|
+
"type": "flexboxlayout",
|
|
54
|
+
"bindings": [
|
|
55
|
+
["left", 0],
|
|
56
|
+
["top", 1],
|
|
57
|
+
["style", 2]
|
|
58
|
+
]
|
|
59
|
+
});
|
|
60
|
+
var __octaneUniversalPlan4 = universalPlan("macos", {
|
|
61
|
+
"kind": "host",
|
|
62
|
+
"type": "flexboxlayout",
|
|
63
|
+
"bindings": [
|
|
64
|
+
["left", 0],
|
|
65
|
+
["top", 1],
|
|
66
|
+
["style", 2]
|
|
67
|
+
]
|
|
68
|
+
});
|
|
69
|
+
var __octaneUniversalPlan5 = universalPlan("macos", {
|
|
70
|
+
"kind": "host",
|
|
71
|
+
"type": "label",
|
|
72
|
+
"propsSlot": 0
|
|
73
|
+
});
|
|
74
|
+
var __octaneUniversalPlan6 = universalPlan("macos", {
|
|
75
|
+
"kind": "host",
|
|
76
|
+
"type": "flexboxlayout",
|
|
77
|
+
"props": {
|
|
78
|
+
"flexDirection": "row",
|
|
79
|
+
"alignItems": "center"
|
|
80
|
+
},
|
|
81
|
+
"bindings": [["style", 0]],
|
|
82
|
+
"children": [{
|
|
83
|
+
"kind": "host",
|
|
84
|
+
"type": "flexboxlayout",
|
|
85
|
+
"bindings": [["style", 1]]
|
|
86
|
+
}, {
|
|
87
|
+
"kind": "host",
|
|
88
|
+
"type": "label",
|
|
89
|
+
"bindings": [["text", 2], ["style", 3]]
|
|
90
|
+
}]
|
|
91
|
+
});
|
|
92
|
+
var __octaneUniversalPlan7 = universalPlan("macos", {
|
|
93
|
+
"kind": "host",
|
|
94
|
+
"type": "flexboxlayout",
|
|
95
|
+
"props": {
|
|
96
|
+
"flexDirection": "row",
|
|
97
|
+
"flexWrap": "wrap"
|
|
98
|
+
},
|
|
99
|
+
"bindings": [["style", 0]],
|
|
100
|
+
"children": [{
|
|
101
|
+
"kind": "slot",
|
|
102
|
+
"slot": 1
|
|
103
|
+
}]
|
|
104
|
+
});
|
|
105
|
+
var __octaneUniversalPlan8 = universalPlan("macos", {
|
|
106
|
+
"kind": "host",
|
|
107
|
+
"type": "flexboxlayout",
|
|
108
|
+
"props": { "accessibilityRole": "image" },
|
|
109
|
+
"bindings": [
|
|
110
|
+
["id", 0],
|
|
111
|
+
["className", 1],
|
|
112
|
+
["style", 2],
|
|
113
|
+
["accessible", 3],
|
|
114
|
+
["accessibilityLabel", 4]
|
|
115
|
+
],
|
|
116
|
+
"children": [
|
|
117
|
+
{
|
|
118
|
+
"kind": "slot",
|
|
119
|
+
"slot": 5
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
"kind": "host",
|
|
123
|
+
"type": "gridlayout",
|
|
124
|
+
"bindings": [["ref", 6], ["style", 7]],
|
|
125
|
+
"children": [{
|
|
126
|
+
"kind": "host",
|
|
127
|
+
"type": "image",
|
|
128
|
+
"props": { "accessibilityLabel": "" },
|
|
129
|
+
"bindings": [
|
|
130
|
+
["id", 8],
|
|
131
|
+
["src", 9],
|
|
132
|
+
["style", 10]
|
|
133
|
+
]
|
|
134
|
+
}, {
|
|
135
|
+
"kind": "host",
|
|
136
|
+
"type": "flexboxlayout",
|
|
137
|
+
"bindings": [
|
|
138
|
+
["id", 11],
|
|
139
|
+
["style", 12],
|
|
140
|
+
["onTap", 13]
|
|
141
|
+
],
|
|
142
|
+
"children": [{
|
|
143
|
+
"kind": "host",
|
|
144
|
+
"type": "absolutelayout",
|
|
145
|
+
"bindings": [["id", 14], ["style", 15]],
|
|
146
|
+
"children": [
|
|
147
|
+
{
|
|
148
|
+
"kind": "slot",
|
|
149
|
+
"slot": 16
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
"kind": "slot",
|
|
153
|
+
"slot": 17
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
"kind": "slot",
|
|
157
|
+
"slot": 18
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
"kind": "slot",
|
|
161
|
+
"slot": 19
|
|
162
|
+
}
|
|
163
|
+
]
|
|
164
|
+
}]
|
|
165
|
+
}]
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
"kind": "slot",
|
|
169
|
+
"slot": 20
|
|
170
|
+
}
|
|
171
|
+
]
|
|
172
|
+
});
|
|
173
|
+
var EMPTY_SVG = "<svg xmlns=\"http://www.w3.org/2000/svg\"/>";
|
|
174
|
+
var TIP_ESTIMATE_CHAR = 6.2;
|
|
175
|
+
var Chart = defineUniversalComponent("macos", function Chart(props) {
|
|
176
|
+
const measure = withSlot(_h$0, useMeasure);
|
|
177
|
+
const [cursor, setCursor] = useState(null, _h$1);
|
|
178
|
+
const sceneRef = useRef(null, _h$2);
|
|
179
|
+
const propsRef = useRef(props, _h$3);
|
|
180
|
+
propsRef.current = props;
|
|
181
|
+
const bounds = measure.bounds;
|
|
182
|
+
const w = props.width ?? bounds?.width ?? 0;
|
|
183
|
+
const h = props.height ?? (props.aspectRatio && w > 0 ? w / props.aspectRatio : bounds?.height && bounds.height > 0 ? bounds.height : w > 0 ? w / 1.6 : 0);
|
|
184
|
+
const scene = useMemo(() => w > 0 && h > 0 ? computeScene(propsRef.current, w, h) : null, [
|
|
185
|
+
props.type,
|
|
186
|
+
props.data,
|
|
187
|
+
props.margin,
|
|
188
|
+
props.stacked,
|
|
189
|
+
props.innerRadiusRatio,
|
|
190
|
+
props.xAxis,
|
|
191
|
+
props.yAxis,
|
|
192
|
+
w,
|
|
193
|
+
h
|
|
194
|
+
], _h$4);
|
|
195
|
+
sceneRef.current = scene;
|
|
196
|
+
const interactive = !!(props.onPress || props.onScrub || props.tooltip || props.crosshair);
|
|
197
|
+
const onTap = useMemo(() => {
|
|
198
|
+
const scrub = createScrub(() => sceneRef.current, () => propsRef.current, setCursor);
|
|
199
|
+
return (e) => {
|
|
200
|
+
const scene = sceneRef.current;
|
|
201
|
+
scrub("tap", e?.getX?.() ?? (scene?.width ?? 0) / 2, e?.getY?.() ?? (scene?.height ?? 0) / 2);
|
|
202
|
+
};
|
|
203
|
+
}, [], _h$5);
|
|
204
|
+
const markup = scene?.markup ?? EMPTY_SVG;
|
|
205
|
+
const imageSource = useMemo(() => appKitImageSource(markup), [markup], _h$6);
|
|
206
|
+
const legend = props.legend === true ? "bottom" : props.legend;
|
|
207
|
+
const plot = scene?.plot;
|
|
208
|
+
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 : void 0;
|
|
209
|
+
const tipText = cursor && scene ? hitLabel(scene, cursor) : null;
|
|
210
|
+
const tipWidth = tipText ? tipText.length * TIP_ESTIMATE_CHAR + 14 : 0;
|
|
211
|
+
const tipLeft = cursor && scene ? Math.min(Math.max(0, cursor.px - tipWidth / 2), Math.max(0, scene.width - tipWidth)) : 0;
|
|
212
|
+
const tipTop = cursor ? cursor.py - 30 : 0;
|
|
213
|
+
return universalValue(__octaneUniversalPlan8, [
|
|
214
|
+
props.id,
|
|
215
|
+
props.className,
|
|
216
|
+
{
|
|
217
|
+
flexDirection: "column",
|
|
218
|
+
...props.style
|
|
219
|
+
},
|
|
220
|
+
props.accessibilityLabel != null,
|
|
221
|
+
props.accessibilityLabel,
|
|
222
|
+
universalIf(legend === "top" && scene, () => {
|
|
223
|
+
return universalValue(__octaneUniversalPlan1, [{ paddingBottom: 4 }, scene.legend.map((item) => universalValue(__octaneUniversalPlan0, [
|
|
224
|
+
{ marginRight: 12 },
|
|
225
|
+
{
|
|
226
|
+
width: 10,
|
|
227
|
+
height: 10,
|
|
228
|
+
borderRadius: 2,
|
|
229
|
+
backgroundColor: item.color,
|
|
230
|
+
marginRight: 4
|
|
231
|
+
},
|
|
232
|
+
item.text,
|
|
233
|
+
{ fontSize: 11 }
|
|
234
|
+
]))]);
|
|
235
|
+
}),
|
|
236
|
+
measure.ref,
|
|
237
|
+
{
|
|
238
|
+
width: props.width,
|
|
239
|
+
height: props.height ?? (props.aspectRatio && w > 0 ? w / props.aspectRatio : void 0),
|
|
240
|
+
flexGrow: 1
|
|
241
|
+
},
|
|
242
|
+
props.id ? props.id + "-image" : void 0,
|
|
243
|
+
imageSource,
|
|
244
|
+
{
|
|
245
|
+
width: w,
|
|
246
|
+
height: h
|
|
247
|
+
},
|
|
248
|
+
props.id ? props.id + "-tap" : void 0,
|
|
249
|
+
{
|
|
250
|
+
width: w,
|
|
251
|
+
height: h
|
|
252
|
+
},
|
|
253
|
+
interactive ? onTap : void 0,
|
|
254
|
+
props.id ? props.id + "-overlay" : void 0,
|
|
255
|
+
{
|
|
256
|
+
width: w,
|
|
257
|
+
height: h
|
|
258
|
+
},
|
|
259
|
+
scene?.labels.map((l) => universalValue(__octaneUniversalPlan2, [universalProps([
|
|
260
|
+
[
|
|
261
|
+
"set",
|
|
262
|
+
"text",
|
|
263
|
+
l.text
|
|
264
|
+
],
|
|
265
|
+
["spread", {
|
|
266
|
+
left: l.x,
|
|
267
|
+
top: l.y
|
|
268
|
+
}],
|
|
269
|
+
[
|
|
270
|
+
"set",
|
|
271
|
+
"style",
|
|
272
|
+
{
|
|
273
|
+
width: l.width,
|
|
274
|
+
fontSize: 10,
|
|
275
|
+
opacity: .7,
|
|
276
|
+
textAlign: l.align === "center" ? "center" : l.align === "end" ? "right" : "left"
|
|
277
|
+
}
|
|
278
|
+
]
|
|
279
|
+
])])),
|
|
280
|
+
universalIf(cursor && props.crosshair && !scene?.pie && plot, () => {
|
|
281
|
+
return universalValue(__octaneUniversalPlan3, [
|
|
282
|
+
cursor.px - .5,
|
|
283
|
+
plot.y,
|
|
284
|
+
{
|
|
285
|
+
width: 1,
|
|
286
|
+
height: plot.height,
|
|
287
|
+
backgroundColor: "#8a8a8a"
|
|
288
|
+
}
|
|
289
|
+
]);
|
|
290
|
+
}),
|
|
291
|
+
universalIf(cursor && (props.crosshair || props.tooltip), () => {
|
|
292
|
+
return universalValue(__octaneUniversalPlan4, [
|
|
293
|
+
cursor.px - 4,
|
|
294
|
+
cursor.py - 4,
|
|
295
|
+
{
|
|
296
|
+
width: 8,
|
|
297
|
+
height: 8,
|
|
298
|
+
borderRadius: 4,
|
|
299
|
+
backgroundColor: dotColor ?? "#4e79a7"
|
|
300
|
+
}
|
|
301
|
+
]);
|
|
302
|
+
}),
|
|
303
|
+
universalIf(cursor && props.tooltip && tipText, () => {
|
|
304
|
+
return universalValue(__octaneUniversalPlan5, [universalProps([
|
|
305
|
+
[
|
|
306
|
+
"set",
|
|
307
|
+
"id",
|
|
308
|
+
props.id ? props.id + "-tooltip" : void 0
|
|
309
|
+
],
|
|
310
|
+
[
|
|
311
|
+
"set",
|
|
312
|
+
"text",
|
|
313
|
+
tipText
|
|
314
|
+
],
|
|
315
|
+
["spread", {
|
|
316
|
+
left: tipLeft,
|
|
317
|
+
top: tipTop < 0 ? cursor.py + 12 : tipTop
|
|
318
|
+
}],
|
|
319
|
+
[
|
|
320
|
+
"set",
|
|
321
|
+
"style",
|
|
322
|
+
{
|
|
323
|
+
width: tipWidth,
|
|
324
|
+
height: 24,
|
|
325
|
+
backgroundColor: "rgba(20,20,20,0.88)",
|
|
326
|
+
borderRadius: 4,
|
|
327
|
+
color: "#ffffff",
|
|
328
|
+
fontSize: 11,
|
|
329
|
+
textAlign: "center"
|
|
330
|
+
}
|
|
331
|
+
]
|
|
332
|
+
])]);
|
|
333
|
+
}),
|
|
334
|
+
universalIf(legend === "bottom" && scene, () => {
|
|
335
|
+
return universalValue(__octaneUniversalPlan7, [{ paddingTop: 4 }, scene.legend.map((item) => universalValue(__octaneUniversalPlan6, [
|
|
336
|
+
{ marginRight: 12 },
|
|
337
|
+
{
|
|
338
|
+
width: 10,
|
|
339
|
+
height: 10,
|
|
340
|
+
borderRadius: 2,
|
|
341
|
+
backgroundColor: item.color,
|
|
342
|
+
marginRight: 4
|
|
343
|
+
},
|
|
344
|
+
item.text,
|
|
345
|
+
{ fontSize: 11 }
|
|
346
|
+
]))]);
|
|
347
|
+
})
|
|
348
|
+
]);
|
|
349
|
+
}, { "module": "@octane-xplat/macos-renderer" });
|
|
350
|
+
//#endregion
|
|
351
|
+
export { Chart };
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { xCenter } from "./scales.js";
|
|
2
|
+
import { format } from "d3-format";
|
|
3
|
+
import { timeFormat } from "d3-time-format";
|
|
4
|
+
//#region src/core/axes.ts
|
|
5
|
+
var AXIS_COLOR = "#9aa3ad";
|
|
6
|
+
var GRID_COLOR = "#9aa3ad";
|
|
7
|
+
var GRID_OPACITY = .22;
|
|
8
|
+
var si = format("~s");
|
|
9
|
+
var plain = format("~g");
|
|
10
|
+
var day = timeFormat("%b %d");
|
|
11
|
+
/** Default tick label formatting — SI suffixes for big magnitudes, plain
|
|
12
|
+
* significant digits otherwise, short month-day for dates. */
|
|
13
|
+
function defaultFormat(value) {
|
|
14
|
+
if (value instanceof Date) return day(value);
|
|
15
|
+
if (typeof value === "number") return Math.abs(value) >= 1e3 ? si(value) : plain(value);
|
|
16
|
+
return String(value);
|
|
17
|
+
}
|
|
18
|
+
/** Cap categorical label count so boxes don't collide — keep every nth. */
|
|
19
|
+
function thinDomain(domain, plotWidth) {
|
|
20
|
+
const maxLabels = Math.max(2, Math.floor(plotWidth / 56));
|
|
21
|
+
if (domain.length <= maxLabels) return domain;
|
|
22
|
+
const step = Math.ceil(domain.length / maxLabels);
|
|
23
|
+
return domain.filter((_, i) => i % step === 0);
|
|
24
|
+
}
|
|
25
|
+
function xAxisModel(kind, scale, spec, plot) {
|
|
26
|
+
const labels = spec?.labels !== false;
|
|
27
|
+
const stubs = spec?.ticks !== false;
|
|
28
|
+
const grid = spec?.grid === true;
|
|
29
|
+
const fmt = spec?.format ?? defaultFormat;
|
|
30
|
+
const frame = [{
|
|
31
|
+
kind: "line",
|
|
32
|
+
x1: plot.x,
|
|
33
|
+
y1: plot.y + plot.height,
|
|
34
|
+
x2: plot.x + plot.width,
|
|
35
|
+
y2: plot.y + plot.height,
|
|
36
|
+
stroke: AXIS_COLOR,
|
|
37
|
+
strokeWidth: 1
|
|
38
|
+
}];
|
|
39
|
+
const gridMarks = [];
|
|
40
|
+
const out = [];
|
|
41
|
+
let values;
|
|
42
|
+
let format = fmt;
|
|
43
|
+
if (kind === "band" || kind === "point") values = thinDomain(scale.domain(), plot.width);
|
|
44
|
+
else {
|
|
45
|
+
const continuous = scale;
|
|
46
|
+
const count = spec?.tickCount ?? 6;
|
|
47
|
+
values = continuous.ticks(count);
|
|
48
|
+
if (!spec?.format && kind === "time") {
|
|
49
|
+
const tf = continuous.tickFormat(count);
|
|
50
|
+
format = (v) => tf(v);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
for (const v of values) {
|
|
54
|
+
const px = plot.x + xCenter(kind, scale, v);
|
|
55
|
+
if (stubs) frame.push({
|
|
56
|
+
kind: "line",
|
|
57
|
+
x1: px,
|
|
58
|
+
y1: plot.y + plot.height,
|
|
59
|
+
x2: px,
|
|
60
|
+
y2: plot.y + plot.height + 4,
|
|
61
|
+
stroke: AXIS_COLOR,
|
|
62
|
+
strokeWidth: 1
|
|
63
|
+
});
|
|
64
|
+
if (grid) gridMarks.push({
|
|
65
|
+
kind: "line",
|
|
66
|
+
x1: px,
|
|
67
|
+
y1: plot.y,
|
|
68
|
+
x2: px,
|
|
69
|
+
y2: plot.y + plot.height,
|
|
70
|
+
stroke: GRID_COLOR,
|
|
71
|
+
strokeOpacity: GRID_OPACITY,
|
|
72
|
+
strokeWidth: 1
|
|
73
|
+
});
|
|
74
|
+
if (labels) {
|
|
75
|
+
const width = Math.min(72, plot.width / Math.max(1, values.length));
|
|
76
|
+
const boxX = Math.min(Math.max(0, px - width / 2), Math.max(0, plot.x + plot.width + 8 - width));
|
|
77
|
+
out.push({
|
|
78
|
+
x: boxX,
|
|
79
|
+
y: plot.y + plot.height + 6,
|
|
80
|
+
width,
|
|
81
|
+
align: "center",
|
|
82
|
+
text: format(v),
|
|
83
|
+
axis: "x"
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
labels: out,
|
|
89
|
+
frame,
|
|
90
|
+
grid: gridMarks
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function yAxisModel(scale, spec, plot) {
|
|
94
|
+
const labels = spec?.labels !== false;
|
|
95
|
+
const stubs = spec?.ticks !== false;
|
|
96
|
+
const grid = spec?.grid === true;
|
|
97
|
+
const fmt = spec?.format ?? defaultFormat;
|
|
98
|
+
const frame = [{
|
|
99
|
+
kind: "line",
|
|
100
|
+
x1: plot.x,
|
|
101
|
+
y1: plot.y,
|
|
102
|
+
x2: plot.x,
|
|
103
|
+
y2: plot.y + plot.height,
|
|
104
|
+
stroke: AXIS_COLOR,
|
|
105
|
+
strokeWidth: 1
|
|
106
|
+
}];
|
|
107
|
+
const gridMarks = [];
|
|
108
|
+
const out = [];
|
|
109
|
+
for (const v of scale.ticks(spec?.tickCount ?? 5)) {
|
|
110
|
+
const py = plot.y + scale(v);
|
|
111
|
+
if (stubs) frame.push({
|
|
112
|
+
kind: "line",
|
|
113
|
+
x1: plot.x - 4,
|
|
114
|
+
y1: py,
|
|
115
|
+
x2: plot.x,
|
|
116
|
+
y2: py,
|
|
117
|
+
stroke: AXIS_COLOR,
|
|
118
|
+
strokeWidth: 1
|
|
119
|
+
});
|
|
120
|
+
if (grid) gridMarks.push({
|
|
121
|
+
kind: "line",
|
|
122
|
+
x1: plot.x,
|
|
123
|
+
y1: py,
|
|
124
|
+
x2: plot.x + plot.width,
|
|
125
|
+
y2: py,
|
|
126
|
+
stroke: GRID_COLOR,
|
|
127
|
+
strokeOpacity: GRID_OPACITY,
|
|
128
|
+
strokeWidth: 1
|
|
129
|
+
});
|
|
130
|
+
if (labels) out.push({
|
|
131
|
+
x: 0,
|
|
132
|
+
y: py - 6,
|
|
133
|
+
width: Math.max(0, plot.x - 8),
|
|
134
|
+
align: "end",
|
|
135
|
+
text: fmt(v),
|
|
136
|
+
axis: "y"
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
labels: out,
|
|
141
|
+
frame,
|
|
142
|
+
grid: gridMarks
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
//#endregion
|
|
146
|
+
export { defaultFormat, xAxisModel, yAxisModel };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { defaultFormat } from "./axes.js";
|
|
2
|
+
/** Map a chart-space pointer position to the nearest datum. Cartesian charts
|
|
3
|
+
* snap to the nearest x group, then pick the closest point inside it —
|
|
4
|
+
* scatter gates on pointer distance instead of always snapping. Pie hits
|
|
5
|
+
* resolve by ring + angle. Coordinates outside the plot (+slop) miss. */
|
|
6
|
+
function hitTest(scene, x, y, slop = 24) {
|
|
7
|
+
if (scene.pie) return pieHit(scene, x, y, slop);
|
|
8
|
+
const { plot, points } = scene;
|
|
9
|
+
if (!points.length) return null;
|
|
10
|
+
if (x < plot.x - slop || x > plot.x + plot.width + slop || y < plot.y - slop || y > plot.y + plot.height + slop) return null;
|
|
11
|
+
const centers = /* @__PURE__ */ new Map();
|
|
12
|
+
for (const p of points) {
|
|
13
|
+
const cur = centers.get(p.key);
|
|
14
|
+
centers.set(p.key, cur === void 0 ? p.cx : (cur + p.cx) / 2);
|
|
15
|
+
}
|
|
16
|
+
let key = null;
|
|
17
|
+
let keyDist = Infinity;
|
|
18
|
+
for (const [k, cx] of centers) {
|
|
19
|
+
const d = Math.abs(cx - x);
|
|
20
|
+
if (d < keyDist) {
|
|
21
|
+
keyDist = d;
|
|
22
|
+
key = k;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (key === null) return null;
|
|
26
|
+
let best = null;
|
|
27
|
+
let bestDist = Infinity;
|
|
28
|
+
for (const p of points) {
|
|
29
|
+
if (p.key !== key) continue;
|
|
30
|
+
const d = Math.hypot(p.cx - x, p.cy - y);
|
|
31
|
+
if (d < bestDist) {
|
|
32
|
+
bestDist = d;
|
|
33
|
+
best = {
|
|
34
|
+
series: p.series,
|
|
35
|
+
index: p.index,
|
|
36
|
+
x: p.x,
|
|
37
|
+
y: p.y,
|
|
38
|
+
px: p.cx,
|
|
39
|
+
py: p.cy
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (best && scene.type === "scatter" && bestDist > slop) return null;
|
|
44
|
+
return best;
|
|
45
|
+
}
|
|
46
|
+
function pieHit(scene, x, y, slop) {
|
|
47
|
+
const pie = scene.pie;
|
|
48
|
+
const dx = x - pie.cx;
|
|
49
|
+
const dy = y - pie.cy;
|
|
50
|
+
const r = Math.hypot(dx, dy);
|
|
51
|
+
if (r < pie.rInner - slop || r > pie.rOuter + slop) return null;
|
|
52
|
+
const angle = (Math.atan2(dx, -dy) + Math.PI * 2) % (Math.PI * 2);
|
|
53
|
+
const slice = pie.slices.find((s) => angle >= s.startAngle && angle < s.endAngle);
|
|
54
|
+
if (!slice) return null;
|
|
55
|
+
return {
|
|
56
|
+
series: slice.series,
|
|
57
|
+
index: slice.index,
|
|
58
|
+
x: slice.label,
|
|
59
|
+
y: slice.value,
|
|
60
|
+
px: slice.cx,
|
|
61
|
+
py: slice.cy
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** Shared gesture → cursor state machine. Both leaves normalize their pointer
|
|
65
|
+
* source (DOM pointer/click events, NS `touch`+`tap`) to phases and call
|
|
66
|
+
* this; hit math stays identical across targets. `move` while pressed
|
|
67
|
+
* scrubs and fires `onScrub`; `hover` (web-only, unpressed move) moves the
|
|
68
|
+
* cursor without scrubbing; `up` ends the scrub and persists the cursor;
|
|
69
|
+
* `tap` is a completed press — fires `onPress` and updates the cursor;
|
|
70
|
+
* `cancel`/`leave` clear. `onPress` lives only on `tap` so NS's tap
|
|
71
|
+
* recognizer and the web click both reach it exactly once. */
|
|
72
|
+
function createScrub(getScene, getProps, setCursor) {
|
|
73
|
+
let downAt = null;
|
|
74
|
+
return (phase, x, y) => {
|
|
75
|
+
const scene = getScene();
|
|
76
|
+
const props = getProps();
|
|
77
|
+
const showCursor = props.tooltip === true || props.crosshair === true;
|
|
78
|
+
switch (phase) {
|
|
79
|
+
case "down": {
|
|
80
|
+
downAt = {
|
|
81
|
+
x,
|
|
82
|
+
y,
|
|
83
|
+
t: Date.now()
|
|
84
|
+
};
|
|
85
|
+
const hit = scene && showCursor ? hitTest(scene, x, y) : null;
|
|
86
|
+
setCursor(hit);
|
|
87
|
+
if (hit) props.onScrub?.(hit);
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case "move": {
|
|
91
|
+
if (!downAt) break;
|
|
92
|
+
const hit = scene && showCursor ? hitTest(scene, x, y) : null;
|
|
93
|
+
setCursor(hit);
|
|
94
|
+
props.onScrub?.(hit);
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case "hover":
|
|
98
|
+
if (downAt) break;
|
|
99
|
+
setCursor(scene && showCursor ? hitTest(scene, x, y) : null);
|
|
100
|
+
break;
|
|
101
|
+
case "up":
|
|
102
|
+
downAt = null;
|
|
103
|
+
setCursor(scene ? hitTest(scene, x, y) : null);
|
|
104
|
+
props.onScrub?.(null);
|
|
105
|
+
break;
|
|
106
|
+
case "tap": {
|
|
107
|
+
downAt = null;
|
|
108
|
+
const hit = scene ? hitTest(scene, x, y) : null;
|
|
109
|
+
setCursor(hit);
|
|
110
|
+
if (hit) props.onPress?.(hit);
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case "cancel":
|
|
114
|
+
case "leave":
|
|
115
|
+
downAt = null;
|
|
116
|
+
setCursor(null);
|
|
117
|
+
props.onScrub?.(null);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** Tooltip body for a hit — series name + formatted y for cartesian charts,
|
|
122
|
+
* slice label + value for pie. */
|
|
123
|
+
function hitLabel(scene, hit) {
|
|
124
|
+
if (scene.pie) {
|
|
125
|
+
const slice = scene.pie.slices.find((s) => s.series === hit.series && s.index === hit.index);
|
|
126
|
+
return slice ? `${slice.label}: ${defaultFormat(slice.value)}` : defaultFormat(hit.y);
|
|
127
|
+
}
|
|
128
|
+
return `${scene.series[hit.series]?.name ?? String(hit.series)}: ${defaultFormat(hit.y)}`;
|
|
129
|
+
}
|
|
130
|
+
//#endregion
|
|
131
|
+
export { createScrub, hitLabel, hitTest };
|