@octane-xplat/charts 0.13.0 → 0.15.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.
@@ -1,146 +0,0 @@
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 };
@@ -1,131 +0,0 @@
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 };
@@ -1,262 +0,0 @@
1
- import { categoryKey, xCenter } from "./scales.js";
2
- import { scaleBand } from "d3-scale";
3
- import { arc, area, curveMonotoneX, line, pie } from "d3-shape";
4
- //#region src/core/marks.ts
5
- function buildMarks(type, layout, series, options) {
6
- switch (type) {
7
- case "line": return lineMarks(layout, series);
8
- case "area": return areaMarks(layout, series);
9
- case "scatter": return scatterMarks(layout, series);
10
- case "bar": return options.stacked ? stackedBarMarks(layout, series) : groupedBarMarks(layout, series);
11
- case "pie": return pieMarks(layout, series, options);
12
- }
13
- }
14
- function lineMarks(layout, series) {
15
- const { xKind, x, y } = layout;
16
- const gen = line().defined((d) => Number.isFinite(d.y)).x((d) => xCenter(xKind, x, d.x)).y((d) => y(d.y)).curve(curveMonotoneX);
17
- const marks = [];
18
- const points = [];
19
- series.forEach((s, si) => {
20
- marks.push({
21
- kind: "path",
22
- d: gen(s.values) ?? "",
23
- fill: "none",
24
- stroke: s.color,
25
- strokeWidth: 2,
26
- strokeLinecap: "round",
27
- series: si
28
- });
29
- s.values.forEach((d, i) => {
30
- points.push({
31
- series: si,
32
- index: i,
33
- key: categoryKey(d.x),
34
- cx: xCenter(xKind, x, d.x),
35
- cy: y(d.y),
36
- x: d.x,
37
- y: d.y
38
- });
39
- });
40
- });
41
- return {
42
- marks,
43
- points
44
- };
45
- }
46
- function areaMarks(layout, series) {
47
- const { xKind, x, y } = layout;
48
- const zero = y(Math.max(y.domain()[0], Math.min(0, y.domain()[1])));
49
- const gen = area().defined((d) => Number.isFinite(d.y)).x((d) => xCenter(xKind, x, d.x)).y0(zero).y1((d) => y(d.y)).curve(curveMonotoneX);
50
- const stroke = line().defined((d) => Number.isFinite(d.y)).x((d) => xCenter(xKind, x, d.x)).y((d) => y(d.y)).curve(curveMonotoneX);
51
- const marks = [];
52
- const points = [];
53
- series.forEach((s, si) => {
54
- marks.push({
55
- kind: "path",
56
- d: gen(s.values) ?? "",
57
- fill: s.color,
58
- fillOpacity: .25,
59
- series: si
60
- });
61
- marks.push({
62
- kind: "path",
63
- d: stroke(s.values) ?? "",
64
- fill: "none",
65
- stroke: s.color,
66
- strokeWidth: 2,
67
- series: si
68
- });
69
- s.values.forEach((d, i) => {
70
- points.push({
71
- series: si,
72
- index: i,
73
- key: categoryKey(d.x),
74
- cx: xCenter(xKind, x, d.x),
75
- cy: y(d.y),
76
- x: d.x,
77
- y: d.y
78
- });
79
- });
80
- });
81
- return {
82
- marks,
83
- points
84
- };
85
- }
86
- function scatterMarks(layout, series) {
87
- const { xKind, x, y } = layout;
88
- const marks = [];
89
- const points = [];
90
- series.forEach((s, si) => {
91
- s.values.forEach((d, i) => {
92
- const cx = xCenter(xKind, x, d.x);
93
- const cy = y(d.y);
94
- marks.push({
95
- kind: "circle",
96
- cx,
97
- cy,
98
- r: 3.5,
99
- fill: d.color ?? s.color,
100
- fillOpacity: .85,
101
- series: si,
102
- index: i
103
- });
104
- points.push({
105
- series: si,
106
- index: i,
107
- key: categoryKey(d.x),
108
- cx,
109
- cy,
110
- x: d.x,
111
- y: d.y
112
- });
113
- });
114
- });
115
- return {
116
- marks,
117
- points
118
- };
119
- }
120
- function groupedBarMarks(layout, series) {
121
- const { x, y } = layout;
122
- const outer = x;
123
- const inner = scaleBand().domain(series.map((_, i) => String(i))).range([0, outer.bandwidth()]).padding(.08);
124
- const marks = [];
125
- const points = [];
126
- series.forEach((s, si) => {
127
- const offset = inner(String(si)) ?? 0;
128
- const width = inner.bandwidth();
129
- s.values.forEach((d, i) => {
130
- const band = outer(String(d.x));
131
- if (band === void 0 || !Number.isFinite(d.y)) return;
132
- const top = y(Math.max(0, d.y));
133
- const bottom = y(Math.min(0, d.y));
134
- marks.push({
135
- kind: "rect",
136
- x: band + offset,
137
- y: top,
138
- width,
139
- height: Math.max(0, bottom - top),
140
- fill: d.color ?? s.color,
141
- series: si,
142
- index: i
143
- });
144
- points.push({
145
- series: si,
146
- index: i,
147
- key: categoryKey(d.x),
148
- cx: band + offset + width / 2,
149
- cy: y(d.y),
150
- x: d.x,
151
- y: d.y
152
- });
153
- });
154
- });
155
- return {
156
- marks,
157
- points
158
- };
159
- }
160
- /** Stacked bars accumulate per category — positive values stack up from the
161
- * baseline, negatives down, so mixed-sign data stays correct. */
162
- function stackedBarMarks(layout, series) {
163
- const { x, y } = layout;
164
- const outer = x;
165
- const posBase = /* @__PURE__ */ new Map();
166
- const negBase = /* @__PURE__ */ new Map();
167
- const marks = [];
168
- const points = [];
169
- series.forEach((s, si) => {
170
- s.values.forEach((d, i) => {
171
- const cat = String(d.x);
172
- const band = outer(cat);
173
- if (band === void 0 || !Number.isFinite(d.y)) return;
174
- const v0 = d.y >= 0 ? posBase.get(cat) ?? 0 : negBase.get(cat) ?? 0;
175
- const v1 = v0 + d.y;
176
- if (d.y >= 0) posBase.set(cat, v1);
177
- else negBase.set(cat, v1);
178
- const top = y(Math.max(v0, v1));
179
- const bottom = y(Math.min(v0, v1));
180
- marks.push({
181
- kind: "rect",
182
- x: band,
183
- y: top,
184
- width: outer.bandwidth(),
185
- height: Math.max(0, bottom - top),
186
- fill: d.color ?? s.color,
187
- series: si,
188
- index: i
189
- });
190
- points.push({
191
- series: si,
192
- index: i,
193
- key: cat,
194
- cx: band + outer.bandwidth() / 2,
195
- cy: y(v1),
196
- x: d.x,
197
- y: d.y
198
- });
199
- });
200
- });
201
- return {
202
- marks,
203
- points
204
- };
205
- }
206
- /** Pie consumes every datum across series — each {x: label, y: value} is a
207
- * slice; slice colors are categorical (palette by order) with datum.color
208
- * overrides. A series color is ignored on pie — it would be monochrome. */
209
- function pieMarks(layout, series, options) {
210
- const { plot } = layout;
211
- const flat = [];
212
- series.forEach((s, si) => s.values.forEach((d, i) => flat.push({
213
- d,
214
- si,
215
- i
216
- })));
217
- const cx = plot.width / 2;
218
- const cy = plot.height / 2;
219
- const rOuter = Math.max(0, Math.min(plot.width, plot.height) / 2);
220
- const ratio = options.innerRadiusRatio ?? 0;
221
- const rInner = rOuter * Math.min(Math.max(ratio, 0), .95);
222
- const arcs = pie().value((v) => Math.max(0, v.d.y)).sort(null)(flat);
223
- const gen = arc().innerRadius(rInner).outerRadius(rOuter);
224
- const marks = [];
225
- const slices = [];
226
- arcs.forEach((a, order) => {
227
- const color = a.data.d.color ?? options.palette[order % options.palette.length];
228
- const centroid = gen.centroid(a);
229
- marks.push({
230
- kind: "path",
231
- d: gen(a) ?? "",
232
- transform: `translate(${cx} ${cy})`,
233
- fill: color,
234
- series: a.data.si,
235
- index: a.data.i
236
- });
237
- slices.push({
238
- series: a.data.si,
239
- index: a.data.i,
240
- label: String(a.data.d.x),
241
- value: a.data.d.y,
242
- color,
243
- startAngle: a.startAngle,
244
- endAngle: a.endAngle,
245
- cx: centroid[0],
246
- cy: centroid[1]
247
- });
248
- });
249
- return {
250
- marks,
251
- points: [],
252
- pie: {
253
- cx,
254
- cy,
255
- rOuter,
256
- rInner,
257
- slices
258
- }
259
- };
260
- }
261
- //#endregion
262
- export { buildMarks };
@@ -1,99 +0,0 @@
1
- import { extent } from "d3-array";
2
- import { scaleBand, scaleLinear, scalePoint, scaleTime } from "d3-scale";
3
- //#region src/core/scales.ts
4
- /** Categorical x values across all series, first-seen order — the band/point
5
- * domain. */
6
- function categoryDomain(data) {
7
- const seen = /* @__PURE__ */ new Set();
8
- const out = [];
9
- for (const series of data) for (const d of series.values) {
10
- const key = String(d.x);
11
- if (!seen.has(key)) {
12
- seen.add(key);
13
- out.push(key);
14
- }
15
- }
16
- return out;
17
- }
18
- /** A categorical x lookup — band/point domains key on String(x). */
19
- function categoryKey(x) {
20
- return String(x);
21
- }
22
- /** Pick the x scale family: bar is always band, other cartesian types infer
23
- * from the first datum — Date → time, string → point, number → linear. */
24
- function inferXKind(type, data) {
25
- if (type === "bar") return "band";
26
- const first = data[0]?.values[0]?.x;
27
- if (first instanceof Date) return "time";
28
- if (typeof first === "string") return "point";
29
- return "linear";
30
- }
31
- function buildXScale(kind, data, width) {
32
- switch (kind) {
33
- case "band": return scaleBand().domain(categoryDomain(data)).range([0, width]).paddingInner(.25).paddingOuter(.15);
34
- case "point": {
35
- const domain = categoryDomain(data);
36
- return scalePoint().domain(domain).range([0, width]).padding(domain.length <= 1 ? 0 : .5);
37
- }
38
- case "time": {
39
- const values = data.flatMap((s) => s.values.map((d) => d.x));
40
- const [lo, hi] = extent(values);
41
- if (lo === void 0 || hi === void 0) {
42
- const now = /* @__PURE__ */ new Date();
43
- return scaleTime().domain([now, now]).range([0, width]);
44
- }
45
- return scaleTime().domain(padDomain(+lo, +hi).map((v) => new Date(v))).range([0, width]).nice();
46
- }
47
- case "linear": {
48
- const values = data.flatMap((s) => s.values.map((d) => Number(d.x)));
49
- const [lo, hi] = extent(values);
50
- return scaleLinear().domain(padDomain(lo ?? 0, hi ?? 1)).range([0, width]).nice();
51
- }
52
- }
53
- }
54
- /** Degenerate domains (one value) get a unit pad so the scale still maps
55
- * somewhere sensible. */
56
- function padDomain(lo, hi) {
57
- return lo === hi ? [lo - .5, hi + .5] : [lo, hi];
58
- }
59
- /** Y domain: bar/area pin the zero baseline; line/scatter take the data
60
- * extent; stacked bars sum per category (positive stack only — negative
61
- * values in stacked mode stack below zero independently). */
62
- function yDomain(data, type, stacked) {
63
- if (stacked && type === "bar") {
64
- const pos = /* @__PURE__ */ new Map();
65
- const neg = /* @__PURE__ */ new Map();
66
- for (const series of data) for (const d of series.values) {
67
- const key = String(d.x);
68
- if (d.y >= 0) pos.set(key, (pos.get(key) ?? 0) + d.y);
69
- else neg.set(key, (neg.get(key) ?? 0) + d.y);
70
- }
71
- const hi = Math.max(0, ...pos.values());
72
- const lo = Math.min(0, ...neg.values());
73
- return lo === hi ? [0, 1] : [lo, hi];
74
- }
75
- const values = data.flatMap((s) => s.values.map((d) => d.y));
76
- const [lo, hi] = extent(values);
77
- const min = lo ?? 0;
78
- const max = hi ?? 1;
79
- if (type === "bar" || type === "area") {
80
- const domain = [Math.min(0, min), Math.max(0, max)];
81
- return domain[0] === domain[1] ? [0, 1] : domain;
82
- }
83
- return padDomain(min, max);
84
- }
85
- function buildYScale(domain, height) {
86
- return scaleLinear().domain(domain).range([height, 0]).nice();
87
- }
88
- /** Pixel center of an x value — band offsets by half the band, point and
89
- * continuous scales map directly. */
90
- function xCenter(kind, scale, value) {
91
- if (kind === "band") {
92
- const band = scale;
93
- return (band(String(value)) ?? NaN) + band.bandwidth() / 2;
94
- }
95
- if (kind === "point") return scale(String(value)) ?? NaN;
96
- return scale(value);
97
- }
98
- //#endregion
99
- export { buildXScale, buildYScale, categoryDomain, categoryKey, inferXKind, xCenter, yDomain };