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