aria.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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');
  55. // See "area enabled" detection code in `GlobalModel.ts`.
  56. if (!ariaModel.get('enabled')) {
  57. return;
  58. }
  59. var defaultOption = zrUtil.clone(DEFAULT_OPTION);
  60. zrUtil.merge(defaultOption.label, ecModel.getLocaleModel().get('aria'), false);
  61. zrUtil.merge(ariaModel.option, defaultOption, false);
  62. setDecal();
  63. setLabel();
  64. function setDecal() {
  65. var decalModel = ariaModel.getModel('decal');
  66. var useDecal = decalModel.get('show');
  67. if (useDecal) {
  68. // Each type of series use one scope.
  69. // Pie and funnel are using different scopes.
  70. var paletteScopeGroupByType_1 = zrUtil.createHashMap();
  71. ecModel.eachSeries(function (seriesModel) {
  72. if (seriesModel.isColorBySeries()) {
  73. return;
  74. }
  75. var decalScope = paletteScopeGroupByType_1.get(seriesModel.type);
  76. if (!decalScope) {
  77. decalScope = {};
  78. paletteScopeGroupByType_1.set(seriesModel.type, decalScope);
  79. }
  80. inner(seriesModel).scope = decalScope;
  81. });
  82. ecModel.eachRawSeries(function (seriesModel) {
  83. if (ecModel.isSeriesFiltered(seriesModel)) {
  84. return;
  85. }
  86. if (zrUtil.isFunction(seriesModel.enableAriaDecal)) {
  87. // Let series define how to use decal palette on data
  88. seriesModel.enableAriaDecal();
  89. return;
  90. }
  91. var data = seriesModel.getData();
  92. if (!seriesModel.isColorBySeries()) {
  93. var dataAll_1 = seriesModel.getRawData();
  94. var idxMap_1 = {};
  95. var decalScope_1 = inner(seriesModel).scope;
  96. data.each(function (idx) {
  97. var rawIdx = data.getRawIndex(idx);
  98. idxMap_1[rawIdx] = idx;
  99. });
  100. var dataCount_1 = dataAll_1.count();
  101. dataAll_1.each(function (rawIdx) {
  102. var idx = idxMap_1[rawIdx];
  103. var name = dataAll_1.getName(rawIdx) || rawIdx + '';
  104. var paletteDecal = getDecalFromPalette(seriesModel.ecModel, name, decalScope_1, dataCount_1);
  105. var specifiedDecal = data.getItemVisual(idx, 'decal');
  106. data.setItemVisual(idx, 'decal', mergeDecal(specifiedDecal, paletteDecal));
  107. });
  108. } else {
  109. var paletteDecal = getDecalFromPalette(seriesModel.ecModel, seriesModel.name, decalPaletteScope, ecModel.getSeriesCount());
  110. var specifiedDecal = data.getVisual('decal');
  111. data.setVisual('decal', mergeDecal(specifiedDecal, paletteDecal));
  112. }
  113. function mergeDecal(specifiedDecal, paletteDecal) {
  114. // Merge decal from palette to decal from itemStyle.
  115. // User do not need to specify all of the decal props.
  116. var resultDecal = specifiedDecal ? zrUtil.extend(zrUtil.extend({}, paletteDecal), specifiedDecal) : paletteDecal;
  117. resultDecal.dirty = true;
  118. return resultDecal;
  119. }
  120. });
  121. }
  122. }
  123. function setLabel() {
  124. var dom = api.getZr().dom;
  125. // TODO: support for SSR
  126. if (!dom) {
  127. return;
  128. }
  129. var labelLocale = ecModel.getLocaleModel().get('aria');
  130. var labelModel = ariaModel.getModel('label');
  131. labelModel.option = zrUtil.defaults(labelModel.option, labelLocale);
  132. if (!labelModel.get('enabled')) {
  133. return;
  134. }
  135. if (labelModel.get('description')) {
  136. dom.setAttribute('aria-label', labelModel.get('description'));
  137. return;
  138. }
  139. var seriesCnt = ecModel.getSeriesCount();
  140. var maxDataCnt = labelModel.get(['data', 'maxCount']) || 10;
  141. var maxSeriesCnt = labelModel.get(['series', 'maxCount']) || 10;
  142. var displaySeriesCnt = Math.min(seriesCnt, maxSeriesCnt);
  143. var ariaLabel;
  144. if (seriesCnt < 1) {
  145. // No series, no aria label
  146. return;
  147. } else {
  148. var title = getTitle();
  149. if (title) {
  150. var withTitle = labelModel.get(['general', 'withTitle']);
  151. ariaLabel = replace(withTitle, {
  152. title: title
  153. });
  154. } else {
  155. ariaLabel = labelModel.get(['general', 'withoutTitle']);
  156. }
  157. var seriesLabels_1 = [];
  158. var prefix = seriesCnt > 1 ? labelModel.get(['series', 'multiple', 'prefix']) : labelModel.get(['series', 'single', 'prefix']);
  159. ariaLabel += replace(prefix, {
  160. seriesCount: seriesCnt
  161. });
  162. ecModel.eachSeries(function (seriesModel, idx) {
  163. if (idx < displaySeriesCnt) {
  164. var seriesLabel = void 0;
  165. var seriesName = seriesModel.get('name');
  166. var withName = seriesName ? 'withName' : 'withoutName';
  167. seriesLabel = seriesCnt > 1 ? labelModel.get(['series', 'multiple', withName]) : labelModel.get(['series', 'single', withName]);
  168. seriesLabel = replace(seriesLabel, {
  169. seriesId: seriesModel.seriesIndex,
  170. seriesName: seriesModel.get('name'),
  171. seriesType: getSeriesTypeName(seriesModel.subType)
  172. });
  173. var data = seriesModel.getData();
  174. if (data.count() > maxDataCnt) {
  175. // Show part of data
  176. var partialLabel = labelModel.get(['data', 'partialData']);
  177. seriesLabel += replace(partialLabel, {
  178. displayCnt: maxDataCnt
  179. });
  180. } else {
  181. seriesLabel += labelModel.get(['data', 'allData']);
  182. }
  183. var middleSeparator_1 = labelModel.get(['data', 'separator', 'middle']);
  184. var endSeparator_1 = labelModel.get(['data', 'separator', 'end']);
  185. var dataLabels = [];
  186. for (var i = 0; i < data.count(); i++) {
  187. if (i < maxDataCnt) {
  188. var name_1 = data.getName(i);
  189. var value = data.getValues(i);
  190. var dataLabel = labelModel.get(['data', name_1 ? 'withName' : 'withoutName']);
  191. dataLabels.push(replace(dataLabel, {
  192. name: name_1,
  193. value: value.join(middleSeparator_1)
  194. }));
  195. }
  196. }
  197. seriesLabel += dataLabels.join(middleSeparator_1) + endSeparator_1;
  198. seriesLabels_1.push(seriesLabel);
  199. }
  200. });
  201. var separatorModel = labelModel.getModel(['series', 'multiple', 'separator']);
  202. var middleSeparator = separatorModel.get('middle');
  203. var endSeparator = separatorModel.get('end');
  204. ariaLabel += seriesLabels_1.join(middleSeparator) + endSeparator;
  205. dom.setAttribute('aria-label', ariaLabel);
  206. }
  207. }
  208. function replace(str, keyValues) {
  209. if (!zrUtil.isString(str)) {
  210. return str;
  211. }
  212. var result = str;
  213. zrUtil.each(keyValues, function (value, key) {
  214. result = result.replace(new RegExp('\\{\\s*' + key + '\\s*\\}', 'g'), value);
  215. });
  216. return result;
  217. }
  218. function getTitle() {
  219. var title = ecModel.get('title');
  220. if (title && title.length) {
  221. title = title[0];
  222. }
  223. return title && title.text;
  224. }
  225. function getSeriesTypeName(type) {
  226. var typeNames = ecModel.getLocaleModel().get(['series', 'typeNames']);
  227. return typeNames[type] || typeNames.chart;
  228. }
  229. }