aria.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import * as zrUtil from 'zrender/lib/core/util.js';
  41. import { makeInner } from '../util/model.js';
  42. import { getDecalFromPalette } from '../model/mixin/palette.js';
  43. var DEFAULT_OPTION = {
  44. label: {
  45. enabled: true
  46. },
  47. decal: {
  48. show: false
  49. }
  50. };
  51. var inner = makeInner();
  52. var decalPaletteScope = {};
  53. export default function ariaVisual(ecModel, api) {
  54. var ariaModel = ecModel.getModel('aria'); // See "area enabled" detection code in `GlobalModel.ts`.
  55. if (!ariaModel.get('enabled')) {
  56. return;
  57. }
  58. var defaultOption = zrUtil.clone(DEFAULT_OPTION);
  59. zrUtil.merge(defaultOption.label, ecModel.getLocaleModel().get('aria'), false);
  60. zrUtil.merge(ariaModel.option, defaultOption, false);
  61. setDecal();
  62. setLabel();
  63. function setDecal() {
  64. var decalModel = ariaModel.getModel('decal');
  65. var useDecal = decalModel.get('show');
  66. if (useDecal) {
  67. // Each type of series use one scope.
  68. // Pie and funnel are using different scopes.
  69. var paletteScopeGroupByType_1 = zrUtil.createHashMap();
  70. ecModel.eachSeries(function (seriesModel) {
  71. if (seriesModel.isColorBySeries()) {
  72. return;
  73. }
  74. var decalScope = paletteScopeGroupByType_1.get(seriesModel.type);
  75. if (!decalScope) {
  76. decalScope = {};
  77. paletteScopeGroupByType_1.set(seriesModel.type, decalScope);
  78. }
  79. inner(seriesModel).scope = decalScope;
  80. });
  81. ecModel.eachRawSeries(function (seriesModel) {
  82. if (ecModel.isSeriesFiltered(seriesModel)) {
  83. return;
  84. }
  85. if (zrUtil.isFunction(seriesModel.enableAriaDecal)) {
  86. // Let series define how to use decal palette on data
  87. seriesModel.enableAriaDecal();
  88. return;
  89. }
  90. var data = seriesModel.getData();
  91. if (!seriesModel.isColorBySeries()) {
  92. var dataAll_1 = seriesModel.getRawData();
  93. var idxMap_1 = {};
  94. var decalScope_1 = inner(seriesModel).scope;
  95. data.each(function (idx) {
  96. var rawIdx = data.getRawIndex(idx);
  97. idxMap_1[rawIdx] = idx;
  98. });
  99. var dataCount_1 = dataAll_1.count();
  100. dataAll_1.each(function (rawIdx) {
  101. var idx = idxMap_1[rawIdx];
  102. var name = dataAll_1.getName(rawIdx) || rawIdx + '';
  103. var paletteDecal = getDecalFromPalette(seriesModel.ecModel, name, decalScope_1, dataCount_1);
  104. var specifiedDecal = data.getItemVisual(idx, 'decal');
  105. data.setItemVisual(idx, 'decal', mergeDecal(specifiedDecal, paletteDecal));
  106. });
  107. } else {
  108. var paletteDecal = getDecalFromPalette(seriesModel.ecModel, seriesModel.name, decalPaletteScope, ecModel.getSeriesCount());
  109. var specifiedDecal = data.getVisual('decal');
  110. data.setVisual('decal', mergeDecal(specifiedDecal, paletteDecal));
  111. }
  112. function mergeDecal(specifiedDecal, paletteDecal) {
  113. // Merge decal from palette to decal from itemStyle.
  114. // User do not need to specify all of the decal props.
  115. var resultDecal = specifiedDecal ? zrUtil.extend(zrUtil.extend({}, paletteDecal), specifiedDecal) : paletteDecal;
  116. resultDecal.dirty = true;
  117. return resultDecal;
  118. }
  119. });
  120. }
  121. }
  122. function setLabel() {
  123. var labelLocale = ecModel.getLocaleModel().get('aria');
  124. var labelModel = ariaModel.getModel('label');
  125. labelModel.option = zrUtil.defaults(labelModel.option, labelLocale);
  126. if (!labelModel.get('enabled')) {
  127. return;
  128. }
  129. var dom = api.getZr().dom;
  130. if (labelModel.get('description')) {
  131. dom.setAttribute('aria-label', labelModel.get('description'));
  132. return;
  133. }
  134. var seriesCnt = ecModel.getSeriesCount();
  135. var maxDataCnt = labelModel.get(['data', 'maxCount']) || 10;
  136. var maxSeriesCnt = labelModel.get(['series', 'maxCount']) || 10;
  137. var displaySeriesCnt = Math.min(seriesCnt, maxSeriesCnt);
  138. var ariaLabel;
  139. if (seriesCnt < 1) {
  140. // No series, no aria label
  141. return;
  142. } else {
  143. var title = getTitle();
  144. if (title) {
  145. var withTitle = labelModel.get(['general', 'withTitle']);
  146. ariaLabel = replace(withTitle, {
  147. title: title
  148. });
  149. } else {
  150. ariaLabel = labelModel.get(['general', 'withoutTitle']);
  151. }
  152. var seriesLabels_1 = [];
  153. var prefix = seriesCnt > 1 ? labelModel.get(['series', 'multiple', 'prefix']) : labelModel.get(['series', 'single', 'prefix']);
  154. ariaLabel += replace(prefix, {
  155. seriesCount: seriesCnt
  156. });
  157. ecModel.eachSeries(function (seriesModel, idx) {
  158. if (idx < displaySeriesCnt) {
  159. var seriesLabel = void 0;
  160. var seriesName = seriesModel.get('name');
  161. var withName = seriesName ? 'withName' : 'withoutName';
  162. seriesLabel = seriesCnt > 1 ? labelModel.get(['series', 'multiple', withName]) : labelModel.get(['series', 'single', withName]);
  163. seriesLabel = replace(seriesLabel, {
  164. seriesId: seriesModel.seriesIndex,
  165. seriesName: seriesModel.get('name'),
  166. seriesType: getSeriesTypeName(seriesModel.subType)
  167. });
  168. var data = seriesModel.getData();
  169. if (data.count() > maxDataCnt) {
  170. // Show part of data
  171. var partialLabel = labelModel.get(['data', 'partialData']);
  172. seriesLabel += replace(partialLabel, {
  173. displayCnt: maxDataCnt
  174. });
  175. } else {
  176. seriesLabel += labelModel.get(['data', 'allData']);
  177. }
  178. var middleSeparator_1 = labelModel.get(['data', 'separator', 'middle']);
  179. var endSeparator_1 = labelModel.get(['data', 'separator', 'end']);
  180. var dataLabels = [];
  181. for (var i = 0; i < data.count(); i++) {
  182. if (i < maxDataCnt) {
  183. var name_1 = data.getName(i);
  184. var value = data.getValues(i);
  185. var dataLabel = labelModel.get(['data', name_1 ? 'withName' : 'withoutName']);
  186. dataLabels.push(replace(dataLabel, {
  187. name: name_1,
  188. value: value.join(middleSeparator_1)
  189. }));
  190. }
  191. }
  192. seriesLabel += dataLabels.join(middleSeparator_1) + endSeparator_1;
  193. seriesLabels_1.push(seriesLabel);
  194. }
  195. });
  196. var separatorModel = labelModel.getModel(['series', 'multiple', 'separator']);
  197. var middleSeparator = separatorModel.get('middle');
  198. var endSeparator = separatorModel.get('end');
  199. ariaLabel += seriesLabels_1.join(middleSeparator) + endSeparator;
  200. dom.setAttribute('aria-label', ariaLabel);
  201. }
  202. }
  203. function replace(str, keyValues) {
  204. if (!zrUtil.isString(str)) {
  205. return str;
  206. }
  207. var result = str;
  208. zrUtil.each(keyValues, function (value, key) {
  209. result = result.replace(new RegExp('\\{\\s*' + key + '\\s*\\}', 'g'), value);
  210. });
  211. return result;
  212. }
  213. function getTitle() {
  214. var title = ecModel.get('title');
  215. if (title && title.length) {
  216. title = title[0];
  217. }
  218. return title && title.text;
  219. }
  220. function getSeriesTypeName(type) {
  221. return ecModel.getLocaleModel().get(['series', 'typeNames'])[type] || '自定义图';
  222. }
  223. }