You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

888 lines
24 KiB

  1. /**
  2. * @license Highmaps JS v1.1.9 (2015-10-07)
  3. *
  4. * (c) 2014 Highsoft AS
  5. * Authors: Jon Arild Nygard / Oystein Moseng
  6. *
  7. * License: www.highcharts.com/license
  8. */
  9. /*global HighchartsAdapter */
  10. (function (H) {
  11. var seriesTypes = H.seriesTypes,
  12. map = H.map,
  13. merge = H.merge,
  14. extend = H.extend,
  15. extendClass = H.extendClass,
  16. defaultOptions = H.getOptions(),
  17. plotOptions = defaultOptions.plotOptions,
  18. noop = function () { return; },
  19. each = H.each,
  20. grep = HighchartsAdapter.grep,
  21. pick = H.pick,
  22. Series = H.Series,
  23. Color = H.Color,
  24. eachObject = function (list, func, context) {
  25. var key;
  26. context = context || this;
  27. for (key in list) {
  28. if (list.hasOwnProperty(key)) {
  29. func.call(context, list[key], key, list);
  30. }
  31. }
  32. },
  33. reduce = function (arr, func, previous, context) {
  34. context = context || this;
  35. arr = arr || []; // @note should each be able to handle empty values automatically?
  36. each(arr, function (current, i) {
  37. previous = func.call(context, previous, current, i, arr);
  38. });
  39. return previous;
  40. },
  41. // @todo find correct name for this function.
  42. recursive = function (item, func, context) {
  43. var next;
  44. context = context || this;
  45. next = func.call(context, item);
  46. if (next !== false) {
  47. recursive(next, func, context);
  48. }
  49. };
  50. // Define default options
  51. plotOptions.treemap = merge(plotOptions.scatter, {
  52. showInLegend: false,
  53. marker: false,
  54. borderColor: '#E0E0E0',
  55. borderWidth: 1,
  56. dataLabels: {
  57. enabled: true,
  58. defer: false,
  59. verticalAlign: 'middle',
  60. formatter: function () { // #2945
  61. return this.point.name || this.point.id;
  62. },
  63. inside: true
  64. },
  65. tooltip: {
  66. headerFormat: '',
  67. pointFormat: '<b>{point.name}</b>: {point.node.val}</b><br/>'
  68. },
  69. layoutAlgorithm: 'sliceAndDice',
  70. layoutStartingDirection: 'vertical',
  71. alternateStartingDirection: false,
  72. levelIsConstant: true,
  73. states: {
  74. hover: {
  75. borderColor: '#A0A0A0',
  76. brightness: seriesTypes.heatmap ? 0 : 0.1,
  77. shadow: false
  78. }
  79. },
  80. drillUpButton: {
  81. position: {
  82. align: 'left',
  83. x: 10,
  84. y: -50
  85. }
  86. }
  87. });
  88. // Stolen from heatmap
  89. var colorSeriesMixin = {
  90. // mapping between SVG attributes and the corresponding options
  91. pointAttrToOptions: {},
  92. pointArrayMap: ['value'],
  93. axisTypes: seriesTypes.heatmap ? ['xAxis', 'yAxis', 'colorAxis'] : ['xAxis', 'yAxis'],
  94. optionalAxis: 'colorAxis',
  95. getSymbol: noop,
  96. parallelArrays: ['x', 'y', 'value', 'colorValue'],
  97. colorKey: 'colorValue', // Point color option key
  98. translateColors: seriesTypes.heatmap && seriesTypes.heatmap.prototype.translateColors
  99. };
  100. // The Treemap series type
  101. seriesTypes.treemap = extendClass(seriesTypes.scatter, merge(colorSeriesMixin, {
  102. type: 'treemap',
  103. trackerGroups: ['group', 'dataLabelsGroup'],
  104. pointClass: extendClass(H.Point, {
  105. setVisible: seriesTypes.pie.prototype.pointClass.prototype.setVisible
  106. }),
  107. /**
  108. * Creates an object map from parent id to childrens index.
  109. * @param {Array} data List of points set in options.
  110. * @param {string} data[].parent Parent id of point.
  111. * @param {Array} ids List of all point ids.
  112. * @return {Object} Map from parent id to children index in data.
  113. */
  114. getListOfParents: function (data, ids) {
  115. var listOfParents = reduce(data, function (prev, curr, i) {
  116. var parent = pick(curr.parent, "");
  117. if (prev[parent] === undefined) {
  118. prev[parent] = [];
  119. }
  120. prev[parent].push(i);
  121. return prev;
  122. }, {});
  123. // If parent does not exist, hoist parent to root of tree.
  124. eachObject(listOfParents, function (children, parent, list) {
  125. if ((parent !== "") && (HighchartsAdapter.inArray(parent, ids) === -1)) {
  126. each(children, function (child) {
  127. list[""].push(child);
  128. });
  129. delete list[parent];
  130. }
  131. });
  132. return listOfParents;
  133. },
  134. /**
  135. * Creates a tree structured object from the series points
  136. */
  137. getTree: function () {
  138. var tree,
  139. series = this,
  140. allIds = map(this.data, function (d) {
  141. return d.id;
  142. }),
  143. parentList = series.getListOfParents(this.data, allIds);
  144. series.nodeMap = [];
  145. tree = series.buildNode("", -1, 0, parentList, null);
  146. recursive(this.nodeMap[this.rootNode], function (node) {
  147. var next = false,
  148. p = node.parent;
  149. node.visible = true;
  150. if (p || p === "") {
  151. next = series.nodeMap[p];
  152. }
  153. return next;
  154. });
  155. recursive(this.nodeMap[this.rootNode].children, function (children) {
  156. var next = false;
  157. each(children, function (child) {
  158. child.visible = true;
  159. if (child.children.length) {
  160. next = (next || []).concat(child.children);
  161. }
  162. });
  163. return next;
  164. });
  165. this.setTreeValues(tree);
  166. return tree;
  167. },
  168. init: function (chart, options) {
  169. var series = this;
  170. Series.prototype.init.call(series, chart, options);
  171. if (series.options.allowDrillToNode) {
  172. series.drillTo();
  173. }
  174. },
  175. buildNode: function (id, i, level, list, parent) {
  176. var series = this,
  177. children = [],
  178. point = series.points[i],
  179. node,
  180. child;
  181. // Actions
  182. each((list[id] || []), function (i) {
  183. child = series.buildNode(series.points[i].id, i, (level + 1), list, id);
  184. children.push(child);
  185. });
  186. node = {
  187. id: id,
  188. i: i,
  189. children: children,
  190. level: level,
  191. parent: parent,
  192. visible: false // @todo move this to better location
  193. };
  194. series.nodeMap[node.id] = node;
  195. if (point) {
  196. point.node = node;
  197. }
  198. return node;
  199. },
  200. setTreeValues: function (tree) {
  201. var series = this,
  202. options = series.options,
  203. childrenTotal = 0,
  204. sorted = [],
  205. val,
  206. point = series.points[tree.i];
  207. // First give the children some values
  208. each(tree.children, function (child) {
  209. child = series.setTreeValues(child);
  210. series.insertElementSorted(sorted, child, function (el, el2) {
  211. return el.val > el2.val;
  212. });
  213. if (!child.ignore) {
  214. childrenTotal += child.val;
  215. } else {
  216. // @todo Add predicate to avoid looping already ignored children
  217. recursive(child.children, function (children) {
  218. var next = false;
  219. each(children, function (node) {
  220. extend(node, {
  221. ignore: true,
  222. isLeaf: false,
  223. visible: false
  224. });
  225. if (node.children.length) {
  226. next = (next || []).concat(node.children);
  227. }
  228. });
  229. return next;
  230. });
  231. }
  232. });
  233. // Set the values
  234. val = pick(point && point.value, childrenTotal);
  235. extend(tree, {
  236. children: sorted,
  237. childrenTotal: childrenTotal,
  238. // Ignore this node if point is not visible
  239. ignore: !(pick(point && point.visible, true) && (val > 0)),
  240. isLeaf: tree.visible && !childrenTotal,
  241. levelDynamic: (options.levelIsConstant ? tree.level : (tree.level - series.nodeMap[series.rootNode].level)),
  242. name: pick(point && point.name, ""),
  243. val: val
  244. });
  245. return tree;
  246. },
  247. /**
  248. * Recursive function which calculates the area for all children of a node.
  249. * @param {Object} node The node which is parent to the children.
  250. * @param {Object} area The rectangular area of the parent.
  251. */
  252. calculateChildrenAreas: function (parent, area) {
  253. var series = this,
  254. options = series.options,
  255. level = this.levelMap[parent.levelDynamic + 1],
  256. algorithm = pick((series[level && level.layoutAlgorithm] && level.layoutAlgorithm), options.layoutAlgorithm),
  257. alternate = options.alternateStartingDirection,
  258. childrenValues = [],
  259. children;
  260. // Collect all children which should be included
  261. children = grep(parent.children, function (n) {
  262. return !n.ignore;
  263. });
  264. if (level && level.layoutStartingDirection) {
  265. area.direction = level.layoutStartingDirection === 'vertical' ? 0 : 1;
  266. }
  267. childrenValues = series[algorithm](area, children);
  268. each(children, function (child, index) {
  269. var values = childrenValues[index];
  270. child.values = merge(values, {
  271. val: child.childrenTotal,
  272. direction: (alternate ? 1 - area.direction : area.direction)
  273. });
  274. child.pointValues = merge(values, {
  275. x: (values.x / series.axisRatio),
  276. width: (values.width / series.axisRatio)
  277. });
  278. // If node has children, then call method recursively
  279. if (child.children.length) {
  280. series.calculateChildrenAreas(child, child.values);
  281. }
  282. });
  283. },
  284. setPointValues: function () {
  285. var series = this,
  286. xAxis = series.xAxis,
  287. yAxis = series.yAxis;
  288. each(series.points, function (point) {
  289. var node = point.node,
  290. values = node.pointValues,
  291. x1,
  292. x2,
  293. y1,
  294. y2;
  295. // Points which is ignored, have no values.
  296. if (values) {
  297. x1 = Math.round(xAxis.translate(values.x, 0, 0, 0, 1));
  298. x2 = Math.round(xAxis.translate(values.x + values.width, 0, 0, 0, 1));
  299. y1 = Math.round(yAxis.translate(values.y, 0, 0, 0, 1));
  300. y2 = Math.round(yAxis.translate(values.y + values.height, 0, 0, 0, 1));
  301. // Set point values
  302. point.shapeType = 'rect';
  303. point.shapeArgs = {
  304. x: Math.min(x1, x2),
  305. y: Math.min(y1, y2),
  306. width: Math.abs(x2 - x1),
  307. height: Math.abs(y2 - y1)
  308. };
  309. point.plotX = point.shapeArgs.x + (point.shapeArgs.width / 2);
  310. point.plotY = point.shapeArgs.y + (point.shapeArgs.height / 2);
  311. } else {
  312. // Reset visibility
  313. delete point.plotX;
  314. delete point.plotY;
  315. }
  316. });
  317. },
  318. setColorRecursive: function (node, color) {
  319. var series = this,
  320. point,
  321. level;
  322. if (node) {
  323. point = series.points[node.i];
  324. level = series.levelMap[node.levelDynamic];
  325. // Select either point color, level color or inherited color.
  326. color = pick(point && point.options.color, level && level.color, color);
  327. if (point) {
  328. point.color = color;
  329. }
  330. // Do it all again with the children
  331. if (node.children.length) {
  332. each(node.children, function (child) {
  333. series.setColorRecursive(child, color);
  334. });
  335. }
  336. }
  337. },
  338. alg_func_group: function (h, w, d, p) {
  339. this.height = h;
  340. this.width = w;
  341. this.plot = p;
  342. this.direction = d;
  343. this.startDirection = d;
  344. this.total = 0;
  345. this.nW = 0;
  346. this.lW = 0;
  347. this.nH = 0;
  348. this.lH = 0;
  349. this.elArr = [];
  350. this.lP = {
  351. total: 0,
  352. lH: 0,
  353. nH: 0,
  354. lW: 0,
  355. nW: 0,
  356. nR: 0,
  357. lR: 0,
  358. aspectRatio: function (w, h) {
  359. return Math.max((w / h), (h / w));
  360. }
  361. };
  362. this.addElement = function (el) {
  363. this.lP.total = this.elArr[this.elArr.length - 1];
  364. this.total = this.total + el;
  365. if (this.direction === 0) {
  366. // Calculate last point old aspect ratio
  367. this.lW = this.nW;
  368. this.lP.lH = this.lP.total / this.lW;
  369. this.lP.lR = this.lP.aspectRatio(this.lW, this.lP.lH);
  370. // Calculate last point new aspect ratio
  371. this.nW = this.total / this.height;
  372. this.lP.nH = this.lP.total / this.nW;
  373. this.lP.nR = this.lP.aspectRatio(this.nW, this.lP.nH);
  374. } else {
  375. // Calculate last point old aspect ratio
  376. this.lH = this.nH;
  377. this.lP.lW = this.lP.total / this.lH;
  378. this.lP.lR = this.lP.aspectRatio(this.lP.lW, this.lH);
  379. // Calculate last point new aspect ratio
  380. this.nH = this.total / this.width;
  381. this.lP.nW = this.lP.total / this.nH;
  382. this.lP.nR = this.lP.aspectRatio(this.lP.nW, this.nH);
  383. }
  384. this.elArr.push(el);
  385. };
  386. this.reset = function () {
  387. this.nW = 0;
  388. this.lW = 0;
  389. this.elArr = [];
  390. this.total = 0;
  391. };
  392. },
  393. alg_func_calcPoints: function (directionChange, last, group, childrenArea) {
  394. var pX,
  395. pY,
  396. pW,
  397. pH,
  398. gW = group.lW,
  399. gH = group.lH,
  400. plot = group.plot,
  401. keep,
  402. i = 0,
  403. end = group.elArr.length - 1;
  404. if (last) {
  405. gW = group.nW;
  406. gH = group.nH;
  407. } else {
  408. keep = group.elArr[group.elArr.length - 1];
  409. }
  410. each(group.elArr, function (p) {
  411. if (last || (i < end)) {
  412. if (group.direction === 0) {
  413. pX = plot.x;
  414. pY = plot.y;
  415. pW = gW;
  416. pH = p / pW;
  417. } else {
  418. pX = plot.x;
  419. pY = plot.y;
  420. pH = gH;
  421. pW = p / pH;
  422. }
  423. childrenArea.push({
  424. x: pX,
  425. y: pY,
  426. width: pW,
  427. height: pH
  428. });
  429. if (group.direction === 0) {
  430. plot.y = plot.y + pH;
  431. } else {
  432. plot.x = plot.x + pW;
  433. }
  434. }
  435. i = i + 1;
  436. });
  437. // Reset variables
  438. group.reset();
  439. if (group.direction === 0) {
  440. group.width = group.width - gW;
  441. } else {
  442. group.height = group.height - gH;
  443. }
  444. plot.y = plot.parent.y + (plot.parent.height - group.height);
  445. plot.x = plot.parent.x + (plot.parent.width - group.width);
  446. if (directionChange) {
  447. group.direction = 1 - group.direction;
  448. }
  449. // If not last, then add uncalculated element
  450. if (!last) {
  451. group.addElement(keep);
  452. }
  453. },
  454. alg_func_lowAspectRatio: function (directionChange, parent, children) {
  455. var childrenArea = [],
  456. series = this,
  457. pTot,
  458. plot = {
  459. x: parent.x,
  460. y: parent.y,
  461. parent: parent
  462. },
  463. direction = parent.direction,
  464. i = 0,
  465. end = children.length - 1,
  466. group = new this.alg_func_group(parent.height, parent.width, direction, plot);
  467. // Loop through and calculate all areas
  468. each(children, function (child) {
  469. pTot = (parent.width * parent.height) * (child.val / parent.val);
  470. group.addElement(pTot);
  471. if (group.lP.nR > group.lP.lR) {
  472. series.alg_func_calcPoints(directionChange, false, group, childrenArea, plot);
  473. }
  474. // If last child, then calculate all remaining areas
  475. if (i === end) {
  476. series.alg_func_calcPoints(directionChange, true, group, childrenArea, plot);
  477. }
  478. i = i + 1;
  479. });
  480. return childrenArea;
  481. },
  482. alg_func_fill: function (directionChange, parent, children) {
  483. var childrenArea = [],
  484. pTot,
  485. direction = parent.direction,
  486. x = parent.x,
  487. y = parent.y,
  488. width = parent.width,
  489. height = parent.height,
  490. pX,
  491. pY,
  492. pW,
  493. pH;
  494. each(children, function (child) {
  495. pTot = (parent.width * parent.height) * (child.val / parent.val);
  496. pX = x;
  497. pY = y;
  498. if (direction === 0) {
  499. pH = height;
  500. pW = pTot / pH;
  501. width = width - pW;
  502. x = x + pW;
  503. } else {
  504. pW = width;
  505. pH = pTot / pW;
  506. height = height - pH;
  507. y = y + pH;
  508. }
  509. childrenArea.push({
  510. x: pX,
  511. y: pY,
  512. width: pW,
  513. height: pH
  514. });
  515. if (directionChange) {
  516. direction = 1 - direction;
  517. }
  518. });
  519. return childrenArea;
  520. },
  521. strip: function (parent, children) {
  522. return this.alg_func_lowAspectRatio(false, parent, children);
  523. },
  524. squarified: function (parent, children) {
  525. return this.alg_func_lowAspectRatio(true, parent, children);
  526. },
  527. sliceAndDice: function (parent, children) {
  528. return this.alg_func_fill(true, parent, children);
  529. },
  530. stripes: function (parent, children) {
  531. return this.alg_func_fill(false, parent, children);
  532. },
  533. translate: function () {
  534. var pointValues,
  535. seriesArea,
  536. tree,
  537. val;
  538. // Call prototype function
  539. Series.prototype.translate.call(this);
  540. if (this.points.length) {
  541. // Assign variables
  542. this.rootNode = pick(this.options.rootId, "");
  543. // Create a object map from level to options
  544. this.levelMap = reduce(this.options.levels, function (arr, item) {
  545. arr[item.level] = item;
  546. return arr;
  547. }, {});
  548. tree = this.tree = this.getTree(); // @todo Only if series.isDirtyData is true
  549. // Calculate plotting values.
  550. this.axisRatio = (this.xAxis.len / this.yAxis.len);
  551. this.nodeMap[""].pointValues = pointValues = {x: 0, y: 0, width: 100, height: 100 };
  552. this.nodeMap[""].values = seriesArea = merge(pointValues, {
  553. width: (pointValues.width * this.axisRatio),
  554. direction: (this.options.layoutStartingDirection === 'vertical' ? 0 : 1),
  555. val: tree.val
  556. });
  557. this.calculateChildrenAreas(tree, seriesArea);
  558. }
  559. // Logic for point colors
  560. if (this.colorAxis) {
  561. this.translateColors();
  562. } else if (!this.options.colorByPoint) {
  563. this.setColorRecursive(this.tree, undefined);
  564. }
  565. // Update axis extremes according to the root node.
  566. val = this.nodeMap[this.rootNode].pointValues;
  567. this.xAxis.setExtremes(val.x, val.x + val.width, false);
  568. this.yAxis.setExtremes(val.y, val.y + val.height, false);
  569. this.xAxis.setScale();
  570. this.yAxis.setScale();
  571. // Assign values to points.
  572. this.setPointValues();
  573. },
  574. /**
  575. * Extend drawDataLabels with logic to handle custom options related to the treemap series:
  576. * - Points which is not a leaf node, has dataLabels disabled by default.
  577. * - Options set on series.levels is merged in.
  578. * - Width of the dataLabel is set to match the width of the point shape.
  579. */
  580. drawDataLabels: function () {
  581. var series = this,
  582. points = grep(series.points, function (n) {
  583. return n.node.visible;
  584. }),
  585. options,
  586. level;
  587. each(points, function (point) {
  588. level = series.levelMap[point.node.levelDynamic];
  589. // Set options to new object to avoid problems with scope
  590. options = {style: {}};
  591. // If not a leaf, then label should be disabled as default
  592. if (!point.node.isLeaf) {
  593. options.enabled = false;
  594. }
  595. // If options for level exists, include them as well
  596. if (level && level.dataLabels) {
  597. options = merge(options, level.dataLabels);
  598. series._hasPointLabels = true;
  599. }
  600. // Set dataLabel width to the width of the point shape.
  601. if (point.shapeArgs) {
  602. options.style.width = point.shapeArgs.width;
  603. }
  604. // Merge custom options with point options
  605. point.dlOptions = merge(options, point.options.dataLabels);
  606. });
  607. Series.prototype.drawDataLabels.call(this);
  608. },
  609. alignDataLabel: seriesTypes.column.prototype.alignDataLabel,
  610. /**
  611. * Get presentational attributes
  612. */
  613. pointAttribs: function (point, state) {
  614. var level = this.levelMap[point.node.levelDynamic] || {},
  615. options = this.options,
  616. attr,
  617. stateOptions = (state && options.states[state]) || {};
  618. // Set attributes by precedence. Point trumps level trumps series. Stroke width uses pick
  619. // because it can be 0.
  620. attr = {
  621. 'stroke': point.borderColor || level.borderColor || stateOptions.borderColor || options.borderColor,
  622. 'stroke-width': pick(point.borderWidth, level.borderWidth, stateOptions.borderWidth, options.borderWidth),
  623. 'dashstyle': point.borderDashStyle || level.borderDashStyle || stateOptions.borderDashStyle || options.borderDashStyle,
  624. 'fill': point.color || this.color
  625. };
  626. if (state === 'hover') {
  627. attr.zIndex = 1;
  628. }
  629. if (point.node.level <= this.nodeMap[this.rootNode].level) {
  630. // Hide levels above the current view
  631. attr.fill = 'none';
  632. attr["stroke-width"] = 0;
  633. } else if (!point.node.isLeaf) {
  634. // If not a leaf, then remove fill
  635. // @todo let users set the opacity
  636. attr.fill = pick(options.interactByLeaf, !options.allowDrillToNode) ? 'none' : Color(attr.fill).setOpacity(state === 'hover' ? 0.75 : 0.15).get();
  637. } else if (state) {
  638. // Brighten and hoist the hover nodes
  639. attr.fill = Color(attr.fill).brighten(stateOptions.brightness).get();
  640. }
  641. return attr;
  642. },
  643. /**
  644. * Extending ColumnSeries drawPoints
  645. */
  646. drawPoints: function () {
  647. var series = this,
  648. points = grep(series.points, function (n) {
  649. return n.node.visible;
  650. });
  651. each(points, function (point) {
  652. var groupKey = "levelGroup-" + point.node.levelDynamic;
  653. if (!series[groupKey]) {
  654. series[groupKey] = series.chart.renderer.g(groupKey)
  655. .attr({
  656. zIndex: 1000 - point.node.levelDynamic // @todo Set the zIndex based upon the number of levels, instead of using 1000
  657. })
  658. .add(series.group);
  659. }
  660. point.group = series[groupKey];
  661. // Preliminary code in prepraration for HC5 that uses pointAttribs for all series
  662. point.pointAttr = {
  663. '': series.pointAttribs(point),
  664. 'hover': series.pointAttribs(point, 'hover'),
  665. 'select': {}
  666. };
  667. });
  668. // Call standard drawPoints
  669. seriesTypes.column.prototype.drawPoints.call(this);
  670. // If drillToNode is allowed, set a point cursor on clickables & add drillId to point
  671. if (series.options.allowDrillToNode) {
  672. each(points, function (point) {
  673. var cursor,
  674. drillId;
  675. if (point.graphic) {
  676. drillId = point.drillId = series.options.interactByLeaf ? series.drillToByLeaf(point) : series.drillToByGroup(point);
  677. cursor = drillId ? "pointer" : "default";
  678. point.graphic.css({ cursor: cursor });
  679. }
  680. });
  681. }
  682. },
  683. /**
  684. * Inserts an element into an array, sorted by a condition.
  685. * Modifies the referenced array
  686. * @param {*[]} arr The array which the element is inserted into.
  687. * @param {*} el The element to insert.
  688. * @param {function} cond The condition to sort on. First parameter is el, second parameter is array element
  689. */
  690. insertElementSorted: function (arr, el, cond) {
  691. var i = 0,
  692. inserted = false;
  693. if (arr.length !== 0) {
  694. each(arr, function (arrayElement) {
  695. if (cond(el, arrayElement) && !inserted) {
  696. arr.splice(i, 0, el);
  697. inserted = true;
  698. }
  699. i = i + 1;
  700. });
  701. }
  702. if (!inserted) {
  703. arr.push(el);
  704. }
  705. },
  706. /**
  707. * Add drilling on the suitable points
  708. */
  709. drillTo: function () {
  710. var series = this;
  711. H.addEvent(series, 'click', function (event) {
  712. var point = event.point,
  713. drillId = point.drillId,
  714. drillName;
  715. // If a drill id is returned, add click event and cursor.
  716. if (drillId) {
  717. drillName = series.nodeMap[series.rootNode].name || series.rootNode;
  718. point.setState(''); // Remove hover
  719. series.drillToNode(drillId);
  720. series.showDrillUpButton(drillName);
  721. }
  722. });
  723. },
  724. /**
  725. * Finds the drill id for a parent node.
  726. * Returns false if point should not have a click event
  727. * @param {Object} point
  728. * @return {string || boolean} Drill to id or false when point should not have a click event
  729. */
  730. drillToByGroup: function (point) {
  731. var series = this,
  732. drillId = false;
  733. if ((point.node.level - series.nodeMap[series.rootNode].level) === 1 && !point.node.isLeaf) {
  734. drillId = point.id;
  735. }
  736. return drillId;
  737. },
  738. /**
  739. * Finds the drill id for a leaf node.
  740. * Returns false if point should not have a click event
  741. * @param {Object} point
  742. * @return {string || boolean} Drill to id or false when point should not have a click event
  743. */
  744. drillToByLeaf: function (point) {
  745. var series = this,
  746. drillId = false,
  747. nodeParent;
  748. if ((point.node.parent !== series.rootNode) && (point.node.isLeaf)) {
  749. nodeParent = point.node;
  750. while (!drillId) {
  751. nodeParent = series.nodeMap[nodeParent.parent];
  752. if (nodeParent.parent === series.rootNode) {
  753. drillId = nodeParent.id;
  754. }
  755. }
  756. }
  757. return drillId;
  758. },
  759. drillUp: function () {
  760. var drillPoint = null,
  761. node,
  762. parent;
  763. if (this.rootNode) {
  764. node = this.nodeMap[this.rootNode];
  765. if (node.parent !== null) {
  766. drillPoint = this.nodeMap[node.parent];
  767. } else {
  768. drillPoint = this.nodeMap[""];
  769. }
  770. }
  771. if (drillPoint !== null) {
  772. this.drillToNode(drillPoint.id);
  773. if (drillPoint.id === "") {
  774. this.drillUpButton = this.drillUpButton.destroy();
  775. } else {
  776. parent = this.nodeMap[drillPoint.parent];
  777. this.showDrillUpButton((parent.name || parent.id));
  778. }
  779. }
  780. },
  781. drillToNode: function (id) {
  782. this.options.rootId = id;
  783. this.isDirty = true; // Force redraw
  784. this.chart.redraw();
  785. },
  786. showDrillUpButton: function (name) {
  787. var series = this,
  788. backText = (name || '< Back'),
  789. buttonOptions = series.options.drillUpButton,
  790. attr,
  791. states;
  792. if (buttonOptions.text) {
  793. backText = buttonOptions.text;
  794. }
  795. if (!this.drillUpButton) {
  796. attr = buttonOptions.theme;
  797. states = attr && attr.states;
  798. this.drillUpButton = this.chart.renderer.button(
  799. backText,
  800. null,
  801. null,
  802. function () {
  803. series.drillUp();
  804. },
  805. attr,
  806. states && states.hover,
  807. states && states.select
  808. )
  809. .attr({
  810. align: buttonOptions.position.align,
  811. zIndex: 9
  812. })
  813. .add()
  814. .align(buttonOptions.position, false, buttonOptions.relativeTo || 'plotBox');
  815. } else {
  816. this.drillUpButton.attr({
  817. text: backText
  818. })
  819. .align();
  820. }
  821. },
  822. buildKDTree: noop,
  823. drawLegendSymbol: H.LegendSymbolMixin.drawRectangle,
  824. getExtremes: function () {
  825. // Get the extremes from the value data
  826. Series.prototype.getExtremes.call(this, this.colorValueData);
  827. this.valueMin = this.dataMin;
  828. this.valueMax = this.dataMax;
  829. // Get the extremes from the y data
  830. Series.prototype.getExtremes.call(this);
  831. },
  832. getExtremesFromAll: true,
  833. bindAxes: function () {
  834. var treeAxis = {
  835. endOnTick: false,
  836. gridLineWidth: 0,
  837. lineWidth: 0,
  838. min: 0,
  839. dataMin: 0,
  840. minPadding: 0,
  841. max: 100,
  842. dataMax: 100,
  843. maxPadding: 0,
  844. startOnTick: false,
  845. title: null,
  846. tickPositions: []
  847. };
  848. Series.prototype.bindAxes.call(this);
  849. H.extend(this.yAxis.options, treeAxis);
  850. H.extend(this.xAxis.options, treeAxis);
  851. }
  852. }));
  853. }(Highcharts));