chartjs-plugin-chart2music 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +23 -0
- package/dist/plugin.js +201 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 julianna-langston
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# chartjs2music
|
|
2
|
+
chartjs plugin for chart2music
|
|
3
|
+
|
|
4
|
+
## Getting started
|
|
5
|
+
|
|
6
|
+
Add the chartjs2music plugin to your existing chart.js code like this:
|
|
7
|
+
|
|
8
|
+
```js
|
|
9
|
+
import {Chart} from "chart.js/auto";
|
|
10
|
+
import chartjs2music from "chartjs2music";
|
|
11
|
+
|
|
12
|
+
Chart.register(chartjs2music);
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Contributing
|
|
16
|
+
|
|
17
|
+
To get started:
|
|
18
|
+
|
|
19
|
+
1. run `yarn` to install dependencies
|
|
20
|
+
2. run `yarn start` to start a server in watch mode
|
|
21
|
+
3. point a browser to `localhost:5173`, or whichever port numberVite indicates after you ran `yarn start`.
|
|
22
|
+
|
|
23
|
+
For tests, run `yarn test`.
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
var chartjs2music = (function (exports, c2mChart) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// let lastDataObj = "";
|
|
5
|
+
const chartjs_c2m_converter = {
|
|
6
|
+
bar: "bar",
|
|
7
|
+
line: "line",
|
|
8
|
+
pie: "pie",
|
|
9
|
+
polarArea: "bar",
|
|
10
|
+
doughnut: "pie"
|
|
11
|
+
};
|
|
12
|
+
const processChartType = (chart) => {
|
|
13
|
+
const topLevelType = chart.config.type;
|
|
14
|
+
const panelTypes = chart.data.datasets.map(({ type }) => type ?? topLevelType);
|
|
15
|
+
const invalid = panelTypes.find((t) => !(t in chartjs_c2m_converter));
|
|
16
|
+
if (invalid) {
|
|
17
|
+
return {
|
|
18
|
+
valid: false,
|
|
19
|
+
invalidType: invalid
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
if ([...new Set(panelTypes)].length === 1) {
|
|
23
|
+
return {
|
|
24
|
+
valid: true,
|
|
25
|
+
c2m_types: chartjs_c2m_converter[panelTypes[0]]
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
valid: true,
|
|
30
|
+
c2m_types: panelTypes.map((t) => chartjs_c2m_converter[t])
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
const generateAxisInfo = (chartAxisInfo, chart) => {
|
|
34
|
+
const axis = {};
|
|
35
|
+
if (chartAxisInfo?.min) {
|
|
36
|
+
if (typeof chartAxisInfo.min === "string") {
|
|
37
|
+
axis.minimum = chart.data.labels.indexOf(chartAxisInfo.min);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
axis.minimum = chartAxisInfo.min;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (chartAxisInfo?.max) {
|
|
44
|
+
if (typeof chartAxisInfo.max === "string") {
|
|
45
|
+
axis.maximum = chart.data.labels.indexOf(chartAxisInfo.max);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
axis.maximum = chartAxisInfo.max;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const label = chartAxisInfo?.title?.text;
|
|
52
|
+
if (label) {
|
|
53
|
+
axis.label = label;
|
|
54
|
+
}
|
|
55
|
+
if (chartAxisInfo?.type === "logarithmic") {
|
|
56
|
+
axis.type = "log10";
|
|
57
|
+
}
|
|
58
|
+
return axis;
|
|
59
|
+
};
|
|
60
|
+
const generateAxes = (chart) => {
|
|
61
|
+
const axes = {
|
|
62
|
+
x: {
|
|
63
|
+
...generateAxisInfo(chart.options?.scales?.x, chart),
|
|
64
|
+
valueLabels: chart.data.labels.slice(0)
|
|
65
|
+
},
|
|
66
|
+
y: {
|
|
67
|
+
...generateAxisInfo(chart.options?.scales?.y, chart),
|
|
68
|
+
format: (value) => value.toLocaleString()
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
return axes;
|
|
72
|
+
};
|
|
73
|
+
const whichDataStructure = (data) => {
|
|
74
|
+
if (Array.isArray(data[0])) {
|
|
75
|
+
return data.map((arr, x) => {
|
|
76
|
+
let [low, high] = arr.sort();
|
|
77
|
+
return { x, low, high };
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return data;
|
|
81
|
+
};
|
|
82
|
+
const scrubX = (data) => {
|
|
83
|
+
const blackboard = JSON.parse(JSON.stringify(data));
|
|
84
|
+
let labels = [];
|
|
85
|
+
if (Array.isArray(data)) {
|
|
86
|
+
// console.log("not grouped");
|
|
87
|
+
// Not grouped
|
|
88
|
+
blackboard.forEach((item, x) => {
|
|
89
|
+
if (typeof item === "object" && "x" in item) {
|
|
90
|
+
labels.push(item.x);
|
|
91
|
+
item.x = x;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return { labels, data: blackboard };
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const processData = (data) => {
|
|
98
|
+
let groups = [];
|
|
99
|
+
if (data.datasets.length === 1) {
|
|
100
|
+
return {
|
|
101
|
+
data: whichDataStructure(data.datasets[0].data)
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const result = {};
|
|
105
|
+
data.datasets.forEach((obj, index) => {
|
|
106
|
+
const groupName = obj.label ?? `Group ${index + 1}`;
|
|
107
|
+
groups.push(groupName);
|
|
108
|
+
result[groupName] = whichDataStructure(obj.data);
|
|
109
|
+
});
|
|
110
|
+
return { groups, data: result };
|
|
111
|
+
};
|
|
112
|
+
const determineChartTitle = (options) => {
|
|
113
|
+
if (options.plugins?.title?.text) {
|
|
114
|
+
if (Array.isArray(options.plugins.title.text)) {
|
|
115
|
+
return options.plugins.title.text.join(", ");
|
|
116
|
+
}
|
|
117
|
+
return options.plugins.title.text;
|
|
118
|
+
}
|
|
119
|
+
return "";
|
|
120
|
+
};
|
|
121
|
+
const determineCCElement = (canvas, provided) => {
|
|
122
|
+
if (provided) {
|
|
123
|
+
return provided;
|
|
124
|
+
}
|
|
125
|
+
const cc = document.createElement("div");
|
|
126
|
+
canvas.insertAdjacentElement("afterend", cc);
|
|
127
|
+
return cc;
|
|
128
|
+
};
|
|
129
|
+
const plugin = {
|
|
130
|
+
id: "chartjs2music",
|
|
131
|
+
afterInit: (chart, args, options) => {
|
|
132
|
+
const { valid, c2m_types, invalidType } = processChartType(chart);
|
|
133
|
+
if (!valid) {
|
|
134
|
+
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(", ")}`);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const axes = generateAxes(chart);
|
|
138
|
+
// Generate CC element
|
|
139
|
+
const cc = determineCCElement(chart.canvas, options.cc);
|
|
140
|
+
const { data, groups } = processData(chart.data);
|
|
141
|
+
// lastDataObj = JSON.stringify(data);
|
|
142
|
+
let scrub = scrubX(data);
|
|
143
|
+
if (scrub?.labels && scrub?.labels?.length > 0) { // Something was scrubbed
|
|
144
|
+
if (!chart.data.labels || chart.data.labels.length === 0) {
|
|
145
|
+
axes.x.valueLabels = scrub.labels.slice(0);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const c2mOptions = {
|
|
149
|
+
cc,
|
|
150
|
+
element: chart.canvas,
|
|
151
|
+
type: c2m_types,
|
|
152
|
+
data: scrub?.data ?? data,
|
|
153
|
+
title: determineChartTitle(chart.options),
|
|
154
|
+
axes,
|
|
155
|
+
options: {
|
|
156
|
+
// @ts-ignore
|
|
157
|
+
onFocusCallback: ({ slice, index }) => {
|
|
158
|
+
try {
|
|
159
|
+
const highlightElements = [{
|
|
160
|
+
datasetIndex: groups?.indexOf(slice) ?? 0,
|
|
161
|
+
index
|
|
162
|
+
}];
|
|
163
|
+
chart?.setActiveElements(highlightElements);
|
|
164
|
+
chart?.tooltip?.setActiveElements(highlightElements, {});
|
|
165
|
+
chart?.update();
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
// console.warn(e);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
if (options.audioEngine) {
|
|
174
|
+
// @ts-ignore
|
|
175
|
+
c2mOptions.audioEngine = options.audioEngine;
|
|
176
|
+
}
|
|
177
|
+
const { err, data: c2m } = c2mChart(c2mOptions);
|
|
178
|
+
// /* istanbul-ignore-next */
|
|
179
|
+
if (err) {
|
|
180
|
+
options.errorCallback?.(err);
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
// afterUpdate: (chart: Chart) => {
|
|
184
|
+
// const {data} = processData(chart.data);
|
|
185
|
+
// if(JSON.stringify(data) !== lastDataObj){
|
|
186
|
+
// c2mChartRef?.setData(data);
|
|
187
|
+
// lastDataObj = JSON.stringify(data);
|
|
188
|
+
// }
|
|
189
|
+
// },
|
|
190
|
+
defaults: {
|
|
191
|
+
cc: null,
|
|
192
|
+
audioEngine: null,
|
|
193
|
+
errorCallback: null
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
exports.plugin = plugin;
|
|
198
|
+
|
|
199
|
+
return exports;
|
|
200
|
+
|
|
201
|
+
})({}, c2mChart);
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "chartjs-plugin-chart2music",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Chart.js plugin for Chart2Music. Turns chart.js charts into music so the blind can hear data.",
|
|
6
|
+
"main": "dist/plugin.js",
|
|
7
|
+
"files": ["dist/*"],
|
|
8
|
+
"keywords": [
|
|
9
|
+
"a11y",
|
|
10
|
+
"accessibility",
|
|
11
|
+
"chart.js",
|
|
12
|
+
"chart",
|
|
13
|
+
"dataviz",
|
|
14
|
+
"screen reader",
|
|
15
|
+
"sonification"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/julianna-langston/chartjs2music"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/julianna-langston/chartjs2music/issues"
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"start": "vite",
|
|
27
|
+
"build": "rollup -c rollup.config.js --silent",
|
|
28
|
+
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
|
|
29
|
+
"depcheck": "depcheck",
|
|
30
|
+
"clean": "rimraf dist coverage"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@rollup/plugin-typescript": "^11.0.0",
|
|
34
|
+
"@types/jest": "^29.4.0",
|
|
35
|
+
"canvas": "^2.11.0",
|
|
36
|
+
"cross-env": "^7.0.3",
|
|
37
|
+
"depcheck": "^1.4.3",
|
|
38
|
+
"jest": "^29.4.2",
|
|
39
|
+
"jest-environment-jsdom": "^29.4.2",
|
|
40
|
+
"rimraf": "^4.1.2",
|
|
41
|
+
"rollup": "^3.15.0",
|
|
42
|
+
"ts-jest": "^29.0.5",
|
|
43
|
+
"tslib": "^2.5.0",
|
|
44
|
+
"typescript": "^4.9.3",
|
|
45
|
+
"vite": "^4.1.0"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"chart.js": "^4.2.1",
|
|
49
|
+
"chart2music": "^1.8.2"
|
|
50
|
+
}
|
|
51
|
+
}
|