chartjs-plugin-chart2music 0.0.5 → 0.0.7
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 -5
- package/dist/plugin.js +396 -353
- package/dist/plugin.mjs +396 -353
- package/package.json +4 -3
package/dist/plugin.mjs
CHANGED
|
@@ -1,360 +1,403 @@
|
|
|
1
1
|
import c2mChart from 'chart2music';
|
|
2
2
|
|
|
3
|
-
const calcMedian = (nums) => (nums.length % 2) ? nums[Math.floor(nums.length / 2)] : (nums[nums.length / 2] + nums[nums.length / 2 - 1]) / 2;
|
|
4
|
-
const fiveNumberSummary = (nums) => {
|
|
5
|
-
const sortedNumbers = nums.sort();
|
|
6
|
-
let min, max, q1, q3;
|
|
7
|
-
const datamin = Math.min(...sortedNumbers);
|
|
8
|
-
const datamax = Math.max(...sortedNumbers);
|
|
9
|
-
const median = calcMedian(sortedNumbers);
|
|
10
|
-
const length = sortedNumbers.length;
|
|
11
|
-
if (length % 2) {
|
|
12
|
-
q1 = calcMedian(sortedNumbers.slice(0, Math.floor(length / 2)));
|
|
13
|
-
q3 = calcMedian(sortedNumbers.slice(Math.ceil(length % 2)));
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
q1 = calcMedian(sortedNumbers.slice(0, length / 2 - 1));
|
|
17
|
-
q3 = calcMedian(sortedNumbers.slice(length / 2));
|
|
18
|
-
}
|
|
19
|
-
const iqr = q3 - q1;
|
|
20
|
-
if (datamax < q3 + iqr) {
|
|
21
|
-
max = datamax;
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
max = sortedNumbers.reverse().find((num) => num < datamax) ?? datamax;
|
|
25
|
-
}
|
|
26
|
-
if (datamin < q1 - iqr) {
|
|
27
|
-
min = datamin;
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
min = sortedNumbers.reverse().find((num) => num < datamin) ?? datamin;
|
|
31
|
-
}
|
|
32
|
-
const outlier = sortedNumbers.filter((num) => num < min || num > max);
|
|
33
|
-
return {
|
|
34
|
-
low: min,
|
|
35
|
-
high: max,
|
|
36
|
-
median,
|
|
37
|
-
q1,
|
|
38
|
-
q3,
|
|
39
|
-
...(outlier.length > 0 ? { outlier } : {})
|
|
40
|
-
};
|
|
41
|
-
};
|
|
42
|
-
const whichBoxData = (data) => {
|
|
43
|
-
return data.map((row, index) => {
|
|
44
|
-
if (typeof row === "object" && "min" in row) {
|
|
45
|
-
return {
|
|
46
|
-
...row,
|
|
47
|
-
low: row.min,
|
|
48
|
-
high: row.max,
|
|
49
|
-
x: index,
|
|
50
|
-
...("outliers" in row ? { outlier: row.outliers } : {})
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
if (Array.isArray(row)) {
|
|
54
|
-
return {
|
|
55
|
-
...fiveNumberSummary(row),
|
|
56
|
-
x: index
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
};
|
|
61
|
-
const processBoxData = (data) => {
|
|
62
|
-
if (data.datasets.length === 1) {
|
|
63
|
-
return {
|
|
64
|
-
data: whichBoxData(data.datasets[0].data)
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
const groups = [];
|
|
68
|
-
const result = {};
|
|
69
|
-
data.datasets.forEach((obj, index) => {
|
|
70
|
-
const groupName = obj.label ?? `Group ${index + 1}`;
|
|
71
|
-
groups.push(groupName);
|
|
72
|
-
result[groupName] = whichBoxData(obj.data);
|
|
73
|
-
});
|
|
74
|
-
return { groups, data: result };
|
|
3
|
+
const calcMedian = (nums) => (nums.length % 2) ? nums[Math.floor(nums.length / 2)] : (nums[nums.length / 2] + nums[nums.length / 2 - 1]) / 2;
|
|
4
|
+
const fiveNumberSummary = (nums) => {
|
|
5
|
+
const sortedNumbers = nums.sort();
|
|
6
|
+
let min, max, q1, q3;
|
|
7
|
+
const datamin = Math.min(...sortedNumbers);
|
|
8
|
+
const datamax = Math.max(...sortedNumbers);
|
|
9
|
+
const median = calcMedian(sortedNumbers);
|
|
10
|
+
const length = sortedNumbers.length;
|
|
11
|
+
if (length % 2) {
|
|
12
|
+
q1 = calcMedian(sortedNumbers.slice(0, Math.floor(length / 2)));
|
|
13
|
+
q3 = calcMedian(sortedNumbers.slice(Math.ceil(length % 2)));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
q1 = calcMedian(sortedNumbers.slice(0, length / 2 - 1));
|
|
17
|
+
q3 = calcMedian(sortedNumbers.slice(length / 2));
|
|
18
|
+
}
|
|
19
|
+
const iqr = q3 - q1;
|
|
20
|
+
if (datamax < q3 + iqr) {
|
|
21
|
+
max = datamax;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
max = sortedNumbers.reverse().find((num) => num < datamax) ?? datamax;
|
|
25
|
+
}
|
|
26
|
+
if (datamin < q1 - iqr) {
|
|
27
|
+
min = datamin;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
min = sortedNumbers.reverse().find((num) => num < datamin) ?? datamin;
|
|
31
|
+
}
|
|
32
|
+
const outlier = sortedNumbers.filter((num) => num < min || num > max);
|
|
33
|
+
return {
|
|
34
|
+
low: min,
|
|
35
|
+
high: max,
|
|
36
|
+
median,
|
|
37
|
+
q1,
|
|
38
|
+
q3,
|
|
39
|
+
...(outlier.length > 0 ? { outlier } : {})
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
const whichBoxData = (data) => {
|
|
43
|
+
return data.map((row, index) => {
|
|
44
|
+
if (typeof row === "object" && "min" in row) {
|
|
45
|
+
return {
|
|
46
|
+
...row,
|
|
47
|
+
low: row.min,
|
|
48
|
+
high: row.max,
|
|
49
|
+
x: index,
|
|
50
|
+
...("outliers" in row ? { outlier: row.outliers } : {})
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (Array.isArray(row)) {
|
|
54
|
+
return {
|
|
55
|
+
...fiveNumberSummary(row),
|
|
56
|
+
x: index
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
const processBoxData = (data) => {
|
|
62
|
+
if (data.datasets.length === 1) {
|
|
63
|
+
return {
|
|
64
|
+
data: whichBoxData(data.datasets[0].data)
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const groups = [];
|
|
68
|
+
const result = {};
|
|
69
|
+
data.datasets.forEach((obj, index) => {
|
|
70
|
+
const groupName = obj.label ?? `Group ${index + 1}`;
|
|
71
|
+
groups.push(groupName);
|
|
72
|
+
result[groupName] = whichBoxData(obj.data);
|
|
73
|
+
});
|
|
74
|
+
return { groups, data: result };
|
|
75
75
|
};
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
delete axes.x.
|
|
220
|
-
delete axes.
|
|
221
|
-
delete axes.y.
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
delete
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
c2mOptions.data = c2mOptions.data.map((
|
|
294
|
-
return {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
custom: {
|
|
325
|
-
group: groupNumber,
|
|
326
|
-
index
|
|
327
|
-
}
|
|
328
|
-
};
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
77
|
+
const chartjs_c2m_converter = {
|
|
78
|
+
bar: "bar",
|
|
79
|
+
line: "line",
|
|
80
|
+
pie: "pie",
|
|
81
|
+
polarArea: "bar",
|
|
82
|
+
doughnut: "pie",
|
|
83
|
+
boxplot: "box",
|
|
84
|
+
radar: "bar",
|
|
85
|
+
wordCloud: "bar",
|
|
86
|
+
scatter: "scatter"
|
|
87
|
+
};
|
|
88
|
+
const processChartType = (chart) => {
|
|
89
|
+
const topLevelType = chart.config.type;
|
|
90
|
+
const panelTypes = chart.data.datasets.map(({ type }) => type ?? topLevelType);
|
|
91
|
+
const invalid = panelTypes.find((t) => !(t in chartjs_c2m_converter));
|
|
92
|
+
if (invalid) {
|
|
93
|
+
return {
|
|
94
|
+
valid: false,
|
|
95
|
+
invalidType: invalid
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
if ([...new Set(panelTypes)].length === 1) {
|
|
99
|
+
return {
|
|
100
|
+
valid: true,
|
|
101
|
+
c2m_types: chartjs_c2m_converter[panelTypes[0]]
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
valid: true,
|
|
106
|
+
c2m_types: panelTypes.map((t) => chartjs_c2m_converter[t])
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
const generateAxisInfo = (chartAxisInfo, chart) => {
|
|
110
|
+
const axis = {};
|
|
111
|
+
if (chartAxisInfo?.min) {
|
|
112
|
+
if (typeof chartAxisInfo.min === "string") {
|
|
113
|
+
axis.minimum = chart.data.labels.indexOf(chartAxisInfo.min);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
axis.minimum = chartAxisInfo.min;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (chartAxisInfo?.max) {
|
|
120
|
+
if (typeof chartAxisInfo.max === "string") {
|
|
121
|
+
axis.maximum = chart.data.labels.indexOf(chartAxisInfo.max);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
axis.maximum = chartAxisInfo.max;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const label = chartAxisInfo?.title?.text;
|
|
128
|
+
if (label) {
|
|
129
|
+
axis.label = label;
|
|
130
|
+
}
|
|
131
|
+
if (chartAxisInfo?.type === "logarithmic") {
|
|
132
|
+
axis.type = "log10";
|
|
133
|
+
}
|
|
134
|
+
return axis;
|
|
135
|
+
};
|
|
136
|
+
const generateAxes = (chart) => {
|
|
137
|
+
const axes = {
|
|
138
|
+
x: {
|
|
139
|
+
...generateAxisInfo(chart.options?.scales?.x, chart),
|
|
140
|
+
valueLabels: chart.data.labels.slice(0)
|
|
141
|
+
},
|
|
142
|
+
y: {
|
|
143
|
+
...generateAxisInfo(chart.options?.scales?.y, chart),
|
|
144
|
+
format: (value) => value.toLocaleString()
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
return axes;
|
|
148
|
+
};
|
|
149
|
+
const whichDataStructure = (data) => {
|
|
150
|
+
if (Array.isArray(data[0])) {
|
|
151
|
+
return data.map((arr, x) => {
|
|
152
|
+
let [low, high] = arr.sort();
|
|
153
|
+
return { x, low, high };
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return data;
|
|
157
|
+
};
|
|
158
|
+
const scrubX = (data) => {
|
|
159
|
+
const blackboard = JSON.parse(JSON.stringify(data));
|
|
160
|
+
let labels = [];
|
|
161
|
+
if (Array.isArray(data)) {
|
|
162
|
+
// console.log("not grouped");
|
|
163
|
+
// Not grouped
|
|
164
|
+
blackboard.forEach((item, x) => {
|
|
165
|
+
if (typeof item === "object" && "x" in item) {
|
|
166
|
+
labels.push(item.x);
|
|
167
|
+
item.x = x;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
return { labels, data: blackboard };
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
const processData = (data, c2m_types) => {
|
|
174
|
+
if (c2m_types === "box") {
|
|
175
|
+
return processBoxData(data);
|
|
176
|
+
}
|
|
177
|
+
let groups = [];
|
|
178
|
+
if (data.datasets.length === 1) {
|
|
179
|
+
return {
|
|
180
|
+
data: whichDataStructure(data.datasets[0].data)
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
const result = {};
|
|
184
|
+
data.datasets.forEach((obj, index) => {
|
|
185
|
+
const groupName = obj.label ?? `Group ${index + 1}`;
|
|
186
|
+
groups.push(groupName);
|
|
187
|
+
result[groupName] = whichDataStructure(obj.data);
|
|
188
|
+
});
|
|
189
|
+
return { groups, data: result };
|
|
190
|
+
};
|
|
191
|
+
const determineChartTitle = (options) => {
|
|
192
|
+
if (options.plugins?.title?.text) {
|
|
193
|
+
if (Array.isArray(options.plugins.title.text)) {
|
|
194
|
+
return options.plugins.title.text.join(", ");
|
|
195
|
+
}
|
|
196
|
+
return options.plugins.title.text;
|
|
197
|
+
}
|
|
198
|
+
return "";
|
|
199
|
+
};
|
|
200
|
+
const determineCCElement = (canvas, provided) => {
|
|
201
|
+
if (provided) {
|
|
202
|
+
return provided;
|
|
203
|
+
}
|
|
204
|
+
const cc = document.createElement("div");
|
|
205
|
+
canvas.insertAdjacentElement("afterend", cc);
|
|
206
|
+
return cc;
|
|
207
|
+
};
|
|
208
|
+
const plugin = {
|
|
209
|
+
id: "chartjs2music",
|
|
210
|
+
afterInit: (chart, args, options) => {
|
|
211
|
+
const { valid, c2m_types, invalidType } = processChartType(chart);
|
|
212
|
+
if (!valid) {
|
|
213
|
+
options.errorCallback?.(`Unable to connect chart2music to chart. The chart is of type "${invalidType}", which is not one of the supported chart types for this plugin. This plugin supports: ${Object.keys(chartjs_c2m_converter).join(", ")}`);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
let axes = generateAxes(chart);
|
|
217
|
+
if (chart.config.type === "wordCloud") {
|
|
218
|
+
delete axes.x.minimum;
|
|
219
|
+
delete axes.x.maximum;
|
|
220
|
+
delete axes.y.minimum;
|
|
221
|
+
delete axes.y.maximum;
|
|
222
|
+
if (!axes.x.label) {
|
|
223
|
+
axes.x.label = "Word";
|
|
224
|
+
}
|
|
225
|
+
if (!axes.y.label) {
|
|
226
|
+
axes.y.label = "Emphasis";
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
// Generate CC element
|
|
230
|
+
const cc = determineCCElement(chart.canvas, options.cc);
|
|
231
|
+
const { data, groups } = processData(chart.data, c2m_types);
|
|
232
|
+
// lastDataObj = JSON.stringify(data);
|
|
233
|
+
let scrub = scrubX(data);
|
|
234
|
+
if (scrub?.labels && scrub?.labels?.length > 0) { // Something was scrubbed
|
|
235
|
+
if (!chart.data.labels || chart.data.labels.length === 0) {
|
|
236
|
+
axes.x.valueLabels = scrub.labels.slice(0);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (c2m_types === "scatter") {
|
|
240
|
+
delete scrub?.data;
|
|
241
|
+
delete axes.x.valueLabels;
|
|
242
|
+
}
|
|
243
|
+
axes = {
|
|
244
|
+
...axes,
|
|
245
|
+
x: {
|
|
246
|
+
...axes.x,
|
|
247
|
+
...(options.axes?.x)
|
|
248
|
+
},
|
|
249
|
+
y: {
|
|
250
|
+
...axes.y,
|
|
251
|
+
...(options.axes?.y)
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
const c2mOptions = {
|
|
255
|
+
cc,
|
|
256
|
+
element: chart.canvas,
|
|
257
|
+
type: c2m_types,
|
|
258
|
+
data: scrub?.data ?? data,
|
|
259
|
+
title: determineChartTitle(chart.options),
|
|
260
|
+
axes,
|
|
261
|
+
options: {
|
|
262
|
+
// @ts-ignore
|
|
263
|
+
onFocusCallback: ({ point, index }) => {
|
|
264
|
+
try {
|
|
265
|
+
const highlightElements = [];
|
|
266
|
+
if ("custom" in point) {
|
|
267
|
+
highlightElements.push({
|
|
268
|
+
datasetIndex: point.custom.group,
|
|
269
|
+
index: point.custom.index
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
const indices = chart.config.options?.plugins?.chartjs2music?.internal.visible_groups;
|
|
274
|
+
indices.forEach((datasetIndex) => {
|
|
275
|
+
highlightElements.push({
|
|
276
|
+
datasetIndex,
|
|
277
|
+
index
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
chart?.setActiveElements(highlightElements);
|
|
282
|
+
chart?.tooltip?.setActiveElements(highlightElements, {});
|
|
283
|
+
chart?.update();
|
|
284
|
+
}
|
|
285
|
+
catch (e) {
|
|
286
|
+
// console.warn(e);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
if (Array.isArray(c2mOptions.data)) {
|
|
292
|
+
if (isNaN(c2mOptions.data[0])) {
|
|
293
|
+
c2mOptions.data = c2mOptions.data.map((point, index) => {
|
|
294
|
+
return {
|
|
295
|
+
...point,
|
|
296
|
+
custom: {
|
|
297
|
+
group: 0,
|
|
298
|
+
index
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
c2mOptions.data = c2mOptions.data.map((num, index) => {
|
|
305
|
+
return {
|
|
306
|
+
x: index,
|
|
307
|
+
y: num,
|
|
308
|
+
custom: {
|
|
309
|
+
group: 0,
|
|
310
|
+
index
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
const groups = Object.keys(c2mOptions.data);
|
|
318
|
+
groups.forEach((groupName, groupNumber) => {
|
|
319
|
+
if (!isNaN(c2mOptions.data[groupName][0])) {
|
|
320
|
+
c2mOptions.data[groupName] = c2mOptions.data[groupName].map((num, index) => {
|
|
321
|
+
return {
|
|
322
|
+
x: index,
|
|
323
|
+
y: num,
|
|
324
|
+
custom: {
|
|
325
|
+
group: groupNumber,
|
|
326
|
+
index
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
c2mOptions.data[groupName] = c2mOptions.data[groupName].map((point, index) => {
|
|
333
|
+
return {
|
|
334
|
+
...point,
|
|
335
|
+
custom: {
|
|
336
|
+
group: groupNumber,
|
|
337
|
+
index
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
if (chart.config.options?.scales?.x?.stacked) {
|
|
345
|
+
c2mOptions.options.stack = true;
|
|
346
|
+
}
|
|
347
|
+
if (options.audioEngine) {
|
|
348
|
+
// @ts-ignore
|
|
349
|
+
c2mOptions.audioEngine = options.audioEngine;
|
|
350
|
+
}
|
|
351
|
+
const { err, data: c2m } = c2mChart(c2mOptions);
|
|
352
|
+
chart.config.options = {
|
|
353
|
+
...(chart.config.options ?? {}),
|
|
354
|
+
plugins: {
|
|
355
|
+
...chart.config.options?.plugins,
|
|
356
|
+
chartjs2music: {
|
|
357
|
+
...chart.config.options?.plugins?.chartjs2music,
|
|
358
|
+
internal: {
|
|
359
|
+
c2m,
|
|
360
|
+
visible_groups: groups?.map((g, i) => i)
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
// /* istanbul-ignore-next */
|
|
366
|
+
if (err) {
|
|
367
|
+
options.errorCallback?.(err);
|
|
368
|
+
}
|
|
369
|
+
},
|
|
370
|
+
afterDatasetUpdate: (chart, args) => {
|
|
371
|
+
if (!args.mode) {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
const ref = chart.config.options?.plugins?.chartjs2music?.internal.c2m;
|
|
375
|
+
const groups = ref._groups.slice(0);
|
|
376
|
+
if (ref._options.stack) {
|
|
377
|
+
groups.shift();
|
|
378
|
+
}
|
|
379
|
+
if (args.mode === "hide") {
|
|
380
|
+
const err = ref.setCategoryVisibility(groups[args.index], false);
|
|
381
|
+
chart.config.options.plugins.chartjs2music?.internal.visible_groups.splice(args.index, 1);
|
|
382
|
+
if (err) {
|
|
383
|
+
console.error(err);
|
|
384
|
+
}
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (args.mode === "show") {
|
|
388
|
+
const err = ref.setCategoryVisibility(groups[args.index], true);
|
|
389
|
+
chart.config.options.plugins.chartjs2music.internal.visible_groups.push(args.index);
|
|
390
|
+
if (err) {
|
|
391
|
+
console.error(err);
|
|
392
|
+
}
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
defaults: {
|
|
397
|
+
cc: null,
|
|
398
|
+
audioEngine: null,
|
|
399
|
+
errorCallback: null
|
|
400
|
+
}
|
|
358
401
|
};
|
|
359
402
|
|
|
360
403
|
export { plugin as default };
|