OverlappingDataLabels.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* *
  2. *
  3. * Highcharts module to hide overlapping data labels.
  4. * This module is included in Highcharts.
  5. *
  6. * (c) 2009-2020 Torstein Honsi
  7. *
  8. * License: www.highcharts.com/license
  9. *
  10. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  11. *
  12. * */
  13. 'use strict';
  14. import Chart from '../Core/Chart/Chart.js';
  15. import U from '../Core/Utilities.js';
  16. var addEvent = U.addEvent, fireEvent = U.fireEvent, isArray = U.isArray, isNumber = U.isNumber, objectEach = U.objectEach, pick = U.pick;
  17. /**
  18. * Internal type
  19. * @private
  20. */
  21. /* eslint-disable no-invalid-this */
  22. // Collect potensial overlapping data labels. Stack labels probably don't need
  23. // to be considered because they are usually accompanied by data labels that lie
  24. // inside the columns.
  25. addEvent(Chart, 'render', function collectAndHide() {
  26. var labels = [];
  27. // Consider external label collectors
  28. (this.labelCollectors || []).forEach(function (collector) {
  29. labels = labels.concat(collector());
  30. });
  31. (this.yAxis || []).forEach(function (yAxis) {
  32. if (yAxis.stacking &&
  33. yAxis.options.stackLabels &&
  34. !yAxis.options.stackLabels.allowOverlap) {
  35. objectEach(yAxis.stacking.stacks, function (stack) {
  36. objectEach(stack, function (stackItem) {
  37. labels.push(stackItem.label);
  38. });
  39. });
  40. }
  41. });
  42. (this.series || []).forEach(function (series) {
  43. var dlOptions = series.options.dataLabels;
  44. if (series.visible &&
  45. !(dlOptions.enabled === false && !series._hasPointLabels)) { // #3866
  46. (series.nodes || series.points).forEach(function (point) {
  47. if (point.visible) {
  48. var dataLabels = (isArray(point.dataLabels) ?
  49. point.dataLabels :
  50. (point.dataLabel ? [point.dataLabel] : []));
  51. dataLabels.forEach(function (label) {
  52. var options = label.options;
  53. label.labelrank = pick(options.labelrank, point.labelrank, point.shapeArgs && point.shapeArgs.height); // #4118
  54. if (!options.allowOverlap) {
  55. labels.push(label);
  56. }
  57. });
  58. }
  59. });
  60. }
  61. });
  62. this.hideOverlappingLabels(labels);
  63. });
  64. /**
  65. * Hide overlapping labels. Labels are moved and faded in and out on zoom to
  66. * provide a smooth visual imression.
  67. *
  68. * @private
  69. * @function Highcharts.Chart#hideOverlappingLabels
  70. * @param {Array<Highcharts.SVGElement>} labels
  71. * Rendered data labels
  72. * @requires modules/overlapping-datalabels
  73. */
  74. Chart.prototype.hideOverlappingLabels = function (labels) {
  75. var chart = this, len = labels.length, ren = chart.renderer, label, i, j, label1, label2, box1, box2, isLabelAffected = false, isIntersectRect = function (box1, box2) {
  76. return !(box2.x >= box1.x + box1.width ||
  77. box2.x + box2.width <= box1.x ||
  78. box2.y >= box1.y + box1.height ||
  79. box2.y + box2.height <= box1.y);
  80. },
  81. // Get the box with its position inside the chart, as opposed to getBBox
  82. // that only reports the position relative to the parent.
  83. getAbsoluteBox = function (label) {
  84. var pos, parent, bBox,
  85. // Substract the padding if no background or border (#4333)
  86. padding = label.box ? 0 : (label.padding || 0), lineHeightCorrection = 0, xOffset = 0, boxWidth, alignValue;
  87. if (label &&
  88. (!label.alignAttr || label.placed)) {
  89. pos = label.alignAttr || {
  90. x: label.attr('x'),
  91. y: label.attr('y')
  92. };
  93. parent = label.parentGroup;
  94. // Get width and height if pure text nodes (stack labels)
  95. if (!label.width) {
  96. bBox = label.getBBox();
  97. label.width = bBox.width;
  98. label.height = bBox.height;
  99. // Labels positions are computed from top left corner, so
  100. // we need to substract the text height from text nodes too.
  101. lineHeightCorrection = ren
  102. .fontMetrics(null, label.element).h;
  103. }
  104. boxWidth = label.width - 2 * padding;
  105. alignValue = {
  106. left: '0',
  107. center: '0.5',
  108. right: '1'
  109. }[label.alignValue];
  110. if (alignValue) {
  111. xOffset = +alignValue * boxWidth;
  112. }
  113. else if (isNumber(label.x) && Math.round(label.x) !== label.translateX) {
  114. xOffset = label.x - label.translateX;
  115. }
  116. return {
  117. x: pos.x + (parent.translateX || 0) + padding -
  118. (xOffset || 0),
  119. y: pos.y + (parent.translateY || 0) + padding -
  120. lineHeightCorrection,
  121. width: label.width - 2 * padding,
  122. height: label.height - 2 * padding
  123. };
  124. }
  125. };
  126. for (i = 0; i < len; i++) {
  127. label = labels[i];
  128. if (label) {
  129. // Mark with initial opacity
  130. label.oldOpacity = label.opacity;
  131. label.newOpacity = 1;
  132. label.absoluteBox = getAbsoluteBox(label);
  133. }
  134. }
  135. // Prevent a situation in a gradually rising slope, that each label will
  136. // hide the previous one because the previous one always has lower rank.
  137. labels.sort(function (a, b) {
  138. return (b.labelrank || 0) - (a.labelrank || 0);
  139. });
  140. // Detect overlapping labels
  141. for (i = 0; i < len; i++) {
  142. label1 = labels[i];
  143. box1 = label1 && label1.absoluteBox;
  144. for (j = i + 1; j < len; ++j) {
  145. label2 = labels[j];
  146. box2 = label2 && label2.absoluteBox;
  147. if (box1 &&
  148. box2 &&
  149. label1 !== label2 && // #6465, polar chart with connectEnds
  150. label1.newOpacity !== 0 &&
  151. label2.newOpacity !== 0) {
  152. if (isIntersectRect(box1, box2)) {
  153. (label1.labelrank < label2.labelrank ? label1 : label2)
  154. .newOpacity = 0;
  155. }
  156. }
  157. }
  158. }
  159. // Hide or show
  160. labels.forEach(function (label) {
  161. var complete, newOpacity;
  162. if (label) {
  163. newOpacity = label.newOpacity;
  164. if (label.oldOpacity !== newOpacity) {
  165. // Make sure the label is completely hidden to avoid catching
  166. // clicks (#4362)
  167. if (label.alignAttr && label.placed) { // data labels
  168. label[newOpacity ? 'removeClass' : 'addClass']('highcharts-data-label-hidden');
  169. complete = function () {
  170. if (!chart.styledMode) {
  171. label.css({ pointerEvents: newOpacity ? 'auto' : 'none' });
  172. }
  173. label.visibility = newOpacity ? 'inherit' : 'hidden';
  174. };
  175. isLabelAffected = true;
  176. // Animate or set the opacity
  177. label.alignAttr.opacity = newOpacity;
  178. label[label.isOld ? 'animate' : 'attr'](label.alignAttr, null, complete);
  179. fireEvent(chart, 'afterHideOverlappingLabel');
  180. }
  181. else { // other labels, tick labels
  182. label.attr({
  183. opacity: newOpacity
  184. });
  185. }
  186. }
  187. label.isOld = true;
  188. }
  189. });
  190. if (isLabelAffected) {
  191. fireEvent(chart, 'afterHideAllOverlappingLabels');
  192. }
  193. };