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.

896 lines
24 KiB

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