Skip to content

Commit 159ca1b

Browse files
author
Alistair-Welch
committed
accessibility in config
1 parent cac174c commit 159ca1b

File tree

3 files changed

+80
-75
lines changed

3 files changed

+80
-75
lines changed

src/plot_api/accessibility.js

Lines changed: 70 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,83 @@
22

33
import c2mChart from "chart2music";
44

5+
const supportedAccessibilityLibraries = ['chart2music'];
6+
57
export function enable (gd) {
68

7-
const c2mData = {};
8-
const labels = [];
9-
10-
const fullData = gd._fullData;
9+
const {library, options} = gd._context.accessibility;
10+
if (!supportedAccessibilityLibraries.includes(library)) {
11+
// 'Accessibility not implemented for library: ' + library
12+
return;
13+
}
14+
15+
if (library === 'chart2music') {
16+
const c2mData = {};
17+
const labels = [];
18+
const info = options.info;
19+
delete options.info;
20+
const fullData = gd._fullData;
1121

12-
for(var i = 0; i < fullData.length; i++) {
13-
var trace = fullData[i] ?? {};
14-
var {type, x = [], y = [], name = i, text = []} = trace;
15-
if(type === 'scatter') {
16-
var traceData = [];
17-
if ('y' in trace) {
18-
for(var p = 0; p < y.length; p++) {
19-
traceData.push(
20-
{
21-
x: x.length > 0 ? x[p] : p,
22-
y: y[p],
23-
label: text[p] ?? p
24-
})
22+
for(var i = 0; i < fullData.length; i++) {
23+
var trace = fullData[i] ?? {};
24+
var {type, x = [], y = [], name = i, text = []} = trace;
25+
if(type === 'scatter') {
26+
var traceData = [];
27+
if ('y' in trace) {
28+
for(var p = 0; p < y.length; p++) {
29+
traceData.push(
30+
{
31+
x: x.length > 0 ? x[p] : p,
32+
y: y[p],
33+
label: text[p] ?? p
34+
})
35+
}
36+
c2mData[name] = traceData;
37+
labels.push(name);
2538
}
26-
c2mData[name] = traceData;
27-
labels.push(name);
2839
}
29-
}
30-
else {
31-
// 'Accessibility not implemented for trace type: ' + trace.type
32-
return;
40+
else {
41+
// 'Accessibility not implemented for trace type: ' + trace.type
42+
return;
43+
};
3344
};
34-
};
35-
36-
var closed_captions = document.createElement('div');
37-
closed_captions.id = 'cc';
38-
closed_captions.className = 'closed_captions';
39-
gd.appendChild(closed_captions);
45+
46+
var closed_captions = document.createElement('div');
47+
closed_captions.id = 'cc';
48+
closed_captions.className = 'closed_captions';
49+
gd.appendChild(closed_captions);
4050

41-
const {
42-
title: {text: title_text = ''} = {},
43-
xaxis: {title: {text: xaxis_text = ''} = {}} = {},
44-
yaxis: {title: {text: yaxis_text = ''} = {}} = {},
45-
} = gd._fullLayout;
46-
47-
c2mChart({
48-
title: title_text,
49-
type: "line",
50-
axes: {
51-
x: {
52-
label: xaxis_text
51+
const {
52+
title: {text: title_text = ''} = {},
53+
xaxis: {title: {text: xaxis_text = ''} = {}} = {},
54+
yaxis: {title: {text: yaxis_text = ''} = {}} = {},
55+
} = gd._fullLayout;
56+
57+
c2mChart({
58+
title: title_text,
59+
type: "line",
60+
axes: {
61+
x: {
62+
label: xaxis_text
63+
},
64+
y: {
65+
label: yaxis_text
66+
}
5367
},
54-
y: {
55-
label: yaxis_text
56-
}
57-
},
58-
element: gd,
59-
cc: closed_captions,
60-
data: c2mData,
61-
options: {
62-
onFocusCallback: ({slice, index}) => {
63-
Plotly.Fx.hover(gd, [{
64-
curveNumber: labels.indexOf(slice),
65-
pointNumber: index
66-
}])
68+
element: gd,
69+
cc: closed_captions,
70+
data: c2mData,
71+
options: {
72+
onFocusCallback: ({slice, index}) => {
73+
Plotly.Fx.hover(gd, [{
74+
curveNumber: labels.indexOf(slice),
75+
pointNumber: index
76+
}])
77+
},
78+
...options
6779
},
68-
...gd._context.chart2musicOptions
69-
},
70-
info: gd._context.chart2musicInfo
71-
}
72-
);
80+
info: info
81+
}
82+
);
83+
};
7384
};

src/plot_api/plot_api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ function _doPlot(gd, data, layout, config) {
380380
saveRangeInitialForInsideTickLabels
381381
);
382382

383-
if(gd._context.chart2music) seq.push(accessibility.enable);
383+
if(gd._context.accessibility.enabled) seq.push(accessibility.enable);
384384

385385
seq.push(Plots.previousPromises);
386386

src/plot_api/plot_config.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -473,22 +473,16 @@ var configAttributes = {
473473
].join(' ')
474474
},
475475

476-
chart2music: {
477-
valType: 'boolean',
478-
dflt: true,
479-
description: 'Whether or not to enable chart2music keyboard and sound accessibility',
480-
},
481-
482-
chart2musicOptions: {
483-
valType: 'any',
484-
dflt: {},
485-
description: 'Additional options to pass to the chart2music API. See https://www.chart2music.com/docs/API/Config',
486-
},
487-
488-
chart2musicInfo: {
476+
accessibility: {
489477
valType: 'any',
490-
dflt: {},
491-
description: 'Info to pass to the chart2music API. See https://www.chart2music.com/docs/API/Config',
478+
dflt: {
479+
library: 'chart2music',
480+
enabled: true,
481+
options: {info: {}}
482+
},
483+
description: ['Accessibility options: which library to use; whether to enable and the options to pass to the library.',
484+
'chart2music is supported and options here include Options and Info from https://www.chart2music.com/docs/API/Config. '
485+
].join(' ')
492486
},
493487
};
494488

0 commit comments

Comments
 (0)