TreeSeries.js 6.9 KB

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