treemap.src.js 22 KB

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