TreeSeries.js 6.9 KB

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