@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/axes.js
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// @clarity-js/charts — axes as Chart.js has them: 'category', 'linear', 'logarithmic', 'time',
|
|
2
|
+
// 'timeseries' and 'radialLinear', with its options (min, max, suggestedMin/Max, beginAtZero, grace,
|
|
3
|
+
// reverse, stacked, offset, position, grid, border, ticks, title). An axis finds its range from the
|
|
4
|
+
// data, chooses its ticks, says how much room it needs, and turns values into pixels.
|
|
5
|
+
|
|
6
|
+
import { niceStep } from './scale.js';
|
|
7
|
+
import { formatNumber } from './format.js';
|
|
8
|
+
|
|
9
|
+
const DAY = 864e5;
|
|
10
|
+
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
11
|
+
export const toTime = (v) => (v instanceof Date ? v.getTime() : typeof v === 'number' ? v : typeof v === 'string' ? Date.parse(v) : NaN);
|
|
12
|
+
const pad = (n, k = 2) => String(n).padStart(k, '0');
|
|
13
|
+
|
|
14
|
+
/** A time written by a pattern: yyyy/YYYY, MMM, MM, M, dd/DD, d/D, HH, H, hh, h, mm, ss, a — in UTC. */
|
|
15
|
+
export function formatTime(ms, pattern = 'MMM d, yyyy') {
|
|
16
|
+
const t = new Date(ms);
|
|
17
|
+
if (!Number.isFinite(t.getTime())) return '';
|
|
18
|
+
const H = t.getUTCHours(), map = {
|
|
19
|
+
yyyy: t.getUTCFullYear(), YYYY: t.getUTCFullYear(), yy: pad(t.getUTCFullYear() % 100), MMMM: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][t.getUTCMonth()],
|
|
20
|
+
MMM: MONTHS[t.getUTCMonth()], MM: pad(t.getUTCMonth() + 1), M: t.getUTCMonth() + 1, dd: pad(t.getUTCDate()), DD: pad(t.getUTCDate()), d: t.getUTCDate(), D: t.getUTCDate(),
|
|
21
|
+
HH: pad(H), H, hh: pad(H % 12 || 12), h: H % 12 || 12, mm: pad(t.getUTCMinutes()), ss: pad(t.getUTCSeconds()), a: H < 12 ? 'am' : 'pm', SSS: pad(t.getUTCMilliseconds(), 3),
|
|
22
|
+
};
|
|
23
|
+
return pattern.replace(/yyyy|YYYY|yy|MMMM|MMM|MM|M|dd|DD|d|D|HH|H|hh|h|mm|ss|SSS|a|\[[^\]]*\]/g, (k) => (k[0] === '[' ? k.slice(1, -1) : String(map[k])));
|
|
24
|
+
}
|
|
25
|
+
export const TIME_FORMATS = { millisecond: 'HH:mm:ss.SSS', second: 'HH:mm:ss', minute: 'HH:mm', hour: 'HH:mm', day: 'MMM d', week: 'MMM d', month: 'MMM yyyy', quarter: 'MMM yyyy', year: 'yyyy' };
|
|
26
|
+
const UNITS = [
|
|
27
|
+
['millisecond', 1, [1, 2, 5, 10, 20, 50, 100, 200, 500]], ['second', 1e3, [1, 2, 5, 10, 15, 30]], ['minute', 6e4, [1, 2, 5, 10, 15, 30]], ['hour', 36e5, [1, 2, 3, 6, 12]],
|
|
28
|
+
['day', DAY, [1, 2, 5]], ['week', 7 * DAY, [1, 2, 4]], ['month', 30.44 * DAY, [1, 2, 3]], ['quarter', 91.3 * DAY, [1, 2]], ['year', 365.25 * DAY, [1, 2, 5, 10, 20, 50, 100]],
|
|
29
|
+
];
|
|
30
|
+
function timeTicks(min, max, count, unit = null, stepOpt = null, weeks = false) {
|
|
31
|
+
const span = max - min || DAY, want = span / Math.max(1, count);
|
|
32
|
+
let best = null;
|
|
33
|
+
for (const [u, ms, steps] of UNITS) {
|
|
34
|
+
if (unit ? u !== unit : (u === 'week' && !weeks) || u === 'quarter') continue; // as Chart.js: weeks and quarters only when asked for
|
|
35
|
+
for (const k of stepOpt ? [stepOpt] : steps) { const size = ms * k; if (!best || (size >= want * 0.75 && (best.size < want * 0.75 || size < best.size))) best = { u, k, size }; }
|
|
36
|
+
}
|
|
37
|
+
const { u, k } = best, out = [];
|
|
38
|
+
if (u === 'month' || u === 'quarter' || u === 'year') {
|
|
39
|
+
const step = u === 'year' ? 12 * k : u === 'quarter' ? 3 * k : k, a = new Date(min);
|
|
40
|
+
let y = a.getUTCFullYear(), mo = u === 'year' ? 0 : a.getUTCMonth() - (a.getUTCMonth() % step);
|
|
41
|
+
if (u === 'year') y -= y % k;
|
|
42
|
+
for (let i = 0; i < 2000; i++) { const t = Date.UTC(y, mo, 1); if (t > max) break; if (t >= min) out.push(t); mo += step; y += Math.floor(mo / 12); mo %= 12; }
|
|
43
|
+
} else if (u === 'week') {
|
|
44
|
+
const a = new Date(min), monday = Date.UTC(a.getUTCFullYear(), a.getUTCMonth(), a.getUTCDate() - ((a.getUTCDay() + 6) % 7));
|
|
45
|
+
for (let t = monday; t <= max && out.length < 2000; t += 7 * DAY * k) if (t >= min) out.push(t);
|
|
46
|
+
} else { const size = best.size; for (let t = Math.ceil(min / size) * size; t <= max && out.length < 2000; t += size) out.push(t); }
|
|
47
|
+
return { ticks: out, unit: u };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const resolve = (v, ctx) => (typeof v === 'function' ? v(ctx) : v);
|
|
51
|
+
const themeOf = (chart) => chart.resolved?.theme ?? {};
|
|
52
|
+
const lineOf = (f) => (f?.size ?? 12) * (f?.lineHeight ?? 1.2);
|
|
53
|
+
const textW = (painter, s, f) => Math.max(...String(s).split('\n').map((l) => painter.measure(l, f)), 0);
|
|
54
|
+
|
|
55
|
+
export class Axis {
|
|
56
|
+
constructor(id, options, chart) {
|
|
57
|
+
this.id = id; this.options = options; this.chart = chart; this.type = options.type;
|
|
58
|
+
this.axis = options.axis ?? (id[0] === 'r' ? 'r' : id[0] === 'y' ? 'y' : 'x');
|
|
59
|
+
this.position = options.position ?? (this.axis === 'y' ? 'left' : 'bottom');
|
|
60
|
+
this.ticks = []; this.min = 0; this.max = 1; this.left = this.top = this.right = this.bottom = this.width = this.height = 0;
|
|
61
|
+
}
|
|
62
|
+
isHorizontal() { return this.position === 'top' || this.position === 'bottom'; }
|
|
63
|
+
get labels() { return this.options.labels ?? this.chart.data.labels ?? []; }
|
|
64
|
+
/** The values on this axis, from every shown dataset that uses it (stacked: the sums). */
|
|
65
|
+
determineDataLimits(metas) {
|
|
66
|
+
const o = this.options;
|
|
67
|
+
let lo = Infinity, hi = -Infinity;
|
|
68
|
+
for (const meta of metas) {
|
|
69
|
+
const [a, b] = meta.controller.limits(meta, this);
|
|
70
|
+
if (a < lo) lo = a; if (b > hi) hi = b;
|
|
71
|
+
}
|
|
72
|
+
if (this.type === 'category') { lo = 0; hi = Math.max(0, this.labels.length - 1); if (!this.labels.length && Number.isFinite(lo)) hi = Math.max(hi, 0); }
|
|
73
|
+
if (!Number.isFinite(lo)) { lo = 0; hi = this.type === 'logarithmic' ? 10 : 1; }
|
|
74
|
+
if (this.type === 'linear' || this.type === 'radialLinear') {
|
|
75
|
+
if (o.beginAtZero) { lo = Math.min(0, lo); hi = Math.max(0, hi); }
|
|
76
|
+
if (o.suggestedMin != null) lo = Math.min(lo, o.suggestedMin);
|
|
77
|
+
if (o.suggestedMax != null) hi = Math.max(hi, o.suggestedMax);
|
|
78
|
+
if (lo === hi) { lo -= 1; hi += 1; if (o.beginAtZero && lo < 0 && hi >= 0) lo = 0; }
|
|
79
|
+
const grace = o.grace;
|
|
80
|
+
if (grace) { const g = typeof grace === 'string' && grace.endsWith('%') ? (parseFloat(grace) / 100) * (hi - lo) : Number(grace) || 0; if (lo !== 0 || !o.beginAtZero) lo -= g; hi += g; }
|
|
81
|
+
}
|
|
82
|
+
const index = (v) => (typeof v === 'string' ? this.labels.indexOf(v) : v);
|
|
83
|
+
this.dataMin = lo; this.dataMax = hi;
|
|
84
|
+
this.min = o.min != null ? (this.type === 'category' ? index(o.min) : this.type.startsWith('time') ? toTime(o.min) : o.min) : lo;
|
|
85
|
+
this.max = o.max != null ? (this.type === 'category' ? index(o.max) : this.type.startsWith('time') ? toTime(o.max) : o.max) : hi;
|
|
86
|
+
this._userMin = o.min != null; this._userMax = o.max != null;
|
|
87
|
+
}
|
|
88
|
+
buildTicks(length) {
|
|
89
|
+
const o = this.options, t = o.ticks ?? {}, horizontal = this.isHorizontal() || this.axis === 'r';
|
|
90
|
+
const fit = Math.max(2, Math.floor(length / (horizontal && this.axis !== 'r' ? 64 : this.axis === 'r' ? 36 : 28)) + 1);
|
|
91
|
+
const limit = Math.min(t.maxTicksLimit ?? 11, fit, t.count ?? Infinity);
|
|
92
|
+
let values = [];
|
|
93
|
+
if (this.type === 'category') { for (let i = Math.max(0, this.min); i <= this.max; i++) values.push(i); }
|
|
94
|
+
else if (this.type === 'logarithmic') {
|
|
95
|
+
const a = Math.max(1e-12, this.min > 0 ? this.min : 1), b = Math.max(a * 10, this.max), e0 = Math.floor(Math.log10(a)), e1 = Math.ceil(Math.log10(b));
|
|
96
|
+
if (!this._userMin) this.min = 10 ** e0; if (!this._userMax) this.max = 10 ** e1;
|
|
97
|
+
const few = e1 - e0 <= 3;
|
|
98
|
+
for (let e = e0; e <= e1; e++) for (const k of few ? [1, 2, 5] : [1]) { const v = k * 10 ** e; if (v >= this.min * 0.9999 && v <= this.max * 1.0001) values.push(+v.toPrecision(12)); }
|
|
99
|
+
} else if (this.type === 'time' || this.type === 'timeseries') {
|
|
100
|
+
const r = timeTicks(this.min, this.max, limit, o.time?.unit, o.time?.stepSize ?? t.stepSize, this.type === 'timeseries');
|
|
101
|
+
this.unit = r.unit; values = r.ticks;
|
|
102
|
+
// a time series (days with gaps, as markets have): a tick at the first point on or after each round time
|
|
103
|
+
if (this.type === 'timeseries' && this._series?.length) {
|
|
104
|
+
const s = this._series, out = [];
|
|
105
|
+
let k = 0;
|
|
106
|
+
for (const tv of values) { while (k < s.length && s[k] < tv) k++; if (k < s.length && s[k] >= this.min && s[k] <= this.max && out.at(-1) !== s[k]) out.push(s[k]); }
|
|
107
|
+
values = out.length ? out : s.filter((v) => v >= this.min && v <= this.max).slice(0, 1);
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
const step = t.stepSize ?? niceStep(this.max - this.min, Math.max(1, (t.count ?? limit) - 1));
|
|
111
|
+
if (!this._userMin) this.min = Math.floor(this.min / step + 1e-9) * step;
|
|
112
|
+
if (!this._userMax) this.max = Math.ceil(this.max / step - 1e-9) * step;
|
|
113
|
+
const digits = t.precision ?? Math.max(0, Math.min(10, -Math.floor(Math.log10(step) + 1e-9)));
|
|
114
|
+
for (let v = Math.ceil(this.min / step - 1e-9) * step, i = 0; v <= this.max + step * 1e-9 && i < 1000; v += step, i++) values.push(Number(v.toFixed(Math.min(20, digits + 2))));
|
|
115
|
+
if (this._userMin && values[0] !== this.min) values.unshift(this.min);
|
|
116
|
+
if (this._userMax && values.at(-1) !== this.max) values.push(this.max);
|
|
117
|
+
this.step = step;
|
|
118
|
+
}
|
|
119
|
+
this.ticks = values.map((value, i) => ({ value, label: this.label(value, i, values) }));
|
|
120
|
+
return this.ticks;
|
|
121
|
+
}
|
|
122
|
+
/** What a tick says: ticks.callback(value, index, ticks) when given, else the axis's own words. */
|
|
123
|
+
label(value, i, values) {
|
|
124
|
+
const o = this.options, cb = o.ticks?.callback;
|
|
125
|
+
const own = this.type === 'category' ? (this.labels[value] ?? value) : this.type.startsWith('time') ? formatTime(value, o.time?.displayFormats?.[this.unit] ?? TIME_FORMATS[this.unit ?? 'day']) : formatNumber(value, { step: this.step, locale: this.chart.resolved.locale, compact: Math.abs(value) >= 1e5, ...(o.ticks?.format ?? {}) });
|
|
126
|
+
if (typeof cb === 'function') return cb.call(this, this.type === 'category' ? value : value, i, values.map((v) => ({ value: v })));
|
|
127
|
+
if (o.stacked === 'percent' || o.stacked === '100%') return own + '%';
|
|
128
|
+
return Array.isArray(own) ? own.join('\n') : String(own);
|
|
129
|
+
}
|
|
130
|
+
/** How much room the axis needs across itself (width if it stands up, height if it lies down). */
|
|
131
|
+
fit(painter, length) {
|
|
132
|
+
const o = this.options;
|
|
133
|
+
if (o.display === false || this.axis === 'r') { this.size = 0; return 0; }
|
|
134
|
+
this.buildTicks(length);
|
|
135
|
+
const f = this.font(), tl = o.grid?.drawTicks === false ? 0 : o.grid?.tickLength ?? 8, pad = o.ticks?.padding ?? 3;
|
|
136
|
+
const titleH = o.title?.display ? lineOf(o.title.font ?? f) + 4 : 0;
|
|
137
|
+
const shown = o.ticks?.display === false ? [] : this.ticks.filter((t) => t.label != null && t.label !== '');
|
|
138
|
+
if (this.isHorizontal()) {
|
|
139
|
+
const widest = Math.max(0, ...shown.map((t) => textW(painter, t.label, f)));
|
|
140
|
+
const room = length / Math.max(1, shown.length);
|
|
141
|
+
this.rotation = 0; this.skip = 1;
|
|
142
|
+
if (widest + 6 > room) {
|
|
143
|
+
if (o.ticks?.autoSkip !== false) this.skip = Math.ceil((widest + 6) / Math.max(1, room));
|
|
144
|
+
else this.rotation = Math.min(o.ticks?.maxRotation ?? 50, 90);
|
|
145
|
+
}
|
|
146
|
+
const labelH = this.rotation ? Math.sin((this.rotation * Math.PI) / 180) * widest + lineOf(f) : lineOf(f) * Math.max(1, ...shown.map((t) => String(t.label).split('\n').length));
|
|
147
|
+
this.size = (o.ticks?.display === false ? 0 : labelH + pad) + tl * 0.6 + titleH;
|
|
148
|
+
} else {
|
|
149
|
+
this.skip = Math.max(1, Math.ceil(shown.length * lineOf(f) * 1.4 / Math.max(1, length)));
|
|
150
|
+
this.size = (o.ticks?.display === false ? 0 : Math.max(0, ...shown.map((t) => textW(painter, t.label, f))) + pad) + tl * 0.6 + titleH;
|
|
151
|
+
}
|
|
152
|
+
return this.size;
|
|
153
|
+
}
|
|
154
|
+
font() { return { ...this.chart.resolved.font, ...(this.options.ticks?.font ?? {}) }; }
|
|
155
|
+
setBox(left, top, right, bottom) { Object.assign(this, { left, top, right, bottom, width: right - left, height: bottom - top }); }
|
|
156
|
+
// a category axis standing up reads from the top down, as in Chart.js: the first label at the top
|
|
157
|
+
get flipped() { return !!this.options.reverse !== (this.type === 'category' && this.axis !== 'r' && !this.isHorizontal()); }
|
|
158
|
+
get start() { return this.isHorizontal() ? this.left : this.bottom; }
|
|
159
|
+
get end() { return this.isHorizontal() ? this.right : this.top; }
|
|
160
|
+
/** A value of the data → a pixel along the axis. */
|
|
161
|
+
getPixelForValue(v, index) {
|
|
162
|
+
const o = this.options;
|
|
163
|
+
let a = this.start, b = this.end;
|
|
164
|
+
if (this.flipped) [a, b] = [b, a];
|
|
165
|
+
if (this.type === 'category') {
|
|
166
|
+
const n = Math.max(1, this.max - this.min + (this.offset ? 1 : 0)), i = (typeof v === 'string' ? this.labels.indexOf(v) : v ?? index) - this.min;
|
|
167
|
+
return a + ((i + (this.offset ? 0.5 : 0)) / (this.offset ? n : Math.max(1, n))) * (b - a);
|
|
168
|
+
}
|
|
169
|
+
if (this.type === 'logarithmic') { const l = (x) => Math.log10(Math.max(1e-12, x)); return a + ((l(v) - l(this.min)) / ((l(this.max) - l(this.min)) || 1)) * (b - a); }
|
|
170
|
+
if (this.type === 'timeseries' && this._series) {
|
|
171
|
+
const k = this._series.indexOf(toTime(v)), n = this._series.length;
|
|
172
|
+
if (k >= 0) return a + ((k + (this.offset ? 0.5 : 0)) / Math.max(1, this.offset ? n : n - 1)) * (b - a);
|
|
173
|
+
}
|
|
174
|
+
const x = this.type.startsWith('time') ? toTime(v) : v;
|
|
175
|
+
return a + ((x - this.min) / ((this.max - this.min) || 1)) * (b - a);
|
|
176
|
+
}
|
|
177
|
+
getValueForPixel(p) {
|
|
178
|
+
let a = this.start, b = this.end;
|
|
179
|
+
if (this.flipped) [a, b] = [b, a];
|
|
180
|
+
const t = (p - a) / ((b - a) || 1);
|
|
181
|
+
if (this.type === 'category') { const n = this.max - this.min + (this.offset ? 1 : 0); return Math.round(this.min + t * (this.offset ? n : n) - (this.offset ? 0.5 : 0)); }
|
|
182
|
+
if (this.type === 'logarithmic') { const l = (x) => Math.log10(Math.max(1e-12, x)); return 10 ** (l(this.min) + t * (l(this.max) - l(this.min))); }
|
|
183
|
+
return this.min + t * (this.max - this.min);
|
|
184
|
+
}
|
|
185
|
+
/** The value bars and filled lines grow from: 0 when it is in range, else the nearer end. */
|
|
186
|
+
getBaseValue() { return this.type === 'logarithmic' ? this.min : this.min > 0 ? this.min : this.max < 0 ? this.max : 0; }
|
|
187
|
+
getBasePixel() { return this.getPixelForValue(this.getBaseValue()); }
|
|
188
|
+
/** The size of one category's slot (bars), in pixels. */
|
|
189
|
+
get bandSize() { const n = this.type === 'category' ? Math.max(1, this.max - this.min + 1) : Math.max(1, this._series?.length ?? 1); return Math.abs(this.end - this.start) / n; }
|
|
190
|
+
|
|
191
|
+
draw(painter, area) {
|
|
192
|
+
const o = this.options;
|
|
193
|
+
if (o.display === false || this.axis === 'r') return;
|
|
194
|
+
const f = this.font(), grid = o.grid ?? {}, border = o.border ?? {}, horizontal = this.isHorizontal(), tl = grid.drawTicks === false ? 0 : grid.tickLength ?? 8;
|
|
195
|
+
const colour = (key, ctx, d) => resolve(key, ctx) ?? d;
|
|
196
|
+
painter.layer('grid');
|
|
197
|
+
if (grid.display !== false) {
|
|
198
|
+
this.ticks.forEach((tick, i) => {
|
|
199
|
+
const ctx = { chart: this.chart, scale: this, tick, index: i, type: 'tick' };
|
|
200
|
+
let p = this.getPixelForValue(tick.value, i);
|
|
201
|
+
if (grid.offset && this.type === 'category') p -= (this.bandSize / 2) * (Math.sign(this.getPixelForValue(1) - this.getPixelForValue(0)) || 1);
|
|
202
|
+
const st = { stroke: colour(grid.color, ctx, themeOf(this.chart).grid ?? 'var(--clarity-chart-grid, rgba(0,0,0,0.1))'), width: resolve(grid.lineWidth, ctx) ?? 1, dash: resolve(grid.borderDash ?? grid.dash, ctx) };
|
|
203
|
+
if (grid.drawOnChartArea !== false) horizontal ? painter.line(p, area.top, p, area.bottom, st) : painter.line(area.left, p, area.right, p, st);
|
|
204
|
+
if (tl) horizontal ? painter.line(p, this.position === 'bottom' ? this.top : this.bottom, p, this.position === 'bottom' ? this.top + tl * 0.6 : this.bottom - tl * 0.6, st) : painter.line(this.position === 'left' ? this.right : this.left, p, this.position === 'left' ? this.right - tl * 0.6 : this.left + tl * 0.6, p, st);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
painter.layer('axes');
|
|
208
|
+
if (border.display !== false) {
|
|
209
|
+
const st = { stroke: border.color ?? themeOf(this.chart).axis ?? 'var(--clarity-chart-axis, rgba(0,0,0,0.25))', width: border.width ?? 1, dash: border.dash };
|
|
210
|
+
if (horizontal) { const y = this.position === 'bottom' ? this.top : this.bottom; painter.line(this.left, y, this.right, y, st); }
|
|
211
|
+
else { const x = this.position === 'left' ? this.right : this.left; painter.line(x, this.top, x, this.bottom, st); }
|
|
212
|
+
}
|
|
213
|
+
if (o.ticks?.display !== false) {
|
|
214
|
+
const colourT = o.ticks?.color, pad = o.ticks?.padding ?? 3;
|
|
215
|
+
this.ticks.forEach((tick, i) => {
|
|
216
|
+
if (tick.label == null || tick.label === '' || (this.skip > 1 && i % this.skip)) return;
|
|
217
|
+
const p = this.getPixelForValue(tick.value, i), fill = resolve(colourT, { tick, index: i }) ?? this.chart.resolved.color;
|
|
218
|
+
const lines = String(tick.label).split('\n');
|
|
219
|
+
lines.forEach((line, k) => {
|
|
220
|
+
if (horizontal) {
|
|
221
|
+
const y = this.position === 'bottom' ? this.top + tl * 0.6 + pad + lineOf(f) * (k + 0.8) : this.bottom - tl * 0.6 - pad - lineOf(f) * (lines.length - k - 1);
|
|
222
|
+
painter.text(line, p, y, { fill, font: f, align: this.rotation ? 'right' : 'center', baseline: this.position === 'bottom' ? 'alphabetic' : 'bottom', rotate: this.rotation ? -this.rotation : 0 });
|
|
223
|
+
} else {
|
|
224
|
+
const x = this.position === 'left' ? this.right - tl * 0.6 - pad : this.left + tl * 0.6 + pad;
|
|
225
|
+
painter.text(line, x, p + (k - (lines.length - 1) / 2) * lineOf(f), { fill, font: f, align: this.position === 'left' ? 'right' : 'left', baseline: 'middle' });
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
if (o.title?.display) {
|
|
231
|
+
const tf = { ...f, ...(o.title.font ?? {}) }, text = String(o.title.text ?? ''), fill = o.title.color ?? this.chart.resolved.color;
|
|
232
|
+
if (horizontal) painter.text(text, (this.left + this.right) / 2, this.position === 'bottom' ? this.bottom - 2 : this.top + lineOf(tf) - 2, { fill, font: tf, align: 'center' });
|
|
233
|
+
else { const x = this.position === 'left' ? this.left + lineOf(tf) * 0.8 : this.right - 4; painter.text(text, x, (this.top + this.bottom) / 2, { fill, font: tf, align: 'center', rotate: this.position === 'left' ? -90 : 90 }); }
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** The round scale of radar and polar-area charts: rings for values, a spoke and a name per label. */
|
|
239
|
+
export class RadialAxis extends Axis {
|
|
240
|
+
constructor(id, options, chart) { super(id, { ...options, type: 'radialLinear' }, chart); this.axis = 'r'; }
|
|
241
|
+
isHorizontal() { return false; }
|
|
242
|
+
setCenter(area, labelCount) {
|
|
243
|
+
const o = this.options, f = { ...this.chart.resolved.font, ...(o.pointLabels?.font ?? {}) };
|
|
244
|
+
const labels = o.pointLabels?.display === false || this.chart.config.type === 'polarArea' && o.pointLabels?.display !== true ? [] : this.labels;
|
|
245
|
+
const room = labels.length ? Math.max(...labels.map((l) => this.chart.painter.measure(String(l), f))) + 10 : 4;
|
|
246
|
+
this.cx = (area.left + area.right) / 2; this.cy = (area.top + area.bottom) / 2;
|
|
247
|
+
this.radius = Math.max(4, Math.min(area.right - area.left - room * 2, area.bottom - area.top - (labels.length ? lineOf(f) * 2 + 10 : 8)) / 2);
|
|
248
|
+
this.count = labelCount; this.pointLabelFont = f; this.showLabels = labels.length > 0;
|
|
249
|
+
this.buildTicks(this.radius);
|
|
250
|
+
}
|
|
251
|
+
get startAngle() { return ((this.options.startAngle ?? 0) * Math.PI) / 180; }
|
|
252
|
+
angleOf(i) { return this.startAngle + (Math.PI * 2 * i) / Math.max(1, this.count); }
|
|
253
|
+
getDistanceFromCenterForValue(v) { const t = (v - this.min) / ((this.max - this.min) || 1); return (this.options.reverse ? 1 - t : t) * this.radius; }
|
|
254
|
+
getPointPosition(i, distance) { const a = this.angleOf(i); return { x: this.cx + Math.sin(a) * distance, y: this.cy - Math.cos(a) * distance, angle: a }; }
|
|
255
|
+
getPointPositionForValue(i, v) { return this.getPointPosition(i, this.getDistanceFromCenterForValue(v)); }
|
|
256
|
+
getBasePixel() { return this.cy; }
|
|
257
|
+
draw(painter) {
|
|
258
|
+
const o = this.options;
|
|
259
|
+
if (o.display === false) return;
|
|
260
|
+
const grid = o.grid ?? {}, f = this.font(), polygon = !grid.circular && this.chart.config.type === 'radar';
|
|
261
|
+
painter.layer('grid');
|
|
262
|
+
if (grid.display !== false) this.ticks.forEach((tick, i) => {
|
|
263
|
+
const d = this.getDistanceFromCenterForValue(tick.value); if (d <= 0) return;
|
|
264
|
+
const st = { stroke: resolve(grid.color, { tick, index: i }) ?? themeOf(this.chart).grid ?? 'var(--clarity-chart-grid, rgba(0,0,0,0.1))', width: grid.lineWidth ?? 1, fill: 'none' };
|
|
265
|
+
if (polygon && this.count > 2) painter.path(Array.from({ length: this.count }, (_, k) => { const p = this.getPointPosition(k, d); return `${k ? 'L' : 'M'}${p.x.toFixed(2)},${p.y.toFixed(2)}`; }).join('') + 'Z', st);
|
|
266
|
+
else painter.circle(this.cx, this.cy, d, st);
|
|
267
|
+
});
|
|
268
|
+
if (o.angleLines?.display !== false && this.chart.config.type === 'radar') for (let k = 0; k < this.count; k++) { const p = this.getPointPosition(k, this.radius); painter.line(this.cx, this.cy, p.x, p.y, { stroke: o.angleLines?.color ?? themeOf(this.chart).grid ?? 'var(--clarity-chart-grid, rgba(0,0,0,0.1))', width: o.angleLines?.lineWidth ?? 1 }); }
|
|
269
|
+
painter.layer('axes');
|
|
270
|
+
if (this.showLabels) this.labels.forEach((label, k) => {
|
|
271
|
+
const p = this.getPointPosition(k, this.radius + 8), s = Math.sin(p.angle), c = Math.cos(p.angle);
|
|
272
|
+
painter.text(String(label), p.x, p.y, { fill: o.pointLabels?.color ?? this.chart.resolved.color, font: this.pointLabelFont, align: Math.abs(s) < 0.1 ? 'center' : s > 0 ? 'left' : 'right', baseline: Math.abs(c) < 0.1 ? 'middle' : c > 0 ? 'bottom' : 'top' });
|
|
273
|
+
});
|
|
274
|
+
if (o.ticks?.display !== false) this.ticks.forEach((tick, i) => {
|
|
275
|
+
if (tick.value === this.min && i === 0) return;
|
|
276
|
+
const d = this.getDistanceFromCenterForValue(tick.value);
|
|
277
|
+
if (o.ticks?.showLabelBackdrop !== false) { const w = painter.measure(tick.label, f) + 6; painter.rect(this.cx - w / 2, this.cy - d - lineOf(f) / 2, w, lineOf(f), { fill: o.ticks?.backdropColor ?? themeOf(this.chart).bg ?? 'var(--clarity-chart-bg, rgba(255,255,255,0.75))', opacity: 0.8 }); }
|
|
278
|
+
painter.text(tick.label, this.cx, this.cy - d, { fill: o.ticks?.color ?? this.chart.resolved.color, font: f, align: 'center', baseline: 'middle' });
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|