@clarity-js/charts 0.0.0-stage → 1.1.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.
@@ -0,0 +1,321 @@
1
+ // @clarity-js/charts — controllers: for each type of dataset, how its data is read, how far its
2
+ // values reach on each axis, where its marks go (elements) and how they are drawn. The types of
3
+ // Chart.js: line, bar, bubble, scatter, pie, doughnut, polarArea, radar — mixed freely in one chart.
4
+
5
+ import { arcPath } from './shape.js';
6
+ import { toTime } from './axes.js';
7
+
8
+ const TAU = Math.PI * 2;
9
+ export const num = (v) => (v == null || v === '' ? NaN : Number(v));
10
+ const r2 = (v) => Math.round(v * 100) / 100;
11
+
12
+ // ── paths ─────────────────────────────────────────────────────────────────────
13
+ /** Chart.js's line through points: straight, tension (its spline), monotone, or stepped. */
14
+ export function pathThrough(pts, { tension = 0, stepped = false, cubicInterpolationMode = 'default', closed = false } = {}) {
15
+ const p = closed && pts.length > 2 ? [...pts] : pts;
16
+ if (!p.length) return '';
17
+ let d = `M${r2(p[0].x)},${r2(p[0].y)}`;
18
+ if (p.length === 1) return d + 'h0';
19
+ if (stepped) {
20
+ const mode = stepped === true ? 'before' : stepped;
21
+ for (let i = 1; i < p.length; i++) {
22
+ const a = p[i - 1], b = p[i];
23
+ if (mode === 'middle') { const mx = (a.x + b.x) / 2; d += `H${r2(mx)}V${r2(b.y)}H${r2(b.x)}`; }
24
+ else if (mode === 'after') d += `V${r2(b.y)}H${r2(b.x)}`;
25
+ else d += `H${r2(b.x)}V${r2(b.y)}`;
26
+ }
27
+ return d + (closed ? 'Z' : '');
28
+ }
29
+ if (cubicInterpolationMode === 'monotone' && p.length > 2 && !closed) {
30
+ const n = p.length, dx = [], m = [], t = [];
31
+ for (let i = 0; i < n - 1; i++) { dx[i] = p[i + 1].x - p[i].x; m[i] = dx[i] ? (p[i + 1].y - p[i].y) / dx[i] : 0; }
32
+ t[0] = m[0]; t[n - 1] = m[n - 2];
33
+ for (let i = 1; i < n - 1; i++) t[i] = m[i - 1] * m[i] <= 0 ? 0 : (3 * (dx[i - 1] + dx[i])) / ((2 * dx[i] + dx[i - 1]) / m[i - 1] + (dx[i] + 2 * dx[i - 1]) / m[i]);
34
+ for (let i = 0; i < n - 1; i++) { const h = dx[i] / 3; d += `C${r2(p[i].x + h)},${r2(p[i].y + h * t[i])} ${r2(p[i + 1].x - h)},${r2(p[i + 1].y - h * t[i + 1])} ${r2(p[i + 1].x)},${r2(p[i + 1].y)}`; }
35
+ return d;
36
+ }
37
+ if (tension > 0 && p.length > 2) {
38
+ // Chart.js's splineCurve: each point's two control points lie along its neighbours' line
39
+ const n = p.length, at = (i) => (closed ? p[(i + n) % n] : p[Math.max(0, Math.min(n - 1, i))]);
40
+ const ctrl = (i) => {
41
+ const a = at(i - 1), b = at(i), c = at(i + 1);
42
+ const d01 = Math.hypot(b.x - a.x, b.y - a.y), d12 = Math.hypot(c.x - b.x, c.y - b.y), s = d01 + d12 || 1;
43
+ const fa = (tension * d01) / s, fb = (tension * d12) / s;
44
+ return [{ x: b.x - fa * (c.x - a.x), y: b.y - fa * (c.y - a.y) }, { x: b.x + fb * (c.x - a.x), y: b.y + fb * (c.y - a.y) }];
45
+ };
46
+ const last = closed ? n : n - 1;
47
+ for (let i = 0; i < last; i++) { const c1 = ctrl(i)[1], c2 = ctrl(i + 1)[0], q = at(i + 1); d += `C${r2(c1.x)},${r2(c1.y)} ${r2(c2.x)},${r2(c2.y)} ${r2(q.x)},${r2(q.y)}`; }
48
+ return d + (closed ? 'Z' : '');
49
+ }
50
+ for (let i = 1; i < p.length; i++) d += `L${r2(p[i].x)},${r2(p[i].y)}`;
51
+ return d + (closed ? 'Z' : '');
52
+ }
53
+ /** The runs of a line between gaps (a point with no value); spanGaps joins them (a number: gaps no wider than it). */
54
+ export function runsOf(points, spanGaps, indexOf = (p) => p.x) {
55
+ const out = []; let cur = [], prev = null;
56
+ for (const pt of points) {
57
+ if (pt.skip) { if (!spanGaps) { if (cur.length) out.push(cur); cur = []; } continue; }
58
+ if (cur.length && typeof spanGaps === 'number' && prev && Math.abs(indexOf(pt) - indexOf(prev)) > spanGaps) { out.push(cur); cur = []; }
59
+ cur.push(pt); prev = pt;
60
+ }
61
+ if (cur.length) out.push(cur);
62
+ return out;
63
+ }
64
+
65
+ /** The shapes points can take (Chart.js's pointStyle). */
66
+ export function pointShape(style, x, y, r, rotation = 0) {
67
+ if (!style || r <= 0) return '';
68
+ const rot = (rotation * Math.PI) / 180, P = (a, d) => `${r2(x + Math.sin(a + rot) * d)},${r2(y - Math.cos(a + rot) * d)}`;
69
+ switch (style) {
70
+ case 'circle': return `M${r2(x - r)},${r2(y)}a${r2(r)},${r2(r)} 0 1,0 ${r2(r * 2)},0a${r2(r)},${r2(r)} 0 1,0 ${r2(-r * 2)},0Z`;
71
+ case 'triangle': return `M${P(0, r * 1.15)}L${P(TAU / 3, r * 1.15)}L${P((2 * TAU) / 3, r * 1.15)}Z`;
72
+ case 'rect': return `M${P(TAU / 8, r * 1.2)}L${P((3 * TAU) / 8, r * 1.2)}L${P((5 * TAU) / 8, r * 1.2)}L${P((7 * TAU) / 8, r * 1.2)}Z`;
73
+ case 'rectRounded': { const s = r * 0.85, k = r * 0.35; return `M${r2(x - s + k)},${r2(y - s)}H${r2(x + s - k)}Q${r2(x + s)},${r2(y - s)} ${r2(x + s)},${r2(y - s + k)}V${r2(y + s - k)}Q${r2(x + s)},${r2(y + s)} ${r2(x + s - k)},${r2(y + s)}H${r2(x - s + k)}Q${r2(x - s)},${r2(y + s)} ${r2(x - s)},${r2(y + s - k)}V${r2(y - s + k)}Q${r2(x - s)},${r2(y - s)} ${r2(x - s + k)},${r2(y - s)}Z`; }
74
+ case 'rectRot': return `M${P(0, r * 1.2)}L${P(TAU / 4, r * 1.2)}L${P(TAU / 2, r * 1.2)}L${P((3 * TAU) / 4, r * 1.2)}Z`;
75
+ case 'cross': return `M${P(0, r)}L${P(TAU / 2, r)}M${P(TAU / 4, r)}L${P((3 * TAU) / 4, r)}`;
76
+ case 'crossRot': return `M${P(TAU / 8, r)}L${P((5 * TAU) / 8, r)}M${P((3 * TAU) / 8, r)}L${P((7 * TAU) / 8, r)}`;
77
+ case 'star': return `M${P(0, r)}L${P(TAU / 2, r)}M${P(TAU / 4, r)}L${P((3 * TAU) / 4, r)}M${P(TAU / 8, r)}L${P((5 * TAU) / 8, r)}M${P((3 * TAU) / 8, r)}L${P((7 * TAU) / 8, r)}`;
78
+ case 'dash': return `M${P(0, 0)}L${P(TAU / 4, r)}`;
79
+ case 'line': return `M${P((3 * TAU) / 4, r)}L${P(TAU / 4, r)}`;
80
+ default: return pointShape('circle', x, y, r, rotation);
81
+ }
82
+ }
83
+ export const OPEN = new Set(['cross', 'crossRot', 'star', 'dash', 'line']);
84
+
85
+ /** A bar's outline: rounded at the corners asked for (Chart.js's borderRadius: a number or { topLeft, topRight, bottomLeft, bottomRight }). */
86
+ export function barPath(x, y, w, h, radius) {
87
+ if (w < 0) { x += w; w = -w; } if (h < 0) { y += h; h = -h; }
88
+ const lim = Math.min(w, h) / 2, c = typeof radius === 'number' ? { topLeft: radius, topRight: radius, bottomLeft: radius, bottomRight: radius } : { topLeft: 0, topRight: 0, bottomLeft: 0, bottomRight: 0, ...(radius ?? {}) };
89
+ const tl = Math.min(lim, c.topLeft), tr = Math.min(lim, c.topRight), br = Math.min(lim, c.bottomRight), bl = Math.min(lim, c.bottomLeft);
90
+ return `M${r2(x + tl)},${r2(y)}H${r2(x + w - tr)}${tr ? `Q${r2(x + w)},${r2(y)} ${r2(x + w)},${r2(y + tr)}` : ''}V${r2(y + h - br)}${br ? `Q${r2(x + w)},${r2(y + h)} ${r2(x + w - br)},${r2(y + h)}` : ''}H${r2(x + bl)}${bl ? `Q${r2(x)},${r2(y + h)} ${r2(x)},${r2(y + h - bl)}` : ''}V${r2(y + tl)}${tl ? `Q${r2(x)},${r2(y)} ${r2(x + tl)},${r2(y)}` : ''}Z`;
91
+ }
92
+
93
+ // ── reading data ───────────────────────────────────────────────────────────────
94
+ /** The data of a dataset read the way Chart.js reads it: numbers, { x, y }, [low, high], { x, y, r }, or objects through `parsing`. */
95
+ export function parse(chart, meta) {
96
+ const ds = meta.dataset, labels = chart.data.labels ?? [], parsing = ds.parsing ?? chart.resolved.parsing;
97
+ const keys = { x: 'x', y: 'y', r: 'r', ...(typeof parsing === 'object' && parsing ? { x: parsing.xAxisKey ?? 'x', y: parsing.yAxisKey ?? 'y', key: parsing.key } : {}) };
98
+ const iScale = chart.scales[meta.iAxisID], indexKey = meta.indexAxis === 'y' ? keys.y : keys.x, valueKey = meta.indexAxis === 'y' ? keys.x : keys.y;
99
+ const readIndex = (v, i) => {
100
+ if (!iScale) return i;
101
+ if (iScale.type === 'category') { if (v == null) return i; const k = typeof v === 'string' ? iScale.labels.indexOf(v) : Number(v); return k < 0 ? i : k; }
102
+ if (iScale.type.startsWith('time')) return toTime(v ?? labels[i]);
103
+ return num(v ?? labels[i]);
104
+ };
105
+ return (ds.data ?? []).map((raw, i) => {
106
+ if (raw == null) return { index: readIndex(labels[i], i), value: NaN, raw, i };
107
+ if (typeof raw === 'number' || typeof raw === 'string') return { index: readIndex(iScale?.type === 'category' ? i : labels[i], i), value: num(raw), raw, i };
108
+ if (Array.isArray(raw)) {
109
+ if (raw.length === 2 && meta.type === 'bar') return { index: readIndex(iScale?.type === 'category' ? i : labels[i], i), value: num(raw[1]), low: num(raw[0]), raw, i };
110
+ return { index: readIndex(raw[0], i), value: num(raw[1]), raw, i };
111
+ }
112
+ if (keys.key) return { index: i, value: num(String(keys.key).split('.').reduce((o, k) => o?.[k], raw)), raw, i };
113
+ const iv = raw[indexKey], vv = raw[valueKey];
114
+ return { index: readIndex(iv === undefined && iScale?.type === 'category' ? i : iv, i), value: num(vv), r: num(raw[keys.r]), raw, i };
115
+ });
116
+ }
117
+
118
+ // ── controllers ────────────────────────────────────────────────────────────────
119
+ const lineLike = {
120
+ element: 'point', stack: true,
121
+ limits(meta, axis) {
122
+ if (axis.id === meta.iAxisID) return extentOf(meta.parsed.filter((p) => Number.isFinite(p.value) || meta.spanAll).map((p) => p.index));
123
+ return extentOf(meta.parsed.flatMap((p, i) => (meta.stack ? (meta.stackMode === 'stream' ? meta.stack[i] ?? [] : [meta.stack[i]?.[1]]) : [p.value])));
124
+ },
125
+ update(chart, meta, mode) {
126
+ const ix = chart.scales[meta.iAxisID], vx = chart.scales[meta.vAxisID], horiz = meta.indexAxis === 'y';
127
+ const pts = meta.decimated ?? meta.parsed;
128
+ meta.elements = pts.map((p) => {
129
+ const i = p.i, v = meta.stack ? meta.stack[i]?.[1] : p.value, skip = !Number.isFinite(v) || !Number.isFinite(p.index);
130
+ const a = ix.getPixelForValue(p.index, i), b = skip ? NaN : vx.getPixelForValue(v);
131
+ return { kind: 'point', datasetIndex: meta.index, index: i, x: horiz ? b : a, y: horiz ? a : b, radius: chart.option(meta, 'pointRadius', i, 'radius'), skip, raw: p.raw, parsed: p, options: chart.pointOptions(meta, i) };
132
+ });
133
+ meta.base = vx.getBasePixel();
134
+ meta.horiz = horiz;
135
+ },
136
+ initial(el, meta, chart) { const vx = chart.scales[meta.vAxisID]; return meta.horiz ? { ...el, x: vx.getBasePixel() } : { ...el, y: vx.getBasePixel() }; },
137
+ draw(painter, meta, chart) {
138
+ const o = chart.lineOptions(meta), els = meta.elements;
139
+ if (o.showLine !== false) {
140
+ const runs = runsOf(els, o.spanGaps, (e) => e.parsed.index);
141
+ const segment = meta.dataset.segment;
142
+ for (const run of runs) {
143
+ if (segment && (segment.borderColor || segment.borderDash || segment.borderWidth)) {
144
+ for (let k = 1; k < run.length; k++) {
145
+ const ctx = { type: 'segment', p0: run[k - 1], p1: run[k], p0DataIndex: run[k - 1].index, p1DataIndex: run[k].index, datasetIndex: meta.index, chart };
146
+ const pick = (key, d) => (typeof segment[key] === 'function' ? segment[key](ctx) ?? d : d);
147
+ painter.path(pathThrough([run[k - 1], run[k]], o), { stroke: pick('borderColor', o.borderColor), width: pick('borderWidth', o.borderWidth), dash: pick('borderDash', o.borderDash), cap: o.borderCapStyle, join: o.borderJoinStyle, clip: chart.clip });
148
+ }
149
+ } else painter.path(pathThrough(run, o), { stroke: o.borderColor, width: o.borderWidth, dash: o.borderDash, dashOffset: o.borderDashOffset, cap: o.borderCapStyle, join: o.borderJoinStyle, clip: chart.clip });
150
+ }
151
+ }
152
+ drawPoints(painter, meta, chart);
153
+ },
154
+ };
155
+ export function drawPoints(painter, meta, chart) {
156
+ for (const e of meta.elements) {
157
+ if (e.skip) continue;
158
+ const active = chart.isActive(meta.index, e.index), po = e.options, r = active ? po.hoverRadius : e.radius;
159
+ if (!(r > 0) || po.pointStyle === false) continue;
160
+ const d = pointShape(po.pointStyle, e.x, e.y, r, po.rotation);
161
+ painter.path(d, OPEN.has(po.pointStyle) ? { stroke: active ? po.hoverBorderColor : po.borderColor, width: Math.max(1, po.borderWidth) } : { fill: active ? po.hoverBackgroundColor : po.backgroundColor, stroke: active ? po.hoverBorderColor : po.borderColor, width: active ? po.hoverBorderWidth : po.borderWidth });
162
+ }
163
+ }
164
+
165
+ /**
166
+ * Where a dataset's marks stand along a category (or time) axis, as Chart.js places bars: each
167
+ * dataset of the same type a slot of the category, categoryPercentage and barPercentage of it.
168
+ * → { width, slot, centerOf(parsed) }
169
+ */
170
+ export function bandOf(chart, meta, { cat: catD = 0.8, bar: barD = 0.9 } = {}) {
171
+ const ix = chart.scales[meta.iAxisID];
172
+ const groups = chart.barGroups(meta), cat = chart.option(meta, 'categoryPercentage', 0, null, catD), barP = chart.option(meta, 'barPercentage', 0, null, barD);
173
+ const slot = ix.type === 'category' || ix.type === 'timeseries' ? ix.bandSize : minGap(meta.parsed.map((p) => ix.getPixelForValue(p.index)));
174
+ const thickness = chart.option(meta, 'barThickness', 0), maxT = chart.option(meta, 'maxBarThickness', 0, null, Infinity);
175
+ const groupW = (slot * cat) / groups.count, width = Math.min(maxT, typeof thickness === 'number' ? thickness : groupW * barP);
176
+ return { width, slot, groupW, centerOf: (p) => ix.getPixelForValue(p.index, p.i) - (slot * cat) / 2 + groupW * (groups.at + 0.5) };
177
+ }
178
+
179
+ const bar = {
180
+ element: 'bar', stack: true,
181
+ limits(meta, axis) {
182
+ if (axis.id === meta.iAxisID) return extentOf(meta.parsed.map((p) => p.index));
183
+ return extentOf(meta.parsed.flatMap((p, i) => (meta.stack ? meta.stack[i] ?? [] : Number.isFinite(p.low) ? [p.low, p.value] : [p.value])));
184
+ },
185
+ update(chart, meta) {
186
+ const vx = chart.scales[meta.vAxisID], horiz = meta.indexAxis === 'y';
187
+ const { width, centerOf } = bandOf(chart, meta), minLen = chart.option(meta, 'minBarLength', 0, null, 0);
188
+ meta.elements = meta.parsed.map((p) => {
189
+ const i = p.i, center = centerOf(p);
190
+ const lo = meta.stack ? meta.stack[i]?.[0] : Number.isFinite(p.low) ? p.low : chart.option(meta, 'base', i) ?? vx.getBaseValue();
191
+ const hi = meta.stack ? meta.stack[i]?.[1] : p.value, skip = !Number.isFinite(hi);
192
+ let base = vx.getPixelForValue(lo), end = skip ? base : vx.getPixelForValue(hi);
193
+ if (minLen && Math.abs(end - base) < minLen) end = base + Math.sign(end - base || -1) * minLen;
194
+ return { kind: 'bar', datasetIndex: meta.index, index: i, x: horiz ? end : center, y: horiz ? center : end, base, width, horizontal: horiz, skip, raw: p.raw, parsed: p, options: chart.barOptions(meta, i) };
195
+ });
196
+ },
197
+ initial(el) { return el.horizontal ? { ...el, x: el.base } : { ...el, y: el.base }; },
198
+ draw(painter, meta, chart) {
199
+ for (const e of meta.elements) {
200
+ if (e.skip) continue;
201
+ const o = e.options, active = chart.isActive(meta.index, e.index);
202
+ const [x, y, w, h] = e.horizontal ? [Math.min(e.base, e.x), e.y - e.width / 2, Math.abs(e.x - e.base), e.width] : [e.x - e.width / 2, Math.min(e.base, e.y), e.width, Math.abs(e.base - e.y)];
203
+ const up = e.horizontal ? e.x >= e.base : e.y <= e.base, rad = o.borderRadius;
204
+ // the corners at the base stay square unless asked (Chart.js rounds the far end)
205
+ const radius = typeof rad === 'number' ? (e.horizontal ? (up ? { topRight: rad, bottomRight: rad } : { topLeft: rad, bottomLeft: rad }) : up ? { topLeft: rad, topRight: rad } : { bottomLeft: rad, bottomRight: rad }) : rad;
206
+ const d = barPath(x, y, w, h, radius);
207
+ painter.path(d, { fill: active ? o.hoverBackgroundColor : o.backgroundColor, clip: chart.clip });
208
+ const bw = active ? o.hoverBorderWidth : o.borderWidth;
209
+ if (bw > 0) painter.path(d, { stroke: active ? o.hoverBorderColor : o.borderColor, width: bw, clip: chart.clip });
210
+ }
211
+ },
212
+ };
213
+
214
+ const bubble = {
215
+ element: 'point',
216
+ limits: lineLike.limits,
217
+ update(chart, meta) {
218
+ const xs = chart.scales[meta.iAxisID], ys = chart.scales[meta.vAxisID];
219
+ meta.elements = meta.parsed.map((p) => ({ kind: 'point', datasetIndex: meta.index, index: p.i, x: xs.getPixelForValue(p.index, p.i), y: ys.getPixelForValue(p.value), radius: Number.isFinite(p.r) ? p.r : chart.option(meta, 'radius', p.i, 'radius'), skip: !Number.isFinite(p.value), raw: p.raw, parsed: p, options: chart.pointOptions(meta, p.i) }));
220
+ },
221
+ initial(el) { return { ...el, radius: 0 }; },
222
+ draw(painter, meta, chart) { drawPoints(painter, meta, chart); },
223
+ };
224
+
225
+ const arcs = {
226
+ element: 'arc',
227
+ limits(meta, axis) { return axis.axis === 'r' ? extentOf(meta.parsed.filter((_, i) => meta.chart.getDataVisibility(i)).map((p) => p.value).concat(0)) : [Infinity, -Infinity]; },
228
+ update(chart, meta) {
229
+ const area = chart.chartArea, polar = meta.type === 'polarArea', ring = chart.ringOf(meta);
230
+ const rotation = ((chart.option(meta, 'rotation', 0, null, 0)) * Math.PI) / 180, sweep = ((chart.option(meta, 'circumference', 0, null, 360)) * Math.PI) / 180;
231
+ const vis = meta.parsed.map((_, i) => chart.getDataVisibility(i));
232
+ let cx = (area.left + area.right) / 2, cy = (area.top + area.bottom) / 2, outerMax = Math.max(0, Math.min(area.right - area.left, area.bottom - area.top) / 2);
233
+ if (polar) { const r = chart.scales.r; cx = r.cx; cy = r.cy; outerMax = r.radius; }
234
+ // a half ring (circumference 180) is drawn larger: it is as wide as it is tall twice over
235
+ if (!polar && sweep <= Math.PI + 1e-6) { const k = Math.min(2, (area.right - area.left) / Math.max(1, area.bottom - area.top)); outerMax = Math.min((area.right - area.left) / 2, (area.bottom - area.top) * Math.min(1, k / 1)); cy = area.bottom - (area.bottom - area.top - outerMax) / 2; }
236
+ const radiusOpt = chart.option(meta, 'radius', 0, null, '100%'), outer = pct(radiusOpt, outerMax);
237
+ const cutOpt = chart.option(meta, 'cutout', 0, null, meta.type === 'doughnut' ? '50%' : 0), cut = pct(cutOpt, outer);
238
+ const band = (outer - cut) / ring.total, rIn = cut + band * ring.before, rOut = rIn + band * ring.weight;
239
+ const total = meta.parsed.reduce((a, p, i) => a + (vis[i] && p.value > 0 ? p.value : 0), 0) || 1;
240
+ const nVis = vis.filter(Boolean).length || 1;
241
+ let a = rotation;
242
+ meta.elements = meta.parsed.map((p, i) => {
243
+ const value = vis[i] && Number.isFinite(p.value) ? Math.max(0, p.value) : 0;
244
+ const span = polar ? (vis[i] ? sweep / nVis : 0) : (value / total) * sweep, a0 = a;
245
+ a += span;
246
+ const outerR = polar ? chart.scales.r.getDistanceFromCenterForValue(value) : rOut;
247
+ return { kind: 'arc', datasetIndex: meta.index, index: i, cx, cy, startAngle: a0, endAngle: a0 + span, innerRadius: polar ? 0 : rIn, outerRadius: outerR, skip: !vis[i], raw: p.raw, parsed: p, value, share: value / total, options: chart.arcOptions(meta, i) };
248
+ });
249
+ },
250
+ initial(el, meta, chart) { const rot = ((chart.option(meta, 'rotation', 0, null, 0)) * Math.PI) / 180; return meta.type === 'polarArea' ? { ...el, outerRadius: 0 } : chart.resolved.animation?.animateRotate === false ? { ...el, outerRadius: chart.resolved.animation?.animateScale ? 0 : el.outerRadius } : { ...el, startAngle: rot, endAngle: rot }; },
251
+ draw(painter, meta, chart) {
252
+ for (const e of meta.elements) {
253
+ if (e.endAngle - e.startAngle <= 1e-6) continue;
254
+ const o = e.options, active = chart.isActive(meta.index, e.index), off = (active ? o.hoverOffset : 0) + (o.offset || 0), mid = (e.startAngle + e.endAngle) / 2;
255
+ const cx = e.cx + Math.sin(mid) * off, cy = e.cy - Math.cos(mid) * off, gap = (o.spacing || 0) / Math.max(1, e.outerRadius);
256
+ const d = arcPath(cx, cy, e.innerRadius, e.outerRadius, e.startAngle + gap / 2, e.endAngle - gap / 2);
257
+ painter.path(d, { fill: active ? o.hoverBackgroundColor : o.backgroundColor, stroke: (active ? o.hoverBorderWidth : o.borderWidth) > 0 ? (active ? o.hoverBorderColor : o.borderColor) : null, width: active ? o.hoverBorderWidth : o.borderWidth, join: o.borderJoinStyle ?? 'round' });
258
+ }
259
+ },
260
+ };
261
+
262
+ const radar = {
263
+ element: 'point',
264
+ limits(meta, axis) { return extentOf(meta.parsed.map((p) => p.value)); },
265
+ update(chart, meta) {
266
+ const r = chart.scales.r;
267
+ meta.elements = meta.parsed.map((p, i) => { const pos = Number.isFinite(p.value) ? r.getPointPositionForValue(i, p.value) : { x: NaN, y: NaN }; return { kind: 'point', datasetIndex: meta.index, index: i, x: pos.x, y: pos.y, radius: chart.option(meta, 'pointRadius', i, 'radius'), skip: !Number.isFinite(p.value), raw: p.raw, parsed: p, options: chart.pointOptions(meta, i) }; });
268
+ },
269
+ initial(el, meta, chart) { return { ...el, x: chart.scales.r.cx, y: chart.scales.r.cy }; },
270
+ draw(painter, meta, chart) {
271
+ const o = chart.lineOptions(meta), pts = meta.elements.filter((e) => !e.skip);
272
+ if (o.showLine !== false && pts.length) painter.path(pathThrough(pts, { ...o, closed: true }), { stroke: o.borderColor, width: o.borderWidth, dash: o.borderDash, join: o.borderJoinStyle ?? 'round' });
273
+ drawPoints(painter, meta, chart);
274
+ },
275
+ };
276
+
277
+ export { lineLike, bar, bubble, arcs, radar };
278
+ /** The controllers by type (register your own with Chart.register({ id: 'mine', controller })). */
279
+ export const CONTROLLERS = { line: lineLike, scatter: lineLike, bar, bubble, pie: arcs, doughnut: arcs, polarArea: arcs, radar };
280
+
281
+ // ── what elements answer when the pointer is near ─────────────────────────────────
282
+ export function inRange(e, x, y, hit = 0) {
283
+ if (e.skip) return false;
284
+ if (typeof e.hit === 'function') return e.hit(x, y, hit);
285
+ if (e.bounds) { const b = e.bounds; return x >= b.l - hit && x <= b.r + hit && y >= b.t - hit && y <= b.b + hit; }
286
+ if (e.kind === 'point') return Math.hypot(e.x - x, e.y - y) <= Math.max(e.radius ?? 3, 1) + (e.options?.hitRadius ?? 1) + hit;
287
+ if (e.kind === 'bar') { const b = barBox(e); return x >= b.l - hit && x <= b.r + hit && y >= b.t - hit && y <= b.b + hit; }
288
+ if (e.kind === 'arc') {
289
+ const dx = x - e.cx, dy = y - e.cy, d = Math.hypot(dx, dy);
290
+ if (d < e.innerRadius || d > e.outerRadius + hit) return false;
291
+ let a = Math.atan2(dx, -dy); while (a < e.startAngle) a += TAU; while (a >= e.startAngle + TAU) a -= TAU;
292
+ return a <= e.endAngle;
293
+ }
294
+ return false;
295
+ }
296
+ export function inAxisRange(e, axis, v) {
297
+ if (e.skip) return false;
298
+ if (e.bounds) { const b = e.bounds; return axis === 'x' ? v >= b.l && v <= b.r : v >= b.t && v <= b.b; }
299
+ if (e.kind === 'bar') { const b = barBox(e); return axis === 'x' ? v >= b.l && v <= b.r : v >= b.t && v <= b.b; }
300
+ const c = centerOf(e), r = (e.radius ?? 3) + (e.options?.hitRadius ?? 1);
301
+ return Math.abs((axis === 'x' ? c.x : c.y) - v) <= r;
302
+ }
303
+ export const barBox = (e) => (e.horizontal ? { l: Math.min(e.base, e.x), r: Math.max(e.base, e.x), t: e.y - e.width / 2, b: e.y + e.width / 2 } : { l: e.x - e.width / 2, r: e.x + e.width / 2, t: Math.min(e.base, e.y), b: Math.max(e.base, e.y) });
304
+ export function centerOf(e) {
305
+ if (e.center) return e.center;
306
+ if (e.bounds && e.kind !== 'bar') return { x: (e.bounds.l + e.bounds.r) / 2, y: (e.bounds.t + e.bounds.b) / 2 };
307
+ if (e.kind === 'arc') { const mid = (e.startAngle + e.endAngle) / 2, rr = (e.innerRadius + e.outerRadius) / 2; return { x: e.cx + Math.sin(mid) * rr, y: e.cy - Math.cos(mid) * rr }; }
308
+ if (e.kind === 'bar') return e.horizontal ? { x: (e.x + e.base) / 2, y: e.y } : { x: e.x, y: (e.y + e.base) / 2 };
309
+ return { x: e.x, y: e.y };
310
+ }
311
+ /** Where a tooltip points: the top of a bar, a point itself, the middle of a slice. */
312
+ export function tipPoint(e) { if (e.tip) return e.tip; if (e.bounds && e.kind !== 'bar') return { x: (e.bounds.l + e.bounds.r) / 2, y: e.bounds.t }; if (e.kind === 'bar') return e.horizontal ? { x: e.x, y: e.y } : { x: e.x, y: Math.min(e.y, e.base) }; return centerOf(e); }
313
+
314
+ // ── helpers ───────────────────────────────────────────────────────────────────
315
+ export function extentOf(values) {
316
+ let lo = Infinity, hi = -Infinity;
317
+ for (const v of values) if (Number.isFinite(v)) { if (v < lo) lo = v; if (v > hi) hi = v; }
318
+ return [lo, hi];
319
+ }
320
+ export const pct = (v, of) => (typeof v === 'string' && v.trim().endsWith('%') ? (parseFloat(v) / 100) * of : Number(v) || 0);
321
+ export function minGap(px) { const s = px.filter(Number.isFinite).sort((a, b) => a - b); let g = Infinity; for (let i = 1; i < s.length; i++) if (s[i] - s[i - 1] > 0) g = Math.min(g, s[i] - s[i - 1]); return Number.isFinite(g) ? g : 40; }