aria.js 5.2 KB

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