@mdgs/pivotjet 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +628 -3
- package/bin/pivotjet.mjs +122 -0
- package/dist/core.cjs +4926 -0
- package/dist/core.cjs.map +7 -0
- package/dist/index.cjs +7277 -0
- package/dist/index.cjs.map +7 -0
- package/dist/pivotjet.min.js +334 -0
- package/dist/pivotjet.min.js.map +7 -0
- package/docs/images/cell-formats.png +0 -0
- package/docs/images/chart.png +0 -0
- package/docs/images/configure-fields.png +0 -0
- package/docs/images/designer.png +0 -0
- package/docs/images/large-dataset.png +0 -0
- package/package.json +108 -4
- package/src/cellfmt.js +218 -0
- package/src/chart.js +595 -0
- package/src/core.d.ts +424 -0
- package/src/core.js +18 -0
- package/src/csv.js +198 -0
- package/src/dataset.js +404 -0
- package/src/designer.js +995 -0
- package/src/elements.js +50 -0
- package/src/engine.js +778 -0
- package/src/errors.js +2 -0
- package/src/export.js +775 -0
- package/src/expr.js +180 -0
- package/src/format.js +42 -0
- package/src/holistic.js +237 -0
- package/src/index.d.ts +155 -0
- package/src/index.js +6 -0
- package/src/layouts.js +105 -0
- package/src/node.d.ts +10 -0
- package/src/node.js +29 -0
- package/src/pdffont.js +361 -0
- package/src/remote.js +164 -0
- package/src/report-render.js +31 -0
- package/src/report.js +103 -0
- package/src/reportstore.js +135 -0
- package/src/server.d.ts +20 -0
- package/src/server.js +272 -0
- package/src/stream.js +285 -0
- package/src/styles.js +162 -0
- package/src/util.js +73 -0
- package/src/viewer.js +632 -0
package/src/chart.js
ADDED
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
// Zero-dependency SVG charts for a PivotResult.
|
|
2
|
+
//
|
|
3
|
+
// buildChartModel(result, opts) -> plain data model (pure, Node-safe)
|
|
4
|
+
// renderSVG(model, opts) -> SVG string (pure, Node-safe)
|
|
5
|
+
// new PivotChart(container, o) -> live, responsive chart with legend toggles, tooltips, click events
|
|
6
|
+
//
|
|
7
|
+
// Live charts reference the viewer's CSS variables (--mp-fg, --mp-muted, --mp-line, --mp-bg) with
|
|
8
|
+
// light fallbacks, so they follow the light/dark theme. `toSVG()` resolves everything to literal colors.
|
|
9
|
+
|
|
10
|
+
import { makeFormatter } from './format.js';
|
|
11
|
+
import { Emitter, esc } from './util.js';
|
|
12
|
+
|
|
13
|
+
export const CHART_TYPES = ['column', 'bar', 'stackedColumn', 'stackedBar', 'line', 'area', 'stackedArea', 'pie', 'donut'];
|
|
14
|
+
|
|
15
|
+
// Okabe-Ito based (color-blind safe); the yellow is darkened and blue lightened so all ten read on white and on dark.
|
|
16
|
+
export const PALETTE = ['#2582CE', '#E69F00', '#009E73', '#CC79A7', '#56B4E9', '#D55E00', '#C4B000', '#7B6FD0', '#8C9096', '#A8643A'];
|
|
17
|
+
const OTHER_COLOR = '#9AA0A6';
|
|
18
|
+
|
|
19
|
+
const THEMES = {
|
|
20
|
+
light: { fg: '#1f2430', muted: '#6b7280', line: '#e3e6ec', bg: '#ffffff' },
|
|
21
|
+
dark: { fg: '#e6e9f0', muted: '#9aa3b5', line: '#2a3040', bg: '#14171f' },
|
|
22
|
+
};
|
|
23
|
+
const FONT = 'system-ui,-apple-system,"Segoe UI",Roboto,sans-serif';
|
|
24
|
+
|
|
25
|
+
// ------------------------------------------------------------------------------------------------
|
|
26
|
+
// model
|
|
27
|
+
// ------------------------------------------------------------------------------------------------
|
|
28
|
+
function nodesAtLevel(root, level) {
|
|
29
|
+
let cur = [root];
|
|
30
|
+
for (let d = 0; d <= level; d++) {
|
|
31
|
+
const next = [];
|
|
32
|
+
for (const n of cur) for (const c of n.children) next.push(c);
|
|
33
|
+
cur = next;
|
|
34
|
+
}
|
|
35
|
+
return cur;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function axisNodes(root, depth, level) {
|
|
39
|
+
if (depth === 0) return { nodes: [root], single: true };
|
|
40
|
+
const lv = level === 'leaf' ? depth - 1 : Math.max(0, Math.min(level ?? 0, depth - 1));
|
|
41
|
+
return { nodes: nodesAtLevel(root, lv), level: lv };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Turn a PivotResult into chart-friendly data.
|
|
46
|
+
* @param {import('./engine.js').PivotResult} result
|
|
47
|
+
* @param {{rowLevel?:number|'leaf', colLevel?:number|'leaf', valueIndex?:number, maxCategories?:number, maxSeries?:number}} [opts]
|
|
48
|
+
*
|
|
49
|
+
* Categories are row nodes at `rowLevel` (default 0). Series are column nodes at `colLevel`; with no
|
|
50
|
+
* column dimension there is one series per value (or just `valueIndex` when given). Grand-total nodes are
|
|
51
|
+
* never used, except that a pivot with *no* row dimension has a single category ("Total") backed by the
|
|
52
|
+
* root node, since there is nothing else to plot.
|
|
53
|
+
*/
|
|
54
|
+
export function buildChartModel(result, opts = {}) {
|
|
55
|
+
const maxCategories = opts.maxCategories ?? 50;
|
|
56
|
+
const maxSeries = opts.maxSeries ?? 12;
|
|
57
|
+
const rowDepth = result.rowDims.length, colDepth = result.colDims.length;
|
|
58
|
+
const nVals = result.values.length;
|
|
59
|
+
|
|
60
|
+
const rows = axisNodes(result.rowRoot, rowDepth, opts.rowLevel);
|
|
61
|
+
let cats = rows.nodes.map((node) => ({ label: rows.single ? 'Total' : node.label, node, path: rows.single ? ['Total'] : node.path() }));
|
|
62
|
+
const catsTotal = cats.length;
|
|
63
|
+
cats = cats.slice(0, maxCategories);
|
|
64
|
+
|
|
65
|
+
let valueIndex = opts.valueIndex;
|
|
66
|
+
if (valueIndex !== undefined) valueIndex = Math.max(0, Math.min(valueIndex, nVals - 1));
|
|
67
|
+
let series = [];
|
|
68
|
+
if (colDepth === 0) {
|
|
69
|
+
const js = valueIndex !== undefined ? [valueIndex] : result.values.map((_, j) => j);
|
|
70
|
+
series = js.map((j) => ({ name: result.values[j].label, colNode: result.colRoot, valueIndex: j }));
|
|
71
|
+
} else {
|
|
72
|
+
const cols = axisNodes(result.colRoot, colDepth, opts.colLevel);
|
|
73
|
+
const j = valueIndex ?? 0;
|
|
74
|
+
series = cols.nodes.map((node) => ({ name: cols.level > 0 ? node.path().join(' › ') : node.label, colNode: node, valueIndex: j }));
|
|
75
|
+
}
|
|
76
|
+
const seriesTotal = series.length;
|
|
77
|
+
series = series.slice(0, maxSeries);
|
|
78
|
+
|
|
79
|
+
for (let s = 0; s < series.length; s++) {
|
|
80
|
+
const ser = series[s];
|
|
81
|
+
const v = result.values[ser.valueIndex];
|
|
82
|
+
ser.color = PALETTE[s % PALETTE.length];
|
|
83
|
+
ser.format = v.format;
|
|
84
|
+
ser.showAs = v.showAs || 'value';
|
|
85
|
+
ser.valueLabel = v.label;
|
|
86
|
+
ser.points = cats.map((c) => {
|
|
87
|
+
const x = result.get(c.node, ser.colNode, ser.valueIndex);
|
|
88
|
+
return typeof x === 'number' && Number.isFinite(x) ? x : null;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const rowLabel = rowDepth ? result.rowDims.map((d) => d.label).slice(0, (rows.level ?? 0) + 1).join(' › ') : '';
|
|
93
|
+
const colLabel = colDepth ? result.colDims.map((d) => d.label).join(' › ') : '';
|
|
94
|
+
const valueLabels = [...new Set(series.map((s) => s.valueLabel))];
|
|
95
|
+
return {
|
|
96
|
+
categories: cats,
|
|
97
|
+
series,
|
|
98
|
+
valueIndex: valueIndex ?? 0,
|
|
99
|
+
truncated: { categories: catsTotal - cats.length, series: seriesTotal - series.length },
|
|
100
|
+
rowLabel,
|
|
101
|
+
colLabel,
|
|
102
|
+
valueLabel: valueLabels.join(', '),
|
|
103
|
+
title: `${valueLabels.join(', ')}${rowLabel ? ' by ' + rowLabel : ''}${colLabel ? ' and ' + colLabel : ''}`,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ------------------------------------------------------------------------------------------------
|
|
108
|
+
// helpers
|
|
109
|
+
// ------------------------------------------------------------------------------------------------
|
|
110
|
+
const r2 = (n) => Math.round(n * 100) / 100;
|
|
111
|
+
const trunc = (s, n) => (s.length > n ? s.slice(0, Math.max(1, n - 1)) + '…' : s);
|
|
112
|
+
|
|
113
|
+
function niceNum(range, round) {
|
|
114
|
+
const exp = Math.floor(Math.log10(range));
|
|
115
|
+
const f = range / 10 ** exp;
|
|
116
|
+
let nf;
|
|
117
|
+
if (round) nf = f < 1.5 ? 1 : f < 3 ? 2 : f < 7 ? 5 : 10;
|
|
118
|
+
else nf = f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10;
|
|
119
|
+
return nf * 10 ** exp;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** "Nice" axis ticks covering [min, max]. */
|
|
123
|
+
export function niceTicks(min, max, count = 5) {
|
|
124
|
+
if (!(max > min)) {
|
|
125
|
+
if (min === 0) max = 1;
|
|
126
|
+
else { const d = Math.abs(min) * 0.1; min -= d; max += d; }
|
|
127
|
+
}
|
|
128
|
+
const step = niceNum(niceNum(max - min, false) / (count - 1), true);
|
|
129
|
+
const lo = Math.floor(min / step + 1e-9) * step;
|
|
130
|
+
const hi = Math.ceil(max / step - 1e-9) * step;
|
|
131
|
+
const ticks = [];
|
|
132
|
+
for (let i = 0; lo + i * step <= hi + step * 1e-9; i++) ticks.push(Number((lo + i * step).toPrecision(12)));
|
|
133
|
+
return ticks;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function textOn(hex) {
|
|
137
|
+
const n = parseInt(hex.slice(1), 16);
|
|
138
|
+
const lin = (c) => { c /= 255; return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; };
|
|
139
|
+
const L = 0.2126 * lin((n >> 16) & 255) + 0.7152 * lin((n >> 8) & 255) + 0.0722 * lin(n & 255);
|
|
140
|
+
return L > 0.4 ? '#111827' : '#ffffff';
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const fmtCache = new Map();
|
|
144
|
+
function formatter(spec, showAs, kind) {
|
|
145
|
+
const key = kind + '|' + (typeof spec === 'function' ? 'fn' : JSON.stringify(spec ?? null)) + '|' + showAs;
|
|
146
|
+
let f = fmtCache.get(key);
|
|
147
|
+
if (!f) {
|
|
148
|
+
const dflt = showAs && showAs !== 'value' ? 'percent' : kind === 'axis' ? 'compact' : 'number';
|
|
149
|
+
f = makeFormatter(spec ?? dflt);
|
|
150
|
+
if (typeof spec !== 'function') fmtCache.set(key, f);
|
|
151
|
+
}
|
|
152
|
+
return f;
|
|
153
|
+
}
|
|
154
|
+
/** exact-ish formatter used for tooltips */
|
|
155
|
+
export function formatPoint(model, si, v) {
|
|
156
|
+
const s = model.series[si];
|
|
157
|
+
return formatter(s.format, s.showAs, 'tip')(v);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let uid = 0;
|
|
161
|
+
|
|
162
|
+
// ------------------------------------------------------------------------------------------------
|
|
163
|
+
// render (pure)
|
|
164
|
+
// ------------------------------------------------------------------------------------------------
|
|
165
|
+
/**
|
|
166
|
+
* @param {ReturnType<typeof buildChartModel>} model
|
|
167
|
+
* @param {{type?:string,width?:number,height?:number,hidden?:number[],inline?:boolean,theme?:'light'|'dark',colors?:object,seriesIndex?:number}} [opts]
|
|
168
|
+
* inline:true -> resolved colors + background rect + xmlns (standalone file). hidden: series indexes
|
|
169
|
+
* (cartesian) or category indexes (pie/donut). seriesIndex: which series feeds a pie/donut.
|
|
170
|
+
*/
|
|
171
|
+
export function renderSVG(model, opts = {}) {
|
|
172
|
+
const o = { type: 'column', width: 720, height: 420, hidden: [], inline: false, theme: 'light', ...opts };
|
|
173
|
+
if (!CHART_TYPES.includes(o.type)) o.type = 'column';
|
|
174
|
+
const W = Math.max(120, Math.round(o.width)), H = Math.max(100, Math.round(o.height));
|
|
175
|
+
const hidden = new Set(o.hidden || []);
|
|
176
|
+
const palette = THEMES[o.theme] || THEMES.light;
|
|
177
|
+
const col = (k) => (o.inline ? (o.colors && o.colors[k]) || palette[k] : `var(--mp-${k},${THEMES.light[k]})`);
|
|
178
|
+
const T = (x, y, s, a = {}) => {
|
|
179
|
+
const { anchor = 'start', size = 11, fill = 'muted', weight, rotate, title, color } = a;
|
|
180
|
+
const rot = rotate ? ` transform="rotate(${rotate} ${r2(x)} ${r2(y)})"` : '';
|
|
181
|
+
return `<text x="${r2(x)}" y="${r2(y)}" font-size="${size}" fill="${color || col(fill)}" text-anchor="${anchor}"${weight ? ` font-weight="${weight}"` : ''}${rot}>${title ? `<title>${esc(title)}</title>` : ''}${esc(s)}</text>`;
|
|
182
|
+
};
|
|
183
|
+
const id = `mpc${++uid}`;
|
|
184
|
+
const N = model.categories.length;
|
|
185
|
+
const typeName = { column: 'Column', bar: 'Bar', stackedColumn: 'Stacked column', stackedBar: 'Stacked bar', line: 'Line', area: 'Area', stackedArea: 'Stacked area', pie: 'Pie', donut: 'Donut' }[o.type];
|
|
186
|
+
const pie = o.type === 'pie' || o.type === 'donut';
|
|
187
|
+
|
|
188
|
+
const wrap = (body, desc) =>
|
|
189
|
+
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${W} ${H}" width="${o.inline ? W : '100%'}" height="${o.inline ? H : '100%'}" role="img" aria-labelledby="${id}t ${id}d" font-family='${FONT}'>` +
|
|
190
|
+
`<title id="${id}t">${esc(model.title || 'Chart')}</title><desc id="${id}d">${esc(desc)}</desc>` +
|
|
191
|
+
(o.inline ? `<rect width="${W}" height="${H}" fill="${col('bg')}"/>` : '') + body + '</svg>';
|
|
192
|
+
|
|
193
|
+
const empty = (msg, extra = '') => wrap(extra + T(W / 2, H / 2, msg, { anchor: 'middle', size: 13 }), `${typeName} chart. ${msg}`);
|
|
194
|
+
|
|
195
|
+
if (!N || !model.series.length) return empty('No data to chart');
|
|
196
|
+
|
|
197
|
+
const pad = 12;
|
|
198
|
+
const noteH = model.truncated && (model.truncated.categories || model.truncated.series) ? 14 : 0;
|
|
199
|
+
const note = noteH
|
|
200
|
+
? T(W - pad, H - 4, `+${model.truncated.categories ? model.truncated.categories + ' more categories' : ''}${model.truncated.categories && model.truncated.series ? ', ' : ''}${model.truncated.series ? model.truncated.series + ' more series' : ''} not shown`, { anchor: 'end', size: 10 })
|
|
201
|
+
: '';
|
|
202
|
+
|
|
203
|
+
// ---------------- legends
|
|
204
|
+
const swatch = (x, y, c) => `<rect class="lg" x="${r2(x)}" y="${r2(y)}" width="10" height="10" rx="2" fill="${c}"/>`;
|
|
205
|
+
const legendTop = (entries) => {
|
|
206
|
+
const rowH = 18, maxRows = 3;
|
|
207
|
+
let x = pad, row = 0, svg = '', shown = 0;
|
|
208
|
+
for (const e of entries) {
|
|
209
|
+
const label = trunc(e.label, 28);
|
|
210
|
+
const w = 14 + label.length * 6.1 + 16;
|
|
211
|
+
if (x + w > W - pad && x > pad) { x = pad; row++; }
|
|
212
|
+
if (row >= maxRows) break;
|
|
213
|
+
svg += `<g data-lg="${e.idx}" style="cursor:pointer" opacity="${hidden.has(e.idx) ? 0.38 : 1}"><title>${esc(e.label)}</title>${swatch(x, row * rowH + pad + 1, e.color)}${T(x + 14, row * rowH + pad + 10, label, { fill: 'fg' })}</g>`;
|
|
214
|
+
x += w;
|
|
215
|
+
shown++;
|
|
216
|
+
}
|
|
217
|
+
if (shown < entries.length) svg += T(x, row * rowH + pad + 10, `+${entries.length - shown} more`, { size: 10 });
|
|
218
|
+
return { svg, height: (Math.min(row, maxRows - 1) + 1) * rowH + pad };
|
|
219
|
+
};
|
|
220
|
+
const legendRight = (entries, x0, maxRows) => {
|
|
221
|
+
let svg = '';
|
|
222
|
+
const n = Math.min(entries.length, maxRows);
|
|
223
|
+
entries.slice(0, n).forEach((e, k) => {
|
|
224
|
+
const label = trunc(e.label, 24);
|
|
225
|
+
svg += `<g data-lg="${e.idx}" style="cursor:pointer" opacity="${hidden.has(e.idx) ? 0.38 : 1}"><title>${esc(e.label)}</title>${swatch(x0, pad + k * 18 + 1, e.color)}${T(x0 + 14, pad + k * 18 + 10, label, { fill: 'fg' })}</g>`;
|
|
226
|
+
});
|
|
227
|
+
if (entries.length > n) svg += T(x0, pad + n * 18 + 10, `+${entries.length - n} more`, { size: 10 });
|
|
228
|
+
return svg;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
// ---------------- pie / donut
|
|
232
|
+
if (pie) {
|
|
233
|
+
const si = Math.max(0, Math.min(o.seriesIndex ?? 0, model.series.length - 1));
|
|
234
|
+
const ser = model.series[si];
|
|
235
|
+
let slices = model.categories.map((c, i) => ({ i, label: c.label, v: ser.points[i], color: PALETTE[i % PALETTE.length] })).filter((s) => s.v != null && s.v > 0);
|
|
236
|
+
if (slices.length > 12) {
|
|
237
|
+
const keep = new Set([...slices].sort((a, b) => b.v - a.v).slice(0, 11).map((s) => s.i));
|
|
238
|
+
const rest = slices.filter((s) => !keep.has(s.i));
|
|
239
|
+
slices = slices.filter((s) => keep.has(s.i));
|
|
240
|
+
slices.push({ i: N, label: `Other (${rest.length})`, v: rest.reduce((a, s) => a + s.v, 0), color: OTHER_COLOR, other: true, count: rest.length });
|
|
241
|
+
}
|
|
242
|
+
const entries = slices.map((s) => ({ idx: s.i, label: s.label, color: s.color }));
|
|
243
|
+
const wide = W >= 480;
|
|
244
|
+
const legW = wide ? Math.min(210, Math.max(90, Math.max(0, ...entries.map((e) => Math.min(24, e.label.length))) * 6.1 + 40)) : 0;
|
|
245
|
+
const lt = wide ? { svg: legendRight(entries, W - legW, Math.floor((H - pad * 2) / 18)), height: 0 } : legendTop(entries);
|
|
246
|
+
const vis = slices.filter((s) => !hidden.has(s.i));
|
|
247
|
+
const total = vis.reduce((a, s) => a + s.v, 0);
|
|
248
|
+
const desc = `${typeName} chart of ${ser.valueLabel} for ${slices.length} categories.`;
|
|
249
|
+
if (!(total > 0)) return wrap(lt.svg + T((W - legW) / 2, H / 2, slices.length ? 'All slices hidden' : 'No positive values to chart', { anchor: 'middle', size: 13 }), desc);
|
|
250
|
+
const aw = W - legW - pad * 2, ah = H - lt.height - pad - noteH;
|
|
251
|
+
const R = Math.max(20, Math.min(aw, ah) / 2 - 6);
|
|
252
|
+
const cx = pad + aw / 2, cy = lt.height + ah / 2 + (wide ? pad : 6);
|
|
253
|
+
const ri = o.type === 'donut' ? R * 0.58 : 0;
|
|
254
|
+
const pt = (rad, a) => [cx + rad * Math.cos(a), cy + rad * Math.sin(a)];
|
|
255
|
+
let a0 = -Math.PI / 2, body = '';
|
|
256
|
+
const tipFmt = formatter(ser.format, ser.showAs, 'tip');
|
|
257
|
+
for (const s of vis) {
|
|
258
|
+
const sweep = (s.v / total) * Math.PI * 2;
|
|
259
|
+
const a1 = a0 + sweep;
|
|
260
|
+
const large = sweep > Math.PI ? 1 : 0;
|
|
261
|
+
let d;
|
|
262
|
+
if (sweep >= Math.PI * 2 - 1e-6) {
|
|
263
|
+
d = `M${r2(cx - R)} ${r2(cy)}A${r2(R)} ${r2(R)} 0 1 1 ${r2(cx + R)} ${r2(cy)}A${r2(R)} ${r2(R)} 0 1 1 ${r2(cx - R)} ${r2(cy)}Z`;
|
|
264
|
+
if (ri) d += `M${r2(cx - ri)} ${r2(cy)}A${r2(ri)} ${r2(ri)} 0 1 0 ${r2(cx + ri)} ${r2(cy)}A${r2(ri)} ${r2(ri)} 0 1 0 ${r2(cx - ri)} ${r2(cy)}Z`;
|
|
265
|
+
} else {
|
|
266
|
+
const [x0, y0] = pt(R, a0), [x1, y1] = pt(R, a1);
|
|
267
|
+
if (ri) {
|
|
268
|
+
const [u0, v0] = pt(ri, a0), [u1, v1] = pt(ri, a1);
|
|
269
|
+
d = `M${r2(x0)} ${r2(y0)}A${r2(R)} ${r2(R)} 0 ${large} 1 ${r2(x1)} ${r2(y1)}L${r2(u1)} ${r2(v1)}A${r2(ri)} ${r2(ri)} 0 ${large} 0 ${r2(u0)} ${r2(v0)}Z`;
|
|
270
|
+
} else d = `M${r2(cx)} ${r2(cy)}L${r2(x0)} ${r2(y0)}A${r2(R)} ${r2(R)} 0 ${large} 1 ${r2(x1)} ${r2(y1)}Z`;
|
|
271
|
+
}
|
|
272
|
+
const pct = (s.v / total) * 100;
|
|
273
|
+
body += `<path class="d" d="${d}" fill="${s.color}" stroke="${col('bg')}" stroke-width="1.5" fill-rule="evenodd" data-ci="${s.other ? -1 : s.i}" data-si="${si}"${s.other ? ` data-other="${s.count}"` : ''} data-pct="${pct.toFixed(1)}" style="cursor:pointer"><title>${esc(s.label)}: ${esc(tipFmt(s.v))} (${pct.toFixed(1)}%)</title></path>`;
|
|
274
|
+
if (sweep / (Math.PI * 2) >= 0.05) {
|
|
275
|
+
const [lx, ly] = pt(ri ? (R + ri) / 2 : R * 0.64, a0 + sweep / 2);
|
|
276
|
+
body += T(lx, ly + 4, `${pct.toFixed(pct < 10 ? 1 : 0)}%`, { anchor: 'middle', size: 11, weight: 600, color: textOn(s.color) });
|
|
277
|
+
}
|
|
278
|
+
a0 = a1;
|
|
279
|
+
}
|
|
280
|
+
if (ri) {
|
|
281
|
+
body += T(cx, cy - 2, formatter(ser.format, ser.showAs, 'axis')(total), { anchor: 'middle', size: Math.max(12, Math.min(22, R / 4)), fill: 'fg', weight: 700 });
|
|
282
|
+
body += T(cx, cy + 14, 'Total', { anchor: 'middle', size: 11 });
|
|
283
|
+
}
|
|
284
|
+
return wrap(lt.svg + body + note, desc);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// ---------------- cartesian
|
|
288
|
+
const stacked = o.type.startsWith('stacked');
|
|
289
|
+
const horiz = o.type === 'bar' || o.type === 'stackedBar';
|
|
290
|
+
const lineish = o.type === 'line' || o.type === 'area' || o.type === 'stackedArea';
|
|
291
|
+
const entries = model.series.map((s, i) => ({ idx: i, label: s.name, color: s.color }));
|
|
292
|
+
const lt = model.series.length > 1 ? legendTop(entries) : { svg: '', height: pad - 4 };
|
|
293
|
+
const vis = model.series.map((_, i) => i).filter((i) => !hidden.has(i));
|
|
294
|
+
const pts = (s) => model.series[s].points;
|
|
295
|
+
|
|
296
|
+
let lo = 0, hi = 0, any = false;
|
|
297
|
+
if (o.type === 'stackedArea') {
|
|
298
|
+
for (let i = 0; i < N; i++) {
|
|
299
|
+
let acc = 0;
|
|
300
|
+
for (const s of vis) { const v = pts(s)[i]; if (v != null) { any = true; acc += v; } }
|
|
301
|
+
hi = Math.max(hi, acc); lo = Math.min(lo, acc);
|
|
302
|
+
}
|
|
303
|
+
} else if (stacked) {
|
|
304
|
+
for (let i = 0; i < N; i++) {
|
|
305
|
+
let pos = 0, neg = 0;
|
|
306
|
+
for (const s of vis) { const v = pts(s)[i]; if (v == null) continue; any = true; if (v >= 0) pos += v; else neg += v; }
|
|
307
|
+
hi = Math.max(hi, pos); lo = Math.min(lo, neg);
|
|
308
|
+
}
|
|
309
|
+
} else {
|
|
310
|
+
let mn = Infinity, mx = -Infinity;
|
|
311
|
+
for (const s of vis) for (const v of pts(s)) if (v != null) { any = true; if (v < mn) mn = v; if (v > mx) mx = v; }
|
|
312
|
+
if (any) {
|
|
313
|
+
if (o.type === 'line') { lo = mn; hi = mx; if (mn >= 0 && mn <= 0.6 * mx) lo = 0; if (mx <= 0 && mx >= 0.6 * mn) hi = 0; }
|
|
314
|
+
else { lo = Math.min(0, mn); hi = Math.max(0, mx); }
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const desc = (() => {
|
|
318
|
+
let best = null;
|
|
319
|
+
for (const s of vis) pts(s).forEach((v, i) => { if (v != null && (!best || v > best.v)) best = { v, i, s }; });
|
|
320
|
+
return `${typeName} chart of ${model.title}. ${N} categories, ${model.series.length} series.` + (best ? ` Largest value: ${formatPoint(model, best.s, best.v)} (${model.categories[best.i].label}${model.series.length > 1 ? ', ' + model.series[best.s].name : ''}).` : '');
|
|
321
|
+
})();
|
|
322
|
+
if (!any) return wrap(lt.svg + T(W / 2, lt.height + (H - lt.height) / 2, vis.length ? 'No data to chart' : 'All series hidden - click the legend to show one', { anchor: 'middle', size: 13 }), desc);
|
|
323
|
+
|
|
324
|
+
const ticks = niceTicks(lo, hi, 5);
|
|
325
|
+
const d0 = ticks[0], d1 = ticks[ticks.length - 1];
|
|
326
|
+
const f0 = vis[0];
|
|
327
|
+
const axisFmt = formatter(model.series[f0].format, model.series[f0].showAs, 'axis');
|
|
328
|
+
const tickLabels = ticks.map((t) => axisFmt(t));
|
|
329
|
+
const catLabels = model.categories.map((c) => c.label);
|
|
330
|
+
const maxCat = Math.max(...catLabels.map((s) => s.length));
|
|
331
|
+
|
|
332
|
+
const top = lt.height + 6;
|
|
333
|
+
let left, bottom;
|
|
334
|
+
if (horiz) {
|
|
335
|
+
const lw = Math.min(160, maxCat * 6 + 14);
|
|
336
|
+
left = Math.max(40, lw);
|
|
337
|
+
bottom = 26 + noteH;
|
|
338
|
+
} else {
|
|
339
|
+
left = Math.max(36, Math.max(...tickLabels.map((s) => s.length)) * 6.2 + 14);
|
|
340
|
+
bottom = 0; // set below
|
|
341
|
+
}
|
|
342
|
+
const right = pad + (lineish ? 4 : 0) + (horiz ? 14 : 0); // room for the last (centered) tick label of a bar chart
|
|
343
|
+
const plotW = W - left - right;
|
|
344
|
+
let band, rotate = false, step = 1;
|
|
345
|
+
if (!horiz) {
|
|
346
|
+
band = plotW / N;
|
|
347
|
+
rotate = maxCat * 6 > band - 4;
|
|
348
|
+
const lab = rotate ? Math.min(maxCat, 16) : maxCat;
|
|
349
|
+
bottom = (rotate ? lab * 4.6 + 16 : 26) + noteH;
|
|
350
|
+
step = rotate ? Math.max(1, Math.ceil(13 / band)) : Math.max(1, Math.ceil((lab * 6 + 6) / band));
|
|
351
|
+
}
|
|
352
|
+
const plotH = H - top - bottom;
|
|
353
|
+
if (plotW < 20 || plotH < 20) return empty('Chart is too small');
|
|
354
|
+
if (horiz) { band = plotH / N; step = Math.max(1, Math.ceil(13 / band)); }
|
|
355
|
+
const L = left, Rr = left + plotW, Tt = top, B = top + plotH;
|
|
356
|
+
const vpos = (v) => (horiz ? L + ((v - d0) / (d1 - d0)) * plotW : B - ((v - d0) / (d1 - d0)) * plotH);
|
|
357
|
+
const cpos = (i) => (horiz ? Tt + i * band : L + i * band);
|
|
358
|
+
const zero = vpos(Math.max(d0, Math.min(0, d1)));
|
|
359
|
+
|
|
360
|
+
let g = '';
|
|
361
|
+
// grid + value axis
|
|
362
|
+
ticks.forEach((t, k) => {
|
|
363
|
+
const p = vpos(t);
|
|
364
|
+
if (horiz) {
|
|
365
|
+
g += `<line x1="${r2(p)}" y1="${r2(Tt)}" x2="${r2(p)}" y2="${r2(B)}" stroke="${col('line')}" stroke-width="1"/>`;
|
|
366
|
+
g += T(p, B + 15, tickLabels[k], { anchor: 'middle' });
|
|
367
|
+
} else {
|
|
368
|
+
g += `<line x1="${r2(L)}" y1="${r2(p)}" x2="${r2(Rr)}" y2="${r2(p)}" stroke="${col('line')}" stroke-width="1"/>`;
|
|
369
|
+
g += T(L - 6, p + 4, tickLabels[k], { anchor: 'end' });
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
// zero baseline
|
|
373
|
+
g += horiz
|
|
374
|
+
? `<line x1="${r2(zero)}" y1="${r2(Tt)}" x2="${r2(zero)}" y2="${r2(B)}" stroke="${col('muted')}" stroke-width="1"/>`
|
|
375
|
+
: `<line x1="${r2(L)}" y1="${r2(zero)}" x2="${r2(Rr)}" y2="${r2(zero)}" stroke="${col('muted')}" stroke-width="1"/>`;
|
|
376
|
+
// category labels
|
|
377
|
+
for (let i = 0; i < N; i += step) {
|
|
378
|
+
const full = catLabels[i];
|
|
379
|
+
const c = cpos(i) + band / 2;
|
|
380
|
+
if (horiz) {
|
|
381
|
+
const mc = Math.floor((left - 12) / 6);
|
|
382
|
+
g += T(L - 6, c + 4, trunc(full, mc), { anchor: 'end', fill: 'fg', title: full.length > mc ? full : undefined });
|
|
383
|
+
} else if (rotate) {
|
|
384
|
+
g += T(c + 3, B + 14, trunc(full, 16), { anchor: 'end', fill: 'fg', rotate: -40, title: full.length > 16 ? full : undefined });
|
|
385
|
+
} else g += T(c, B + 16, full, { anchor: 'middle', fill: 'fg' });
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const datum = (tag, attrs, i, s, title) => `<${tag} class="d" ${attrs} data-ci="${i}" data-si="${s}" style="cursor:pointer"><title>${esc(title)}</title></${tag}>`;
|
|
389
|
+
const tip = (i, s, v) => `${catLabels[i]}${model.series.length > 1 ? ' · ' + model.series[s].name : ''}: ${formatPoint(model, s, v)}`;
|
|
390
|
+
|
|
391
|
+
if (!lineish) {
|
|
392
|
+
const nv = vis.length;
|
|
393
|
+
if (stacked) {
|
|
394
|
+
const bw = Math.min(band * 0.66, 64);
|
|
395
|
+
for (let i = 0; i < N; i++) {
|
|
396
|
+
let pos = 0, neg = 0;
|
|
397
|
+
for (const s of vis) {
|
|
398
|
+
const v = pts(s)[i];
|
|
399
|
+
if (v == null || v === 0) continue;
|
|
400
|
+
const a = v >= 0 ? pos : neg, b = a + v;
|
|
401
|
+
if (v >= 0) pos = b; else neg = b;
|
|
402
|
+
const p0 = vpos(a), p1 = vpos(b);
|
|
403
|
+
const off = cpos(i) + (band - bw) / 2;
|
|
404
|
+
const attrs = horiz
|
|
405
|
+
? `x="${r2(Math.min(p0, p1))}" y="${r2(off)}" width="${r2(Math.max(1, Math.abs(p1 - p0)))}" height="${r2(bw)}"`
|
|
406
|
+
: `x="${r2(off)}" y="${r2(Math.min(p0, p1))}" width="${r2(bw)}" height="${r2(Math.max(1, Math.abs(p1 - p0)))}"`;
|
|
407
|
+
g += datum('rect', `${attrs} fill="${model.series[s].color}" stroke="${col('bg')}" stroke-width="0.75"`, i, s, tip(i, s, v));
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
} else {
|
|
411
|
+
const group = nv === 1 ? Math.min(band * 0.66, 56) : Math.min(band * 0.8, nv * 44);
|
|
412
|
+
const gap = nv > 1 ? 2 : 0;
|
|
413
|
+
const bw = Math.max(1, (group - (nv - 1) * gap) / nv);
|
|
414
|
+
vis.forEach((s, k) => {
|
|
415
|
+
for (let i = 0; i < N; i++) {
|
|
416
|
+
const v = pts(s)[i];
|
|
417
|
+
if (v == null) continue;
|
|
418
|
+
const off = cpos(i) + (band - group) / 2 + k * (bw + gap);
|
|
419
|
+
const p = vpos(v);
|
|
420
|
+
const a = Math.min(p, zero), len = Math.max(v === 0 ? 0 : 1, Math.abs(p - zero));
|
|
421
|
+
const attrs = horiz
|
|
422
|
+
? `x="${r2(v >= 0 ? zero : zero - len)}" y="${r2(off)}" width="${r2(len)}" height="${r2(bw)}"`
|
|
423
|
+
: `x="${r2(off)}" y="${r2(a)}" width="${r2(bw)}" height="${r2(len)}"`;
|
|
424
|
+
g += datum('rect', `${attrs} rx="2" fill="${model.series[s].color}"`, i, s, tip(i, s, v));
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
} else {
|
|
429
|
+
const cx = (i) => L + i * band + band / 2;
|
|
430
|
+
const acc = new Array(N).fill(0);
|
|
431
|
+
const baseY = vpos(Math.max(d0, Math.min(0, d1)));
|
|
432
|
+
const dots = N * vis.length <= 400 ? 3.5 : 2.2;
|
|
433
|
+
vis.forEach((s) => {
|
|
434
|
+
const col_ = model.series[s].color;
|
|
435
|
+
const ys = pts(s).map((v, i) => {
|
|
436
|
+
if (o.type === 'stackedArea') { if (v != null) acc[i] += v; return { v: v == null ? null : acc[i], raw: v, prev: acc[i] - (v ?? 0) }; }
|
|
437
|
+
return { v, raw: v };
|
|
438
|
+
});
|
|
439
|
+
// split into contiguous runs at gaps (stacked areas never gap: a null contributes 0)
|
|
440
|
+
const runs = [];
|
|
441
|
+
let cur = [];
|
|
442
|
+
ys.forEach((p, i) => { if (p.v == null && o.type !== 'stackedArea') { if (cur.length) runs.push(cur); cur = []; } else cur.push({ i, ...p, v: p.v ?? acc[i] }); });
|
|
443
|
+
if (cur.length) runs.push(cur);
|
|
444
|
+
for (const run of runs) {
|
|
445
|
+
const pathPts = run.map((p, k) => `${k ? 'L' : 'M'}${r2(cx(p.i))} ${r2(vpos(p.v))}`).join('');
|
|
446
|
+
if (o.type === 'area' || o.type === 'stackedArea') {
|
|
447
|
+
let lower;
|
|
448
|
+
if (o.type === 'stackedArea') lower = [...run].reverse().map((p) => `L${r2(cx(p.i))} ${r2(vpos(p.prev))}`).join('');
|
|
449
|
+
else lower = `L${r2(cx(run[run.length - 1].i))} ${r2(baseY)}L${r2(cx(run[0].i))} ${r2(baseY)}`;
|
|
450
|
+
g += `<path d="${pathPts}${lower}Z" fill="${col_}" fill-opacity="${o.type === 'area' ? 0.22 : 0.55}" stroke="none"/>`;
|
|
451
|
+
}
|
|
452
|
+
if (run.length > 1) g += `<path d="${pathPts}" fill="none" stroke="${col_}" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/>`;
|
|
453
|
+
}
|
|
454
|
+
ys.forEach((p, i) => {
|
|
455
|
+
if (p.raw == null) return;
|
|
456
|
+
g += datum('circle', `cx="${r2(cx(i))}" cy="${r2(vpos(p.v))}" r="${dots}" fill="${col_}" stroke="transparent" stroke-width="12" paint-order="stroke"`, i, s, tip(i, s, p.raw));
|
|
457
|
+
});
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
return wrap(lt.svg + g + note, desc);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// ------------------------------------------------------------------------------------------------
|
|
464
|
+
// live chart
|
|
465
|
+
// ------------------------------------------------------------------------------------------------
|
|
466
|
+
const MODEL_KEYS = ['rowLevel', 'colLevel', 'valueIndex', 'maxCategories', 'maxSeries'];
|
|
467
|
+
|
|
468
|
+
export class PivotChart extends Emitter {
|
|
469
|
+
/**
|
|
470
|
+
* @param {HTMLElement|string} container
|
|
471
|
+
* @param {{type?:string, rowLevel?:number|'leaf', colLevel?:number|'leaf', valueIndex?:number,
|
|
472
|
+
* maxCategories?:number, maxSeries?:number, hidden?:number[], seriesIndex?:number, height?:number|string}} [opts]
|
|
473
|
+
*/
|
|
474
|
+
constructor(container, opts = {}) {
|
|
475
|
+
super();
|
|
476
|
+
this.el = typeof container === 'string' ? document.querySelector(container) : container;
|
|
477
|
+
if (!this.el) throw new Error('PivotChart: container not found');
|
|
478
|
+
this.opts = { type: 'column', hidden: [], ...opts };
|
|
479
|
+
this.result = opts.result || null;
|
|
480
|
+
this.model = null;
|
|
481
|
+
const root = (this.root = document.createElement('div'));
|
|
482
|
+
root.className = 'mp-chart';
|
|
483
|
+
root.style.cssText = `position:relative;width:100%;height:${typeof this.opts.height === 'number' ? this.opts.height + 'px' : this.opts.height || '100%'};min-height:200px;overflow:hidden;color:var(--mp-fg,${THEMES.light.fg});`;
|
|
484
|
+
root.innerHTML = '<div class="mp-chart-svg" style="position:absolute;inset:0"></div><div class="mp-chart-tip" style="position:absolute;display:none;pointer-events:none;z-index:5;white-space:nowrap;padding:6px 9px;border-radius:6px;font:12px/1.4 system-ui,sans-serif;box-shadow:0 4px 14px rgba(0,0,0,.25);background:var(--mp-bg,#fff);color:var(--mp-fg,#1f2430);border:1px solid var(--mp-line,#e3e6ec)"></div>';
|
|
485
|
+
this.el.appendChild(root);
|
|
486
|
+
this.$svg = root.firstChild;
|
|
487
|
+
this.$tip = root.lastChild;
|
|
488
|
+
this._ro = typeof ResizeObserver !== 'undefined' ? new ResizeObserver(() => this._schedule()) : null;
|
|
489
|
+
if (this._ro) this._ro.observe(root);
|
|
490
|
+
root.addEventListener('mousemove', (e) => this._hover(e));
|
|
491
|
+
root.addEventListener('mouseleave', () => { this.$tip.style.display = 'none'; });
|
|
492
|
+
root.addEventListener('click', (e) => this._click(e));
|
|
493
|
+
if (this.result) this.setResult(this.result);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
setResult(result) {
|
|
497
|
+
this.result = result;
|
|
498
|
+
this.model = result ? buildChartModel(result, this.opts) : null;
|
|
499
|
+
this.render();
|
|
500
|
+
}
|
|
501
|
+
setOptions(o) {
|
|
502
|
+
const rebuild = MODEL_KEYS.some((k) => k in o && o[k] !== this.opts[k]);
|
|
503
|
+
Object.assign(this.opts, o);
|
|
504
|
+
if (rebuild && this.result) this.model = buildChartModel(this.result, this.opts);
|
|
505
|
+
this.render();
|
|
506
|
+
}
|
|
507
|
+
getModel() { return this.model; }
|
|
508
|
+
destroy() {
|
|
509
|
+
if (this._ro) this._ro.disconnect();
|
|
510
|
+
if (this._raf) cancelAnimationFrame(this._raf);
|
|
511
|
+
this.root.remove();
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
_size() {
|
|
515
|
+
return { w: this.root.clientWidth || 720, h: this.root.clientHeight || 360 };
|
|
516
|
+
}
|
|
517
|
+
_schedule() {
|
|
518
|
+
if (this._raf) return;
|
|
519
|
+
this._raf = requestAnimationFrame(() => { this._raf = 0; this.render(); });
|
|
520
|
+
}
|
|
521
|
+
render() {
|
|
522
|
+
if (!this.model) { this.$svg.innerHTML = ''; return; }
|
|
523
|
+
const { w, h } = this._size();
|
|
524
|
+
this._last = `${w}x${h}`;
|
|
525
|
+
this.$svg.innerHTML = renderSVG(this.model, { ...this.opts, width: w, height: h });
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/** Standalone SVG (literal colors, background, xmlns) for download/embedding. */
|
|
529
|
+
toSVG() {
|
|
530
|
+
const { w, h } = this._size();
|
|
531
|
+
const colors = {};
|
|
532
|
+
if (typeof getComputedStyle === 'function') {
|
|
533
|
+
const cs = getComputedStyle(this.root);
|
|
534
|
+
for (const k of ['fg', 'muted', 'line', 'bg']) {
|
|
535
|
+
const v = cs.getPropertyValue(`--mp-${k}`).trim();
|
|
536
|
+
if (v) colors[k] = v;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return renderSVG(this.model || { categories: [], series: [], title: 'Chart', truncated: {} }, { ...this.opts, width: w, height: h, inline: true, colors });
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
_pick(e) {
|
|
543
|
+
const t = e.target.closest && e.target.closest('[data-ci]');
|
|
544
|
+
if (!t || !this.model) return null;
|
|
545
|
+
const ci = +t.dataset.ci, si = +t.dataset.si;
|
|
546
|
+
return { t, ci, si, other: t.dataset.other, pct: t.dataset.pct };
|
|
547
|
+
}
|
|
548
|
+
_hover(e) {
|
|
549
|
+
const p = this._pick(e);
|
|
550
|
+
const tip = this.$tip;
|
|
551
|
+
if (!p) { tip.style.display = 'none'; return; }
|
|
552
|
+
const m = this.model, ser = m.series[p.si];
|
|
553
|
+
const pie = this.opts.type === 'pie' || this.opts.type === 'donut';
|
|
554
|
+
let html;
|
|
555
|
+
if (p.other) {
|
|
556
|
+
// the "Other" slice has no single category; its <title> already carries label, value and share
|
|
557
|
+
html = `<b>${esc(p.t.querySelector('title').textContent)}</b>`;
|
|
558
|
+
} else {
|
|
559
|
+
const v = ser.points[p.ci];
|
|
560
|
+
html = `<b>${esc(m.categories[p.ci].label)}</b><br><span style="display:inline-block;width:9px;height:9px;border-radius:2px;background:${ser.color};margin-right:5px"></span>${esc(ser.name)}: ${esc(formatPoint(m, p.si, v))}`;
|
|
561
|
+
let pct = null;
|
|
562
|
+
if (pie) pct = p.pct;
|
|
563
|
+
else if (this.opts.type.startsWith('stacked')) {
|
|
564
|
+
const hid = new Set(this.opts.hidden || []);
|
|
565
|
+
const tot = m.series.reduce((a, s, k) => a + (hid.has(k) || s.points[p.ci] == null ? 0 : s.points[p.ci]), 0);
|
|
566
|
+
if (tot) pct = ((v / tot) * 100).toFixed(1);
|
|
567
|
+
}
|
|
568
|
+
if (pct !== null) html += ` <span style="color:var(--mp-muted,#6b7280)">(${pct}%)</span>`;
|
|
569
|
+
}
|
|
570
|
+
tip.innerHTML = html;
|
|
571
|
+
tip.style.display = 'block';
|
|
572
|
+
const rr = this.root.getBoundingClientRect();
|
|
573
|
+
let x = e.clientX - rr.left + 14, y = e.clientY - rr.top + 14;
|
|
574
|
+
if (x + tip.offsetWidth > rr.width - 4) x = Math.max(4, e.clientX - rr.left - tip.offsetWidth - 14);
|
|
575
|
+
if (y + tip.offsetHeight > rr.height - 4) y = Math.max(4, e.clientY - rr.top - tip.offsetHeight - 14);
|
|
576
|
+
tip.style.left = x + 'px';
|
|
577
|
+
tip.style.top = y + 'px';
|
|
578
|
+
}
|
|
579
|
+
_click(e) {
|
|
580
|
+
const lg = e.target.closest && e.target.closest('[data-lg]');
|
|
581
|
+
if (lg) {
|
|
582
|
+
const idx = +lg.dataset.lg;
|
|
583
|
+
const hid = new Set(this.opts.hidden || []);
|
|
584
|
+
if (hid.has(idx)) hid.delete(idx); else hid.add(idx);
|
|
585
|
+
this.opts.hidden = [...hid];
|
|
586
|
+
this.render();
|
|
587
|
+
this.emit('legendtoggle', { hidden: this.opts.hidden });
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
const p = this._pick(e);
|
|
591
|
+
if (!p || p.other) return;
|
|
592
|
+
const c = this.model.categories[p.ci], s = this.model.series[p.si];
|
|
593
|
+
this.emit('pointclick', { category: c.label, series: s.name, value: s.points[p.ci], rowNode: c.node, colNode: s.colNode });
|
|
594
|
+
}
|
|
595
|
+
}
|