@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.
- package/README.md +132 -2
- package/index.d.ts +407 -0
- package/maps/index.d.ts +5 -0
- package/maps/turkey.js +3 -0
- package/maps/world.js +3 -0
- package/package.json +21 -4
- package/src/axes.js +281 -0
- package/src/compat.js +414 -0
- package/src/components.js +111 -0
- package/src/controllers.js +321 -0
- package/src/core.js +914 -0
- package/src/depth.js +316 -0
- package/src/features.js +783 -0
- package/src/fills.js +187 -0
- package/src/format.js +42 -0
- package/src/full.js +23 -0
- package/src/graphs.js +342 -0
- package/src/index.js +53 -0
- package/src/layouts.js +272 -0
- package/src/lite.js +40 -0
- package/src/maps.js +227 -0
- package/src/painter.js +162 -0
- package/src/plugins.js +381 -0
- package/src/scale.js +108 -0
- package/src/shape.js +119 -0
- package/src/themes.js +79 -0
- package/src/types.js +1193 -0
package/src/scale.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// @clarity-js/charts — scales: a value of the data → a place on the picture, and the ticks
|
|
2
|
+
// worth printing along an axis. Plain functions, no DOM: they run on the server too.
|
|
3
|
+
|
|
4
|
+
/** A round step that cuts `span` into about `count` parts: 1, 2 or 5 times a power of ten. */
|
|
5
|
+
export function niceStep(span, count = 5) {
|
|
6
|
+
const raw = Math.abs(span) / Math.max(1, count);
|
|
7
|
+
if (!Number.isFinite(raw) || raw === 0) return 1;
|
|
8
|
+
const p = 10 ** Math.floor(Math.log10(raw)), e = raw / p;
|
|
9
|
+
return (e >= 7.07 ? 10 : e >= 3.16 ? 5 : e >= 1.41 ? 2 : 1) * p;
|
|
10
|
+
}
|
|
11
|
+
const fix = (v, step) => { const d = Math.max(0, -Math.floor(Math.log10(step)) + 1); return Number(v.toFixed(Math.min(12, d))); };
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A straight scale: domain [d0, d1] → range [r0, r1].
|
|
15
|
+
* s(v) · s.invert(px) · s.ticks(count) · s.nice(count) → a scale whose ends are on round ticks
|
|
16
|
+
*/
|
|
17
|
+
export function linear(domain = [0, 1], range = [0, 1]) {
|
|
18
|
+
const [d0, d1] = domain, [r0, r1] = range, span = d1 - d0 || 1;
|
|
19
|
+
const s = (v) => r0 + ((v - d0) / span) * (r1 - r0);
|
|
20
|
+
s.invert = (p) => d0 + ((p - r0) / ((r1 - r0) || 1)) * span;
|
|
21
|
+
s.domain = [d0, d1]; s.range = [r0, r1]; s.kind = 'linear';
|
|
22
|
+
s.ticks = (count = 5) => {
|
|
23
|
+
const lo = Math.min(d0, d1), hi = Math.max(d0, d1), step = niceStep(hi - lo, count), out = [];
|
|
24
|
+
for (let v = Math.ceil(lo / step - 1e-9) * step; v <= hi + step * 1e-9; v += step) out.push(fix(v, step));
|
|
25
|
+
s.step = step;
|
|
26
|
+
return out;
|
|
27
|
+
};
|
|
28
|
+
s.nice = (count = 5) => {
|
|
29
|
+
const lo = Math.min(d0, d1), hi = Math.max(d0, d1);
|
|
30
|
+
if (lo === hi) return linear(lo === 0 ? [0, 1] : [Math.min(0, lo), Math.max(0, hi)], range);
|
|
31
|
+
const step = niceStep(hi - lo, count), a = Math.floor(lo / step) * step, b = Math.ceil(hi / step) * step;
|
|
32
|
+
return linear(d0 <= d1 ? [fix(a, step), fix(b, step)] : [fix(b, step), fix(a, step)], range);
|
|
33
|
+
};
|
|
34
|
+
return s;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A logarithmic scale (base 10): for values from 1 to a million on one axis. Values ≤ 0 are not on it. */
|
|
38
|
+
export function log(domain = [1, 10], range = [0, 1]) {
|
|
39
|
+
const l = (v) => Math.log10(Math.max(1e-12, v)), inner = linear([l(domain[0]), l(domain[1])], range);
|
|
40
|
+
const s = (v) => inner(l(v));
|
|
41
|
+
s.invert = (p) => 10 ** inner.invert(p);
|
|
42
|
+
s.domain = domain; s.range = range; s.kind = 'log';
|
|
43
|
+
s.ticks = () => { const out = []; for (let e = Math.floor(l(Math.min(...domain))); e <= Math.ceil(l(Math.max(...domain))); e++) out.push(10 ** e); return out.filter((v) => v >= Math.min(...domain) * 0.999 && v <= Math.max(...domain) * 1.001); };
|
|
44
|
+
s.nice = () => log([10 ** Math.floor(l(domain[0])), 10 ** Math.ceil(l(domain[1]))], range);
|
|
45
|
+
return s;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Bands: one slot per name (a bar chart's categories). s(name) → where its slot starts ·
|
|
50
|
+
* s.bandwidth · s.step · s.center(name)
|
|
51
|
+
*/
|
|
52
|
+
export function band(domain = [], range = [0, 1], { padding = 0.2, outer = padding / 2 } = {}) {
|
|
53
|
+
const n = domain.length, [r0, r1] = range, step = (r1 - r0) / Math.max(1, n - padding + 2 * outer), bw = step * (1 - padding);
|
|
54
|
+
const index = new Map(domain.map((d, i) => [d, i]));
|
|
55
|
+
const s = (v) => r0 + step * (outer + (index.get(v) ?? 0));
|
|
56
|
+
s.bandwidth = Math.max(0, bw); s.step = step; s.domain = domain; s.range = range; s.kind = 'band';
|
|
57
|
+
s.center = (v) => s(v) + bw / 2;
|
|
58
|
+
s.ticks = () => domain;
|
|
59
|
+
s.invert = (p) => domain[Math.max(0, Math.min(n - 1, Math.floor((p - r0) / step - outer + padding / 2)))];
|
|
60
|
+
return s;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ── time ────────────────────────────────────────────────────────────────────
|
|
64
|
+
const SECOND = 1000, MINUTE = 60 * SECOND, HOUR = 60 * MINUTE, DAY = 24 * HOUR;
|
|
65
|
+
const STEPS = [
|
|
66
|
+
[SECOND, 's', 1], [5 * SECOND, 's', 5], [15 * SECOND, 's', 15], [30 * SECOND, 's', 30],
|
|
67
|
+
[MINUTE, 'm', 1], [5 * MINUTE, 'm', 5], [15 * MINUTE, 'm', 15], [30 * MINUTE, 'm', 30],
|
|
68
|
+
[HOUR, 'h', 1], [3 * HOUR, 'h', 3], [6 * HOUR, 'h', 6], [12 * HOUR, 'h', 12],
|
|
69
|
+
[DAY, 'd', 1], [2 * DAY, 'd', 2], [7 * DAY, 'w', 1], [30 * DAY, 'mo', 1], [91 * DAY, 'mo', 3], [182 * DAY, 'mo', 6], [365 * DAY, 'y', 1],
|
|
70
|
+
];
|
|
71
|
+
/**
|
|
72
|
+
* Time (milliseconds, or Dates, or ISO strings) → a place. Ticks fall on whole seconds, minutes,
|
|
73
|
+
* hours, days, Mondays, months or years, in UTC. s.unit says which, for the labels.
|
|
74
|
+
*/
|
|
75
|
+
export function time(domain = [0, DAY], range = [0, 1]) {
|
|
76
|
+
const ms = (v) => (v instanceof Date ? v.getTime() : typeof v === 'string' ? Date.parse(v) : Number(v));
|
|
77
|
+
const d0 = ms(domain[0]), d1 = ms(domain[1]), inner = linear([d0, d1], range);
|
|
78
|
+
const s = (v) => inner(ms(v));
|
|
79
|
+
s.invert = (p) => inner.invert(p);
|
|
80
|
+
s.domain = [d0, d1]; s.range = range; s.kind = 'time';
|
|
81
|
+
s.ticks = (count = 6) => {
|
|
82
|
+
const span = Math.abs(d1 - d0) || DAY, want = span / Math.max(1, count);
|
|
83
|
+
let best = STEPS[0];
|
|
84
|
+
for (const st of STEPS) if (Math.abs(Math.log(st[0] / want)) < Math.abs(Math.log(best[0] / want))) best = st;
|
|
85
|
+
if (want > 365 * DAY) best = [365 * DAY * niceStep(want / (365 * DAY), 1), 'y', niceStep(want / (365 * DAY), 1)];
|
|
86
|
+
s.unit = best[1];
|
|
87
|
+
const [, unit, k] = best, lo = Math.min(d0, d1), hi = Math.max(d0, d1), out = [];
|
|
88
|
+
if (unit === 'mo' || unit === 'y') {
|
|
89
|
+
const a = new Date(lo); let y = a.getUTCFullYear(), m = unit === 'y' ? 0 : a.getUTCMonth();
|
|
90
|
+
if (unit === 'y') y = Math.floor(y / k) * k; else m = Math.floor(m / k) * k;
|
|
91
|
+
for (let i = 0; i < 400; i++) {
|
|
92
|
+
const t = Date.UTC(y, m, 1);
|
|
93
|
+
if (t > hi) break;
|
|
94
|
+
if (t >= lo) out.push(t);
|
|
95
|
+
if (unit === 'y') y += k; else { m += k; if (m >= 12) { y += Math.floor(m / 12); m %= 12; } }
|
|
96
|
+
}
|
|
97
|
+
} else if (unit === 'w') {
|
|
98
|
+
const a = new Date(lo), day = a.getUTCDay(), monday = Date.UTC(a.getUTCFullYear(), a.getUTCMonth(), a.getUTCDate() - ((day + 6) % 7));
|
|
99
|
+
for (let t = monday; t <= hi && out.length < 400; t += 7 * DAY) if (t >= lo) out.push(t);
|
|
100
|
+
} else {
|
|
101
|
+
const step = best[0];
|
|
102
|
+
for (let t = Math.ceil(lo / step) * step; t <= hi && out.length < 400; t += step) out.push(t);
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
};
|
|
106
|
+
s.nice = () => s;
|
|
107
|
+
return s;
|
|
108
|
+
}
|
package/src/shape.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// @clarity-js/charts — shapes: the paths of lines, areas and slices, stacked series, the bins of a
|
|
2
|
+
// histogram, and a line of a million points cut down to what the picture can show. No DOM.
|
|
3
|
+
|
|
4
|
+
const r = (v) => Math.round(v * 100) / 100;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Points [[x, y], …] → an SVG path. curve: 'linear' · 'monotone' (smooth, and never above or below
|
|
8
|
+
* the points it passes: no false peaks) · 'step' (level, then a jump at each point's middle).
|
|
9
|
+
* A point with a y that is not a number breaks the line there.
|
|
10
|
+
*/
|
|
11
|
+
export function linePath(points, curve = 'linear') {
|
|
12
|
+
let d = '';
|
|
13
|
+
for (const run of runs(points)) d += (d ? ' ' : '') + segment(run, curve);
|
|
14
|
+
return d;
|
|
15
|
+
}
|
|
16
|
+
function runs(points) {
|
|
17
|
+
const out = []; let cur = [];
|
|
18
|
+
for (const p of points) { if (Number.isFinite(p[0]) && Number.isFinite(p[1])) cur.push(p); else if (cur.length) { out.push(cur); cur = []; } }
|
|
19
|
+
if (cur.length) out.push(cur);
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
function segment(p, curve) {
|
|
23
|
+
if (p.length === 1) return `M${r(p[0][0])},${r(p[0][1])}h0`;
|
|
24
|
+
if (curve === 'step') {
|
|
25
|
+
let d = `M${r(p[0][0])},${r(p[0][1])}`;
|
|
26
|
+
for (let i = 1; i < p.length; i++) { const mx = (p[i - 1][0] + p[i][0]) / 2; d += `H${r(mx)}V${r(p[i][1])}`; }
|
|
27
|
+
return d + `H${r(p.at(-1)[0])}`;
|
|
28
|
+
}
|
|
29
|
+
if (curve !== 'monotone' || p.length < 3) return 'M' + p.map((q) => `${r(q[0])},${r(q[1])}`).join('L');
|
|
30
|
+
// Fritsch–Carlson: slopes that keep each piece between its two points
|
|
31
|
+
const n = p.length, dx = [], m = [], t = new Array(n);
|
|
32
|
+
for (let i = 0; i < n - 1; i++) { dx[i] = p[i + 1][0] - p[i][0]; m[i] = dx[i] ? (p[i + 1][1] - p[i][1]) / dx[i] : 0; }
|
|
33
|
+
t[0] = m[0]; t[n - 1] = m[n - 2];
|
|
34
|
+
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]);
|
|
35
|
+
let d = `M${r(p[0][0])},${r(p[0][1])}`;
|
|
36
|
+
for (let i = 0; i < n - 1; i++) {
|
|
37
|
+
const h = dx[i] / 3;
|
|
38
|
+
d += `C${r(p[i][0] + h)},${r(p[i][1] + h * t[i])} ${r(p[i + 1][0] - h)},${r(p[i + 1][1] - h * t[i + 1])} ${r(p[i + 1][0])},${r(p[i + 1][1])}`;
|
|
39
|
+
}
|
|
40
|
+
return d;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The area between a line and a base: points [[x, y], …] and the base's y (a number, or a y per point). */
|
|
44
|
+
export function areaPath(points, base, curve = 'linear') {
|
|
45
|
+
let d = '';
|
|
46
|
+
for (const run of runs(points.map((p, i) => [...p, i]))) {
|
|
47
|
+
const top = segment(run.map((q) => [q[0], q[1]]), curve);
|
|
48
|
+
const back = run.slice().reverse().map((q) => [q[0], Array.isArray(base) ? base[q[2]] : base]);
|
|
49
|
+
const bottom = curve === 'monotone' && Array.isArray(base) ? segment(back, curve).replace(/^M/, 'L') : 'L' + back.map((q) => `${r(q[0])},${r(q[1])}`).join('L');
|
|
50
|
+
d += (d ? ' ' : '') + top + bottom + 'Z';
|
|
51
|
+
}
|
|
52
|
+
return d;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A slice of a ring: centre, inner and outer radius, from angle a0 to a1 (radians, from twelve
|
|
57
|
+
* o'clock, clockwise). Inner radius 0: a slice of a pie.
|
|
58
|
+
*/
|
|
59
|
+
export function arcPath(cx, cy, r0, r1, a0, a1) {
|
|
60
|
+
const at = (rad, a) => [cx + rad * Math.sin(a), cy - rad * Math.cos(a)];
|
|
61
|
+
const full = a1 - a0 >= Math.PI * 2 - 1e-6;
|
|
62
|
+
// a whole circle: two half arcs (one arc from a point back to itself draws nothing)
|
|
63
|
+
if (full) {
|
|
64
|
+
let d = `M${r(cx)},${r(cy - r1)}A${r(r1)},${r(r1)} 0 1 1 ${r(cx)},${r(cy + r1)}A${r(r1)},${r(r1)} 0 1 1 ${r(cx)},${r(cy - r1)}Z`;
|
|
65
|
+
if (r0 > 0) d += `M${r(cx)},${r(cy - r0)}A${r(r0)},${r(r0)} 0 1 0 ${r(cx)},${r(cy + r0)}A${r(r0)},${r(r0)} 0 1 0 ${r(cx)},${r(cy - r0)}Z`;
|
|
66
|
+
return d;
|
|
67
|
+
}
|
|
68
|
+
const big = a1 - a0 > Math.PI ? 1 : 0, [x0, y0] = at(r1, a0), [x1, y1] = at(r1, a1);
|
|
69
|
+
let d = `M${r(x0)},${r(y0)}A${r(r1)},${r(r1)} 0 ${big} 1 ${r(x1)},${r(y1)}`;
|
|
70
|
+
if (r0 > 0) { const [x2, y2] = at(r0, a1), [x3, y3] = at(r0, a0); d += `L${r(x2)},${r(y2)}A${r(r0)},${r(r0)} 0 ${big} 0 ${r(x3)},${r(y3)}Z`; }
|
|
71
|
+
else d += `L${r(cx)},${r(cy)}Z`;
|
|
72
|
+
return d;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Series stacked one on another: rows and the keys to stack → for each key, [[y0, y1], …] per row. Negative values stack down. */
|
|
76
|
+
export function stack(rows, keys, value = (row, key) => Number(row[key]) || 0) {
|
|
77
|
+
const up = rows.map(() => 0), down = rows.map(() => 0);
|
|
78
|
+
return keys.map((k) => rows.map((row, i) => { const v = value(row, k); if (v >= 0) { const a = up[i]; up[i] += v; return [a, up[i]]; } const a = down[i]; down[i] += v; return [down[i], a]; }));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The smallest and largest finite number of a list ([0, 0] for none). */
|
|
82
|
+
export function extent(values) {
|
|
83
|
+
let lo = Infinity, hi = -Infinity;
|
|
84
|
+
for (const v of values) if (Number.isFinite(v)) { if (v < lo) lo = v; if (v > hi) hi = v; }
|
|
85
|
+
return lo <= hi ? [lo, hi] : [0, 0];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Numbers sorted into bins of equal width with round edges: → [{ x0, x1, count }, …]. */
|
|
89
|
+
export function bins(values, count = 10) {
|
|
90
|
+
const nums = values.filter(Number.isFinite);
|
|
91
|
+
if (!nums.length) return [];
|
|
92
|
+
let [lo, hi] = extent(nums);
|
|
93
|
+
if (lo === hi) { lo -= 0.5; hi += 0.5; }
|
|
94
|
+
const raw = (hi - lo) / Math.max(1, count), p = 10 ** Math.floor(Math.log10(raw)), e = raw / p;
|
|
95
|
+
const w = (e >= 7.07 ? 10 : e >= 3.16 ? 5 : e >= 1.41 ? 2 : 1) * p, start = Math.floor(lo / w) * w, n = Math.max(1, Math.ceil((hi - start) / w + 1e-9));
|
|
96
|
+
const out = Array.from({ length: n }, (_, i) => ({ x0: +(start + i * w).toPrecision(12), x1: +(start + (i + 1) * w).toPrecision(12), count: 0 }));
|
|
97
|
+
for (const v of nums) out[Math.min(n - 1, Math.floor((v - start) / w))].count++;
|
|
98
|
+
return out;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* A long line cut down to what a picture `width` pixels wide can show: for each pixel's column
|
|
103
|
+
* the first, lowest, highest and last point, so no peak is lost. Points must be in order of x.
|
|
104
|
+
* → [[x, y], …], at most about 4 × width points.
|
|
105
|
+
*/
|
|
106
|
+
export function decimate(points, width, x0 = points[0]?.[0], x1 = points.at(-1)?.[0]) {
|
|
107
|
+
if (points.length <= width * 4) return points;
|
|
108
|
+
const out = [], span = (x1 - x0) || 1;
|
|
109
|
+
let col = -1, first, lo, hi, last;
|
|
110
|
+
const flush = () => { if (!first) return; const seen = new Set(); for (const q of [first, lo, hi, last].sort((a, b) => a[0] - b[0])) if (!seen.has(q)) { seen.add(q); out.push(q); } };
|
|
111
|
+
for (const p of points) {
|
|
112
|
+
if (!Number.isFinite(p[1])) continue;
|
|
113
|
+
const c = Math.floor(((p[0] - x0) / span) * width);
|
|
114
|
+
if (c !== col) { flush(); col = c; first = lo = hi = last = p; continue; }
|
|
115
|
+
if (p[1] < lo[1]) lo = p; if (p[1] > hi[1]) hi = p; last = p;
|
|
116
|
+
}
|
|
117
|
+
flush();
|
|
118
|
+
return out;
|
|
119
|
+
}
|
package/src/themes.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// @clarity-js/charts — themes and palettes. A theme is the colours a chart takes when it names none:
|
|
2
|
+
// its words, its grid, its axes, the gap between slices, the tooltip, and the palette datasets take
|
|
3
|
+
// their colours from. options.theme: 'light' (the default), 'dark', 'auto' (follows the system), the
|
|
4
|
+
// name of one you registered, or an object ({ mode: 'dark', palette: 'vivid', background: '#10161d' }).
|
|
5
|
+
//
|
|
6
|
+
// Chart.registerTheme('ocean', { extends: 'dark', palette: ['#5ec8f2', '#7be0b5', '#f2c94c'], grid: 'rgba(94,200,242,0.15)' })
|
|
7
|
+
//
|
|
8
|
+
// Every colour is a CSS variable with the theme's value as its fallback, so a page's own
|
|
9
|
+
// --clarity-chart-text, --clarity-chart-grid, --clarity-chart-axis, --clarity-chart-ink and
|
|
10
|
+
// --clarity-chart-bg still win.
|
|
11
|
+
|
|
12
|
+
export const PALETTES = {
|
|
13
|
+
default: ['#3d7dd8', '#e0793a', '#2a9d8f', '#c94f7c', '#8a6fd1', '#c99a1e', '#5b8c3a', '#6f7d8c'],
|
|
14
|
+
vivid: ['#2f6fed', '#ff6b35', '#00a878', '#e2336b', '#7b4dff', '#f5b700', '#00a6d6', '#8f5a3c'],
|
|
15
|
+
pastel: ['#8fb8ed', '#f4b183', '#9fd8cb', '#eaa4c0', '#bfaee9', '#efd48a', '#b5d39a', '#b9c3cc'],
|
|
16
|
+
// Okabe and Ito's eight: told apart by people who see red and green alike
|
|
17
|
+
colorblind: ['#0072b2', '#e69f00', '#009e73', '#cc79a7', '#56b4e9', '#d55e00', '#f0e442', '#555555'],
|
|
18
|
+
mono: ['#1f4e8c', '#2f6db3', '#4a8bd1', '#6fa7e0', '#97c1ea', '#bcd8f2', '#174075', '#0e2b52'],
|
|
19
|
+
warm: ['#c0392b', '#e67e22', '#f1c40f', '#d35400', '#e84393', '#a0522d', '#ff7f50', '#b8860b'],
|
|
20
|
+
cool: ['#1abc9c', '#3498db', '#9b59b6', '#16a085', '#2980b9', '#8e44ad', '#48c9b0', '#5dade2'],
|
|
21
|
+
// bright enough to stand on a dark background
|
|
22
|
+
night: ['#6aa6ff', '#ff9f5a', '#4fd1c0', '#ff7aa8', '#b49cff', '#f2c94c', '#8fd16a', '#a9b4c2'],
|
|
23
|
+
};
|
|
24
|
+
// ApexCharts's palette names, mapped onto these
|
|
25
|
+
const APEX = { palette1: 'default', palette2: 'vivid', palette3: 'cool', palette4: 'warm', palette5: 'pastel', palette6: 'colorblind', palette7: 'night', palette8: 'mono', palette9: 'vivid', palette10: 'default' };
|
|
26
|
+
|
|
27
|
+
const LIGHT = {
|
|
28
|
+
mode: 'light', palette: null,
|
|
29
|
+
text: 'var(--clarity-chart-text, #5b6573)', ink: 'var(--clarity-chart-ink, #1b2430)',
|
|
30
|
+
grid: 'var(--clarity-chart-grid, rgba(0,0,0,0.1))', axis: 'var(--clarity-chart-axis, rgba(0,0,0,0.25))',
|
|
31
|
+
bg: 'var(--clarity-chart-bg, #fff)', background: null,
|
|
32
|
+
tooltip: { backgroundColor: 'rgba(20, 32, 43, 0.92)', color: '#fff' },
|
|
33
|
+
control: { background: 'rgba(255,255,255,0.92)', color: '#1b2430', border: 'rgba(0,0,0,0.15)', active: '#3d7dd8' },
|
|
34
|
+
mask: 'rgba(61,125,216,0.12)',
|
|
35
|
+
};
|
|
36
|
+
const DARK = {
|
|
37
|
+
mode: 'dark', palette: PALETTES.night,
|
|
38
|
+
text: 'var(--clarity-chart-text, #b7c0cc)', ink: 'var(--clarity-chart-ink, #eef2f6)',
|
|
39
|
+
grid: 'var(--clarity-chart-grid, rgba(255,255,255,0.12))', axis: 'var(--clarity-chart-axis, rgba(255,255,255,0.3))',
|
|
40
|
+
bg: 'var(--clarity-chart-bg, #161b22)', background: null,
|
|
41
|
+
tooltip: { backgroundColor: 'rgba(240, 244, 248, 0.96)', color: '#14202b' },
|
|
42
|
+
control: { background: 'rgba(30,37,46,0.92)', color: '#eef2f6', border: 'rgba(255,255,255,0.18)', active: '#6aa6ff' },
|
|
43
|
+
mask: 'rgba(106,166,255,0.16)',
|
|
44
|
+
};
|
|
45
|
+
export const THEMES = { light: LIGHT, dark: DARK };
|
|
46
|
+
|
|
47
|
+
const isObj = (v) => v && typeof v === 'object' && !Array.isArray(v);
|
|
48
|
+
const deep = (a, b) => { const o = { ...a }; for (const k in b) if (b[k] !== undefined) o[k] = isObj(b[k]) && isObj(a?.[k]) ? deep(a[k], b[k]) : b[k]; return o; };
|
|
49
|
+
|
|
50
|
+
/** Add a theme by name: { extends: 'light' | 'dark' | another theme, …the colours it changes }. */
|
|
51
|
+
export function registerTheme(name, theme) { THEMES[name] = resolveTheme({ ...theme, mode: theme.mode ?? THEMES[theme.extends]?.mode }); return THEMES[name]; }
|
|
52
|
+
export const paletteOf = (p) => (Array.isArray(p) ? p : typeof p === 'string' ? PALETTES[p] ?? PALETTES[APEX[p]] ?? null : null);
|
|
53
|
+
const systemDark = () => { try { return !!globalThis.matchMedia?.('(prefers-color-scheme: dark)').matches; } catch { return false; } };
|
|
54
|
+
|
|
55
|
+
/** A theme as given (a name, 'auto', an object) → its colours. */
|
|
56
|
+
export function resolveTheme(spec) {
|
|
57
|
+
if (spec == null || spec === false) return LIGHT;
|
|
58
|
+
if (typeof spec === 'string') return spec === 'auto' ? (systemDark() ? DARK : LIGHT) : THEMES[spec] ?? LIGHT;
|
|
59
|
+
const base = spec.extends ? resolveTheme(spec.extends) : spec.mode === 'auto' ? (systemDark() ? DARK : LIGHT) : spec.mode === 'dark' ? DARK : LIGHT;
|
|
60
|
+
const t = deep(base, { ...spec, extends: undefined });
|
|
61
|
+
t.mode = base.mode;
|
|
62
|
+
if (spec.palette !== undefined) t.palette = paletteOf(spec.palette) ?? base.palette;
|
|
63
|
+
// ApexCharts writes a monochrome palette as { monochrome: { enabled, color } }
|
|
64
|
+
if (spec.monochrome?.enabled) t.palette = monochrome(spec.monochrome.color ?? '#3d7dd8');
|
|
65
|
+
return t;
|
|
66
|
+
}
|
|
67
|
+
/** Eight shades of one colour, darkest first. */
|
|
68
|
+
export function monochrome(hex) {
|
|
69
|
+
const m = /^#?([0-9a-f]{6})$/i.exec(hex); if (!m) return PALETTES.mono;
|
|
70
|
+
const n = parseInt(m[1], 16), c = [n >> 16, (n >> 8) & 255, n & 255];
|
|
71
|
+
return Array.from({ length: 8 }, (_, i) => { const k = -0.35 + i * 0.12; return '#' + c.map((v) => Math.round(k < 0 ? v * (1 + k) : v + (255 - v) * k).toString(16).padStart(2, '0')).join(''); });
|
|
72
|
+
}
|
|
73
|
+
/** What a theme changes in a chart's options (the rest is read from chart.resolved.theme). */
|
|
74
|
+
export function themeOptions(t) {
|
|
75
|
+
const o = { color: t.text, elements: { arc: { borderColor: t.bg } }, plugins: { tooltip: { backgroundColor: t.tooltip.backgroundColor, titleColor: t.tooltip.color, bodyColor: t.tooltip.color, footerColor: t.tooltip.color } } };
|
|
76
|
+
if (t.palette) o.plugins.colors = { palette: t.palette };
|
|
77
|
+
if (t.font) o.font = t.font;
|
|
78
|
+
return o;
|
|
79
|
+
}
|