chartjs-plugin-chart2music 0.0.1 → 0.0.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 (3) hide show
  1. package/README.md +45 -10
  2. package/package.json +5 -1
  3. package/dist/plugin.js +0 -201
package/README.md CHANGED
@@ -1,5 +1,9 @@
1
- # chartjs2music
2
- chartjs plugin for chart2music
1
+ # chartjs-plugin-chart2music
2
+ chartjs plugin for chart2music.
3
+
4
+ **This is a beta release of this plugin**
5
+
6
+ Turns your chart.js charts into music so the blind can hear data. This plugin will automatically add Chart2Music, an interactive sonification library, to your chart.js charts. The contents of the chart element will be modified to best support screen reader users, and the interactions will be visually synchronized to provide support for keyboard-only users.
3
7
 
4
8
  ## Getting started
5
9
 
@@ -7,17 +11,48 @@ Add the chartjs2music plugin to your existing chart.js code like this:
7
11
 
8
12
  ```js
9
13
  import {Chart} from "chart.js/auto";
10
- import chartjs2music from "chartjs2music";
14
+ import plugin from "chartjs-plugin-chart2music";
11
15
 
12
- Chart.register(chartjs2music);
16
+ Chart.register(plugin);
13
17
  ```
14
18
 
15
- ## Contributing
19
+ That will register the plugin globally. Alternatively, if you only want to enable for a given chart, you can do this:
20
+
21
+ ```js
22
+ import {Chart} from "chart.js/auto";
23
+ import chartjs2music from "chartjs-plugin-chart2music";
24
+
25
+ new Chart(canvasElement, {
26
+ type: "bar",
27
+ data: {
28
+ datasets: [{
29
+ data: [1,4,2,8]
30
+ }]
31
+ },
32
+ plugins: [chartjs2music]
33
+ })
34
+
35
+ ```
36
+
37
+ ## Supported features
38
+
39
+ This plugin is currently in beta, so not all of the chart.js features are currently supported.
16
40
 
17
- To get started:
41
+ A quick list of chart.js features we currently support includes:
42
+ * Chart types: bar, line, pie, doughnut, polar, and combinations therein.
43
+ * Axes options: `title`, `min`, `max`, `type="linear"`, `type="logarithmic"`.
44
+ * Chart title
45
+ * Most data structures (not including `parsing` or non-standard axes identifiers)
18
46
 
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`.
47
+ Note that visual-specific chart features are ignored. This includes things like color, padding, line thickness, etc.
22
48
 
23
- For tests, run `yarn test`.
49
+ Things we plan to support in the future:
50
+ * Interactions and updates
51
+ * Other chart.js plugins
52
+ * Complex `parsing` options for data
53
+ * Date/Time support for axes
54
+ * Subtitle
55
+ * Locale
56
+ * Dataset visibility (when you show/hide a category from the legend)
57
+ * Radar charts
58
+ * Custom formatting for axis tick values
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "chartjs-plugin-chart2music",
3
- "version": "0.0.1",
3
+ "version": "0.0.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",
7
+ "module": "dist/plugin.mjs",
8
+ "exports": {
9
+ "import": "./dist/plugin.mjs"
10
+ },
7
11
  "files": ["dist/*"],
8
12
  "keywords": [
9
13
  "a11y",
package/dist/plugin.js DELETED
@@ -1,201 +0,0 @@
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);