SeriesDescriber.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* *
  2. *
  3. * (c) 2009-2021 Øystein Moseng
  4. *
  5. * Place desriptions on a series and its points.
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import AnnotationsA11y from '../AnnotationsA11y.js';
  14. var getPointAnnotationTexts = AnnotationsA11y.getPointAnnotationTexts;
  15. import ChartUtilities from '../../Utils/ChartUtilities.js';
  16. var getAxisDescription = ChartUtilities.getAxisDescription, getSeriesFirstPointElement = ChartUtilities.getSeriesFirstPointElement, getSeriesA11yElement = ChartUtilities.getSeriesA11yElement, unhideChartElementFromAT = ChartUtilities.unhideChartElementFromAT;
  17. import HTMLUtilities from '../../Utils/HTMLUtilities.js';
  18. var reverseChildNodes = HTMLUtilities.reverseChildNodes, stripHTMLTags = HTMLUtilities.stripHTMLTagsFromString;
  19. import Tooltip from '../../../Core/Tooltip.js';
  20. import U from '../../../Core/Utilities.js';
  21. var find = U.find, format = U.format, isNumber = U.isNumber, numberFormat = U.numberFormat, pick = U.pick, defined = U.defined;
  22. /* eslint-disable valid-jsdoc */
  23. /**
  24. * @private
  25. */
  26. function findFirstPointWithGraphic(point) {
  27. var sourcePointIndex = point.index;
  28. if (!point.series || !point.series.data || !defined(sourcePointIndex)) {
  29. return null;
  30. }
  31. return find(point.series.data, function (p) {
  32. return !!(p &&
  33. typeof p.index !== 'undefined' &&
  34. p.index > sourcePointIndex &&
  35. p.graphic &&
  36. p.graphic.element);
  37. }) || null;
  38. }
  39. /**
  40. * @private
  41. */
  42. function shouldAddDummyPoint(point) {
  43. // Note: Sunburst series use isNull for hidden points on drilldown.
  44. // Ignore these.
  45. var isSunburst = point.series && point.series.is('sunburst'), isNull = point.isNull;
  46. return isNull && !isSunburst;
  47. }
  48. /**
  49. * @private
  50. */
  51. function makeDummyElement(point, pos) {
  52. var renderer = point.series.chart.renderer, dummy = renderer.rect(pos.x, pos.y, 1, 1);
  53. dummy.attr({
  54. 'class': 'highcharts-a11y-dummy-point',
  55. fill: 'none',
  56. opacity: 0,
  57. 'fill-opacity': 0,
  58. 'stroke-opacity': 0
  59. });
  60. return dummy;
  61. }
  62. /**
  63. * @private
  64. * @param {Highcharts.Point} point
  65. * @return {Highcharts.HTMLDOMElement|Highcharts.SVGDOMElement|undefined}
  66. */
  67. function addDummyPointElement(point) {
  68. var series = point.series, firstPointWithGraphic = findFirstPointWithGraphic(point), firstGraphic = firstPointWithGraphic && firstPointWithGraphic.graphic, parentGroup = firstGraphic ?
  69. firstGraphic.parentGroup :
  70. series.graph || series.group, dummyPos = firstPointWithGraphic ? {
  71. x: pick(point.plotX, firstPointWithGraphic.plotX, 0),
  72. y: pick(point.plotY, firstPointWithGraphic.plotY, 0)
  73. } : {
  74. x: pick(point.plotX, 0),
  75. y: pick(point.plotY, 0)
  76. }, dummyElement = makeDummyElement(point, dummyPos);
  77. if (parentGroup && parentGroup.element) {
  78. point.graphic = dummyElement;
  79. point.hasDummyGraphic = true;
  80. dummyElement.add(parentGroup);
  81. // Move to correct pos in DOM
  82. parentGroup.element.insertBefore(dummyElement.element, firstGraphic ? firstGraphic.element : null);
  83. return dummyElement.element;
  84. }
  85. }
  86. /**
  87. * @private
  88. * @param {Highcharts.Series} series
  89. * @return {boolean}
  90. */
  91. function hasMorePointsThanDescriptionThreshold(series) {
  92. var chartA11yOptions = series.chart.options.accessibility, threshold = (chartA11yOptions.series.pointDescriptionEnabledThreshold);
  93. return !!(threshold !== false &&
  94. series.points &&
  95. series.points.length >= threshold);
  96. }
  97. /**
  98. * @private
  99. * @param {Highcharts.Series} series
  100. * @return {boolean}
  101. */
  102. function shouldSetScreenReaderPropsOnPoints(series) {
  103. var seriesA11yOptions = series.options.accessibility || {};
  104. return !hasMorePointsThanDescriptionThreshold(series) &&
  105. !seriesA11yOptions.exposeAsGroupOnly;
  106. }
  107. /**
  108. * @private
  109. * @param {Highcharts.Series} series
  110. * @return {boolean}
  111. */
  112. function shouldSetKeyboardNavPropsOnPoints(series) {
  113. var chartA11yOptions = series.chart.options.accessibility, seriesNavOptions = chartA11yOptions.keyboardNavigation.seriesNavigation;
  114. return !!(series.points && (series.points.length <
  115. seriesNavOptions.pointNavigationEnabledThreshold ||
  116. seriesNavOptions.pointNavigationEnabledThreshold === false));
  117. }
  118. /**
  119. * @private
  120. * @param {Highcharts.Series} series
  121. * @return {boolean}
  122. */
  123. function shouldDescribeSeriesElement(series) {
  124. var chart = series.chart, chartOptions = chart.options.chart || {}, chartHas3d = chartOptions.options3d && chartOptions.options3d.enabled, hasMultipleSeries = chart.series.length > 1, describeSingleSeriesOption = chart.options.accessibility.series.describeSingleSeries, exposeAsGroupOnlyOption = (series.options.accessibility || {}).exposeAsGroupOnly, noDescribe3D = chartHas3d && hasMultipleSeries;
  125. return !noDescribe3D && (hasMultipleSeries || describeSingleSeriesOption ||
  126. exposeAsGroupOnlyOption || hasMorePointsThanDescriptionThreshold(series));
  127. }
  128. /**
  129. * @private
  130. * @param {Highcharts.Point} point
  131. * @param {number} value
  132. * @return {string}
  133. */
  134. function pointNumberToString(point, value) {
  135. var chart = point.series.chart, a11yPointOptions = chart.options.accessibility.point || {}, tooltipOptions = point.series.tooltipOptions || {}, lang = chart.options.lang;
  136. if (isNumber(value)) {
  137. return numberFormat(value, a11yPointOptions.valueDecimals ||
  138. tooltipOptions.valueDecimals ||
  139. -1, lang.decimalPoint, lang.accessibility.thousandsSep || lang.thousandsSep);
  140. }
  141. return value;
  142. }
  143. /**
  144. * @private
  145. * @param {Highcharts.Series} series
  146. * @return {string}
  147. */
  148. function getSeriesDescriptionText(series) {
  149. var seriesA11yOptions = series.options.accessibility || {}, descOpt = seriesA11yOptions.description;
  150. return descOpt && series.chart.langFormat('accessibility.series.description', {
  151. description: descOpt,
  152. series: series
  153. }) || '';
  154. }
  155. /**
  156. * @private
  157. * @param {Highcharts.series} series
  158. * @param {string} axisCollection
  159. * @return {string}
  160. */
  161. function getSeriesAxisDescriptionText(series, axisCollection) {
  162. var axis = series[axisCollection];
  163. return series.chart.langFormat('accessibility.series.' + axisCollection + 'Description', {
  164. name: getAxisDescription(axis),
  165. series: series
  166. });
  167. }
  168. /**
  169. * Get accessible time description for a point on a datetime axis.
  170. *
  171. * @private
  172. * @function Highcharts.Point#getTimeDescription
  173. * @param {Highcharts.Point} point
  174. * @return {string|undefined}
  175. * The description as string.
  176. */
  177. function getPointA11yTimeDescription(point) {
  178. var series = point.series, chart = series.chart, a11yOptions = chart.options.accessibility.point || {}, hasDateXAxis = series.xAxis && series.xAxis.dateTime;
  179. if (hasDateXAxis) {
  180. var tooltipDateFormat = Tooltip.prototype.getXDateFormat.call({
  181. getDateFormat: Tooltip.prototype.getDateFormat,
  182. chart: chart
  183. }, point, chart.options.tooltip, series.xAxis), dateFormat = a11yOptions.dateFormatter &&
  184. a11yOptions.dateFormatter(point) ||
  185. a11yOptions.dateFormat ||
  186. tooltipDateFormat;
  187. return chart.time.dateFormat(dateFormat, point.x, void 0);
  188. }
  189. }
  190. /**
  191. * @private
  192. * @param {Highcharts.Point} point
  193. * @return {string}
  194. */
  195. function getPointXDescription(point) {
  196. var timeDesc = getPointA11yTimeDescription(point), xAxis = point.series.xAxis || {}, pointCategory = xAxis.categories && defined(point.category) &&
  197. ('' + point.category).replace('<br/>', ' '), canUseId = point.id && point.id.indexOf('highcharts-') < 0, fallback = 'x, ' + point.x;
  198. return point.name || timeDesc || pointCategory ||
  199. (canUseId ? point.id : fallback);
  200. }
  201. /**
  202. * @private
  203. * @param {Highcharts.Point} point
  204. * @param {string} prefix
  205. * @param {string} suffix
  206. * @return {string}
  207. */
  208. function getPointArrayMapValueDescription(point, prefix, suffix) {
  209. var pre = prefix || '', suf = suffix || '', keyToValStr = function (key) {
  210. var num = pointNumberToString(point, pick(point[key], point.options[key]));
  211. return key + ': ' + pre + num + suf;
  212. }, pointArrayMap = point.series.pointArrayMap;
  213. return pointArrayMap.reduce(function (desc, key) {
  214. return desc + (desc.length ? ', ' : '') + keyToValStr(key);
  215. }, '');
  216. }
  217. /**
  218. * @private
  219. * @param {Highcharts.Point} point
  220. * @return {string}
  221. */
  222. function getPointValue(point) {
  223. var series = point.series, a11yPointOpts = series.chart.options.accessibility.point || {}, tooltipOptions = series.tooltipOptions || {}, valuePrefix = a11yPointOpts.valuePrefix ||
  224. tooltipOptions.valuePrefix || '', valueSuffix = a11yPointOpts.valueSuffix ||
  225. tooltipOptions.valueSuffix || '', fallbackKey = (typeof point.value !==
  226. 'undefined' ?
  227. 'value' : 'y'), fallbackDesc = pointNumberToString(point, point[fallbackKey]);
  228. if (point.isNull) {
  229. return series.chart.langFormat('accessibility.series.nullPointValue', {
  230. point: point
  231. });
  232. }
  233. if (series.pointArrayMap) {
  234. return getPointArrayMapValueDescription(point, valuePrefix, valueSuffix);
  235. }
  236. return valuePrefix + fallbackDesc + valueSuffix;
  237. }
  238. /**
  239. * Return the description for the annotation(s) connected to a point, or empty
  240. * string if none.
  241. *
  242. * @private
  243. * @param {Highcharts.Point} point The data point to get the annotation info from.
  244. * @return {string} Annotation description
  245. */
  246. function getPointAnnotationDescription(point) {
  247. var chart = point.series.chart;
  248. var langKey = 'accessibility.series.pointAnnotationsDescription';
  249. var annotations = getPointAnnotationTexts(point);
  250. var context = { point: point, annotations: annotations };
  251. return annotations.length ? chart.langFormat(langKey, context) : '';
  252. }
  253. /**
  254. * Return string with information about point.
  255. * @private
  256. * @return {string}
  257. */
  258. function getPointValueDescription(point) {
  259. var series = point.series, chart = series.chart, pointValueDescriptionFormat = chart.options.accessibility
  260. .point.valueDescriptionFormat, showXDescription = pick(series.xAxis &&
  261. series.xAxis.options.accessibility &&
  262. series.xAxis.options.accessibility.enabled, !chart.angular), xDesc = showXDescription ? getPointXDescription(point) : '', context = {
  263. point: point,
  264. index: defined(point.index) ? (point.index + 1) : '',
  265. xDescription: xDesc,
  266. value: getPointValue(point),
  267. separator: showXDescription ? ', ' : ''
  268. };
  269. return format(pointValueDescriptionFormat, context, chart);
  270. }
  271. /**
  272. * Return string with information about point.
  273. * @private
  274. * @return {string}
  275. */
  276. function defaultPointDescriptionFormatter(point) {
  277. var series = point.series, chart = series.chart, valText = getPointValueDescription(point), description = point.options && point.options.accessibility &&
  278. point.options.accessibility.description, userDescText = description ? ' ' + description : '', seriesNameText = chart.series.length > 1 && series.name ?
  279. ' ' + series.name + '.' : '', annotationsDesc = getPointAnnotationDescription(point), pointAnnotationsText = annotationsDesc ? ' ' + annotationsDesc : '';
  280. point.accessibility = point.accessibility || {};
  281. point.accessibility.valueDescription = valText;
  282. return valText + userDescText + seriesNameText + pointAnnotationsText;
  283. }
  284. /**
  285. * Set a11y props on a point element
  286. * @private
  287. * @param {Highcharts.Point} point
  288. * @param {Highcharts.HTMLDOMElement|Highcharts.SVGDOMElement} pointElement
  289. */
  290. function setPointScreenReaderAttribs(point, pointElement) {
  291. var series = point.series, a11yPointOptions = series.chart.options.accessibility.point || {}, seriesA11yOptions = series.options.accessibility || {}, label = stripHTMLTags(seriesA11yOptions.pointDescriptionFormatter &&
  292. seriesA11yOptions.pointDescriptionFormatter(point) ||
  293. a11yPointOptions.descriptionFormatter &&
  294. a11yPointOptions.descriptionFormatter(point) ||
  295. defaultPointDescriptionFormatter(point));
  296. pointElement.setAttribute('role', 'img');
  297. pointElement.setAttribute('aria-label', label);
  298. }
  299. /**
  300. * Add accessible info to individual point elements of a series
  301. * @private
  302. * @param {Highcharts.Series} series
  303. */
  304. function describePointsInSeries(series) {
  305. var setScreenReaderProps = shouldSetScreenReaderPropsOnPoints(series), setKeyboardProps = shouldSetKeyboardNavPropsOnPoints(series);
  306. if (setScreenReaderProps || setKeyboardProps) {
  307. series.points.forEach(function (point) {
  308. var _a, _b;
  309. var pointEl = point.graphic && point.graphic.element ||
  310. shouldAddDummyPoint(point) && addDummyPointElement(point);
  311. var pointA11yDisabled = ((_b = (_a = point.options) === null || _a === void 0 ? void 0 : _a.accessibility) === null || _b === void 0 ? void 0 : _b.enabled) === false;
  312. if (pointEl) {
  313. // We always set tabindex, as long as we are setting props.
  314. // When setting tabindex, also remove default outline to
  315. // avoid ugly border on click.
  316. pointEl.setAttribute('tabindex', '-1');
  317. pointEl.style.outline = '0';
  318. if (setScreenReaderProps && !pointA11yDisabled) {
  319. setPointScreenReaderAttribs(point, pointEl);
  320. }
  321. else {
  322. pointEl.setAttribute('aria-hidden', true);
  323. }
  324. }
  325. });
  326. }
  327. }
  328. /**
  329. * Return string with information about series.
  330. * @private
  331. * @return {string}
  332. */
  333. function defaultSeriesDescriptionFormatter(series) {
  334. var chart = series.chart, chartTypes = chart.types || [], description = getSeriesDescriptionText(series), shouldDescribeAxis = function (coll) {
  335. return chart[coll] && chart[coll].length > 1 && series[coll];
  336. }, xAxisInfo = getSeriesAxisDescriptionText(series, 'xAxis'), yAxisInfo = getSeriesAxisDescriptionText(series, 'yAxis'), summaryContext = {
  337. name: series.name || '',
  338. ix: series.index + 1,
  339. numSeries: chart.series && chart.series.length,
  340. numPoints: series.points && series.points.length,
  341. series: series
  342. }, combinationSuffix = chartTypes.length > 1 ? 'Combination' : '', summary = chart.langFormat('accessibility.series.summary.' + series.type + combinationSuffix, summaryContext) || chart.langFormat('accessibility.series.summary.default' + combinationSuffix, summaryContext);
  343. return summary + (description ? ' ' + description : '') + (shouldDescribeAxis('yAxis') ? ' ' + yAxisInfo : '') + (shouldDescribeAxis('xAxis') ? ' ' + xAxisInfo : '');
  344. }
  345. /**
  346. * Set a11y props on a series element
  347. * @private
  348. * @param {Highcharts.Series} series
  349. * @param {Highcharts.HTMLDOMElement|Highcharts.SVGDOMElement} seriesElement
  350. */
  351. function describeSeriesElement(series, seriesElement) {
  352. var seriesA11yOptions = series.options.accessibility || {}, a11yOptions = series.chart.options.accessibility, landmarkVerbosity = a11yOptions.landmarkVerbosity;
  353. // Handle role attribute
  354. if (seriesA11yOptions.exposeAsGroupOnly) {
  355. seriesElement.setAttribute('role', 'img');
  356. }
  357. else if (landmarkVerbosity === 'all') {
  358. seriesElement.setAttribute('role', 'region');
  359. } /* else do not add role */
  360. seriesElement.setAttribute('tabindex', '-1');
  361. seriesElement.style.outline = '0'; // Don't show browser outline on click, despite tabindex
  362. seriesElement.setAttribute('aria-label', stripHTMLTags(a11yOptions.series.descriptionFormatter &&
  363. a11yOptions.series.descriptionFormatter(series) ||
  364. defaultSeriesDescriptionFormatter(series)));
  365. }
  366. /**
  367. * Put accessible info on series and points of a series.
  368. * @param {Highcharts.Series} series The series to add info on.
  369. */
  370. function describeSeries(series) {
  371. var chart = series.chart, firstPointEl = getSeriesFirstPointElement(series), seriesEl = getSeriesA11yElement(series), is3d = chart.is3d && chart.is3d();
  372. if (seriesEl) {
  373. // For some series types the order of elements do not match the
  374. // order of points in series. In that case we have to reverse them
  375. // in order for AT to read them out in an understandable order.
  376. // Due to z-index issues we can not do this for 3D charts.
  377. if (seriesEl.lastChild === firstPointEl && !is3d) {
  378. reverseChildNodes(seriesEl);
  379. }
  380. describePointsInSeries(series);
  381. unhideChartElementFromAT(chart, seriesEl);
  382. if (shouldDescribeSeriesElement(series)) {
  383. describeSeriesElement(series, seriesEl);
  384. }
  385. else {
  386. seriesEl.setAttribute('aria-label', '');
  387. }
  388. }
  389. }
  390. var SeriesDescriber = {
  391. describeSeries: describeSeries,
  392. defaultPointDescriptionFormatter: defaultPointDescriptionFormatter,
  393. defaultSeriesDescriptionFormatter: defaultSeriesDescriptionFormatter,
  394. getPointA11yTimeDescription: getPointA11yTimeDescription,
  395. getPointXDescription: getPointXDescription,
  396. getPointValue: getPointValue,
  397. getPointValueDescription: getPointValueDescription
  398. };
  399. export default SeriesDescriber;