chartist 1.5.0 → 2.0.0-rc.2

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 (138) hide show
  1. package/README.md +45 -101
  2. package/dist/_settings.scss +1 -89
  3. package/dist/chartist.min.css +2 -0
  4. package/dist/chartist.min.js +2 -0
  5. package/dist/compat.cjs +624 -0
  6. package/dist/compat.cjs.map +1 -0
  7. package/dist/compat.d.cts +202 -0
  8. package/dist/compat.d.ts +201 -0
  9. package/dist/compat.js +603 -0
  10. package/dist/compat.js.map +1 -0
  11. package/dist/compat.min.js +2 -0
  12. package/dist/compat.min.js.map +1 -0
  13. package/dist/compat.umd.js +2 -0
  14. package/dist/compat.umd.js.map +1 -0
  15. package/dist/index.cjs +5970 -3489
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.css +903 -2
  18. package/dist/index.css.map +1 -1
  19. package/dist/index.d.cts +5167 -0
  20. package/dist/index.d.ts +5166 -8
  21. package/dist/index.js +5935 -3484
  22. package/dist/index.js.map +1 -1
  23. package/dist/index.min.css +2 -0
  24. package/dist/index.min.css.map +1 -0
  25. package/dist/index.min.js +2 -0
  26. package/dist/index.min.js.map +1 -0
  27. package/dist/index.scss +1 -204
  28. package/dist/index.umd.js +2 -2
  29. package/dist/index.umd.js.map +1 -1
  30. package/dist/scss/_settings.scss +155 -0
  31. package/dist/scss/index.scss +262 -0
  32. package/dist/scss/themes/accessible.scss +53 -0
  33. package/dist/themes/accessible.css +34 -0
  34. package/dist/themes/accessible.min.css +1 -0
  35. package/package.json +238 -12
  36. package/dist/axes/AutoScaleAxis.d.ts +0 -12
  37. package/dist/axes/AutoScaleAxis.d.ts.map +0 -1
  38. package/dist/axes/Axis.d.ts +0 -40
  39. package/dist/axes/Axis.d.ts.map +0 -1
  40. package/dist/axes/FixedScaleAxis.d.ts +0 -11
  41. package/dist/axes/FixedScaleAxis.d.ts.map +0 -1
  42. package/dist/axes/StepAxis.d.ts +0 -9
  43. package/dist/axes/StepAxis.d.ts.map +0 -1
  44. package/dist/axes/index.d.ts +0 -6
  45. package/dist/axes/index.d.ts.map +0 -1
  46. package/dist/axes/types.d.ts +0 -5
  47. package/dist/axes/types.d.ts.map +0 -1
  48. package/dist/charts/BarChart/BarChart.d.ts +0 -50
  49. package/dist/charts/BarChart/BarChart.d.ts.map +0 -1
  50. package/dist/charts/BarChart/BarChart.types.d.ts +0 -66
  51. package/dist/charts/BarChart/BarChart.types.d.ts.map +0 -1
  52. package/dist/charts/BarChart/index.d.ts +0 -3
  53. package/dist/charts/BarChart/index.d.ts.map +0 -1
  54. package/dist/charts/BaseChart.d.ts +0 -47
  55. package/dist/charts/BaseChart.d.ts.map +0 -1
  56. package/dist/charts/LineChart/LineChart.d.ts +0 -97
  57. package/dist/charts/LineChart/LineChart.d.ts.map +0 -1
  58. package/dist/charts/LineChart/LineChart.types.d.ts +0 -83
  59. package/dist/charts/LineChart/LineChart.types.d.ts.map +0 -1
  60. package/dist/charts/LineChart/index.d.ts +0 -3
  61. package/dist/charts/LineChart/index.d.ts.map +0 -1
  62. package/dist/charts/PieChart/PieChart.d.ts +0 -101
  63. package/dist/charts/PieChart/PieChart.d.ts.map +0 -1
  64. package/dist/charts/PieChart/PieChart.types.d.ts +0 -95
  65. package/dist/charts/PieChart/PieChart.types.d.ts.map +0 -1
  66. package/dist/charts/PieChart/index.d.ts +0 -3
  67. package/dist/charts/PieChart/index.d.ts.map +0 -1
  68. package/dist/charts/index.d.ts +0 -6
  69. package/dist/charts/index.d.ts.map +0 -1
  70. package/dist/charts/types.d.ts +0 -11
  71. package/dist/charts/types.d.ts.map +0 -1
  72. package/dist/core/constants.d.ts +0 -13
  73. package/dist/core/constants.d.ts.map +0 -1
  74. package/dist/core/creation.d.ts +0 -61
  75. package/dist/core/creation.d.ts.map +0 -1
  76. package/dist/core/data/bounds.d.ts +0 -14
  77. package/dist/core/data/bounds.d.ts.map +0 -1
  78. package/dist/core/data/data.d.ts +0 -23
  79. package/dist/core/data/data.d.ts.map +0 -1
  80. package/dist/core/data/highLow.d.ts +0 -13
  81. package/dist/core/data/highLow.d.ts.map +0 -1
  82. package/dist/core/data/index.d.ts +0 -7
  83. package/dist/core/data/index.d.ts.map +0 -1
  84. package/dist/core/data/normalize.d.ts +0 -11
  85. package/dist/core/data/normalize.d.ts.map +0 -1
  86. package/dist/core/data/segments.d.ts +0 -29
  87. package/dist/core/data/segments.d.ts.map +0 -1
  88. package/dist/core/data/serialize.d.ts +0 -12
  89. package/dist/core/data/serialize.d.ts.map +0 -1
  90. package/dist/core/index.d.ts +0 -8
  91. package/dist/core/index.d.ts.map +0 -1
  92. package/dist/core/lang.d.ts +0 -23
  93. package/dist/core/lang.d.ts.map +0 -1
  94. package/dist/core/math.d.ts +0 -42
  95. package/dist/core/math.d.ts.map +0 -1
  96. package/dist/core/optionsProvider.d.ts +0 -15
  97. package/dist/core/optionsProvider.d.ts.map +0 -1
  98. package/dist/core/types.d.ts +0 -274
  99. package/dist/core/types.d.ts.map +0 -1
  100. package/dist/event/EventEmitter.d.ts +0 -27
  101. package/dist/event/EventEmitter.d.ts.map +0 -1
  102. package/dist/event/index.d.ts +0 -2
  103. package/dist/event/index.d.ts.map +0 -1
  104. package/dist/index.d.ts.map +0 -1
  105. package/dist/interpolation/cardinal.d.ts +0 -30
  106. package/dist/interpolation/cardinal.d.ts.map +0 -1
  107. package/dist/interpolation/index.d.ts +0 -6
  108. package/dist/interpolation/index.d.ts.map +0 -1
  109. package/dist/interpolation/monotoneCubic.d.ts +0 -30
  110. package/dist/interpolation/monotoneCubic.d.ts.map +0 -1
  111. package/dist/interpolation/none.d.ts +0 -22
  112. package/dist/interpolation/none.d.ts.map +0 -1
  113. package/dist/interpolation/simple.d.ts +0 -30
  114. package/dist/interpolation/simple.d.ts.map +0 -1
  115. package/dist/interpolation/step.d.ts +0 -26
  116. package/dist/interpolation/step.d.ts.map +0 -1
  117. package/dist/svg/Svg.d.ts +0 -201
  118. package/dist/svg/Svg.d.ts.map +0 -1
  119. package/dist/svg/SvgList.d.ts +0 -31
  120. package/dist/svg/SvgList.d.ts.map +0 -1
  121. package/dist/svg/SvgPath.d.ts +0 -125
  122. package/dist/svg/SvgPath.d.ts.map +0 -1
  123. package/dist/svg/animation.d.ts +0 -37
  124. package/dist/svg/animation.d.ts.map +0 -1
  125. package/dist/svg/index.d.ts +0 -6
  126. package/dist/svg/index.d.ts.map +0 -1
  127. package/dist/svg/types.d.ts +0 -55
  128. package/dist/svg/types.d.ts.map +0 -1
  129. package/dist/utils/extend.d.ts +0 -10
  130. package/dist/utils/extend.d.ts.map +0 -1
  131. package/dist/utils/functional.d.ts +0 -31
  132. package/dist/utils/functional.d.ts.map +0 -1
  133. package/dist/utils/index.d.ts +0 -5
  134. package/dist/utils/index.d.ts.map +0 -1
  135. package/dist/utils/types.d.ts +0 -7
  136. package/dist/utils/types.d.ts.map +0 -1
  137. package/dist/utils/utils.d.ts +0 -31
  138. package/dist/utils/utils.d.ts.map +0 -1
@@ -0,0 +1,624 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __exportAll = (all, no_symbols) => {
10
+ let target = {};
11
+ for (var name in all) __defProp(target, name, {
12
+ get: all[name],
13
+ enumerable: true
14
+ });
15
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
16
+ return target;
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
20
+ key = keys[i];
21
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
22
+ get: ((k) => from[k]).bind(null, key),
23
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
24
+ });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
29
+ value: mod,
30
+ enumerable: true
31
+ }) : target, mod));
32
+ //#endregion
33
+ let src_index = require("./index.cjs");
34
+ src_index = __toESM(src_index, 1);
35
+ //#region src/chart/types.ts
36
+ /** The same handle, seen without event typing. Internal. */
37
+ const untyped = (handle) => handle;
38
+ //#endregion
39
+ //#region src/core/data/range.ts
40
+ /**
41
+ * Repairs a value range that's empty because high is at or below low, from
42
+ * flat data or a misconfigured `high`/`low`: sets the bound on the other
43
+ * side of zero to 0, or gives `0` to `1` when there are no values. Chart
44
+ * code that overrides a bound after `getHighLow` calls it again.
45
+ *
46
+ * @param highLow The range to check.
47
+ * @returns The range, or the repaired one.
48
+ */
49
+ function nonEmptyRange(highLow) {
50
+ const { high, low } = highLow;
51
+ if (high > low) return {
52
+ high,
53
+ low
54
+ };
55
+ if (low === 0) return {
56
+ high: 1,
57
+ low
58
+ };
59
+ if (low < 0) return {
60
+ high: 0,
61
+ low
62
+ };
63
+ if (high > 0) return {
64
+ high,
65
+ low: 0
66
+ };
67
+ return {
68
+ high: 1,
69
+ low: 0
70
+ };
71
+ }
72
+ //#endregion
73
+ //#region src/compat/index.ts
74
+ /**
75
+ * The compat plugin: 1.x events, `Svg` wrappers and 1.x plugins on v2
76
+ * charts. Opt-in: `plugins: [compat]`, or convert a 1.x plugin with
77
+ * `fromLegacyPlugin()`.
78
+ *
79
+ * Imports only from the package root, so the built `chartist/compat` entry
80
+ * shares the core with `chartist` instead of bundling a copy.
81
+ */
82
+ var compat_exports = /* @__PURE__ */ __exportAll({
83
+ compat: () => compat,
84
+ fromLegacyPlugin: () => fromLegacyPlugin
85
+ });
86
+ /** The 1.x events the compat plugin emits. */
87
+ const LEGACY_EVENTS = [
88
+ "data",
89
+ "optionsChanged",
90
+ "draw",
91
+ "created",
92
+ "animationBegin",
93
+ "animationEnd"
94
+ ];
95
+ const states = /* @__PURE__ */ new WeakMap();
96
+ /**
97
+ * Where `Svg#animate()` looks for the emitter of an element's animations
98
+ * when it gets none (on the element or an ancestor). The core reads the
99
+ * same global symbol.
100
+ */
101
+ const ANIMATION_EMITTER = Symbol.for("chartist.animationEmitter");
102
+ /**
103
+ * Where a preset's chart handle holds its preset state from the moment
104
+ * plugins install. The core sets the same global symbol.
105
+ */
106
+ const PRESET_STATE = Symbol.for("chartist.presetState");
107
+ const presetOf = (handle) => handle[PRESET_STATE];
108
+ /** The 1.x class of each preset, for 0.x and 1.x type checks in plugins. */
109
+ const CLASSES = {
110
+ line: src_index.LineChart,
111
+ bar: src_index.BarChart,
112
+ pie: src_index.PieChart
113
+ };
114
+ /** Copies plain arrays and objects; other values are kept by reference. */
115
+ function mutableCopy(value) {
116
+ if (Array.isArray(value)) return value.map(mutableCopy);
117
+ if (value && typeof value === "object") {
118
+ const proto = Object.getPrototypeOf(value);
119
+ if (proto === Object.prototype || proto === null) return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, mutableCopy(entry)]));
120
+ }
121
+ return value;
122
+ }
123
+ const isLegacyAxisClass = (type) => typeof type === "function" && typeof type.prototype?.projectValue === "function";
124
+ function chartRectOf(layout) {
125
+ const { area, padding } = layout;
126
+ return {
127
+ x1: area.x1,
128
+ x2: area.x2,
129
+ y1: area.y1,
130
+ y2: area.y2,
131
+ padding,
132
+ width() {
133
+ return this.x2 - this.x1;
134
+ },
135
+ height() {
136
+ return this.y1 - this.y2;
137
+ }
138
+ };
139
+ }
140
+ /** The 1.x axes, constructed as 1.x `LineChart.createChart` does. */
141
+ function lineAxes(options, data, chartRect) {
142
+ const normalized = (0, src_index.normalizeData)(data, options.reverseData, true);
143
+ const series = normalized.series;
144
+ return {
145
+ axisX: isLegacyAxisClass(options.axisX.type) ? new options.axisX.type(src_index.axisUnits.x, series, chartRect, options.axisX) : new src_index.StepAxis(src_index.axisUnits.x, series, chartRect, {
146
+ ...options.axisX,
147
+ ticks: normalized.labels,
148
+ stretch: options.fullWidth
149
+ }),
150
+ axisY: isLegacyAxisClass(options.axisY.type) ? new options.axisY.type(src_index.axisUnits.y, series, chartRect, options.axisY) : new src_index.AutoScaleAxis(src_index.axisUnits.y, series, chartRect, {
151
+ ...options.axisY,
152
+ high: (0, src_index.isNumeric)(options.high) ? options.high : options.axisY.high,
153
+ low: (0, src_index.isNumeric)(options.low) ? options.low : options.axisY.low
154
+ })
155
+ };
156
+ }
157
+ /** Sums per index across series, as 1.x `BarChart` stacks them. */
158
+ function serialSums(series) {
159
+ return (0, src_index.serialMap)(series, (...values) => values.reduce((sum, value) => ({
160
+ x: sum.x + ((0, src_index.safeHasProperty)(value, "x") ? value.x : 0),
161
+ y: sum.y + ((0, src_index.safeHasProperty)(value, "y") ? value.y : 0)
162
+ }), {
163
+ x: 0,
164
+ y: 0
165
+ }));
166
+ }
167
+ /** The 1.x axes, constructed as 1.x `BarChart.createChart` does. */
168
+ function barAxes(options, data, chartRect) {
169
+ const horizontal = Boolean(options.horizontalBars);
170
+ const valueDimension = horizontal ? "x" : "y";
171
+ const labelDimension = horizontal ? "y" : "x";
172
+ const normalized = (0, src_index.normalizeData)(data, options.reverseData, valueDimension, true);
173
+ const series = normalized.series;
174
+ const found = options.stackBars && options.stackMode !== true && series.length ? (0, src_index.getHighLow)([serialSums(series)], options, valueDimension) : (0, src_index.getHighLow)(series, options, valueDimension);
175
+ const highLow = nonEmptyRange({
176
+ high: typeof options.high === "number" ? options.high : found.high,
177
+ low: typeof options.low === "number" ? options.low : found.low
178
+ });
179
+ const valueOptions = horizontal ? options.axisX : options.axisY;
180
+ const labelOptions = horizontal ? options.axisY : options.axisX;
181
+ const valueAxis = new (isLegacyAxisClass(valueOptions.type) ? valueOptions.type : src_index.AutoScaleAxis)(src_index.axisUnits[valueDimension], series, chartRect, {
182
+ ...valueOptions,
183
+ highLow,
184
+ referenceValue: 0
185
+ });
186
+ const labelAxis = isLegacyAxisClass(labelOptions.type) ? new labelOptions.type(src_index.axisUnits[labelDimension], series, chartRect, labelOptions) : new src_index.StepAxis(src_index.axisUnits[labelDimension], series, chartRect, { ticks: options.distributeSeries && options.stackBars ? normalized.labels.slice(0, 1) : normalized.labels });
187
+ return horizontal ? {
188
+ axisX: valueAxis,
189
+ axisY: labelAxis
190
+ } : {
191
+ axisX: labelAxis,
192
+ axisY: valueAxis
193
+ };
194
+ }
195
+ const AXES = {
196
+ line: lineAxes,
197
+ bar: barAxes,
198
+ pie: () => ({
199
+ axisX: void 0,
200
+ axisY: void 0
201
+ })
202
+ };
203
+ /** 1.x `draw` payloads in 1.x order, then `created`. */
204
+ function emitDrawEvents(handle, state, scene, svgElement, elementOf) {
205
+ const options = state.preset?.options;
206
+ const data = state.copy ?? state.preset?.data;
207
+ const chartRect = chartRectOf(state.layout);
208
+ const { axisX, axisY } = AXES[state.preset.kind](options, data, chartRect);
209
+ const axisOf = (dimension) => dimension === "x" ? axisX : axisY;
210
+ const wrap = (element) => new src_index.Svg(element);
211
+ const draw = (payload) => untyped(handle).emit("draw", payload);
212
+ const parts = (node) => {
213
+ const element = elementOf(node);
214
+ return {
215
+ element: wrap(element),
216
+ group: wrap(element.parentElement)
217
+ };
218
+ };
219
+ const grids = (0, src_index.findAll)(scene, { role: "grid" });
220
+ const allLabels = (0, src_index.findAll)(scene, { role: "label" });
221
+ const labels = allLabels.filter((n) => n.ctx.kind === "axis");
222
+ const sliceLabels = allLabels.filter((n) => n.ctx.kind === "pie");
223
+ const order = [...new Set([...grids, ...labels].map((n) => n.ctx.axis.dimension))];
224
+ for (const dimension of order) {
225
+ const onAxis = (node) => node.ctx.axis.dimension === dimension;
226
+ const indexes = [...new Set([...grids, ...labels].filter(onAxis).map((n) => n.ctx.index))].sort((a, b) => a - b);
227
+ for (const index of indexes) {
228
+ const grid = grids.find((n) => onAxis(n) && n.ctx.index === index);
229
+ if (grid) {
230
+ const { x1, y1, x2, y2 } = grid.attrs;
231
+ draw({
232
+ type: "grid",
233
+ axis: axisOf(dimension),
234
+ index,
235
+ ...parts(grid),
236
+ x1,
237
+ y1,
238
+ x2,
239
+ y2
240
+ });
241
+ }
242
+ const label = labels.find((n) => onAxis(n) && n.ctx.index === index);
243
+ if (label) {
244
+ const { x, y, width, height } = label.attrs;
245
+ draw({
246
+ type: "label",
247
+ axis: axisOf(dimension),
248
+ index,
249
+ ...parts(label),
250
+ text: label.ctx.text,
251
+ x,
252
+ y,
253
+ width,
254
+ height
255
+ });
256
+ }
257
+ }
258
+ }
259
+ for (const background of (0, src_index.findAll)(scene, { role: "gridBackground" })) draw({
260
+ type: "gridBackground",
261
+ ...parts(background)
262
+ });
263
+ const rawSeries = data.series ?? [];
264
+ for (const series of (0, src_index.findAll)(scene, { role: "series" })) {
265
+ const seriesIndex = series.ctx.seriesIndex;
266
+ const common = {
267
+ series: rawSeries[options.reverseData ? rawSeries.length - 1 - seriesIndex : seriesIndex],
268
+ seriesIndex,
269
+ axisX,
270
+ axisY
271
+ };
272
+ const children = series.children;
273
+ const slice = children.find((n) => n.role === "slice");
274
+ if (slice) {
275
+ const ctx = slice.ctx;
276
+ draw({
277
+ type: "slice",
278
+ value: ctx.value,
279
+ totalDataSum: ctx.totalDataSum,
280
+ index: ctx.index,
281
+ meta: ctx.meta,
282
+ series: ctx.series,
283
+ ...parts(slice),
284
+ path: ctx.path.clone(),
285
+ center: ctx.center,
286
+ radius: ctx.radius,
287
+ startAngle: ctx.startAngle,
288
+ endAngle: ctx.endAngle,
289
+ chartRect
290
+ });
291
+ const label = sliceLabels.find((n) => n.ctx.index === ctx.index);
292
+ if (label) {
293
+ const labelCtx = label.ctx;
294
+ draw({
295
+ type: "label",
296
+ index: labelCtx.index,
297
+ ...parts(label),
298
+ text: labelCtx.text,
299
+ chartRect,
300
+ series: labelCtx.series,
301
+ meta: labelCtx.meta,
302
+ x: labelCtx.x,
303
+ y: labelCtx.y
304
+ });
305
+ }
306
+ continue;
307
+ }
308
+ for (const barNode of children.filter((n) => n.role === "bar")) {
309
+ const ctx = barNode.ctx;
310
+ draw({
311
+ type: "bar",
312
+ value: ctx.value,
313
+ index: ctx.valueIndex,
314
+ meta: ctx.meta,
315
+ series: ctx.series,
316
+ seriesIndex,
317
+ axisX,
318
+ axisY,
319
+ chartRect,
320
+ ...parts(barNode),
321
+ x1: ctx.x1,
322
+ y1: ctx.y1,
323
+ x2: ctx.x2,
324
+ y2: ctx.y2
325
+ });
326
+ }
327
+ for (const point of children.filter((n) => n.role === "point")) {
328
+ const ctx = point.ctx;
329
+ draw({
330
+ type: "point",
331
+ value: ctx.value,
332
+ index: ctx.valueIndex,
333
+ meta: ctx.meta,
334
+ ...common,
335
+ ...parts(point),
336
+ x: ctx.x,
337
+ y: ctx.y,
338
+ chartRect
339
+ });
340
+ }
341
+ const shapes = [...children.filter((n) => n.role === "line"), ...children.filter((n) => n.role === "area").reverse()];
342
+ for (const shape of shapes) {
343
+ const ctx = shape.ctx;
344
+ draw({
345
+ type: shape.role,
346
+ values: ctx.values,
347
+ path: ctx.path.clone(),
348
+ chartRect,
349
+ index: seriesIndex,
350
+ ...common,
351
+ meta: ctx.meta,
352
+ ...parts(shape)
353
+ });
354
+ }
355
+ }
356
+ const valueAxis = options.horizontalBars ? axisX : axisY;
357
+ untyped(handle).emit("created", state.preset.kind === "pie" ? {
358
+ chartRect,
359
+ svg: wrap(svgElement),
360
+ options
361
+ } : {
362
+ chartRect,
363
+ axisX,
364
+ axisY,
365
+ bounds: valueAxis.bounds,
366
+ svg: wrap(svgElement),
367
+ options
368
+ });
369
+ }
370
+ /** Turns the 1.x events on for a chart, once. */
371
+ function enable(handle) {
372
+ const existing = states.get(handle);
373
+ if (existing) return existing;
374
+ const state = { instances: [] };
375
+ states.set(handle, state);
376
+ handle.on("resolve", ({ config }) => {
377
+ state.preset = config.preset;
378
+ if (config.data !== state.source) {
379
+ state.copy = mutableCopy(config.data);
380
+ untyped(handle).emit("data", {
381
+ type: state.source === void 0 ? "initial" : "update",
382
+ data: state.copy
383
+ });
384
+ state.source = config.data;
385
+ }
386
+ return { config: {
387
+ ...config,
388
+ data: state.copy
389
+ } };
390
+ });
391
+ handle.on("layout", (layout) => {
392
+ state.layout = layout;
393
+ });
394
+ handle.on("responsive", (change) => "previousOptions" in change ? untyped(handle).emit("optionsChanged", {
395
+ previousOptions: change.previousOptions,
396
+ currentOptions: change.currentOptions
397
+ }) : void 0);
398
+ handle.on("mount", ({ scene, output, elementOf }) => {
399
+ const svg = output.svg;
400
+ if (!svg) throw new Error("Chartist: the compat plugin requires the DOM renderer");
401
+ state.svg = new src_index.Svg(svg);
402
+ for (const instance of state.instances) instance.svg = state.svg;
403
+ svg[ANIMATION_EMITTER] = { emit: (event, payload) => untyped(handle).emit(event, payload) };
404
+ if (state.preset) emitDrawEvents(handle, state, scene, svg, elementOf);
405
+ });
406
+ return state;
407
+ }
408
+ /** 1.x `optionsProvider`: the options in effect, after responsive options. */
409
+ function optionsProviderOf(getOptions) {
410
+ return {
411
+ getCurrentOptions: getOptions,
412
+ removeMediaQueryListeners: () => void 0
413
+ };
414
+ }
415
+ /**
416
+ * A 1.x-style chart object for 1.x plugins on the functional API. For a
417
+ * preset it is an instance of the preset's 1.x class (without running its
418
+ * constructor), so type checks such as `chart instanceof LineChart` hold;
419
+ * for `chart()` compositions a plain object.
420
+ */
421
+ function legacyChart(handle, state) {
422
+ const preset = presetOf(handle);
423
+ const chart = {
424
+ get container() {
425
+ return handle.container;
426
+ },
427
+ get data() {
428
+ return state.copy ?? preset?.data;
429
+ },
430
+ get options() {
431
+ return preset?.options;
432
+ },
433
+ get svg() {
434
+ return state.svg;
435
+ },
436
+ eventEmitter: {
437
+ on: (event, listener) => untyped(handle).on(event, listener),
438
+ off: (event, listener) => untyped(handle).off(event, listener),
439
+ emit: (event, payload) => untyped(handle).emit(event, payload)
440
+ },
441
+ optionsProvider: optionsProviderOf(() => preset?.options),
442
+ on(event, listener) {
443
+ untyped(handle).on(event, listener);
444
+ return chart;
445
+ },
446
+ off(event, listener) {
447
+ untyped(handle).off(event, listener);
448
+ return chart;
449
+ },
450
+ update(data, options, override) {
451
+ if (preset) preset.update(data, options, override);
452
+ else handle.update(data ? { data } : {});
453
+ return chart;
454
+ },
455
+ detach() {
456
+ if (preset) preset.detach();
457
+ else handle.detach();
458
+ return chart;
459
+ }
460
+ };
461
+ const type = preset && CLASSES[preset.kind];
462
+ return type ? Object.setPrototypeOf(chart, type.prototype) : chart;
463
+ }
464
+ /**
465
+ * Runs a 1.x plugin function as a v2 plugin, with the given chart object.
466
+ * Used by the 1.x chart classes, which pass themselves.
467
+ */
468
+ function legacyPlugin(plugin, options, chart) {
469
+ return {
470
+ name: plugin.name || "legacy-plugin",
471
+ provides: LEGACY_EVENTS,
472
+ install(handle) {
473
+ const state = enable(handle);
474
+ if (chart) {
475
+ state.instances.push(chart);
476
+ const instance = chart;
477
+ if (!instance.optionsProvider) instance.optionsProvider = optionsProviderOf(() => presetOf(handle)?.options);
478
+ }
479
+ const chartObject = chart ?? legacyChart(handle, state);
480
+ if (options === void 0) plugin(chartObject);
481
+ else plugin(chartObject, options);
482
+ }
483
+ };
484
+ }
485
+ /**
486
+ * The compat plugin. Restores the 1.x events (`data`, `optionsChanged`,
487
+ * `draw`, `created`, `animationBegin`, `animationEnd`) with 1.x payloads.
488
+ * With it, the 1.x chart classes also run 1.x plugin functions.
489
+ *
490
+ * Add it to `plugins`, of a 1.x class, a preset or `chart()`. The `draw`
491
+ * events come in the 1.x order, right after the chart is in the DOM, so
492
+ * `element` is a live element; then `created`. Animations started with
493
+ * `animate()` on the chart's elements emit `animationBegin` and
494
+ * `animationEnd`. Importing
495
+ * `chartist/compat` also types the 1.x events on every chart handle. It
496
+ * needs the DOM renderer, and the `draw` and `created` events need a
497
+ * chart made by a preset or a 1.x class.
498
+ *
499
+ * @example
500
+ * ```ts
501
+ * import { lineChart } from 'chartist';
502
+ * import { compat } from 'chartist/compat';
503
+ *
504
+ * const chart = lineChart(
505
+ * '#chart',
506
+ * {
507
+ * labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
508
+ * series: [[12, 9, 7, 8, 5]]
509
+ * },
510
+ * { plugins: [compat] }
511
+ * );
512
+ *
513
+ * // Registered after the first render, so it runs from the next one on.
514
+ * chart.on('draw', data => {
515
+ * if (data.type === 'point' && data.index === 4) {
516
+ * data.element.addClass('is-friday');
517
+ * }
518
+ * });
519
+ * ```
520
+ */
521
+ const compat = {
522
+ name: "compat",
523
+ provides: LEGACY_EVENTS,
524
+ install: (handle) => void enable(handle),
525
+ legacyPlugin
526
+ };
527
+ /**
528
+ * Converts a 1.x plugin function into a v2 plugin for `chart()` and the
529
+ * preset functions. It turns the 1.x events on for the chart and calls the
530
+ * plugin with a 1.x-style chart object (`container`, `data`, `options`,
531
+ * `svg`, `eventEmitter`, `on`, `off`, `update`, `detach`). You don't need
532
+ * {@link compat} in the list as well.
533
+ *
534
+ * @param plugin The 1.x plugin: a function that gets the chart (and its
535
+ * options)
536
+ * @param options The plugin's options; without them, the plugin is called
537
+ * with the chart only, as in 1.x
538
+ * @example
539
+ * ```ts
540
+ * import { lineChart, type Plugin } from 'chartist';
541
+ * import { fromLegacyPlugin } from 'chartist/compat';
542
+ *
543
+ * // A 1.x plugin: a function that gets the chart and its options.
544
+ * const caption: Plugin = (chart, options) => {
545
+ * chart.on('created', () => {
546
+ * chart.container.setAttribute('aria-label', options.text);
547
+ * });
548
+ * };
549
+ *
550
+ * lineChart(
551
+ * '#chart',
552
+ * { series: [[1, 3, 2, 4]] },
553
+ * { plugins: [fromLegacyPlugin(caption, { text: 'Visitors per week' })] }
554
+ * );
555
+ * ```
556
+ */
557
+ function fromLegacyPlugin(plugin, options) {
558
+ return legacyPlugin(plugin, options);
559
+ }
560
+ //#endregion
561
+ //#region src/compat/global.ts
562
+ /**
563
+ * The global `Chartist` that plugin scripts of the 0.x and 1.x era read.
564
+ * They register on `Chartist.plugins`, check the chart type with
565
+ * `instanceof Chartist.Line`, and call helpers such as `Chartist.extend`.
566
+ * Importing `chartist/compat` (any build: module, CommonJS or the UMD
567
+ * script) sets this up, so such a script works when it loads after it.
568
+ *
569
+ * The names are added to the global only, never exported: the module
570
+ * builds keep the 1.x and v2 names.
571
+ */
572
+ /**
573
+ * 0.x: the values of each series as plain numbers (`undefined` for holes),
574
+ * normalized as the 1.x charts do; reversed when `reverse` is true. Line
575
+ * and bar data give an array per series, pie data one array.
576
+ */
577
+ function getDataArray(data, reverse) {
578
+ return (0, src_index.normalizeData)(data, reverse).series;
579
+ }
580
+ /** The 0.x names plugin scripts use, next to the library's own. */
581
+ const LEGACY_NAMES = {
582
+ Line: src_index.LineChart,
583
+ Bar: src_index.BarChart,
584
+ Pie: src_index.PieChart,
585
+ getDataArray
586
+ };
587
+ /**
588
+ * Adds the core, compat and the 0.x names to the global `Chartist`, only
589
+ * what it lacks: a global set by the UMD build or by plugin scripts keeps
590
+ * its values, and `plugins` keeps what scripts registered. If the global
591
+ * belongs to another copy of Chartist (say the UMD build next to these
592
+ * modules), it is left alone with a warning: mixing two copies breaks the
593
+ * plugins' type checks. Outside a browser it does nothing.
594
+ */
595
+ function installGlobal(compat, root = globalThis) {
596
+ if (root.document === void 0) return;
597
+ const existing = root.Chartist;
598
+ if (existing && "LineChart" in existing && existing.LineChart !== src_index.LineChart) {
599
+ console.warn("Chartist: the global `Chartist` belongs to another copy of Chartist, so chartist/compat leaves it alone. Load Chartist once: as modules, or as the UMD build with compat.umd.js.");
600
+ return;
601
+ }
602
+ const target = existing ?? (root.Chartist = {});
603
+ const add = (name, value) => {
604
+ if (!(name in target)) target[name] = value;
605
+ };
606
+ for (const [name, value] of Object.entries(src_index)) add(name, value);
607
+ for (const [name, value] of Object.entries(compat)) add(name, value);
608
+ add("plugins", {});
609
+ for (const [name, value] of Object.entries(LEGACY_NAMES)) add(name, value);
610
+ }
611
+ //#endregion
612
+ //#region src/compat/entry.ts
613
+ /**
614
+ * The published entry of `chartist/compat`, in every build. Next to the
615
+ * compat API, importing it sets up the global `Chartist` for plugin
616
+ * scripts of the 0.x and 1.x era (see ./global.ts). That's why
617
+ * package.json lists the compat files under `sideEffects`.
618
+ */
619
+ installGlobal(compat_exports);
620
+ //#endregion
621
+ exports.compat = compat;
622
+ exports.fromLegacyPlugin = fromLegacyPlugin;
623
+
624
+ //# sourceMappingURL=compat.cjs.map