Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for adding accessibility features at chart generation #1738

Closed
wants to merge 14 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
First pass at making internal chart elements accessible from keyboard
gherka committed Sep 27, 2020
commit 612ef285e971b8ed02446b358c612e6896e6d52a
43 changes: 42 additions & 1 deletion src/base/base-mixin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {select} from 'd3-selection';
import {select, event as d3Event} from 'd3-selection';
import {dispatch} from 'd3-dispatch';
import {ascending} from 'd3-array';

@@ -73,6 +73,7 @@ export class BaseMixin {
constructor () {
this.__dcFlag__ = utils.uniqueId();
this._svgDescription = this.constructor.name || '';
this._keyboardAccessible = false;

this._dimension = undefined;
this._group = undefined;
@@ -548,6 +549,19 @@ export class BaseMixin {
return this;
}

/**
* Whether interactive chart elements will be accessible by keyboard using tabindex.
* @param {Boolean} [keyboardAccessible=false]
* @returns {Boolean|BarChart}
*/
keyboardAccessible (keyboardAccessible) {
if (!arguments.length) {
return this._keyboardAccessible;
}
this._keyboardAccessible = keyboardAccessible;
return this;
}

/**
* Set or get the filter printer function. The filter printer function is used to generate human
* friendly text for filter value(s) associated with the chart instance. The text will get shown
@@ -686,11 +700,34 @@ export class BaseMixin {
this._legend.render();
}

if (this._keyboardAccessible) {
this._makeKeyboardAccessible();
}

this._activateRenderlets('postRender');

return result;
}

_makeKeyboardAccessible () {
this._svg.selectAll('.dc-tabbable')
.attr('tabindex', 0)
.on('keydown', d => {

if (d3Event.keyCode === 13) {
console.log('clicked!');
this.onClick(d);
}
// special case for space key press - prevent scrolling
if (d3Event.keyCode === 32) {
console.log('clicked!');
this.onClick(d);
d3Event.preventDefault();
}

})
}

_activateRenderlets (event) {
this._listeners.call('pretransition', this, this);
if (this.transitionDuration() > 0 && this._svg) {
@@ -729,6 +766,10 @@ export class BaseMixin {
this._legend.render();
}

if (this._keyboardAccessible) {
this._makeKeyboardAccessible();
}

this._activateRenderlets('postRedraw');

return result;
2 changes: 1 addition & 1 deletion src/charts/bar-chart.js
Original file line number Diff line number Diff line change
@@ -176,7 +176,7 @@ export class BarChart extends StackMixin {

const enter = bars.enter()
.append('rect')
.attr('class', 'bar')
.attr('class', `bar ${this._keyboardAccessible ? 'dc-tabbable' : ''}`)
.attr('fill', pluck('data', this.getColor))
.attr('x', d => this._barXPos(d))
.attr('y', this.yAxisHeight())
10 changes: 5 additions & 5 deletions style/dc.scss
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ $font_sans_serif: sans-serif;
&.bar {
stroke: none;
cursor: pointer;
&:hover {
&:hover,&:focus {
fill-opacity: .5;
}
}
@@ -50,7 +50,7 @@ $font_sans_serif: sans-serif;
&.external {
fill: $color_black;
}
:hover, &.highlight {
:hover,:focus &.highlight {
fill-opacity: .8;
}
}
@@ -123,7 +123,7 @@ $font_sans_serif: sans-serif;
g {
&.state {
cursor: pointer;
:hover {
:hover,:focus {
fill-opacity: .8;
}
path {
@@ -142,7 +142,7 @@ $font_sans_serif: sans-serif;
rect {
fill-opacity: 0.8;
cursor: pointer;
&:hover {
&:hover,&:focus {
fill-opacity: 0.6;
}
}
@@ -173,7 +173,7 @@ $font_sans_serif: sans-serif;
.node {
font-size: 0.7em;
cursor: pointer;
:hover {
:hover,:focus {
fill-opacity: .8;
}
}