treemap.src.js 35 KB

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