aria.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import * as zrUtil from 'zrender/src/core/util';
  20. import lang from '../lang';
  21. import { retrieveRawValue } from '../data/helper/dataProvider';
  22. export default function (dom, ecModel) {
  23. var ariaModel = ecModel.getModel('aria');
  24. if (!ariaModel.get('show')) {
  25. return;
  26. }
  27. else if (ariaModel.get('description')) {
  28. dom.setAttribute('aria-label', ariaModel.get('description'));
  29. return;
  30. }
  31. var seriesCnt = 0;
  32. ecModel.eachSeries(function (seriesModel, idx) {
  33. ++seriesCnt;
  34. }, this);
  35. var maxDataCnt = ariaModel.get('data.maxCount') || 10;
  36. var maxSeriesCnt = ariaModel.get('series.maxCount') || 10;
  37. var displaySeriesCnt = Math.min(seriesCnt, maxSeriesCnt);
  38. var ariaLabel;
  39. if (seriesCnt < 1) {
  40. // No series, no aria label
  41. return;
  42. }
  43. else {
  44. var title = getTitle();
  45. if (title) {
  46. ariaLabel = replace(getConfig('general.withTitle'), {
  47. title: title
  48. });
  49. }
  50. else {
  51. ariaLabel = getConfig('general.withoutTitle');
  52. }
  53. var seriesLabels = [];
  54. var prefix = seriesCnt > 1
  55. ? 'series.multiple.prefix'
  56. : 'series.single.prefix';
  57. ariaLabel += replace(getConfig(prefix), { seriesCount: seriesCnt });
  58. ecModel.eachSeries(function (seriesModel, idx) {
  59. if (idx < displaySeriesCnt) {
  60. var seriesLabel;
  61. var seriesName = seriesModel.get('name');
  62. var seriesTpl = 'series.'
  63. + (seriesCnt > 1 ? 'multiple' : 'single') + '.';
  64. seriesLabel = getConfig(seriesName
  65. ? seriesTpl + 'withName'
  66. : seriesTpl + 'withoutName');
  67. seriesLabel = replace(seriesLabel, {
  68. seriesId: seriesModel.seriesIndex,
  69. seriesName: seriesModel.get('name'),
  70. seriesType: getSeriesTypeName(seriesModel.subType)
  71. });
  72. var data = seriesModel.getData();
  73. window.data = data;
  74. if (data.count() > maxDataCnt) {
  75. // Show part of data
  76. seriesLabel += replace(getConfig('data.partialData'), {
  77. displayCnt: maxDataCnt
  78. });
  79. }
  80. else {
  81. seriesLabel += getConfig('data.allData');
  82. }
  83. var dataLabels = [];
  84. for (var i = 0; i < data.count(); i++) {
  85. if (i < maxDataCnt) {
  86. var name = data.getName(i);
  87. var value = retrieveRawValue(data, i);
  88. dataLabels.push(
  89. replace(
  90. name
  91. ? getConfig('data.withName')
  92. : getConfig('data.withoutName'),
  93. {
  94. name: name,
  95. value: value
  96. }
  97. )
  98. );
  99. }
  100. }
  101. seriesLabel += dataLabels
  102. .join(getConfig('data.separator.middle'))
  103. + getConfig('data.separator.end');
  104. seriesLabels.push(seriesLabel);
  105. }
  106. });
  107. ariaLabel += seriesLabels
  108. .join(getConfig('series.multiple.separator.middle'))
  109. + getConfig('series.multiple.separator.end');
  110. dom.setAttribute('aria-label', ariaLabel);
  111. }
  112. function replace(str, keyValues) {
  113. if (typeof str !== 'string') {
  114. return str;
  115. }
  116. var result = str;
  117. zrUtil.each(keyValues, function (value, key) {
  118. result = result.replace(
  119. new RegExp('\\{\\s*' + key + '\\s*\\}', 'g'),
  120. value
  121. );
  122. });
  123. return result;
  124. }
  125. function getConfig(path) {
  126. var userConfig = ariaModel.get(path);
  127. if (userConfig == null) {
  128. var pathArr = path.split('.');
  129. var result = lang.aria;
  130. for (var i = 0; i < pathArr.length; ++i) {
  131. result = result[pathArr[i]];
  132. }
  133. return result;
  134. }
  135. else {
  136. return userConfig;
  137. }
  138. }
  139. function getTitle() {
  140. var title = ecModel.getModel('title').option;
  141. if (title && title.length) {
  142. title = title[0];
  143. }
  144. return title && title.text;
  145. }
  146. function getSeriesTypeName(type) {
  147. return lang.series.typeNames[type] || '自定义图';
  148. }
  149. }