aria.js 7.8 KB
Newer Older
O
Ovilia 已提交
1 2 3 4 5 6 7
import * as zrUtil from 'zrender/src/core/util';

export default function (dom, ecModel) {
    var ariaModel = ecModel.getModel('aria');
    if (!ariaModel.get('show')) {
        return;
    }
O
Ovilia 已提交
8 9
    else if (ariaModel.get('description')) {
        dom.setAttribute('aria-label', ariaModel.get('description'));
10 11
        return;
    }
O
Ovilia 已提交
12

O
Ovilia 已提交
13 14
    var maxDataCnt = ariaModel.get('maxDataCount') || 10;
    var maxSeriesCnt = ariaModel.get('maxSeriesCount') || 10;
15

O
Ovilia 已提交
16
    var series = [];
O
Ovilia 已提交
17
    var seriesCnt = 0;
O
Ovilia 已提交
18
    ecModel.eachSeries(function (seriesModel, idx) {
O
Ovilia 已提交
19 20 21 22 23 24 25 26 27
        if (idx < maxSeriesCnt) {
            var type = ariaModel.get('renderAs')
                || seriesModel.type.substr('series.'.length);
            series.push({
                type: type,
                desc: getSeriesDesc(type, seriesModel)
            });
        }
        ++seriesCnt;
O
Ovilia 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    }, this);

    var ariaLabel;
    if (series.length < 1) {
        // No series, no aria label
        return;
    }
    else {
        ariaLabel = '这是一个';

        var title = getTitle();
        if (title) {
            ariaLabel += '关于“' + title + '”的';
        }

43
        if (series.length > 1) {
O
Ovilia 已提交
44
            ariaLabel += '图表,它由' + seriesCnt + '个图表系列组成。';
O
Ovilia 已提交
45 46
        }

O
Ovilia 已提交
47 48 49 50
        zrUtil.each(series, function (s, id) {
            if (series.length > 1) {
                ariaLabel += '' + (id + 1) + '个系列是一个';
            }
O
Ovilia 已提交
51 52 53 54 55 56 57 58
            ariaLabel += s.desc;
        });

        dom.setAttribute('aria-label', ariaLabel);
    }

    function getTitle() {
        var title = ecModel.getModel('title').option;
O
Ovilia 已提交
59
        if (title && title.length) {
O
Ovilia 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72
            title = title[0];
        }
        return title && title.text;
    }

    function getSeriesTypeName(type) {
        switch (type) {
            case 'pie':
                return '饼图';
            case 'bar':
                return '柱状图';
            case 'line':
                return '折线图';
73 74 75
            case 'scatter':
            case 'effectScatter':
                return '散点图';
O
Ovilia 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
            case 'radar':
                return '雷达图';
            case 'tree':
                return '树图';
            case 'treemap':
                return '矩形树图';
            case 'boxplot':
                return '箱型图';
            case 'candlestick':
                return 'K线图';
            case 'heatmap':
                return '热力图';
            case 'map':
                return '地图';
            case 'parallel':
                return '平行坐标图';
            case 'lines':
                return '线图';
            case 'graph':
                return '关系图';
            case 'sankey':
                return '桑基图';
            case 'funnel':
                return '漏斗图';
            case 'gauge':
                return '仪表盘图';
            case 'pictorialBar':
                return '象形柱图';
            case 'themeRiver':
                return '主题河流图';
O
Ovilia 已提交
106 107 108 109 110 111 112
            default:
                return '';
        }
    }

    function getSeriesDesc(type, seriesModel) {
        var data = seriesModel.getData();
O
Ovilia 已提交
113
        var dataCnt = data.indices ? data.indices.length : 0;
O
Ovilia 已提交
114
        var seriesName = seriesModel.get('name');
O
Ovilia 已提交
115
        var displayDataCnt = Math.min(dataCnt, maxDataCnt);
O
Ovilia 已提交
116

O
Ovilia 已提交
117
        var desc = (seriesName ? '表示' + seriesName + '' : '')
O
Ovilia 已提交
118
            + getSeriesTypeName(type) + ',包括' + dataCnt + '个数据项。';
O
Ovilia 已提交
119

O
Ovilia 已提交
120
        var dataDesc = '';
O
Ovilia 已提交
121 122 123
        switch (type) {
            case 'pie':
                data.each('value', function (value, id) {
O
Ovilia 已提交
124
                    if (id < displayDataCnt) {
125 126
                        var percent = seriesModel.getDataParams(id).percent;

O
Ovilia 已提交
127
                        dataDesc += data.getName(id) + '的数据是' + value
128 129
                            + ',占' + percent + '%';

O
Ovilia 已提交
130 131
                        if (id < displayDataCnt - 1) {
                            dataDesc += '';
132 133
                        }
                        else {
O
Ovilia 已提交
134
                            dataDesc += '';
135
                        }
O
Ovilia 已提交
136 137
                    }
                });
O
Ovilia 已提交
138 139 140 141 142 143 144 145
                break;

            case 'line':
            case 'bar':
                var baseAxis = seriesModel.getBaseAxis();
                var labels = baseAxis.scale.getTicksLabels();

                zrUtil.each(data.indices, function (id, i) {
O
Ovilia 已提交
146 147
                    if (id < displayDataCnt) {
                        dataDesc += labels[id] + '' + seriesModel.getRawValue(id);
148

O
Ovilia 已提交
149 150
                        if (i < displayDataCnt - 1) {
                            dataDesc += '';
151 152
                        }
                        else {
O
Ovilia 已提交
153
                            dataDesc += '';
154
                        }
O
Ovilia 已提交
155 156 157
                    }
                });
                break;
158 159 160

            case 'scatter':
            case 'effectScatter':
O
Ovilia 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
            case 'parallel':
                zrUtil.each(data.indices, function (id, i) {
                    if (id < displayDataCnt) {
                        dataDesc += '[' + seriesModel.getRawValue(id) + ']';

                        if (i < displayDataCnt - 1) {
                            dataDesc += '';
                        }
                        else {
                            dataDesc += '';
                        }
                    }
                });
                break;

            case 'radar':
            case 'gauge':
                var data = seriesModel.option.data;
                zrUtil.each(data, function (d, i) {
                    if (i < displayDataCnt) {
                        dataDesc += (d.name ? d.name + '' : '') + d.value;

                        if (i < displayDataCnt - 1) {
                            dataDesc += '';
                        }
                        else {
                            dataDesc += '';
                        }
                    }
                });
                break;

            case 'tree':
            case 'treemap':
                var root = seriesModel.getData().tree.root;
                desc += '根节点';
                if (root.name) {
                    desc += '' + root.name + '';
                }
                desc += '包含' + root.children.length + '个数据。';
                break;

            case 'boxplot':
                var baseAxis = seriesModel.getBaseAxis();
                var labels = baseAxis.scale.getTicksLabels();

207
                zrUtil.each(data.indices, function (id, i) {
O
Ovilia 已提交
208 209 210
                    if (id < displayDataCnt) {
                        dataDesc += labels[id] + ''
                            + seriesModel.getRawValue(id).join(', ');
211

O
Ovilia 已提交
212 213
                        if (i < displayDataCnt - 1) {
                            dataDesc += '';
214 215
                        }
                        else {
O
Ovilia 已提交
216
                            dataDesc += '';
217 218 219 220 221
                        }
                    }
                });
                break;

O
Ovilia 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
            case 'funnel':
                data.each('value', function (value, id) {
                    if (id < displayDataCnt) {
                        dataDesc += data.getName(id) + '的数据占' + value + '%';

                        if (id < displayDataCnt - 1) {
                            dataDesc += '';
                        }
                        else {
                            dataDesc += '';
                        }
                    }
                });
                break;
        }

        if (dataDesc) {
            if (dataCnt === displayDataCnt) {
                // Display all data
                desc += '其数据是——' + dataDesc;
            }
            else {
                // Display part of data
                desc += '其中,前' + displayDataCnt + '项是——' + dataDesc;
            }
O
Ovilia 已提交
247 248 249 250 251
        }

        return desc;
    }
}