SunburstUtilities.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* *
  2. *
  3. * This module implements sunburst charts in Highcharts.
  4. *
  5. * (c) 2016-2021 Highsoft AS
  6. *
  7. * Authors: Jon Arild Nygard
  8. *
  9. * License: www.highcharts.com/license
  10. *
  11. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  12. *
  13. * */
  14. 'use strict';
  15. import SeriesRegistry from '../../Core/Series/SeriesRegistry.js';
  16. var TreemapSeries = SeriesRegistry.seriesTypes.treemap;
  17. import U from '../../Core/Utilities.js';
  18. var isNumber = U.isNumber, isObject = U.isObject, merge = U.merge;
  19. /* *
  20. *
  21. * Namespace
  22. *
  23. * */
  24. var SunburstUtilities;
  25. (function (SunburstUtilities) {
  26. /* *
  27. *
  28. * Constants
  29. *
  30. * */
  31. SunburstUtilities.recursive = TreemapSeries.prototype.utils.recursive;
  32. /* *
  33. *
  34. * Functions
  35. *
  36. * */
  37. /* eslint-disable valid-jsdoc */
  38. /**
  39. * @private
  40. * @function calculateLevelSizes
  41. *
  42. * @param {object} levelOptions
  43. * Map of level to its options.
  44. *
  45. * @param {Highcharts.Dictionary<number>} params
  46. * Object containing number parameters `innerRadius` and `outerRadius`.
  47. *
  48. * @return {Highcharts.SunburstSeriesLevelsOptions|undefined}
  49. * Returns the modified options, or undefined.
  50. */
  51. function calculateLevelSizes(levelOptions, params) {
  52. var result, p = isObject(params) ? params : {}, totalWeight = 0, diffRadius, levels, levelsNotIncluded, remainingSize, from, to;
  53. if (isObject(levelOptions)) {
  54. result = merge({}, levelOptions);
  55. from = isNumber(p.from) ? p.from : 0;
  56. to = isNumber(p.to) ? p.to : 0;
  57. levels = range(from, to);
  58. levelsNotIncluded = Object.keys(result).filter(function (k) {
  59. return levels.indexOf(+k) === -1;
  60. });
  61. diffRadius = remainingSize = isNumber(p.diffRadius) ? p.diffRadius : 0;
  62. // Convert percentage to pixels.
  63. // Calculate the remaining size to divide between "weight" levels.
  64. // Calculate total weight to use in convertion from weight to
  65. // pixels.
  66. levels.forEach(function (level) {
  67. var options = result[level], unit = options.levelSize.unit, value = options.levelSize.value;
  68. if (unit === 'weight') {
  69. totalWeight += value;
  70. }
  71. else if (unit === 'percentage') {
  72. options.levelSize = {
  73. unit: 'pixels',
  74. value: (value / 100) * diffRadius
  75. };
  76. remainingSize -= options.levelSize.value;
  77. }
  78. else if (unit === 'pixels') {
  79. remainingSize -= value;
  80. }
  81. });
  82. // Convert weight to pixels.
  83. levels.forEach(function (level) {
  84. var options = result[level], weight;
  85. if (options.levelSize.unit === 'weight') {
  86. weight = options.levelSize.value;
  87. result[level].levelSize = {
  88. unit: 'pixels',
  89. value: (weight / totalWeight) * remainingSize
  90. };
  91. }
  92. });
  93. // Set all levels not included in interval [from,to] to have 0
  94. // pixels.
  95. levelsNotIncluded.forEach(function (level) {
  96. result[level].levelSize = {
  97. value: 0,
  98. unit: 'pixels'
  99. };
  100. });
  101. }
  102. return result;
  103. }
  104. SunburstUtilities.calculateLevelSizes = calculateLevelSizes;
  105. /**
  106. * @private
  107. */
  108. function getLevelFromAndTo(_a) {
  109. var level = _a.level, height = _a.height;
  110. // Never displays level below 1
  111. var from = level > 0 ? level : 1;
  112. var to = level + height;
  113. return { from: from, to: to };
  114. }
  115. SunburstUtilities.getLevelFromAndTo = getLevelFromAndTo;
  116. /**
  117. * TODO introduce step, which should default to 1.
  118. * @private
  119. */
  120. function range(from, to) {
  121. var result = [], i;
  122. if (isNumber(from) && isNumber(to) && from <= to) {
  123. for (i = from; i <= to; i++) {
  124. result.push(i);
  125. }
  126. }
  127. return result;
  128. }
  129. SunburstUtilities.range = range;
  130. /* eslint-enable valid-jsdoc */
  131. })(SunburstUtilities || (SunburstUtilities = {}));
  132. /* *
  133. *
  134. * Default Export
  135. *
  136. * */
  137. export default SunburstUtilities;