Skip to content

Commit 7d69af7

Browse files
authored
feat(aria): 设置无障碍标签 (#3656)
* feat(aria): 设置无障碍标签 * docs: add aria document
1 parent 6bf5c9b commit 7d69af7

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

docs/api/general/chart.zh.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ changeSize(width: number, height: number): Chart;
4343
changeVisible(visible: boolean): Chart;
4444
```
4545

46+
### chart.aria()
47+
48+
开启或者关闭无障碍标签,`false` 表示关闭,否则需要填入无障碍标签配置内容,默认关闭。
49+
50+
```sign
51+
aria(false | AriaOptions): void
52+
```
53+
54+
Example:
55+
56+
```ts
57+
chart.aria({ label: '这张图表展示的是不同城市的交易额对比情况。' });
58+
```
59+
4660
## View API
4761

4862
> View 上的 api 同样适用于 Chart 上

src/chart/chart.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { debounce, each, isString, get } from '@antv/util';
1+
import { debounce, each, isString } from '@antv/util';
22
import { ChartCfg } from '../interface';
33
import { GROUP_Z_INDEX, VIEW_LIFE_CIRCLE } from '../constant';
44
import { getEngine } from '../engine';
55
import { createDom, getChartSize, removeDom, modifyCSS } from '../util/dom';
66
import View from './view';
7+
import { AriaOption } from '../interface';
78

89
/**
910
* Chart 类,是使用 G2 进行绘图的入口。
@@ -103,6 +104,19 @@ export default class Chart extends View {
103104
});
104105
}
105106

107+
/**
108+
* 设置 WAI-ARIA 无障碍标签。如何根据图形语法自动生成 arial 内容?
109+
* @param ariaOption
110+
*/
111+
public aria(ariaOption: AriaOption) {
112+
const ATTR = 'aria-label';
113+
if (ariaOption === false) {
114+
this.ele.removeAttribute(ATTR);
115+
} else {
116+
this.ele.setAttribute(ATTR, ariaOption.label);
117+
}
118+
}
119+
106120
/**
107121
* 改变图表大小,同时重新渲染。
108122
* @param width 图表宽度
@@ -129,6 +143,15 @@ export default class Chart extends View {
129143
return this;
130144
}
131145

146+
/**
147+
* 清空图表,同时清除掉 aria 配置
148+
*/
149+
public clear() {
150+
super.clear();
151+
152+
this.aria(false);
153+
}
154+
132155
/**
133156
* 销毁图表,同时解绑事件,销毁创建的 G.Canvas 实例。
134157
* @returns void

src/interface.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ export interface RangePoint {
7070
readonly y?: number | number[];
7171
}
7272

73+
/**
74+
* WAI-ARIA 无障碍标签配置
75+
*/
76+
export type AriaOption = false | {
77+
readonly label: string;
78+
}
79+
7380
/** 用户数据经过图形映射处理后的数据结构 */
7481
export interface MappingDatum {
7582
/** 原始数据 */

tests/unit/chart/chart-aria-spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Chart } from '../../../src';
2+
import { CITY_SALE } from '../../util/data';
3+
import { createDiv } from '../../util/dom';
4+
5+
describe('Chart aria', () => {
6+
const div = createDiv();
7+
8+
const chart = new Chart({
9+
container: div,
10+
autoFit: true,
11+
});
12+
13+
chart.data(CITY_SALE);
14+
15+
chart.interval().position('city*sale').color('category').adjust('stack');
16+
17+
test('aria', () => {
18+
19+
const label = '不同城市按照产品类别的堆积柱形图。';
20+
21+
chart.aria({
22+
label,
23+
});
24+
25+
expect(chart.ele.getAttribute('aria-label')).toBe(label);
26+
27+
chart.aria(false);
28+
expect(chart.ele.getAttribute('aria-label')).toBe(null);
29+
30+
chart.aria({
31+
label,
32+
});
33+
34+
chart.destroy();
35+
36+
expect(div.getAttribute('aria-label')).toBe(null);
37+
});
38+
39+
});

0 commit comments

Comments
 (0)