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