@trismegisto/charts 3.4.0 → 4.0.1

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.
Files changed (230) hide show
  1. package/MIGRATION.md +831 -0
  2. package/NOTICE +1 -6
  3. package/README.md +24 -14
  4. package/dist/core/bar-chart.d.ts +23 -3
  5. package/dist/core/bar-chart.js +98 -20
  6. package/dist/core/bar-chart.js.map +1 -1
  7. package/dist/core/boxplot-chart.js +12 -13
  8. package/dist/core/boxplot-chart.js.map +1 -1
  9. package/dist/core/bullet-chart.js +30 -32
  10. package/dist/core/bullet-chart.js.map +1 -1
  11. package/dist/core/calendar-heatmap.d.ts +2 -0
  12. package/dist/core/calendar-heatmap.js +22 -23
  13. package/dist/core/calendar-heatmap.js.map +1 -1
  14. package/dist/core/category-range.d.ts +15 -0
  15. package/dist/core/category-range.js +35 -0
  16. package/dist/core/category-range.js.map +1 -0
  17. package/dist/core/chart-host.d.ts +25 -8
  18. package/dist/core/chart-host.js +124 -24
  19. package/dist/core/chart-host.js.map +1 -1
  20. package/dist/core/charts-messages.d.ts +71 -16
  21. package/dist/core/charts-messages.en.js +50 -13
  22. package/dist/core/charts-messages.en.js.map +1 -1
  23. package/dist/core/charts-messages.es.js +50 -13
  24. package/dist/core/charts-messages.es.js.map +1 -1
  25. package/dist/core/charts-messages.js +50 -13
  26. package/dist/core/charts-messages.js.map +1 -1
  27. package/dist/core/colors.d.ts +27 -0
  28. package/dist/core/colors.js +76 -0
  29. package/dist/core/colors.js.map +1 -0
  30. package/dist/core/context.d.ts +22 -2
  31. package/dist/core/context.js +25 -2
  32. package/dist/core/context.js.map +1 -1
  33. package/dist/core/engine-locale.d.ts +3 -2
  34. package/dist/core/engine-locale.js +23 -44
  35. package/dist/core/engine-locale.js.map +1 -1
  36. package/dist/core/frame.d.ts +13 -1
  37. package/dist/core/frame.js +37 -1
  38. package/dist/core/frame.js.map +1 -1
  39. package/dist/core/funnel-chart.js +9 -12
  40. package/dist/core/funnel-chart.js.map +1 -1
  41. package/dist/core/gauge-chart.js +31 -63
  42. package/dist/core/gauge-chart.js.map +1 -1
  43. package/dist/core/heatmap-chart.d.ts +5 -0
  44. package/dist/core/heatmap-chart.js +28 -21
  45. package/dist/core/heatmap-chart.js.map +1 -1
  46. package/dist/core/index.d.ts +14 -0
  47. package/dist/core/index.js +14 -0
  48. package/dist/core/index.js.map +1 -1
  49. package/dist/core/line-chart.d.ts +34 -3
  50. package/dist/core/line-chart.js +90 -20
  51. package/dist/core/line-chart.js.map +1 -1
  52. package/dist/core/line-extras.d.ts +20 -0
  53. package/dist/core/line-extras.js +49 -0
  54. package/dist/core/line-extras.js.map +1 -0
  55. package/dist/core/linear-gauge.d.ts +64 -0
  56. package/dist/core/linear-gauge.js +78 -0
  57. package/dist/core/linear-gauge.js.map +1 -0
  58. package/dist/core/live-session.d.ts +58 -0
  59. package/dist/core/live-session.js +200 -0
  60. package/dist/core/live-session.js.map +1 -0
  61. package/dist/core/live.d.ts +78 -0
  62. package/dist/core/live.js +245 -0
  63. package/dist/core/live.js.map +1 -0
  64. package/dist/core/navigation.d.ts +3 -8
  65. package/dist/core/navigation.js +12 -7
  66. package/dist/core/navigation.js.map +1 -1
  67. package/dist/core/normalize.d.ts +19 -0
  68. package/dist/core/normalize.js +51 -0
  69. package/dist/core/normalize.js.map +1 -0
  70. package/dist/core/observe.js +51 -15
  71. package/dist/core/observe.js.map +1 -1
  72. package/dist/core/options.d.ts +9 -7
  73. package/dist/core/options.js +16 -11
  74. package/dist/core/options.js.map +1 -1
  75. package/dist/core/pie-chart.d.ts +18 -2
  76. package/dist/core/pie-chart.js +83 -18
  77. package/dist/core/pie-chart.js.map +1 -1
  78. package/dist/core/radar-chart.js +10 -10
  79. package/dist/core/radar-chart.js.map +1 -1
  80. package/dist/core/range-interaction.d.ts +62 -0
  81. package/dist/core/range-interaction.js +196 -0
  82. package/dist/core/range-interaction.js.map +1 -0
  83. package/dist/core/range.d.ts +83 -0
  84. package/dist/core/range.js +200 -0
  85. package/dist/core/range.js.map +1 -0
  86. package/dist/core/references.d.ts +25 -0
  87. package/dist/core/references.js +71 -0
  88. package/dist/core/references.js.map +1 -0
  89. package/dist/core/sankey-chart.js +8 -12
  90. package/dist/core/sankey-chart.js.map +1 -1
  91. package/dist/core/scale-legend.d.ts +10 -0
  92. package/dist/core/scale-legend.js +16 -0
  93. package/dist/core/scale-legend.js.map +1 -0
  94. package/dist/core/scatter-chart.d.ts +5 -0
  95. package/dist/core/scatter-chart.js +34 -22
  96. package/dist/core/scatter-chart.js.map +1 -1
  97. package/dist/core/scope-cache.d.ts +6 -0
  98. package/dist/core/scope-cache.js +49 -0
  99. package/dist/core/scope-cache.js.map +1 -0
  100. package/dist/core/series-extras.d.ts +46 -0
  101. package/dist/core/series-extras.js +125 -0
  102. package/dist/core/series-extras.js.map +1 -0
  103. package/dist/core/sparkline-chart.d.ts +11 -0
  104. package/dist/core/sparkline-chart.js +27 -18
  105. package/dist/core/sparkline-chart.js.map +1 -1
  106. package/dist/core/theme.d.ts +3 -2
  107. package/dist/core/theme.js +36 -69
  108. package/dist/core/theme.js.map +1 -1
  109. package/dist/core/time-series-chart.d.ts +91 -0
  110. package/dist/core/time-series-chart.js +390 -0
  111. package/dist/core/time-series-chart.js.map +1 -0
  112. package/dist/core/treemap-chart.js +12 -16
  113. package/dist/core/treemap-chart.js.map +1 -1
  114. package/dist/core/types.d.ts +57 -0
  115. package/dist/core/view.d.ts +73 -2
  116. package/dist/core/warn.d.ts +5 -0
  117. package/dist/core/warn.js +22 -0
  118. package/dist/core/warn.js.map +1 -0
  119. package/dist/core/waterfall-chart.js +17 -18
  120. package/dist/core/waterfall-chart.js.map +1 -1
  121. package/dist/engine/aria.d.ts +1 -6
  122. package/dist/engine/aria.js +1 -53
  123. package/dist/engine/aria.js.map +1 -1
  124. package/dist/engine/component/legend.js +11 -12
  125. package/dist/engine/component/legend.js.map +1 -1
  126. package/dist/engine/component/reference-marks.d.ts +46 -0
  127. package/dist/engine/component/reference-marks.js +197 -0
  128. package/dist/engine/component/reference-marks.js.map +1 -0
  129. package/dist/engine/component/text-style.d.ts +1 -1
  130. package/dist/engine/component/text-style.js +2 -2
  131. package/dist/engine/component/text-style.js.map +1 -1
  132. package/dist/engine/component/tooltip.js +8 -32
  133. package/dist/engine/component/tooltip.js.map +1 -1
  134. package/dist/engine/component/view-window.d.ts +29 -0
  135. package/dist/engine/component/view-window.js +39 -0
  136. package/dist/engine/component/view-window.js.map +1 -0
  137. package/dist/engine/component/visual-map.js +2 -2
  138. package/dist/engine/component/visual-map.js.map +1 -1
  139. package/dist/engine/contract.d.ts +29 -64
  140. package/dist/engine/contract.js +4 -5
  141. package/dist/engine/contract.js.map +1 -1
  142. package/dist/engine/coord/axis.js +12 -3
  143. package/dist/engine/coord/axis.js.map +1 -1
  144. package/dist/engine/coord/calendar.js +1 -1
  145. package/dist/engine/coord/calendar.js.map +1 -1
  146. package/dist/engine/coord/cartesian.js +37 -5
  147. package/dist/engine/coord/cartesian.js.map +1 -1
  148. package/dist/engine/coord/radar.d.ts +1 -1
  149. package/dist/engine/index.d.ts +3 -27
  150. package/dist/engine/index.js +4 -15
  151. package/dist/engine/index.js.map +1 -1
  152. package/dist/engine/instance.d.ts +54 -32
  153. package/dist/engine/instance.js +399 -179
  154. package/dist/engine/instance.js.map +1 -1
  155. package/dist/engine/locale.d.ts +3 -6
  156. package/dist/engine/locale.js +26 -80
  157. package/dist/engine/locale.js.map +1 -1
  158. package/dist/engine/model.d.ts +3 -8
  159. package/dist/engine/model.js +13 -160
  160. package/dist/engine/model.js.map +1 -1
  161. package/dist/engine/option/components.d.ts +29 -19
  162. package/dist/engine/registry.d.ts +0 -2
  163. package/dist/engine/registry.js +3 -5
  164. package/dist/engine/registry.js.map +1 -1
  165. package/dist/engine/render/path.d.ts +2 -3
  166. package/dist/engine/render/path.js +8 -17
  167. package/dist/engine/render/path.js.map +1 -1
  168. package/dist/engine/render/svg.js +32 -5
  169. package/dist/engine/render/svg.js.map +1 -1
  170. package/dist/engine/scale.d.ts +12 -2
  171. package/dist/engine/scale.js +86 -18
  172. package/dist/engine/scale.js.map +1 -1
  173. package/dist/engine/series/bar.d.ts +3 -1
  174. package/dist/engine/series/bar.js +15 -4
  175. package/dist/engine/series/bar.js.map +1 -1
  176. package/dist/engine/series/boxplot.js +2 -2
  177. package/dist/engine/series/boxplot.js.map +1 -1
  178. package/dist/engine/series/cartesian-data.d.ts +1 -1
  179. package/dist/engine/series/cartesian-data.js +1 -1
  180. package/dist/engine/series/circular-common.d.ts +6 -7
  181. package/dist/engine/series/circular-common.js +8 -26
  182. package/dist/engine/series/circular-common.js.map +1 -1
  183. package/dist/engine/series/common.d.ts +2 -5
  184. package/dist/engine/series/common.js +6 -18
  185. package/dist/engine/series/common.js.map +1 -1
  186. package/dist/engine/series/funnel.js +1 -1
  187. package/dist/engine/series/funnel.js.map +1 -1
  188. package/dist/engine/series/gauge.js +2 -2
  189. package/dist/engine/series/gauge.js.map +1 -1
  190. package/dist/engine/series/heatmap.js +2 -2
  191. package/dist/engine/series/heatmap.js.map +1 -1
  192. package/dist/engine/series/label-fit.d.ts +1 -1
  193. package/dist/engine/series/label-fit.js +4 -12
  194. package/dist/engine/series/label-fit.js.map +1 -1
  195. package/dist/engine/series/line.d.ts +3 -1
  196. package/dist/engine/series/line.js +14 -7
  197. package/dist/engine/series/line.js.map +1 -1
  198. package/dist/engine/series/mark-point.d.ts +4 -0
  199. package/dist/engine/series/mark-point.js +196 -0
  200. package/dist/engine/series/mark-point.js.map +1 -0
  201. package/dist/engine/series/pie.d.ts +5 -0
  202. package/dist/engine/series/pie.js +8 -2
  203. package/dist/engine/series/pie.js.map +1 -1
  204. package/dist/engine/series/radar.js +7 -7
  205. package/dist/engine/series/radar.js.map +1 -1
  206. package/dist/engine/series/sankey.js +4 -4
  207. package/dist/engine/series/sankey.js.map +1 -1
  208. package/dist/engine/series/scatter.d.ts +3 -1
  209. package/dist/engine/series/scatter.js.map +1 -1
  210. package/dist/engine/series/treemap.js +6 -6
  211. package/dist/engine/series/treemap.js.map +1 -1
  212. package/dist/engine/spec/compile.d.ts +9 -0
  213. package/dist/engine/spec/compile.js +593 -0
  214. package/dist/engine/spec/compile.js.map +1 -0
  215. package/dist/engine/spec/theme.d.ts +5 -0
  216. package/dist/engine/spec/theme.js +83 -0
  217. package/dist/engine/spec/theme.js.map +1 -0
  218. package/dist/engine/spec/types.d.ts +564 -0
  219. package/dist/engine/spec/types.js +3 -0
  220. package/dist/engine/spec/types.js.map +1 -0
  221. package/dist/engine/theme.d.ts +2 -3
  222. package/dist/engine/theme.js +10 -9
  223. package/dist/engine/theme.js.map +1 -1
  224. package/dist/engine/util.d.ts +12 -7
  225. package/dist/engine/util.js +26 -10
  226. package/dist/engine/util.js.map +1 -1
  227. package/package.json +4 -3
  228. package/dist/engine/option.d.ts +0 -59
  229. package/dist/engine/option.js +0 -2
  230. package/dist/engine/option.js.map +0 -1
@@ -1,18 +1,38 @@
1
- import { decalFor, describeChart } from './aria';
1
+ import { compileSpec } from './spec/compile';
2
+ import { themePlan } from './spec/theme';
3
+ import { decalFor } from './aria';
2
4
  import { nextLegendSelection, scrollLegend } from './component/legend';
3
5
  import { TooltipController } from './component/tooltip';
4
6
  import { createVisualResolver, toggleVisualPiece } from './component/visual-map';
5
7
  import { layoutLabels } from './label-layout';
8
+ import { markPointNodes } from './series/mark-point';
9
+ import { referenceMarkNodes } from './component/reference-marks';
10
+ import { sweepNodes, viewWindowOf } from './component/view-window';
6
11
  import { createLocale, languageOf } from './locale';
7
- import { dataCount, mergeOption, normalizeOption } from './model';
12
+ import { dataCount, normalizeOption } from './model';
8
13
  import { COMPONENT_TYPES, COORDINATE_TYPES, SERIES_TYPES } from './registry';
9
14
  import { SvgRenderer } from './render/svg';
10
15
  import { measureText } from './render/text';
11
16
  import { buildTheme } from './theme';
12
- import { clone, isPlainObject, toArray } from './util';
17
+ import { isPlainObject, isUnnamed, toArray } from './util';
13
18
  const EASINGS = ['linear', 'cubicOut', 'cubicInOut', 'quadraticOut', 'elasticOut', 'backOut', 'exponentialOut'];
14
19
  const FALLBACK_WIDTH = 600;
15
20
  const FALLBACK_HEIGHT = 400;
21
+ /** De onde o evento veio, no vocabulário do contrato. */
22
+ function partOf(componentType) {
23
+ switch (componentType) {
24
+ case 'series':
25
+ return 'series';
26
+ case 'legend':
27
+ return 'legend';
28
+ case 'blank':
29
+ return 'plane';
30
+ case 'sweepSelect':
31
+ return 'sweep';
32
+ default:
33
+ return 'chart';
34
+ }
35
+ }
16
36
  const coordKey = (type, index) => `${type}:${index}`;
17
37
  function easingOf(value, fallback) {
18
38
  return EASINGS.includes(value) ? value : fallback;
@@ -25,15 +45,15 @@ function prefersReducedMotion() {
25
45
  return false;
26
46
  }
27
47
  }
28
- export class ChartInstanceImpl {
48
+ export class Chart {
29
49
  dom;
30
50
  raw = {};
31
51
  theme;
32
52
  locale;
33
53
  renderer;
34
54
  tooltip;
35
- width;
36
- height;
55
+ plotWidth;
56
+ plotHeight;
37
57
  fixedWidth;
38
58
  fixedHeight;
39
59
  legendSelected = {};
@@ -46,24 +66,31 @@ export class ChartInstanceImpl {
46
66
  frame = null;
47
67
  renderTicket = 0;
48
68
  animateNext = true;
49
- disposed = false;
50
- constructor(dom, theme, opts = {}) {
69
+ gone = false;
70
+ current = null;
71
+ /** A opção nova ainda não desenhou: a translação de `slide` vale nesse quadro só. */
72
+ slidePending = false;
73
+ /** Faixa temporária da seleção por arrasto (`sweepSelect`), enquanto o botão está apertado. */
74
+ sweep = null;
75
+ domListeners = [];
76
+ constructor(dom, opts = {}) {
51
77
  this.dom = dom;
52
- this.theme = buildTheme(theme);
78
+ this.theme = buildTheme(themePlan(opts.theme));
53
79
  this.locale = createLocale(opts.locale ?? null, languageOf(dom));
54
80
  this.fixedWidth = opts.width;
55
81
  this.fixedHeight = opts.height;
56
- this.width = this.measureWidth();
57
- this.height = this.measureHeight();
58
- if (getComputedStyleSafe(dom, 'position') === 'static')
82
+ this.plotWidth = this.measureWidth();
83
+ this.plotHeight = this.measureHeight();
84
+ if (!opts.positioned && getComputedStyleSafe(dom, 'position') === 'static')
59
85
  dom.style.position = 'relative';
60
86
  this.renderer = new SvgRenderer(dom, {
61
87
  onPointer: (item, x, y, e) => this.onPointer(item, x, y, e),
62
88
  onClick: (item, x, y, e) => this.onClick(item, x, y, e),
63
89
  onLeave: (e) => this.onLeave(e),
64
90
  });
65
- this.renderer.onFinished = () => this.emit('finished', { componentType: 'series', type: 'finished' });
66
- this.renderer.setSize(this.width, this.height);
91
+ this.renderer.onFinished = () => this.emit('settled', { componentType: 'chart' });
92
+ this.listenSweep();
93
+ this.renderer.setSize(this.plotWidth, this.plotHeight);
67
94
  this.tooltip = new TooltipController(dom, () => this.currentCtx(), {
68
95
  paramsOf: (model, dataIndex) => SERIES_TYPES.get(model.type)?.tooltipParams(model, dataIndex, this.currentCtx()) ?? null,
69
96
  itemsAtAxisValue: (model, axisDim, value) => SERIES_TYPES.get(model.type)?.itemsAtAxisValue?.(model, axisDim, value, this.currentCtx()) ?? [],
@@ -76,73 +103,76 @@ export class ChartInstanceImpl {
76
103
  measureHeight() {
77
104
  return this.fixedHeight ?? (this.dom.clientHeight || FALLBACK_HEIGHT);
78
105
  }
79
- getWidth() {
80
- return this.width;
106
+ get width() {
107
+ return this.plotWidth;
81
108
  }
82
- getHeight() {
83
- return this.height;
109
+ get height() {
110
+ return this.plotHeight;
84
111
  }
85
- getDom() {
112
+ get element() {
86
113
  return this.dom;
87
114
  }
88
- resize(opts = {}) {
89
- if (this.disposed)
115
+ get disposed() {
116
+ return this.gone;
117
+ }
118
+ get spec() {
119
+ return this.current;
120
+ }
121
+ resize(size = {}) {
122
+ if (this.gone)
90
123
  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' });
124
+ this.plotWidth = typeof size.width === 'number' ? size.width : this.dom.clientWidth || this.fixedWidth || this.plotWidth;
125
+ this.plotHeight = typeof size.height === 'number' ? size.height : this.dom.clientHeight || this.fixedHeight || this.plotHeight;
126
+ this.renderer.setSize(this.plotWidth, this.plotHeight);
127
+ this.animateNext = !!size.animate;
128
+ this.draw();
129
+ this.emit('resize', { componentType: 'chart' });
97
130
  }
98
131
  // ---------------------------------------------------------------- opção
99
- setOption(option, opts) {
100
- if (this.disposed || !isPlainObject(option))
132
+ render(spec) {
133
+ if (this.gone || !spec || !Array.isArray(spec.series))
101
134
  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)
135
+ this.current = spec;
136
+ this.apply(compileSpec(spec));
137
+ }
138
+ /** Troca o plano interno inteiro (sem mesclar com o anterior). */
139
+ apply(plan) {
140
+ this.raw = plan;
141
+ this.highlights.clear();
142
+ this.pointer = null;
143
+ if (!this.legendInit)
114
144
  this.legendSelected = {};
115
145
  this.legendInit = true;
116
146
  this.animateNext = true;
117
- if (o.lazyUpdate)
118
- this.requestRender();
119
- else
120
- this.render();
147
+ this.slidePending = true;
148
+ this.draw();
121
149
  }
122
- getOption() {
150
+ /** Plano interno normalizado (padrões aplicados) do quadro atual: só para os testes do motor. */
151
+ normalizedPlan() {
123
152
  const frame = this.frame ?? this.build();
124
- return clone(frame.normalized.option);
153
+ return frame.normalized.option;
125
154
  }
126
155
  setTheme(theme) {
127
- this.theme = buildTheme(theme);
156
+ this.theme = buildTheme(themePlan(theme));
128
157
  this.animateNext = false;
129
- this.render();
158
+ this.draw();
130
159
  }
131
160
  clear() {
132
161
  this.raw = {};
162
+ this.current = null;
133
163
  this.highlights.clear();
134
164
  this.hover = null;
135
165
  this.pointer = null;
136
166
  this.tooltip.hide();
137
- this.render();
138
- }
139
- isDisposed() {
140
- return this.disposed;
167
+ this.draw();
141
168
  }
142
169
  dispose() {
143
- if (this.disposed)
170
+ if (this.gone)
144
171
  return;
145
- this.disposed = true;
172
+ this.gone = true;
173
+ for (const [type, fn, opts] of this.domListeners)
174
+ this.dom.removeEventListener(type, fn, opts);
175
+ this.domListeners.length = 0;
146
176
  this.tooltip.dispose();
147
177
  this.renderer.dispose();
148
178
  this.listeners.length = 0;
@@ -152,8 +182,8 @@ export class ChartInstanceImpl {
152
182
  requestRender() {
153
183
  const ticket = ++this.renderTicket;
154
184
  const run = () => {
155
- if (ticket === this.renderTicket && !this.disposed)
156
- this.render();
185
+ if (ticket === this.renderTicket && !this.gone)
186
+ this.draw();
157
187
  };
158
188
  if (typeof requestAnimationFrame === 'function')
159
189
  requestAnimationFrame(run);
@@ -180,8 +210,8 @@ export class ChartInstanceImpl {
180
210
  return type?.legendEntries ? true : legendSelected(m.name);
181
211
  });
182
212
  const ctx = {
183
- width: this.width,
184
- height: this.height,
213
+ width: this.plotWidth,
214
+ height: this.plotHeight,
185
215
  theme: this.theme,
186
216
  locale: this.locale,
187
217
  option,
@@ -223,8 +253,8 @@ export class ChartInstanceImpl {
223
253
  }
224
254
  return frame;
225
255
  }
226
- render() {
227
- if (this.disposed)
256
+ draw() {
257
+ if (this.gone)
228
258
  return;
229
259
  ++this.renderTicket;
230
260
  const frame = this.build();
@@ -232,6 +262,10 @@ export class ChartInstanceImpl {
232
262
  const option = normalized.option;
233
263
  const nodes = [];
234
264
  const labels = [];
265
+ // Tempo real: as séries vão num grupo interno que desliza pelo intervalo novo (o recorte do grupo de fora fica parado).
266
+ const slideOption = isPlainObject(option['slide']) ? option['slide'] : null;
267
+ const slide = slideOption && this.slidePending ? { dx: Number(slideOption.dx) || 0, duration: Number(slideOption.duration) || 0 } : null;
268
+ this.slidePending = false;
235
269
  for (const [key, coord] of coords) {
236
270
  try {
237
271
  nodes.push({ kind: 'group', key: `coord/${key}`, z: 0, children: coord.render(ctx) });
@@ -249,13 +283,34 @@ export class ChartInstanceImpl {
249
283
  nodes.push({ kind: 'group', key: 'axis-pointer', z: kind === 'shadow' ? 1 : 5, silent: true, children: coord.renderAxisPointer(axis, this.pointer.value, kind, ctx) });
250
284
  }
251
285
  }
286
+ // Marcas de referência: faixas atrás das séries, linhas na frente delas e atrás dos rótulos.
287
+ if (option['referenceMarks']?.length) {
288
+ try {
289
+ const refs = referenceMarkNodes(ctx);
290
+ if (refs.bands.length)
291
+ nodes.push({ kind: 'group', key: 'reference-bands', z: 1.5, silent: true, children: refs.bands });
292
+ if (refs.lines.length)
293
+ nodes.push({ kind: 'group', key: 'reference-lines', z: 2.9, silent: true, children: refs.lines });
294
+ }
295
+ catch (error) {
296
+ logError('marcas de referência', error);
297
+ }
298
+ }
252
299
  for (const model of visible) {
253
300
  const type = SERIES_TYPES.get(model.type);
254
301
  if (!type)
255
302
  continue;
256
303
  try {
257
304
  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 });
305
+ // Com janela de dados no grid, a série é recortada na largura do plano (o ponto logo fora leva a linha à borda).
306
+ let clip;
307
+ if (model.coordinateSystem === 'cartesian2d' && viewWindowOf(option, model.coordinateIndex)) {
308
+ const rect = coords.get(coordKey('cartesian2d', model.coordinateIndex))?.rect;
309
+ if (rect)
310
+ clip = { x: rect.x, y: 0, width: rect.width, height: this.plotHeight };
311
+ }
312
+ const children = slideOption ? [{ kind: 'group', key: `series/${model.index}/slide`, children: result.nodes, ...(slide ? { slide } : {}) }] : result.nodes;
313
+ nodes.push({ kind: 'group', key: `series/${model.index}`, z: 2 + Number(model.option['z'] ?? 0) / 100, children, ...(clip ? { clip } : {}) });
259
314
  if (result.labels)
260
315
  labels.push(...result.labels);
261
316
  }
@@ -265,6 +320,26 @@ export class ChartInstanceImpl {
265
320
  }
266
321
  if (labels.length)
267
322
  nodes.push({ kind: 'group', key: 'labels', z: 3, children: layoutLabels(labels, measureText) });
323
+ // markPoint: acima das séries e dos rótulos delas, fora da disposição (o selo não some por sobreposição).
324
+ const marks = [];
325
+ for (const model of visible) {
326
+ if (!model.option['markPoint'] || !ctx.legendSelected(model.name))
327
+ continue;
328
+ try {
329
+ const children = markPointNodes(model, ctx);
330
+ if (children.length) {
331
+ const group = { kind: 'group', key: `markPoint/${model.index}`, z: 3.5, silent: true, children };
332
+ nodes.push(group);
333
+ marks.push(group);
334
+ }
335
+ }
336
+ catch (error) {
337
+ logError(`markPoint da série ${model.index}`, error);
338
+ }
339
+ }
340
+ stackOverlappingBadges(marks);
341
+ if (this.sweep)
342
+ nodes.push({ kind: 'group', key: 'sweep', z: 4.5, silent: true, children: sweepNodes(this.sweep, ctx) });
268
343
  // Componentes: a legenda recebe as entradas das séries.
269
344
  const entries = this.legendEntries(normalized.models, ctx);
270
345
  for (const [key, type] of COMPONENT_TYPES) {
@@ -280,9 +355,9 @@ export class ChartInstanceImpl {
280
355
  }
281
356
  });
282
357
  }
283
- this.renderer.setSize(this.width, this.height);
358
+ this.renderer.setSize(this.plotWidth, this.plotHeight);
284
359
  this.renderer.setBackground(typeof option['backgroundColor'] === 'string' ? option['backgroundColor'] : undefined);
285
- this.renderer.setLabel(describeChart(option, visible, this.locale));
360
+ this.renderer.setLabel(null);
286
361
  this.renderer.render(nodes, {
287
362
  animate: ctx.animation.enabled && this.animateNext,
288
363
  duration: ctx.animation.duration,
@@ -291,7 +366,7 @@ export class ChartInstanceImpl {
291
366
  easingUpdate: easingOf(option['animationEasingUpdate'], 'cubicInOut'),
292
367
  });
293
368
  this.animateNext = true;
294
- this.emit('rendered', { componentType: 'series', type: 'rendered' });
369
+ this.emit('rendered', { componentType: 'chart' });
295
370
  }
296
371
  /** Entradas da legenda: as que cada série dá (pizza por item) ou uma por série com nome. */
297
372
  legendEntries(models, ctx) {
@@ -301,9 +376,9 @@ export class ChartInstanceImpl {
301
376
  const type = SERIES_TYPES.get(model.type);
302
377
  const list = type?.legendEntries
303
378
  ? type.legendEntries(model, ctx)
304
- : model.name.startsWith('series\u0000')
379
+ : isUnnamed(model.name)
305
380
  ? []
306
- : [{ name: model.name, color: model.color, icon: model.type === 'line' ? 'line' : 'roundRect', seriesIndex: model.index, decal: model.decal, lineStyle: undefined }];
381
+ : [{ name: model.name, color: model.color, icon: model.type === 'line' ? 'line' : 'rounded-square', seriesIndex: model.index, decal: model.decal, lineStyle: undefined }];
307
382
  for (const entry of list) {
308
383
  if (seen.has(entry.name))
309
384
  continue;
@@ -351,6 +426,9 @@ export class ChartInstanceImpl {
351
426
  return frame.normalized.option['tooltip'][0] ?? { show: false };
352
427
  }
353
428
  onPointer(item, x, y, event) {
429
+ // Arrastando a seleção: sem tooltip nem destaque.
430
+ if (this.sweep)
431
+ return;
354
432
  const frame = this.frame ?? this.build();
355
433
  const tip = this.tooltipOption();
356
434
  const tipOn = !!frame.normalized.option['tooltip'].length && tip['show'] !== false && tip['showContent'] !== false && String(tip['triggerOn'] ?? 'mousemove').includes('mousemove');
@@ -358,10 +436,10 @@ export class ChartInstanceImpl {
358
436
  const seriesItem = item?.componentType === 'series' ? item : null;
359
437
  if (!this.sameItem(this.hover, seriesItem)) {
360
438
  if (this.hover)
361
- this.emit('mouseout', { ...this.hover, type: 'mouseout', event });
439
+ this.emit('pointerleave', { ...this.hover, event });
362
440
  this.hover = seriesItem;
363
441
  if (seriesItem)
364
- this.emit('mouseover', { ...this.fillItem(seriesItem, frame), type: 'mouseover', event });
442
+ this.emit('pointerenter', { ...this.fillItem(seriesItem, frame), event });
365
443
  needsRender = true;
366
444
  }
367
445
  if (tipOn && tip['trigger'] === 'axis') {
@@ -385,7 +463,7 @@ export class ChartInstanceImpl {
385
463
  }
386
464
  if (needsRender) {
387
465
  this.animateNext = true;
388
- this.render();
466
+ this.draw();
389
467
  }
390
468
  }
391
469
  axisAt(x, y, frame) {
@@ -404,16 +482,16 @@ export class ChartInstanceImpl {
404
482
  this.tooltip.hide();
405
483
  const changed = !!this.hover || !!this.pointer;
406
484
  if (this.hover)
407
- this.emit('mouseout', { ...this.hover, type: 'mouseout', event });
485
+ this.emit('pointerleave', { ...this.hover, event });
408
486
  this.hover = null;
409
487
  this.pointer = null;
410
488
  if (changed)
411
- this.render();
489
+ this.draw();
412
490
  }
413
491
  onClick(item, x, y, event) {
414
492
  const frame = this.frame ?? this.build();
415
493
  if (!item) {
416
- this.emit('click', { componentType: 'blank', type: 'click', event });
494
+ this.emit('click', { componentType: 'blank', event });
417
495
  return;
418
496
  }
419
497
  if (item.componentType === 'legend' && item.name !== undefined) {
@@ -432,12 +510,12 @@ export class ChartInstanceImpl {
432
510
  return;
433
511
  }
434
512
  const full = this.fillItem(item, frame);
435
- this.emit('click', { ...full, type: 'click', event });
513
+ this.emit('click', { ...full, event });
436
514
  if (item.componentType === 'series' && item.seriesIndex !== undefined) {
437
515
  const model = frame.normalized.models[item.seriesIndex];
438
516
  const type = model ? SERIES_TYPES.get(model.type) : undefined;
439
517
  if (model && type?.onClick?.(model, item, frame.ctx))
440
- this.render();
518
+ this.draw();
441
519
  const tip = this.tooltipOption();
442
520
  if (String(tip['triggerOn'] ?? '').includes('click') && tip['trigger'] !== 'axis' && model && item.dataIndex !== undefined)
443
521
  this.tooltip.showItem(model, item.dataIndex, x, y);
@@ -452,7 +530,7 @@ export class ChartInstanceImpl {
452
530
  return item;
453
531
  const data = item.dataIndex !== undefined ? model.data[item.dataIndex] : undefined;
454
532
  let name = item.name || data?.name || '';
455
- // Sem nome no item: a categoria do eixo (como o formato entrega em `params.name`).
533
+ // Sem nome no item: a categoria do eixo.
456
534
  if (!name && item.dataIndex !== undefined && model.coordinateSystem !== 'none') {
457
535
  const coord = frame.coords.get(coordKey(model.coordinateSystem, model.coordinateIndex));
458
536
  const base = coord && Object.values(coord.axes).find((a) => a.type === 'category');
@@ -475,7 +553,7 @@ export class ChartInstanceImpl {
475
553
  const frame = this.frame ?? this.build();
476
554
  const legendOpt = (frame.normalized.option['legend'] ?? [])[index] ?? {};
477
555
  this.legendSelected = nextLegendSelection(legendOpt, this.legendSelected, name, this.legendNames(frame));
478
- this.afterLegendChange(name, 'legendselectchanged');
556
+ this.afterLegendChange(name, 'legendtoggle');
479
557
  }
480
558
  afterLegendChange(name, type) {
481
559
  const frame = this.frame ?? this.build();
@@ -483,12 +561,25 @@ export class ChartInstanceImpl {
483
561
  for (const n of this.legendNames(frame))
484
562
  selected[n] = this.legendSelected[n] !== false;
485
563
  this.animateNext = true;
486
- this.render();
487
- this.emit(type, { componentType: 'legend', type, name, selected });
564
+ this.draw();
565
+ this.emit(type, { componentType: 'legend', name, selected });
566
+ }
567
+ // ---------------------------------------------------------------- destaque e tooltip (contrato)
568
+ highlight(target) {
569
+ this.act({ type: 'highlight', seriesIndex: target.series, dataIndex: target.point });
488
570
  }
489
- // ---------------------------------------------------------------- ações
490
- dispatchAction(action) {
491
- if (this.disposed || !isPlainObject(action))
571
+ clearHighlight(target) {
572
+ this.act(target ? { type: 'downplay', seriesIndex: target.series, dataIndex: target.point } : { type: 'downplay' });
573
+ }
574
+ showTooltip(target) {
575
+ this.act({ type: 'showTip', seriesIndex: target.series, dataIndex: target.point, axisValue: target.axisValue });
576
+ }
577
+ hideTooltip() {
578
+ this.act({ type: 'hideTip' });
579
+ }
580
+ // ---------------------------------------------------------------- ações internas
581
+ act(action) {
582
+ if (this.gone || !isPlainObject(action))
492
583
  return;
493
584
  const frame = this.frame ?? this.build();
494
585
  const models = frame.normalized.models;
@@ -547,15 +638,30 @@ export class ChartInstanceImpl {
547
638
  }
548
639
  }
549
640
  this.animateNext = true;
550
- this.render();
551
- this.emit(type, { componentType: 'series', type, seriesIndex: action['seriesIndex'], dataIndex: action['dataIndex'], name: action['name'] });
641
+ this.draw();
642
+ this.emit(type === 'highlight' ? 'highlight' : 'clearhighlight', { componentType: 'series', seriesIndex: action['seriesIndex'], dataIndex: action['dataIndex'], name: action['name'] });
552
643
  return;
553
644
  }
554
645
  case 'showTip': {
555
646
  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);
647
+ const x = pos?.[0] ?? (typeof action['x'] === 'number' ? action['x'] : this.plotWidth / 2);
648
+ const y = pos?.[1] ?? (typeof action['y'] === 'number' ? action['y'] : this.plotHeight / 2);
558
649
  const model = seriesOf()[0];
650
+ // Valor no eixo-base (instante): o tooltip de eixo nesse valor, com o ponteiro de eixo.
651
+ if (action['axisValue'] !== undefined) {
652
+ const target = model ?? models.find((m) => m.coordinateSystem === 'cartesian2d');
653
+ const coord = target ? frame.coords.get(coordKey(target.coordinateSystem, target.coordinateIndex)) : undefined;
654
+ const base = coord && (Object.values(coord.axes).find((a) => a.type === 'category') ?? coord.axes['x']);
655
+ if (coord && base) {
656
+ const value = Number(action['axisValue']);
657
+ this.pointer = { coordKey: coordKey(target.coordinateSystem, target.coordinateIndex), axisDim: base.dim, value };
658
+ const px = base.toPixel(value);
659
+ const pos = base.dim === 'x' ? [px, coord.rect.y + coord.rect.height / 2] : [coord.rect.x + coord.rect.width / 2, px];
660
+ this.tooltip.showAxis(coord, base, value, pos[0], pos[1]);
661
+ this.draw();
662
+ }
663
+ return;
664
+ }
559
665
  if (model) {
560
666
  const idx = indicesOf(model);
561
667
  const dataIndex = idx === 'all' ? 0 : idx[0];
@@ -570,7 +676,7 @@ export class ChartInstanceImpl {
570
676
  this.pointer = { coordKey: coordKey(model.coordinateSystem, model.coordinateIndex), axisDim: base.dim, value: dataIndex };
571
677
  this.tooltip.showAxis(coord, base, dataIndex, x, y);
572
678
  void value;
573
- this.render();
679
+ this.draw();
574
680
  return;
575
681
  }
576
682
  }
@@ -582,7 +688,7 @@ export class ChartInstanceImpl {
582
688
  if (found) {
583
689
  this.pointer = { coordKey: found.key, axisDim: found.axis.dim, value: found.value };
584
690
  this.tooltip.showAxis(found.coord, found.axis, found.value, action['x'], action['y']);
585
- this.render();
691
+ this.draw();
586
692
  }
587
693
  }
588
694
  return;
@@ -591,56 +697,150 @@ export class ChartInstanceImpl {
591
697
  this.tooltip.hide();
592
698
  if (this.pointer) {
593
699
  this.pointer = null;
594
- this.render();
700
+ this.draw();
595
701
  }
596
702
  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');
703
+ }
704
+ }
705
+ // ---------------------------------------------------------------- seleção por arrasto, Ctrl + roda, duplo clique
706
+ sweepOption() {
707
+ const frame = this.frame ?? this.build();
708
+ return toArray(frame.normalized.option['sweepSelect'])[0] ?? null;
709
+ }
710
+ /** Grid, eixo X e posição local do ponteiro, quando ele está dentro do plano do grid do componente. */
711
+ sweepTarget(e) {
712
+ const option = this.sweepOption();
713
+ if (!option)
714
+ return null;
715
+ const grid = option.gridIndex ?? 0;
716
+ const coord = (this.frame ?? this.build()).coords.get(coordKey('cartesian2d', grid));
717
+ const axis = coord?.xAxes[0];
718
+ if (!coord || !axis)
719
+ return null;
720
+ const box = this.renderer.svg.getBoundingClientRect();
721
+ const x = e.clientX - box.left;
722
+ const y = e.clientY - box.top;
723
+ const r = coord.rect;
724
+ if (x < r.x || x > r.x + r.width || y < r.y || y > r.y + r.height)
725
+ return null;
726
+ return { grid, axis, x };
727
+ }
728
+ listenSweep() {
729
+ const on = (type, fn, opts) => {
730
+ this.dom.addEventListener(type, fn, opts);
731
+ this.domListeners.push([type, fn, opts]);
732
+ };
733
+ const valueAt = (axis, px) => (axis.type === 'category' ? Math.round(axis.fromPixel(px)) : axis.fromPixel(px));
734
+ on('pointerdown', (e) => {
735
+ // Um dedo continua rolando a página: arrasto de seleção só com mouse ou caneta.
736
+ if (e.button !== 0 || e.pointerType === 'touch' || !this.sweepOption() || this.sweepOption()?.drag === false)
737
+ return;
738
+ const target = this.sweepTarget(e);
739
+ if (!target)
606
740
  return;
741
+ this.sweep = { gridIndex: target.grid, from: target.x, to: target.x };
742
+ try {
743
+ this.dom.setPointerCapture?.(e.pointerId);
744
+ }
745
+ catch {
746
+ // ponteiro sintético (testes): sem captura
607
747
  }
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');
748
+ });
749
+ on('pointermove', (e) => {
750
+ if (!this.sweep)
751
+ return;
752
+ const coord = (this.frame ?? this.build()).coords.get(coordKey('cartesian2d', this.sweep.gridIndex));
753
+ if (!coord)
754
+ return;
755
+ const box = this.renderer.svg.getBoundingClientRect();
756
+ const x = Math.max(coord.rect.x, Math.min(coord.rect.x + coord.rect.width, e.clientX - box.left));
757
+ if (x === this.sweep.to)
758
+ return;
759
+ this.sweep = { ...this.sweep, to: x };
760
+ this.tooltip.hide();
761
+ this.pointer = null;
762
+ this.animateNext = false;
763
+ this.draw();
764
+ this.emit('sweepmove', { componentType: 'sweepSelect' });
765
+ });
766
+ const finish = (e, cancel) => {
767
+ const sweep = this.sweep;
768
+ if (!sweep)
615
769
  return;
770
+ this.sweep = null;
771
+ try {
772
+ this.dom.releasePointerCapture?.(e.pointerId);
773
+ }
774
+ catch {
775
+ // idem
616
776
  }
617
- case 'legendScroll':
618
- scrollLegend(frame.ctx, Number(action['legendIndex'] ?? 0), Number(action['scrollDataIndex'] ?? 0) - Number(frame.ctx.componentState.get('legend-0/page') ?? 0));
777
+ this.animateNext = false;
778
+ this.draw();
779
+ const coord = (this.frame ?? this.build()).coords.get(coordKey('cartesian2d', sweep.gridIndex));
780
+ const axis = coord?.xAxes[0];
781
+ if (cancel || !axis || Math.abs(sweep.to - sweep.from) < 4)
619
782
  return;
620
- default:
621
- this.emit(type, { componentType: 'series', type });
622
- }
783
+ const a = valueAt(axis, Math.min(sweep.from, sweep.to));
784
+ const b = valueAt(axis, Math.max(sweep.from, sweep.to));
785
+ this.emit('sweepselect', { componentType: 'sweepSelect', data: { start: Math.min(a, b), end: Math.max(a, b) } });
786
+ };
787
+ on('pointerup', (e) => finish(e, false));
788
+ on('pointercancel', (e) => finish(e, true));
789
+ on('keydown', (e) => {
790
+ if (e.key !== 'Escape' || !this.sweep)
791
+ return;
792
+ this.sweep = null;
793
+ this.animateNext = false;
794
+ this.draw();
795
+ this.emit('sweepcancel', { componentType: 'sweepSelect' });
796
+ });
797
+ // Roda só com Ctrl (ou a pinça do touchpad): a roda sozinha rola a página, nunca é presa.
798
+ on('wheel', (e) => {
799
+ if (!e.ctrlKey)
800
+ return;
801
+ const target = this.sweepTarget(e);
802
+ if (!target)
803
+ return;
804
+ e.preventDefault();
805
+ this.emit('sweepzoom', { componentType: 'sweepSelect', data: { center: target.axis.fromPixel(target.x), factor: e.deltaY < 0 ? 0.8 : 1.25 } });
806
+ }, { passive: false });
807
+ on('dblclick', (e) => {
808
+ if (this.sweepTarget(e))
809
+ this.emit('sweepreset', { componentType: 'sweepSelect' });
810
+ });
623
811
  }
624
812
  // ---------------------------------------------------------------- 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
- }
813
+ listen(type, handler) {
814
+ const entry = { event: type.toLowerCase(), handler };
815
+ this.listeners.push(entry);
816
+ return () => {
817
+ const at = this.listeners.indexOf(entry);
818
+ if (at >= 0)
819
+ this.listeners.splice(at, 1);
820
+ };
637
821
  }
638
822
  emit(type, params) {
639
- const event = { ...params, type };
823
+ if (!this.listeners.length)
824
+ return;
825
+ const model = params.seriesIndex !== undefined ? this.frame?.normalized.models[params.seriesIndex] : undefined;
826
+ const event = {
827
+ type,
828
+ part: partOf(params.componentType),
829
+ seriesIndex: params.seriesIndex,
830
+ seriesId: model?.option['id'],
831
+ seriesName: params.seriesName ?? (model && !isUnnamed(model.name) ? model.name : undefined),
832
+ pointIndex: params.dataIndex,
833
+ name: params.name,
834
+ value: params.value,
835
+ detail: params.data,
836
+ selected: params.selected,
837
+ native: params.event,
838
+ };
839
+ for (const key of Object.keys(event))
840
+ if (event[key] === undefined)
841
+ delete event[key];
640
842
  for (const l of [...this.listeners]) {
641
- if (l.event.toLowerCase() !== type.toLowerCase())
642
- continue;
643
- if (l.query && !matchesQuery(l.query, event))
843
+ if (l.event !== type)
644
844
  continue;
645
845
  try {
646
846
  l.handler(event);
@@ -651,9 +851,9 @@ export class ChartInstanceImpl {
651
851
  }
652
852
  }
653
853
  // ---------------------------------------------------------------- conversão de coordenadas
654
- coordFor(finder) {
854
+ coordFor(target) {
655
855
  const frame = this.frame ?? this.build();
656
- const f = typeof finder === 'string' ? { [`${finder}Index`]: 0 } : finder;
856
+ const f = 'series' in target ? { seriesIndex: target.series } : { gridIndex: target.plane };
657
857
  const get = (type, index) => frame.coords.get(coordKey(type, index));
658
858
  if (f['seriesIndex'] !== undefined) {
659
859
  const model = frame.normalized.models[f['seriesIndex']];
@@ -666,33 +866,20 @@ export class ChartInstanceImpl {
666
866
  const coord = get('cartesian2d', f['gridIndex']);
667
867
  return coord ? { coord } : undefined;
668
868
  }
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
869
  return undefined;
683
870
  }
684
- convertToPixel(finder, value) {
685
- const found = this.coordFor(finder);
871
+ toPixel(target, value) {
872
+ const found = this.coordFor(target);
686
873
  if (!found)
687
874
  return undefined;
688
875
  if (found.axisDim) {
689
876
  const axis = found.coord.axes[found.axisDim];
690
877
  return axis ? axis.toPixel(Array.isArray(value) ? value[0] : value) : undefined;
691
878
  }
692
- return found.coord.dataToPoint(Array.isArray(value) ? value : [value]);
879
+ return found.coord.dataToPoint(Array.isArray(value) ? [...value] : [value]);
693
880
  }
694
- convertFromPixel(finder, value) {
695
- const found = this.coordFor(finder);
881
+ fromPixel(target, value) {
882
+ const found = this.coordFor(target);
696
883
  if (!found)
697
884
  return undefined;
698
885
  if (found.axisDim) {
@@ -706,8 +893,8 @@ export class ChartInstanceImpl {
706
893
  return [x.fromPixel(value[0]), y.fromPixel(value[1])];
707
894
  return undefined;
708
895
  }
709
- containPixel(finder, value) {
710
- const found = this.coordFor(finder);
896
+ containsPixel(target, value) {
897
+ const found = this.coordFor(target);
711
898
  if (!found)
712
899
  return false;
713
900
  if (found.coord.contains)
@@ -716,33 +903,64 @@ export class ChartInstanceImpl {
716
903
  return value[0] >= r.x && value[0] <= r.x + r.width && value[1] >= r.y && value[1] <= r.y + r.height;
717
904
  }
718
905
  // ---------------------------------------------------------------- exportar
719
- renderToSVGString() {
906
+ toSVG() {
720
907
  return this.renderer.toString();
721
908
  }
722
- getDataURL(opts = {}) {
723
- let svg = this.renderToSVGString();
724
- if (opts.backgroundColor)
725
- svg = svg.replace(/<svg([^>]*?)style="/, `<svg$1style="background-color:${opts.backgroundColor};`);
909
+ toDataURL(options = {}) {
910
+ let svg = this.toSVG();
911
+ if (options.background)
912
+ svg = svg.replace(/<svg([^>]*?)style="/, `<svg$1style="background-color:${options.background};`);
726
913
  return `data:image/svg+xml;charset=UTF-8,${encodeURIComponent(svg)}`;
727
914
  }
728
915
  }
729
916
  // ---------------------------------------------------------------- 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
- });
917
+ /**
918
+ * Selos com `stackOverlap` (último valor de cada série) que colidem sobem acima do anterior, na ordem em que vieram:
919
+ * nenhum some e nenhum esconde o outro.
920
+ */
921
+ export function stackOverlappingBadges(groups) {
922
+ const badges = [];
923
+ const collect = (node) => {
924
+ if (node.kind === 'group')
925
+ node.children.forEach(collect);
926
+ else if (node.kind === 'text' && node.stackOverlap)
927
+ badges.push(node);
928
+ };
929
+ groups.forEach(collect);
930
+ if (badges.length < 2)
931
+ return;
932
+ const placed = [];
933
+ const boxOf = (n) => {
934
+ const m = measureText(n.text, n.style);
935
+ const [pt, pr, pb, pl] = paddingOf(n.style.padding);
936
+ const w = m.width + pl + pr;
937
+ const h = m.height + pt + pb;
938
+ const align = n.style.align ?? 'left';
939
+ const valign = n.style.verticalAlign ?? 'top';
940
+ const x = align === 'center' ? n.x - w / 2 : align === 'right' ? n.x - w : n.x;
941
+ const y = valign === 'middle' ? n.y - h / 2 : valign === 'bottom' ? n.y - h : n.y;
942
+ return { x, y, w, h };
943
+ };
944
+ for (const badge of badges) {
945
+ let box = boxOf(badge);
946
+ for (let guard = 0; guard < badges.length; guard++) {
947
+ const hit = placed.find((p) => box.x < p.x + p.w && p.x < box.x + box.w && box.y < p.y + p.h && p.y < box.y + box.h);
948
+ if (!hit)
949
+ break;
950
+ const shift = box.y + box.h - hit.y + 2;
951
+ badge.y -= shift;
952
+ box = { ...box, y: box.y - shift };
953
+ }
954
+ placed.push(box);
955
+ }
956
+ }
957
+ function paddingOf(padding) {
958
+ if (padding === undefined)
959
+ return [0, 0, 0, 0];
960
+ if (typeof padding === 'number')
961
+ return [padding, padding, padding, padding];
962
+ const [a = 0, b = a, c = a, d = b] = padding;
963
+ return [a, b, c, d];
746
964
  }
747
965
  function getComputedStyleSafe(el, prop) {
748
966
  try {
@@ -753,21 +971,23 @@ function getComputedStyleSafe(el, prop) {
753
971
  }
754
972
  }
755
973
  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).
974
+ // Um pedaço que falha não derruba o gráfico inteiro; o erro vai para o console.
757
975
  console.error(`[hermes-charts] ${where}:`, error);
758
976
  }
759
977
  // ---------------------------------------------------------------- registro por elemento
760
978
  const instances = new WeakMap();
761
- export function createInstance(dom, theme, opts) {
762
- const existing = instances.get(dom);
763
- if (existing && !existing.isDisposed())
979
+ /** Cria o gráfico no elemento (um por elemento: criar de novo descarta o anterior). Desenho sempre em SVG. */
980
+ export function createChart(element, options) {
981
+ const existing = instances.get(element);
982
+ if (existing && !existing.disposed)
764
983
  existing.dispose();
765
- const instance = new ChartInstanceImpl(dom, theme, opts);
766
- instances.set(dom, instance);
767
- return instance;
984
+ const chart = new Chart(element, options);
985
+ instances.set(element, chart);
986
+ return chart;
768
987
  }
769
- export function instanceByDom(dom) {
770
- const inst = instances.get(dom);
771
- return inst && !inst.isDisposed() ? inst : undefined;
988
+ /** O gráfico vivo no elemento, se houver. */
989
+ export function chartOf(element) {
990
+ const chart = instances.get(element);
991
+ return chart && !chart.disposed ? chart : undefined;
772
992
  }
773
993
  //# sourceMappingURL=instance.js.map