TreeSeries.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* *
  2. *
  3. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  4. *
  5. * */
  6. 'use strict';
  7. import Color from '../Core/Color/Color.js';
  8. import U from '../Core/Utilities.js';
  9. var extend = U.extend, isArray = U.isArray, isNumber = U.isNumber, isObject = U.isObject, merge = U.merge, pick = U.pick;
  10. var isBoolean = function (x) {
  11. return typeof x === 'boolean';
  12. }, isFn = function (x) {
  13. return typeof x === 'function';
  14. };
  15. /* eslint-disable valid-jsdoc */
  16. /**
  17. * @todo Combine buildTree and buildNode with setTreeValues
  18. * @todo Remove logic from Treemap and make it utilize this mixin.
  19. * @private
  20. */
  21. var setTreeValues = function setTreeValues(tree, options) {
  22. var before = options.before, idRoot = options.idRoot, mapIdToNode = options.mapIdToNode, nodeRoot = mapIdToNode[idRoot], levelIsConstant = (isBoolean(options.levelIsConstant) ?
  23. options.levelIsConstant :
  24. true), points = options.points, point = points[tree.i], optionsPoint = point && point.options || {}, childrenTotal = 0, children = [], value;
  25. tree.levelDynamic = tree.level - (levelIsConstant ? 0 : nodeRoot.level);
  26. tree.name = pick(point && point.name, '');
  27. tree.visible = (idRoot === tree.id ||
  28. (isBoolean(options.visible) ? options.visible : false));
  29. if (isFn(before)) {
  30. tree = before(tree, options);
  31. }
  32. // First give the children some values
  33. tree.children.forEach(function (child, i) {
  34. var newOptions = extend({}, options);
  35. extend(newOptions, {
  36. index: i,
  37. siblings: tree.children.length,
  38. visible: tree.visible
  39. });
  40. child = setTreeValues(child, newOptions);
  41. children.push(child);
  42. if (child.visible) {
  43. childrenTotal += child.val;
  44. }
  45. });
  46. tree.visible = childrenTotal > 0 || tree.visible;
  47. // Set the values
  48. value = pick(optionsPoint.value, childrenTotal);
  49. tree.children = children;
  50. tree.childrenTotal = childrenTotal;
  51. tree.isLeaf = tree.visible && !childrenTotal;
  52. tree.val = value;
  53. return tree;
  54. };
  55. /**
  56. * @private
  57. */
  58. var getColor = function getColor(node, options) {
  59. var index = options.index, mapOptionsToLevel = options.mapOptionsToLevel, parentColor = options.parentColor, parentColorIndex = options.parentColorIndex, series = options.series, colors = options.colors, siblings = options.siblings, points = series.points, getColorByPoint, chartOptionsChart = series.chart.options.chart, point, level, colorByPoint, colorIndexByPoint, color, colorIndex;
  60. /**
  61. * @private
  62. */
  63. function variation(color) {
  64. var colorVariation = level && level.colorVariation;
  65. if (colorVariation) {
  66. if (colorVariation.key === 'brightness') {
  67. return Color.parse(color).brighten(colorVariation.to * (index / siblings)).get();
  68. }
  69. }
  70. return color;
  71. }
  72. if (node) {
  73. point = points[node.i];
  74. level = mapOptionsToLevel[node.level] || {};
  75. getColorByPoint = point && level.colorByPoint;
  76. if (getColorByPoint) {
  77. colorIndexByPoint = point.index % (colors ?
  78. colors.length :
  79. chartOptionsChart.colorCount);
  80. colorByPoint = colors && colors[colorIndexByPoint];
  81. }
  82. // Select either point color, level color or inherited color.
  83. if (!series.chart.styledMode) {
  84. color = pick(point && point.options.color, level && level.color, colorByPoint, parentColor && variation(parentColor), series.color);
  85. }
  86. colorIndex = pick(point && point.options.colorIndex, level && level.colorIndex, colorIndexByPoint, parentColorIndex, options.colorIndex);
  87. }
  88. return {
  89. color: color,
  90. colorIndex: colorIndex
  91. };
  92. };
  93. /**
  94. * Creates a map from level number to its given options.
  95. *
  96. * @private
  97. * @function getLevelOptions
  98. * @param {object} params
  99. * Object containing parameters.
  100. * - `defaults` Object containing default options. The default options
  101. * are merged with the userOptions to get the final options for a
  102. * specific level.
  103. * - `from` The lowest level number.
  104. * - `levels` User options from series.levels.
  105. * - `to` The highest level number.
  106. * @return {Highcharts.Dictionary<object>|null}
  107. * Returns a map from level number to its given options.
  108. */
  109. var getLevelOptions = function getLevelOptions(params) {
  110. var result = null, defaults, converted, i, from, to, levels;
  111. if (isObject(params)) {
  112. result = {};
  113. from = isNumber(params.from) ? params.from : 1;
  114. levels = params.levels;
  115. converted = {};
  116. defaults = isObject(params.defaults) ? params.defaults : {};
  117. if (isArray(levels)) {
  118. converted = levels.reduce(function (obj, item) {
  119. var level, levelIsConstant, options;
  120. if (isObject(item) && isNumber(item.level)) {
  121. options = merge({}, item);
  122. levelIsConstant = (isBoolean(options.levelIsConstant) ?
  123. options.levelIsConstant :
  124. defaults.levelIsConstant);
  125. // Delete redundant properties.
  126. delete options.levelIsConstant;
  127. delete options.level;
  128. // Calculate which level these options apply to.
  129. level = item.level + (levelIsConstant ? 0 : from - 1);
  130. if (isObject(obj[level])) {
  131. extend(obj[level], options);
  132. }
  133. else {
  134. obj[level] = options;
  135. }
  136. }
  137. return obj;
  138. }, {});
  139. }
  140. to = isNumber(params.to) ? params.to : 1;
  141. for (i = 0; i <= to; i++) {
  142. result[i] = merge({}, defaults, isObject(converted[i]) ? converted[i] : {});
  143. }
  144. }
  145. return result;
  146. };
  147. /**
  148. * Update the rootId property on the series. Also makes sure that it is
  149. * accessible to exporting.
  150. *
  151. * @private
  152. * @function updateRootId
  153. *
  154. * @param {object} series
  155. * The series to operate on.
  156. *
  157. * @return {string}
  158. * Returns the resulting rootId after update.
  159. */
  160. var updateRootId = function (series) {
  161. var rootId, options;
  162. if (isObject(series)) {
  163. // Get the series options.
  164. options = isObject(series.options) ? series.options : {};
  165. // Calculate the rootId.
  166. rootId = pick(series.rootNode, options.rootId, '');
  167. // Set rootId on series.userOptions to pick it up in exporting.
  168. if (isObject(series.userOptions)) {
  169. series.userOptions.rootId = rootId;
  170. }
  171. // Set rootId on series to pick it up on next update.
  172. series.rootNode = rootId;
  173. }
  174. return rootId;
  175. };
  176. var result = {
  177. getColor: getColor,
  178. getLevelOptions: getLevelOptions,
  179. setTreeValues: setTreeValues,
  180. updateRootId: updateRootId
  181. };
  182. export default result;