@trismegisto/charts 3.3.13 → 3.4.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 +36 -25
- package/dist/core/bar-chart.d.ts +12 -0
- package/dist/core/bar-chart.js +30 -0
- package/dist/core/bar-chart.js.map +1 -0
- package/dist/core/boxplot-chart.d.ts +15 -0
- package/dist/core/boxplot-chart.js +45 -0
- package/dist/core/boxplot-chart.js.map +1 -0
- package/dist/core/bullet-chart.d.ts +29 -0
- package/dist/core/bullet-chart.js +95 -0
- package/dist/core/bullet-chart.js.map +1 -0
- package/dist/core/calendar-heatmap.d.ts +18 -0
- package/dist/core/calendar-heatmap.js +79 -0
- package/dist/core/calendar-heatmap.js.map +1 -0
- package/dist/core/chart-host.d.ts +19 -0
- package/dist/core/chart-host.js +109 -0
- package/dist/core/chart-host.js.map +1 -0
- package/dist/core/charts-i18n.d.ts +25 -0
- package/dist/core/charts-i18n.js +33 -0
- package/dist/core/charts-i18n.js.map +1 -0
- package/dist/core/charts-messages.d.ts +88 -0
- package/dist/core/charts-messages.en.d.ts +2 -0
- package/dist/core/charts-messages.en.js +106 -0
- package/dist/core/charts-messages.en.js.map +1 -0
- package/dist/core/charts-messages.es.d.ts +2 -0
- package/dist/core/charts-messages.es.js +106 -0
- package/dist/core/charts-messages.es.js.map +1 -0
- package/dist/core/charts-messages.js +106 -0
- package/dist/core/charts-messages.js.map +1 -0
- package/dist/core/context.d.ts +22 -0
- package/dist/core/context.js +13 -0
- package/dist/core/context.js.map +1 -0
- package/dist/core/engine-locale.d.ts +3 -0
- package/dist/core/engine-locale.js +59 -0
- package/dist/core/engine-locale.js.map +1 -0
- package/dist/core/format.d.ts +13 -0
- package/dist/core/format.js +64 -0
- package/dist/core/format.js.map +1 -0
- package/dist/core/frame.d.ts +19 -0
- package/dist/core/frame.js +28 -0
- package/dist/core/frame.js.map +1 -0
- package/dist/core/funnel-chart.d.ts +8 -0
- package/dist/core/funnel-chart.js +33 -0
- package/dist/core/funnel-chart.js.map +1 -0
- package/dist/core/gauge-chart.d.ts +44 -0
- package/dist/core/gauge-chart.js +218 -0
- package/dist/core/gauge-chart.js.map +1 -0
- package/dist/core/gauge-layout.d.ts +67 -0
- package/dist/core/gauge-layout.js +140 -0
- package/dist/core/gauge-layout.js.map +1 -0
- package/dist/core/heatmap-chart.d.ts +17 -0
- package/dist/core/heatmap-chart.js +55 -0
- package/dist/core/heatmap-chart.js.map +1 -0
- package/dist/core/index.d.ts +32 -0
- package/dist/core/index.js +38 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/line-chart.d.ts +12 -0
- package/dist/core/line-chart.js +29 -0
- package/dist/core/line-chart.js.map +1 -0
- package/dist/core/navigation.d.ts +25 -0
- package/dist/core/navigation.js +57 -0
- package/dist/core/navigation.js.map +1 -0
- package/dist/core/observe.d.ts +28 -0
- package/dist/core/observe.js +115 -0
- package/dist/core/observe.js.map +1 -0
- package/dist/core/options.d.ts +27 -0
- package/dist/core/options.js +64 -0
- package/dist/core/options.js.map +1 -0
- package/dist/core/pie-chart.d.ts +9 -0
- package/dist/core/pie-chart.js +34 -0
- package/dist/core/pie-chart.js.map +1 -0
- package/dist/core/radar-chart.d.ts +18 -0
- package/dist/core/radar-chart.js +34 -0
- package/dist/core/radar-chart.js.map +1 -0
- package/dist/core/sankey-chart.d.ts +15 -0
- package/dist/core/sankey-chart.js +32 -0
- package/dist/core/sankey-chart.js.map +1 -0
- package/dist/core/scatter-chart.d.ts +33 -0
- package/dist/core/scatter-chart.js +62 -0
- package/dist/core/scatter-chart.js.map +1 -0
- package/dist/core/sparkline-chart.d.ts +20 -0
- package/dist/core/sparkline-chart.js +60 -0
- package/dist/core/sparkline-chart.js.map +1 -0
- package/dist/core/stats.d.ts +12 -0
- package/dist/core/stats.js +23 -0
- package/dist/core/stats.js.map +1 -0
- package/dist/core/theme.d.ts +8 -0
- package/dist/core/theme.js +171 -0
- package/dist/core/theme.js.map +1 -0
- package/dist/core/treemap-chart.d.ts +21 -0
- package/dist/core/treemap-chart.js +45 -0
- package/dist/core/treemap-chart.js.map +1 -0
- package/dist/core/types.d.ts +10 -0
- package/dist/core/types.js +2 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/view.d.ts +24 -0
- package/dist/core/view.js +2 -0
- package/dist/core/view.js.map +1 -0
- package/dist/core/waterfall-chart.d.ts +32 -0
- package/dist/core/waterfall-chart.js +115 -0
- package/dist/core/waterfall-chart.js.map +1 -0
- package/dist/engine/aria.d.ts +8 -0
- package/dist/engine/aria.js +70 -0
- package/dist/engine/aria.js.map +1 -0
- package/dist/engine/component/legend.d.ts +11 -0
- package/dist/engine/component/legend.js +349 -0
- package/dist/engine/component/legend.js.map +1 -0
- package/dist/engine/component/text-style.d.ts +6 -0
- package/dist/engine/component/text-style.js +26 -0
- package/dist/engine/component/text-style.js.map +1 -0
- package/dist/engine/component/title.d.ts +2 -0
- package/dist/engine/component/title.js +90 -0
- package/dist/engine/component/title.js.map +1 -0
- package/dist/engine/component/tooltip.d.ts +40 -0
- package/dist/engine/component/tooltip.js +418 -0
- package/dist/engine/component/tooltip.js.map +1 -0
- package/dist/engine/component/visual-map.d.ts +9 -0
- package/dist/engine/component/visual-map.js +381 -0
- package/dist/engine/component/visual-map.js.map +1 -0
- package/dist/engine/contract.d.ts +504 -0
- package/dist/engine/contract.js +10 -0
- package/dist/engine/contract.js.map +1 -0
- package/dist/engine/coord/axis.d.ts +55 -0
- package/dist/engine/coord/axis.js +349 -0
- package/dist/engine/coord/axis.js.map +1 -0
- package/dist/engine/coord/calendar.d.ts +44 -0
- package/dist/engine/coord/calendar.js +282 -0
- package/dist/engine/coord/calendar.js.map +1 -0
- package/dist/engine/coord/cartesian.d.ts +22 -0
- package/dist/engine/coord/cartesian.js +449 -0
- package/dist/engine/coord/cartesian.js.map +1 -0
- package/dist/engine/coord/polar.d.ts +24 -0
- package/dist/engine/coord/polar.js +232 -0
- package/dist/engine/coord/polar.js.map +1 -0
- package/dist/engine/coord/radar.d.ts +90 -0
- package/dist/engine/coord/radar.js +288 -0
- package/dist/engine/coord/radar.js.map +1 -0
- package/dist/engine/index.d.ts +27 -0
- package/dist/engine/index.js +16 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/instance.d.ts +79 -0
- package/dist/engine/instance.js +773 -0
- package/dist/engine/instance.js.map +1 -0
- package/dist/engine/label-layout.d.ts +3 -0
- package/dist/engine/label-layout.js +35 -0
- package/dist/engine/label-layout.js.map +1 -0
- package/dist/engine/locale.d.ts +9 -0
- package/dist/engine/locale.js +154 -0
- package/dist/engine/locale.js.map +1 -0
- package/dist/engine/model.d.ts +26 -0
- package/dist/engine/model.js +307 -0
- package/dist/engine/model.js.map +1 -0
- package/dist/engine/option/axis.d.ts +167 -0
- package/dist/engine/option/axis.js +2 -0
- package/dist/engine/option/axis.js.map +1 -0
- package/dist/engine/option/components.d.ts +424 -0
- package/dist/engine/option/components.js +2 -0
- package/dist/engine/option/components.js.map +1 -0
- package/dist/engine/option.d.ts +59 -0
- package/dist/engine/option.js +2 -0
- package/dist/engine/option.js.map +1 -0
- package/dist/engine/registry.d.ts +9 -0
- package/dist/engine/registry.js +42 -0
- package/dist/engine/registry.js.map +1 -0
- package/dist/engine/render/path.d.ts +26 -0
- package/dist/engine/render/path.js +387 -0
- package/dist/engine/render/path.js.map +1 -0
- package/dist/engine/render/svg.d.ts +60 -0
- package/dist/engine/render/svg.js +624 -0
- package/dist/engine/render/svg.js.map +1 -0
- package/dist/engine/render/text.d.ts +13 -0
- package/dist/engine/render/text.js +144 -0
- package/dist/engine/render/text.js.map +1 -0
- package/dist/engine/scale.d.ts +38 -0
- package/dist/engine/scale.js +170 -0
- package/dist/engine/scale.js.map +1 -0
- package/dist/engine/series/bar.d.ts +65 -0
- package/dist/engine/series/bar.js +417 -0
- package/dist/engine/series/bar.js.map +1 -0
- package/dist/engine/series/boxplot.d.ts +31 -0
- package/dist/engine/series/boxplot.js +125 -0
- package/dist/engine/series/boxplot.js.map +1 -0
- package/dist/engine/series/cartesian-data.d.ts +38 -0
- package/dist/engine/series/cartesian-data.js +159 -0
- package/dist/engine/series/cartesian-data.js.map +1 -0
- package/dist/engine/series/circular-common.d.ts +51 -0
- package/dist/engine/series/circular-common.js +196 -0
- package/dist/engine/series/circular-common.js.map +1 -0
- package/dist/engine/series/circular-options.d.ts +151 -0
- package/dist/engine/series/circular-options.js +2 -0
- package/dist/engine/series/circular-options.js.map +1 -0
- package/dist/engine/series/common.d.ts +51 -0
- package/dist/engine/series/common.js +210 -0
- package/dist/engine/series/common.js.map +1 -0
- package/dist/engine/series/funnel.d.ts +37 -0
- package/dist/engine/series/funnel.js +204 -0
- package/dist/engine/series/funnel.js.map +1 -0
- package/dist/engine/series/gauge.d.ts +106 -0
- package/dist/engine/series/gauge.js +349 -0
- package/dist/engine/series/gauge.js.map +1 -0
- package/dist/engine/series/heatmap.d.ts +31 -0
- package/dist/engine/series/heatmap.js +222 -0
- package/dist/engine/series/heatmap.js.map +1 -0
- package/dist/engine/series/label-fit.d.ts +7 -0
- package/dist/engine/series/label-fit.js +56 -0
- package/dist/engine/series/label-fit.js.map +1 -0
- package/dist/engine/series/line.d.ts +63 -0
- package/dist/engine/series/line.js +347 -0
- package/dist/engine/series/line.js.map +1 -0
- package/dist/engine/series/pie.d.ts +59 -0
- package/dist/engine/series/pie.js +396 -0
- package/dist/engine/series/pie.js.map +1 -0
- package/dist/engine/series/radar.d.ts +34 -0
- package/dist/engine/series/radar.js +168 -0
- package/dist/engine/series/radar.js.map +1 -0
- package/dist/engine/series/sankey.d.ts +64 -0
- package/dist/engine/series/sankey.js +486 -0
- package/dist/engine/series/sankey.js.map +1 -0
- package/dist/engine/series/scatter.d.ts +38 -0
- package/dist/engine/series/scatter.js +167 -0
- package/dist/engine/series/scatter.js.map +1 -0
- package/dist/engine/series/treemap.d.ts +77 -0
- package/dist/engine/series/treemap.js +493 -0
- package/dist/engine/series/treemap.js.map +1 -0
- package/dist/engine/theme.d.ts +9 -0
- package/dist/engine/theme.js +84 -0
- package/dist/engine/theme.js.map +1 -0
- package/dist/engine/util.d.ts +68 -0
- package/dist/engine/util.js +269 -0
- package/dist/engine/util.js.map +1 -0
- package/package.json +35 -24
- package/fesm2022/trismegisto-charts.mjs +0 -12428
- package/fesm2022/trismegisto-charts.mjs.map +0 -1
- package/types/trismegisto-charts.d.ts +0 -1970
- package/types/trismegisto-charts.d.ts.map +0 -1
|
@@ -0,0 +1,773 @@
|
|
|
1
|
+
import { decalFor, describeChart } from './aria';
|
|
2
|
+
import { nextLegendSelection, scrollLegend } from './component/legend';
|
|
3
|
+
import { TooltipController } from './component/tooltip';
|
|
4
|
+
import { createVisualResolver, toggleVisualPiece } from './component/visual-map';
|
|
5
|
+
import { layoutLabels } from './label-layout';
|
|
6
|
+
import { createLocale, languageOf } from './locale';
|
|
7
|
+
import { dataCount, mergeOption, normalizeOption } from './model';
|
|
8
|
+
import { COMPONENT_TYPES, COORDINATE_TYPES, SERIES_TYPES } from './registry';
|
|
9
|
+
import { SvgRenderer } from './render/svg';
|
|
10
|
+
import { measureText } from './render/text';
|
|
11
|
+
import { buildTheme } from './theme';
|
|
12
|
+
import { clone, isPlainObject, toArray } from './util';
|
|
13
|
+
const EASINGS = ['linear', 'cubicOut', 'cubicInOut', 'quadraticOut', 'elasticOut', 'backOut', 'exponentialOut'];
|
|
14
|
+
const FALLBACK_WIDTH = 600;
|
|
15
|
+
const FALLBACK_HEIGHT = 400;
|
|
16
|
+
const coordKey = (type, index) => `${type}:${index}`;
|
|
17
|
+
function easingOf(value, fallback) {
|
|
18
|
+
return EASINGS.includes(value) ? value : fallback;
|
|
19
|
+
}
|
|
20
|
+
function prefersReducedMotion() {
|
|
21
|
+
try {
|
|
22
|
+
return typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export class ChartInstanceImpl {
|
|
29
|
+
dom;
|
|
30
|
+
raw = {};
|
|
31
|
+
theme;
|
|
32
|
+
locale;
|
|
33
|
+
renderer;
|
|
34
|
+
tooltip;
|
|
35
|
+
width;
|
|
36
|
+
height;
|
|
37
|
+
fixedWidth;
|
|
38
|
+
fixedHeight;
|
|
39
|
+
legendSelected = {};
|
|
40
|
+
legendInit = false;
|
|
41
|
+
highlights = new Map();
|
|
42
|
+
hover = null;
|
|
43
|
+
pointer = null;
|
|
44
|
+
componentState = new Map();
|
|
45
|
+
listeners = [];
|
|
46
|
+
frame = null;
|
|
47
|
+
renderTicket = 0;
|
|
48
|
+
animateNext = true;
|
|
49
|
+
disposed = false;
|
|
50
|
+
constructor(dom, theme, opts = {}) {
|
|
51
|
+
this.dom = dom;
|
|
52
|
+
this.theme = buildTheme(theme);
|
|
53
|
+
this.locale = createLocale(opts.locale ?? null, languageOf(dom));
|
|
54
|
+
this.fixedWidth = opts.width;
|
|
55
|
+
this.fixedHeight = opts.height;
|
|
56
|
+
this.width = this.measureWidth();
|
|
57
|
+
this.height = this.measureHeight();
|
|
58
|
+
if (getComputedStyleSafe(dom, 'position') === 'static')
|
|
59
|
+
dom.style.position = 'relative';
|
|
60
|
+
this.renderer = new SvgRenderer(dom, {
|
|
61
|
+
onPointer: (item, x, y, e) => this.onPointer(item, x, y, e),
|
|
62
|
+
onClick: (item, x, y, e) => this.onClick(item, x, y, e),
|
|
63
|
+
onLeave: (e) => this.onLeave(e),
|
|
64
|
+
});
|
|
65
|
+
this.renderer.onFinished = () => this.emit('finished', { componentType: 'series', type: 'finished' });
|
|
66
|
+
this.renderer.setSize(this.width, this.height);
|
|
67
|
+
this.tooltip = new TooltipController(dom, () => this.currentCtx(), {
|
|
68
|
+
paramsOf: (model, dataIndex) => SERIES_TYPES.get(model.type)?.tooltipParams(model, dataIndex, this.currentCtx()) ?? null,
|
|
69
|
+
itemsAtAxisValue: (model, axisDim, value) => SERIES_TYPES.get(model.type)?.itemsAtAxisValue?.(model, axisDim, value, this.currentCtx()) ?? [],
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
// ---------------------------------------------------------------- tamanho
|
|
73
|
+
measureWidth() {
|
|
74
|
+
return this.fixedWidth ?? (this.dom.clientWidth || FALLBACK_WIDTH);
|
|
75
|
+
}
|
|
76
|
+
measureHeight() {
|
|
77
|
+
return this.fixedHeight ?? (this.dom.clientHeight || FALLBACK_HEIGHT);
|
|
78
|
+
}
|
|
79
|
+
getWidth() {
|
|
80
|
+
return this.width;
|
|
81
|
+
}
|
|
82
|
+
getHeight() {
|
|
83
|
+
return this.height;
|
|
84
|
+
}
|
|
85
|
+
getDom() {
|
|
86
|
+
return this.dom;
|
|
87
|
+
}
|
|
88
|
+
resize(opts = {}) {
|
|
89
|
+
if (this.disposed)
|
|
90
|
+
return;
|
|
91
|
+
this.width = typeof opts.width === 'number' ? opts.width : this.dom.clientWidth || this.fixedWidth || this.width;
|
|
92
|
+
this.height = typeof opts.height === 'number' ? opts.height : this.dom.clientHeight || this.fixedHeight || this.height;
|
|
93
|
+
this.renderer.setSize(this.width, this.height);
|
|
94
|
+
this.animateNext = !!opts.animation?.duration;
|
|
95
|
+
this.render();
|
|
96
|
+
this.emit('resize', { componentType: 'series', type: 'resize' });
|
|
97
|
+
}
|
|
98
|
+
// ---------------------------------------------------------------- opção
|
|
99
|
+
setOption(option, opts) {
|
|
100
|
+
if (this.disposed || !isPlainObject(option))
|
|
101
|
+
return;
|
|
102
|
+
const o = typeof opts === 'boolean' ? { notMerge: opts } : (opts ?? {});
|
|
103
|
+
const replace = toArray(o.replaceMerge);
|
|
104
|
+
this.raw = o.notMerge ? clone(option) : mergeOption(this.raw, option, replace);
|
|
105
|
+
if (o.notMerge) {
|
|
106
|
+
this.highlights.clear();
|
|
107
|
+
this.pointer = null;
|
|
108
|
+
}
|
|
109
|
+
// A seleção da legenda que a opção traz vale como ponto de partida (e quando a opção muda essa seleção).
|
|
110
|
+
const legendSel = toArray(option['legend'])[0]?.['selected'];
|
|
111
|
+
if (isPlainObject(legendSel))
|
|
112
|
+
this.legendSelected = { ...legendSel };
|
|
113
|
+
else if (!this.legendInit)
|
|
114
|
+
this.legendSelected = {};
|
|
115
|
+
this.legendInit = true;
|
|
116
|
+
this.animateNext = true;
|
|
117
|
+
if (o.lazyUpdate)
|
|
118
|
+
this.requestRender();
|
|
119
|
+
else
|
|
120
|
+
this.render();
|
|
121
|
+
}
|
|
122
|
+
getOption() {
|
|
123
|
+
const frame = this.frame ?? this.build();
|
|
124
|
+
return clone(frame.normalized.option);
|
|
125
|
+
}
|
|
126
|
+
setTheme(theme) {
|
|
127
|
+
this.theme = buildTheme(theme);
|
|
128
|
+
this.animateNext = false;
|
|
129
|
+
this.render();
|
|
130
|
+
}
|
|
131
|
+
clear() {
|
|
132
|
+
this.raw = {};
|
|
133
|
+
this.highlights.clear();
|
|
134
|
+
this.hover = null;
|
|
135
|
+
this.pointer = null;
|
|
136
|
+
this.tooltip.hide();
|
|
137
|
+
this.render();
|
|
138
|
+
}
|
|
139
|
+
isDisposed() {
|
|
140
|
+
return this.disposed;
|
|
141
|
+
}
|
|
142
|
+
dispose() {
|
|
143
|
+
if (this.disposed)
|
|
144
|
+
return;
|
|
145
|
+
this.disposed = true;
|
|
146
|
+
this.tooltip.dispose();
|
|
147
|
+
this.renderer.dispose();
|
|
148
|
+
this.listeners.length = 0;
|
|
149
|
+
instances.delete(this.dom);
|
|
150
|
+
}
|
|
151
|
+
// ---------------------------------------------------------------- quadro
|
|
152
|
+
requestRender() {
|
|
153
|
+
const ticket = ++this.renderTicket;
|
|
154
|
+
const run = () => {
|
|
155
|
+
if (ticket === this.renderTicket && !this.disposed)
|
|
156
|
+
this.render();
|
|
157
|
+
};
|
|
158
|
+
if (typeof requestAnimationFrame === 'function')
|
|
159
|
+
requestAnimationFrame(run);
|
|
160
|
+
else
|
|
161
|
+
setTimeout(run, 0);
|
|
162
|
+
}
|
|
163
|
+
currentCtx() {
|
|
164
|
+
return (this.frame ?? this.build()).ctx;
|
|
165
|
+
}
|
|
166
|
+
/** Monta o contexto, os modelos e os sistemas de coordenadas do quadro (sem desenhar). */
|
|
167
|
+
build() {
|
|
168
|
+
const normalized = normalizeOption(this.raw, this.theme, { series: SERIES_TYPES, components: COMPONENT_TYPES });
|
|
169
|
+
const option = normalized.option;
|
|
170
|
+
const models = normalized.models;
|
|
171
|
+
const palette = option['color'].filter((c) => typeof c === 'string');
|
|
172
|
+
const aria = option['aria'][0] ?? {};
|
|
173
|
+
const animationOn = option['animation'] !== false && !prefersReducedMotion() && dataCount(models) <= Number(option['animationThreshold'] ?? 2000);
|
|
174
|
+
const delayOpt = option['animationDelay'];
|
|
175
|
+
const coords = new Map();
|
|
176
|
+
const legendSelected = (name) => this.legendSelected[name] !== false;
|
|
177
|
+
const visible = models.filter((m) => {
|
|
178
|
+
const type = SERIES_TYPES.get(m.type);
|
|
179
|
+
// Séries que dão a legenda por item (pizza, funil) filtram os itens; as outras somem inteiras.
|
|
180
|
+
return type?.legendEntries ? true : legendSelected(m.name);
|
|
181
|
+
});
|
|
182
|
+
const ctx = {
|
|
183
|
+
width: this.width,
|
|
184
|
+
height: this.height,
|
|
185
|
+
theme: this.theme,
|
|
186
|
+
locale: this.locale,
|
|
187
|
+
option,
|
|
188
|
+
models: visible,
|
|
189
|
+
measureText,
|
|
190
|
+
paletteColor: (i) => palette[((i % palette.length) + palette.length) % palette.length] ?? '#5470c6',
|
|
191
|
+
decal: (i) => (aria['decal']?.['show'] ? decalFor(i, toArray(aria['decal']?.['decals'])[i]) : null),
|
|
192
|
+
coordinateSystem: (type, index) => coords.get(coordKey(type, index)),
|
|
193
|
+
state: (si, di) => this.stateOf(si, di, models),
|
|
194
|
+
legendSelected,
|
|
195
|
+
visual: () => undefined,
|
|
196
|
+
animation: {
|
|
197
|
+
enabled: animationOn,
|
|
198
|
+
duration: typeof option['animationDuration'] === 'number' ? option['animationDuration'] : 1000,
|
|
199
|
+
easing: easingOf(option['animationEasing'], 'cubicOut'),
|
|
200
|
+
durationUpdate: typeof option['animationDurationUpdate'] === 'number' ? option['animationDurationUpdate'] : 300,
|
|
201
|
+
delay: (i) => (typeof delayOpt === 'function' ? Number(delayOpt(i)) || 0 : Number(delayOpt) || 0),
|
|
202
|
+
},
|
|
203
|
+
formatNumber: (v) => this.locale.number(v),
|
|
204
|
+
componentState: this.componentState,
|
|
205
|
+
requestRender: () => this.requestRender(),
|
|
206
|
+
};
|
|
207
|
+
ctx.visual = createVisualResolver(option, visible, this.componentState, ctx);
|
|
208
|
+
const frame = { normalized, visible, coords, ctx };
|
|
209
|
+
this.frame = frame;
|
|
210
|
+
// Sistemas de coordenadas: um por componente usado pelas séries (as visíveis dão as extensões).
|
|
211
|
+
for (const [type, coordType] of COORDINATE_TYPES) {
|
|
212
|
+
const users = models.filter((m) => m.coordinateSystem === type);
|
|
213
|
+
const declared = type === 'cartesian2d' ? option['xAxis'] || option['grid'] : type === 'polar' ? option['polar'] : type === 'radar' ? option['radar'] : option['calendar'];
|
|
214
|
+
if (!users.length && !declared)
|
|
215
|
+
continue;
|
|
216
|
+
try {
|
|
217
|
+
for (const coord of coordType.create(option, visible.filter((m) => m.coordinateSystem === type), ctx))
|
|
218
|
+
coords.set(coordKey(type, coord.index), coord);
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
logError(`coordenadas ${type}`, error);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return frame;
|
|
225
|
+
}
|
|
226
|
+
render() {
|
|
227
|
+
if (this.disposed)
|
|
228
|
+
return;
|
|
229
|
+
++this.renderTicket;
|
|
230
|
+
const frame = this.build();
|
|
231
|
+
const { normalized, visible, coords, ctx } = frame;
|
|
232
|
+
const option = normalized.option;
|
|
233
|
+
const nodes = [];
|
|
234
|
+
const labels = [];
|
|
235
|
+
for (const [key, coord] of coords) {
|
|
236
|
+
try {
|
|
237
|
+
nodes.push({ kind: 'group', key: `coord/${key}`, z: 0, children: coord.render(ctx) });
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
logError(`coordenadas ${key}`, error);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (this.pointer) {
|
|
244
|
+
const coord = coords.get(this.pointer.coordKey);
|
|
245
|
+
const axis = coord?.axes[this.pointer.axisDim];
|
|
246
|
+
const tip = option['tooltip'][0] ?? {};
|
|
247
|
+
const kind = (tip['axisPointer']?.['type'] ?? 'line');
|
|
248
|
+
if (coord?.renderAxisPointer && axis && kind !== 'none') {
|
|
249
|
+
nodes.push({ kind: 'group', key: 'axis-pointer', z: kind === 'shadow' ? 1 : 5, silent: true, children: coord.renderAxisPointer(axis, this.pointer.value, kind, ctx) });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
for (const model of visible) {
|
|
253
|
+
const type = SERIES_TYPES.get(model.type);
|
|
254
|
+
if (!type)
|
|
255
|
+
continue;
|
|
256
|
+
try {
|
|
257
|
+
const result = type.render(model, ctx);
|
|
258
|
+
nodes.push({ kind: 'group', key: `series/${model.index}`, z: 2 + Number(model.option['z'] ?? 0) / 100, children: result.nodes });
|
|
259
|
+
if (result.labels)
|
|
260
|
+
labels.push(...result.labels);
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
logError(`série ${model.type}`, error);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (labels.length)
|
|
267
|
+
nodes.push({ kind: 'group', key: 'labels', z: 3, children: layoutLabels(labels, measureText) });
|
|
268
|
+
// Componentes: a legenda recebe as entradas das séries.
|
|
269
|
+
const entries = this.legendEntries(normalized.models, ctx);
|
|
270
|
+
for (const [key, type] of COMPONENT_TYPES) {
|
|
271
|
+
if (!type.render)
|
|
272
|
+
continue;
|
|
273
|
+
option[key]?.forEach((componentOption, index) => {
|
|
274
|
+
const opt = key === 'legend' ? { ...componentOption, __entries: entries } : componentOption;
|
|
275
|
+
try {
|
|
276
|
+
nodes.push({ kind: 'group', key: `component/${key}/${index}`, z: 4, children: type.render(opt, index, ctx) });
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
logError(`componente ${key}`, error);
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
this.renderer.setSize(this.width, this.height);
|
|
284
|
+
this.renderer.setBackground(typeof option['backgroundColor'] === 'string' ? option['backgroundColor'] : undefined);
|
|
285
|
+
this.renderer.setLabel(describeChart(option, visible, this.locale));
|
|
286
|
+
this.renderer.render(nodes, {
|
|
287
|
+
animate: ctx.animation.enabled && this.animateNext,
|
|
288
|
+
duration: ctx.animation.duration,
|
|
289
|
+
durationUpdate: ctx.animation.durationUpdate,
|
|
290
|
+
easing: ctx.animation.easing,
|
|
291
|
+
easingUpdate: easingOf(option['animationEasingUpdate'], 'cubicInOut'),
|
|
292
|
+
});
|
|
293
|
+
this.animateNext = true;
|
|
294
|
+
this.emit('rendered', { componentType: 'series', type: 'rendered' });
|
|
295
|
+
}
|
|
296
|
+
/** Entradas da legenda: as que cada série dá (pizza por item) ou uma por série com nome. */
|
|
297
|
+
legendEntries(models, ctx) {
|
|
298
|
+
const out = [];
|
|
299
|
+
const seen = new Set();
|
|
300
|
+
for (const model of models) {
|
|
301
|
+
const type = SERIES_TYPES.get(model.type);
|
|
302
|
+
const list = type?.legendEntries
|
|
303
|
+
? type.legendEntries(model, ctx)
|
|
304
|
+
: model.name.startsWith('series\u0000')
|
|
305
|
+
? []
|
|
306
|
+
: [{ name: model.name, color: model.color, icon: model.type === 'line' ? 'line' : 'roundRect', seriesIndex: model.index, decal: model.decal, lineStyle: undefined }];
|
|
307
|
+
for (const entry of list) {
|
|
308
|
+
if (seen.has(entry.name))
|
|
309
|
+
continue;
|
|
310
|
+
seen.add(entry.name);
|
|
311
|
+
out.push(entry);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return out;
|
|
315
|
+
}
|
|
316
|
+
// ---------------------------------------------------------------- destaque
|
|
317
|
+
stateOf(si, di, models) {
|
|
318
|
+
const active = [];
|
|
319
|
+
for (const [s, set] of this.highlights) {
|
|
320
|
+
if (set === 'all')
|
|
321
|
+
active.push({ si: s, di: 'all' });
|
|
322
|
+
else
|
|
323
|
+
for (const d of set)
|
|
324
|
+
active.push({ si: s, di: d });
|
|
325
|
+
}
|
|
326
|
+
if (this.hover?.componentType === 'series' && this.hover.seriesIndex !== undefined) {
|
|
327
|
+
active.push({ si: this.hover.seriesIndex, di: this.hover.dataIndex ?? 'all' });
|
|
328
|
+
}
|
|
329
|
+
if (!active.length)
|
|
330
|
+
return 'normal';
|
|
331
|
+
let blur = false;
|
|
332
|
+
for (const a of active) {
|
|
333
|
+
const focus = models[a.si]?.option['emphasis']?.['focus'] ?? 'none';
|
|
334
|
+
if (a.si === si && (a.di === 'all' || a.di === di || di === undefined || focus === 'series'))
|
|
335
|
+
return 'emphasis';
|
|
336
|
+
if (focus === 'series' && a.si !== si)
|
|
337
|
+
blur = true;
|
|
338
|
+
if (focus === 'self')
|
|
339
|
+
blur = true;
|
|
340
|
+
}
|
|
341
|
+
return blur ? 'blur' : 'normal';
|
|
342
|
+
}
|
|
343
|
+
sameItem(a, b) {
|
|
344
|
+
if (!a || !b)
|
|
345
|
+
return a === b;
|
|
346
|
+
return a.componentType === b.componentType && a.seriesIndex === b.seriesIndex && a.dataIndex === b.dataIndex && a.name === b.name && a.dataType === b.dataType;
|
|
347
|
+
}
|
|
348
|
+
// ---------------------------------------------------------------- mouse
|
|
349
|
+
tooltipOption() {
|
|
350
|
+
const frame = this.frame ?? this.build();
|
|
351
|
+
return frame.normalized.option['tooltip'][0] ?? { show: false };
|
|
352
|
+
}
|
|
353
|
+
onPointer(item, x, y, event) {
|
|
354
|
+
const frame = this.frame ?? this.build();
|
|
355
|
+
const tip = this.tooltipOption();
|
|
356
|
+
const tipOn = !!frame.normalized.option['tooltip'].length && tip['show'] !== false && tip['showContent'] !== false && String(tip['triggerOn'] ?? 'mousemove').includes('mousemove');
|
|
357
|
+
let needsRender = false;
|
|
358
|
+
const seriesItem = item?.componentType === 'series' ? item : null;
|
|
359
|
+
if (!this.sameItem(this.hover, seriesItem)) {
|
|
360
|
+
if (this.hover)
|
|
361
|
+
this.emit('mouseout', { ...this.hover, type: 'mouseout', event });
|
|
362
|
+
this.hover = seriesItem;
|
|
363
|
+
if (seriesItem)
|
|
364
|
+
this.emit('mouseover', { ...this.fillItem(seriesItem, frame), type: 'mouseover', event });
|
|
365
|
+
needsRender = true;
|
|
366
|
+
}
|
|
367
|
+
if (tipOn && tip['trigger'] === 'axis') {
|
|
368
|
+
const found = this.axisAt(x, y, frame);
|
|
369
|
+
const next = found ? { coordKey: found.key, axisDim: found.axis.dim, value: found.value } : null;
|
|
370
|
+
if (JSON.stringify(next) !== JSON.stringify(this.pointer)) {
|
|
371
|
+
this.pointer = next;
|
|
372
|
+
needsRender = true;
|
|
373
|
+
}
|
|
374
|
+
if (found)
|
|
375
|
+
this.tooltip.showAxis(found.coord, found.axis, found.value, x, y);
|
|
376
|
+
else
|
|
377
|
+
this.tooltip.hide();
|
|
378
|
+
}
|
|
379
|
+
else if (tipOn && tip['trigger'] !== 'none') {
|
|
380
|
+
const model = seriesItem?.seriesIndex !== undefined ? frame.normalized.models[seriesItem.seriesIndex] : undefined;
|
|
381
|
+
if (model && seriesItem?.dataIndex !== undefined)
|
|
382
|
+
this.tooltip.showItem(model, seriesItem.dataIndex, x, y);
|
|
383
|
+
else
|
|
384
|
+
this.tooltip.hide();
|
|
385
|
+
}
|
|
386
|
+
if (needsRender) {
|
|
387
|
+
this.animateNext = true;
|
|
388
|
+
this.render();
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
axisAt(x, y, frame) {
|
|
392
|
+
for (const [key, coord] of frame.coords) {
|
|
393
|
+
if (!coord.pointerAt)
|
|
394
|
+
continue;
|
|
395
|
+
const hit = coord.pointerAt(x, y);
|
|
396
|
+
if (hit)
|
|
397
|
+
return { key, coord, axis: hit.axis, value: hit.value };
|
|
398
|
+
}
|
|
399
|
+
return null;
|
|
400
|
+
}
|
|
401
|
+
onLeave(event) {
|
|
402
|
+
const tip = this.tooltipOption();
|
|
403
|
+
if (!tip['alwaysShowContent'])
|
|
404
|
+
this.tooltip.hide();
|
|
405
|
+
const changed = !!this.hover || !!this.pointer;
|
|
406
|
+
if (this.hover)
|
|
407
|
+
this.emit('mouseout', { ...this.hover, type: 'mouseout', event });
|
|
408
|
+
this.hover = null;
|
|
409
|
+
this.pointer = null;
|
|
410
|
+
if (changed)
|
|
411
|
+
this.render();
|
|
412
|
+
}
|
|
413
|
+
onClick(item, x, y, event) {
|
|
414
|
+
const frame = this.frame ?? this.build();
|
|
415
|
+
if (!item) {
|
|
416
|
+
this.emit('click', { componentType: 'blank', type: 'click', event });
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (item.componentType === 'legend' && item.name !== undefined) {
|
|
420
|
+
this.toggleLegend(String(item.name), item.componentIndex ?? 0);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
if (item.componentType === 'legendScroll') {
|
|
424
|
+
const delta = Number(item.data?.delta ?? 0);
|
|
425
|
+
scrollLegend(frame.ctx, item.componentIndex ?? 0, delta);
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (item.componentType === 'visualMap') {
|
|
429
|
+
const piece = item.data?.pieceIndex;
|
|
430
|
+
if (piece !== undefined)
|
|
431
|
+
toggleVisualPiece(frame.ctx, item.componentIndex ?? 0, piece);
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
const full = this.fillItem(item, frame);
|
|
435
|
+
this.emit('click', { ...full, type: 'click', event });
|
|
436
|
+
if (item.componentType === 'series' && item.seriesIndex !== undefined) {
|
|
437
|
+
const model = frame.normalized.models[item.seriesIndex];
|
|
438
|
+
const type = model ? SERIES_TYPES.get(model.type) : undefined;
|
|
439
|
+
if (model && type?.onClick?.(model, item, frame.ctx))
|
|
440
|
+
this.render();
|
|
441
|
+
const tip = this.tooltipOption();
|
|
442
|
+
if (String(tip['triggerOn'] ?? '').includes('click') && tip['trigger'] !== 'axis' && model && item.dataIndex !== undefined)
|
|
443
|
+
this.tooltip.showItem(model, item.dataIndex, x, y);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
/** Completa o item do evento com o que o modelo sabe (nome, valor, dado, tipo e nome da série). */
|
|
447
|
+
fillItem(item, frame) {
|
|
448
|
+
if (item.componentType !== 'series' || item.seriesIndex === undefined)
|
|
449
|
+
return item;
|
|
450
|
+
const model = frame.normalized.models[item.seriesIndex];
|
|
451
|
+
if (!model)
|
|
452
|
+
return item;
|
|
453
|
+
const data = item.dataIndex !== undefined ? model.data[item.dataIndex] : undefined;
|
|
454
|
+
let name = item.name || data?.name || '';
|
|
455
|
+
// Sem nome no item: a categoria do eixo (como o formato entrega em `params.name`).
|
|
456
|
+
if (!name && item.dataIndex !== undefined && model.coordinateSystem !== 'none') {
|
|
457
|
+
const coord = frame.coords.get(coordKey(model.coordinateSystem, model.coordinateIndex));
|
|
458
|
+
const base = coord && Object.values(coord.axes).find((a) => a.type === 'category');
|
|
459
|
+
name = base?.categories[item.dataIndex] ?? '';
|
|
460
|
+
}
|
|
461
|
+
return {
|
|
462
|
+
...item,
|
|
463
|
+
seriesType: item.seriesType ?? model.type,
|
|
464
|
+
seriesName: item.seriesName ?? model.name,
|
|
465
|
+
name,
|
|
466
|
+
value: item.value ?? data?.value,
|
|
467
|
+
data: item.data ?? data?.raw,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
// ---------------------------------------------------------------- legenda
|
|
471
|
+
legendNames(frame) {
|
|
472
|
+
return this.legendEntries(frame.normalized.models, frame.ctx).map((e) => e.name);
|
|
473
|
+
}
|
|
474
|
+
toggleLegend(name, index) {
|
|
475
|
+
const frame = this.frame ?? this.build();
|
|
476
|
+
const legendOpt = (frame.normalized.option['legend'] ?? [])[index] ?? {};
|
|
477
|
+
this.legendSelected = nextLegendSelection(legendOpt, this.legendSelected, name, this.legendNames(frame));
|
|
478
|
+
this.afterLegendChange(name, 'legendselectchanged');
|
|
479
|
+
}
|
|
480
|
+
afterLegendChange(name, type) {
|
|
481
|
+
const frame = this.frame ?? this.build();
|
|
482
|
+
const selected = {};
|
|
483
|
+
for (const n of this.legendNames(frame))
|
|
484
|
+
selected[n] = this.legendSelected[n] !== false;
|
|
485
|
+
this.animateNext = true;
|
|
486
|
+
this.render();
|
|
487
|
+
this.emit(type, { componentType: 'legend', type, name, selected });
|
|
488
|
+
}
|
|
489
|
+
// ---------------------------------------------------------------- ações
|
|
490
|
+
dispatchAction(action) {
|
|
491
|
+
if (this.disposed || !isPlainObject(action))
|
|
492
|
+
return;
|
|
493
|
+
const frame = this.frame ?? this.build();
|
|
494
|
+
const models = frame.normalized.models;
|
|
495
|
+
const type = String(action['type']);
|
|
496
|
+
const seriesOf = () => {
|
|
497
|
+
if (action['seriesIndex'] !== undefined)
|
|
498
|
+
return toArray(action['seriesIndex']).map((i) => models[i]).filter(Boolean);
|
|
499
|
+
if (action['seriesName'] !== undefined) {
|
|
500
|
+
const names = toArray(action['seriesName']);
|
|
501
|
+
return models.filter((m) => names.includes(m.name));
|
|
502
|
+
}
|
|
503
|
+
if (action['seriesId'] !== undefined) {
|
|
504
|
+
const ids = toArray(action['seriesId']);
|
|
505
|
+
return models.filter((m) => ids.includes(m.option['id']));
|
|
506
|
+
}
|
|
507
|
+
return [];
|
|
508
|
+
};
|
|
509
|
+
const indicesOf = (model) => {
|
|
510
|
+
if (action['dataIndex'] !== undefined)
|
|
511
|
+
return toArray(action['dataIndex']);
|
|
512
|
+
if (action['name'] !== undefined) {
|
|
513
|
+
const names = toArray(action['name']).map(String);
|
|
514
|
+
return model.data.map((d, i) => (d.name !== undefined && names.includes(d.name) ? i : -1)).filter((i) => i >= 0);
|
|
515
|
+
}
|
|
516
|
+
return 'all';
|
|
517
|
+
};
|
|
518
|
+
switch (type) {
|
|
519
|
+
case 'highlight':
|
|
520
|
+
case 'downplay': {
|
|
521
|
+
const list = seriesOf();
|
|
522
|
+
if (type === 'downplay' && !list.length && action['dataIndex'] === undefined && action['name'] === undefined)
|
|
523
|
+
this.highlights.clear();
|
|
524
|
+
// Sem série: pelo nome, em qualquer série (pizza e legenda chamam assim).
|
|
525
|
+
const targets = list.length ? list : action['name'] !== undefined ? models : [];
|
|
526
|
+
for (const model of targets) {
|
|
527
|
+
const idx = indicesOf(model);
|
|
528
|
+
if (type === 'highlight') {
|
|
529
|
+
if (idx === 'all')
|
|
530
|
+
this.highlights.set(model.index, 'all');
|
|
531
|
+
else if (idx.length) {
|
|
532
|
+
const cur = this.highlights.get(model.index);
|
|
533
|
+
const set = cur instanceof Set ? cur : new Set();
|
|
534
|
+
idx.forEach((i) => set.add(i));
|
|
535
|
+
this.highlights.set(model.index, set);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
else {
|
|
539
|
+
const cur = this.highlights.get(model.index);
|
|
540
|
+
if (idx === 'all' || cur === 'all')
|
|
541
|
+
this.highlights.delete(model.index);
|
|
542
|
+
else if (cur) {
|
|
543
|
+
idx.forEach((i) => cur.delete(i));
|
|
544
|
+
if (!cur.size)
|
|
545
|
+
this.highlights.delete(model.index);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
this.animateNext = true;
|
|
550
|
+
this.render();
|
|
551
|
+
this.emit(type, { componentType: 'series', type, seriesIndex: action['seriesIndex'], dataIndex: action['dataIndex'], name: action['name'] });
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
case 'showTip': {
|
|
555
|
+
const pos = Array.isArray(action['position']) ? action['position'] : undefined;
|
|
556
|
+
const x = pos?.[0] ?? (typeof action['x'] === 'number' ? action['x'] : this.width / 2);
|
|
557
|
+
const y = pos?.[1] ?? (typeof action['y'] === 'number' ? action['y'] : this.height / 2);
|
|
558
|
+
const model = seriesOf()[0];
|
|
559
|
+
if (model) {
|
|
560
|
+
const idx = indicesOf(model);
|
|
561
|
+
const dataIndex = idx === 'all' ? 0 : idx[0];
|
|
562
|
+
if (dataIndex === undefined)
|
|
563
|
+
return;
|
|
564
|
+
const tip = this.tooltipOption();
|
|
565
|
+
if (tip['trigger'] === 'axis' && model.coordinateSystem !== 'none') {
|
|
566
|
+
const coord = frame.coords.get(coordKey(model.coordinateSystem, model.coordinateIndex));
|
|
567
|
+
const base = coord && Object.values(coord.axes).find((a) => a.type === 'category');
|
|
568
|
+
if (coord && base) {
|
|
569
|
+
const value = base.categories[dataIndex] ?? dataIndex;
|
|
570
|
+
this.pointer = { coordKey: coordKey(model.coordinateSystem, model.coordinateIndex), axisDim: base.dim, value: dataIndex };
|
|
571
|
+
this.tooltip.showAxis(coord, base, dataIndex, x, y);
|
|
572
|
+
void value;
|
|
573
|
+
this.render();
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
this.tooltip.showItem(model, dataIndex, x, y);
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
if (typeof action['x'] === 'number' && typeof action['y'] === 'number') {
|
|
581
|
+
const found = this.axisAt(action['x'], action['y'], frame);
|
|
582
|
+
if (found) {
|
|
583
|
+
this.pointer = { coordKey: found.key, axisDim: found.axis.dim, value: found.value };
|
|
584
|
+
this.tooltip.showAxis(found.coord, found.axis, found.value, action['x'], action['y']);
|
|
585
|
+
this.render();
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
case 'hideTip':
|
|
591
|
+
this.tooltip.hide();
|
|
592
|
+
if (this.pointer) {
|
|
593
|
+
this.pointer = null;
|
|
594
|
+
this.render();
|
|
595
|
+
}
|
|
596
|
+
return;
|
|
597
|
+
case 'legendToggleSelect':
|
|
598
|
+
case 'legendSelect':
|
|
599
|
+
case 'legendUnSelect': {
|
|
600
|
+
const name = String(action['name']);
|
|
601
|
+
if (type === 'legendToggleSelect')
|
|
602
|
+
this.legendSelected = { ...this.legendSelected, [name]: this.legendSelected[name] === false };
|
|
603
|
+
else
|
|
604
|
+
this.legendSelected = { ...this.legendSelected, [name]: type === 'legendSelect' };
|
|
605
|
+
this.afterLegendChange(name, type === 'legendToggleSelect' ? 'legendselectchanged' : type === 'legendSelect' ? 'legendselected' : 'legendunselected');
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
case 'legendAllSelect':
|
|
609
|
+
case 'legendInverseSelect': {
|
|
610
|
+
const next = {};
|
|
611
|
+
for (const n of this.legendNames(frame))
|
|
612
|
+
next[n] = type === 'legendAllSelect' ? true : this.legendSelected[n] === false;
|
|
613
|
+
this.legendSelected = next;
|
|
614
|
+
this.afterLegendChange(undefined, type === 'legendAllSelect' ? 'legendselectall' : 'legendinverseselect');
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
case 'legendScroll':
|
|
618
|
+
scrollLegend(frame.ctx, Number(action['legendIndex'] ?? 0), Number(action['scrollDataIndex'] ?? 0) - Number(frame.ctx.componentState.get('legend-0/page') ?? 0));
|
|
619
|
+
return;
|
|
620
|
+
default:
|
|
621
|
+
this.emit(type, { componentType: 'series', type });
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
// ---------------------------------------------------------------- eventos
|
|
625
|
+
on(event, queryOrHandler, maybeHandler) {
|
|
626
|
+
if (typeof queryOrHandler === 'function')
|
|
627
|
+
this.listeners.push({ event, query: null, handler: queryOrHandler });
|
|
628
|
+
else if (maybeHandler)
|
|
629
|
+
this.listeners.push({ event, query: queryOrHandler, handler: maybeHandler });
|
|
630
|
+
}
|
|
631
|
+
off(event, handler) {
|
|
632
|
+
for (let i = this.listeners.length - 1; i >= 0; i--) {
|
|
633
|
+
const l = this.listeners[i];
|
|
634
|
+
if (l.event === event && (!handler || l.handler === handler))
|
|
635
|
+
this.listeners.splice(i, 1);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
emit(type, params) {
|
|
639
|
+
const event = { ...params, type };
|
|
640
|
+
for (const l of [...this.listeners]) {
|
|
641
|
+
if (l.event.toLowerCase() !== type.toLowerCase())
|
|
642
|
+
continue;
|
|
643
|
+
if (l.query && !matchesQuery(l.query, event))
|
|
644
|
+
continue;
|
|
645
|
+
try {
|
|
646
|
+
l.handler(event);
|
|
647
|
+
}
|
|
648
|
+
catch (error) {
|
|
649
|
+
logError(`evento ${type}`, error);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
// ---------------------------------------------------------------- conversão de coordenadas
|
|
654
|
+
coordFor(finder) {
|
|
655
|
+
const frame = this.frame ?? this.build();
|
|
656
|
+
const f = typeof finder === 'string' ? { [`${finder}Index`]: 0 } : finder;
|
|
657
|
+
const get = (type, index) => frame.coords.get(coordKey(type, index));
|
|
658
|
+
if (f['seriesIndex'] !== undefined) {
|
|
659
|
+
const model = frame.normalized.models[f['seriesIndex']];
|
|
660
|
+
if (!model)
|
|
661
|
+
return undefined;
|
|
662
|
+
const coord = get(model.coordinateSystem, model.coordinateIndex);
|
|
663
|
+
return coord ? { coord } : undefined;
|
|
664
|
+
}
|
|
665
|
+
if (f['gridIndex'] !== undefined) {
|
|
666
|
+
const coord = get('cartesian2d', f['gridIndex']);
|
|
667
|
+
return coord ? { coord } : undefined;
|
|
668
|
+
}
|
|
669
|
+
for (const [key, dim] of [['xAxisIndex', 'x'], ['yAxisIndex', 'y']]) {
|
|
670
|
+
if (f[key] === undefined)
|
|
671
|
+
continue;
|
|
672
|
+
const axis = (frame.normalized.option[key === 'xAxisIndex' ? 'xAxis' : 'yAxis'] ?? [])[f[key]];
|
|
673
|
+
const coord = get('cartesian2d', Number(axis?.['gridIndex'] ?? 0));
|
|
674
|
+
return coord ? { coord, axisDim: dim } : undefined;
|
|
675
|
+
}
|
|
676
|
+
for (const [key, type] of [['polarIndex', 'polar'], ['radarIndex', 'radar'], ['calendarIndex', 'calendar']]) {
|
|
677
|
+
if (f[key] === undefined)
|
|
678
|
+
continue;
|
|
679
|
+
const coord = get(type, f[key]);
|
|
680
|
+
return coord ? { coord } : undefined;
|
|
681
|
+
}
|
|
682
|
+
return undefined;
|
|
683
|
+
}
|
|
684
|
+
convertToPixel(finder, value) {
|
|
685
|
+
const found = this.coordFor(finder);
|
|
686
|
+
if (!found)
|
|
687
|
+
return undefined;
|
|
688
|
+
if (found.axisDim) {
|
|
689
|
+
const axis = found.coord.axes[found.axisDim];
|
|
690
|
+
return axis ? axis.toPixel(Array.isArray(value) ? value[0] : value) : undefined;
|
|
691
|
+
}
|
|
692
|
+
return found.coord.dataToPoint(Array.isArray(value) ? value : [value]);
|
|
693
|
+
}
|
|
694
|
+
convertFromPixel(finder, value) {
|
|
695
|
+
const found = this.coordFor(finder);
|
|
696
|
+
if (!found)
|
|
697
|
+
return undefined;
|
|
698
|
+
if (found.axisDim) {
|
|
699
|
+
const axis = found.coord.axes[found.axisDim];
|
|
700
|
+
const pixel = found.axisDim === 'x' ? value[0] : (value[1] ?? value[0]);
|
|
701
|
+
return axis ? axis.fromPixel(pixel) : undefined;
|
|
702
|
+
}
|
|
703
|
+
const x = found.coord.axes['x'];
|
|
704
|
+
const y = found.coord.axes['y'];
|
|
705
|
+
if (x && y)
|
|
706
|
+
return [x.fromPixel(value[0]), y.fromPixel(value[1])];
|
|
707
|
+
return undefined;
|
|
708
|
+
}
|
|
709
|
+
containPixel(finder, value) {
|
|
710
|
+
const found = this.coordFor(finder);
|
|
711
|
+
if (!found)
|
|
712
|
+
return false;
|
|
713
|
+
if (found.coord.contains)
|
|
714
|
+
return found.coord.contains(value[0], value[1]);
|
|
715
|
+
const r = found.coord.rect;
|
|
716
|
+
return value[0] >= r.x && value[0] <= r.x + r.width && value[1] >= r.y && value[1] <= r.y + r.height;
|
|
717
|
+
}
|
|
718
|
+
// ---------------------------------------------------------------- exportar
|
|
719
|
+
renderToSVGString() {
|
|
720
|
+
return this.renderer.toString();
|
|
721
|
+
}
|
|
722
|
+
getDataURL(opts = {}) {
|
|
723
|
+
let svg = this.renderToSVGString();
|
|
724
|
+
if (opts.backgroundColor)
|
|
725
|
+
svg = svg.replace(/<svg([^>]*?)style="/, `<svg$1style="background-color:${opts.backgroundColor};`);
|
|
726
|
+
return `data:image/svg+xml;charset=UTF-8,${encodeURIComponent(svg)}`;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
// ---------------------------------------------------------------- auxiliares
|
|
730
|
+
/** `query` de `on`: `'series'`, `'series.bar'`, `'legend'` ou objeto com as chaves do evento. */
|
|
731
|
+
function matchesQuery(query, event) {
|
|
732
|
+
if (typeof query === 'string') {
|
|
733
|
+
const [component, sub] = query.split('.');
|
|
734
|
+
if (component !== event.componentType)
|
|
735
|
+
return false;
|
|
736
|
+
return !sub || sub === event.seriesType;
|
|
737
|
+
}
|
|
738
|
+
const e = event;
|
|
739
|
+
return Object.entries(query).every(([k, v]) => {
|
|
740
|
+
if (k === 'seriesType' || k === 'componentType' || k === 'name' || k === 'dataType' || k === 'seriesName')
|
|
741
|
+
return e[k] === v;
|
|
742
|
+
if (k.endsWith('Index'))
|
|
743
|
+
return e[k] === v;
|
|
744
|
+
return e[k] === v;
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
function getComputedStyleSafe(el, prop) {
|
|
748
|
+
try {
|
|
749
|
+
return el.ownerDocument.defaultView?.getComputedStyle(el).getPropertyValue(prop) ?? '';
|
|
750
|
+
}
|
|
751
|
+
catch {
|
|
752
|
+
return '';
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
function logError(where, error) {
|
|
756
|
+
// Um pedaço que falha não derruba o gráfico inteiro; o erro vai para o console (como no formato).
|
|
757
|
+
console.error(`[hermes-charts] ${where}:`, error);
|
|
758
|
+
}
|
|
759
|
+
// ---------------------------------------------------------------- registro por elemento
|
|
760
|
+
const instances = new WeakMap();
|
|
761
|
+
export function createInstance(dom, theme, opts) {
|
|
762
|
+
const existing = instances.get(dom);
|
|
763
|
+
if (existing && !existing.isDisposed())
|
|
764
|
+
existing.dispose();
|
|
765
|
+
const instance = new ChartInstanceImpl(dom, theme, opts);
|
|
766
|
+
instances.set(dom, instance);
|
|
767
|
+
return instance;
|
|
768
|
+
}
|
|
769
|
+
export function instanceByDom(dom) {
|
|
770
|
+
const inst = instances.get(dom);
|
|
771
|
+
return inst && !inst.isDisposed() ? inst : undefined;
|
|
772
|
+
}
|
|
773
|
+
//# sourceMappingURL=instance.js.map
|