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.

713 lines
18 KiB

9 years ago
  1. /**
  2. * Highcharts Drilldown plugin
  3. *
  4. * Author: Torstein Honsi
  5. * License: MIT License
  6. *
  7. * Demo: http://jsfiddle.net/highcharts/Vf3yT/
  8. */
  9. /*global Highcharts,HighchartsAdapter*/
  10. (function (H) {
  11. "use strict";
  12. var noop = function () {},
  13. defaultOptions = H.getOptions(),
  14. each = H.each,
  15. extend = H.extend,
  16. format = H.format,
  17. pick = H.pick,
  18. wrap = H.wrap,
  19. Chart = H.Chart,
  20. seriesTypes = H.seriesTypes,
  21. PieSeries = seriesTypes.pie,
  22. ColumnSeries = seriesTypes.column,
  23. Tick = H.Tick,
  24. fireEvent = HighchartsAdapter.fireEvent,
  25. inArray = HighchartsAdapter.inArray,
  26. ddSeriesId = 1;
  27. // Utilities
  28. /*
  29. * Return an intermediate color between two colors, according to pos where 0
  30. * is the from color and 1 is the to color. This method is copied from ColorAxis.js
  31. * and should always be kept updated, until we get AMD support.
  32. */
  33. function tweenColors(from, to, pos) {
  34. // Check for has alpha, because rgba colors perform worse due to lack of
  35. // support in WebKit.
  36. var hasAlpha,
  37. ret;
  38. // Unsupported color, return to-color (#3920)
  39. if (!to.rgba.length || !from.rgba.length) {
  40. ret = to.raw || 'none';
  41. // Interpolate
  42. } else {
  43. from = from.rgba;
  44. to = to.rgba;
  45. hasAlpha = (to[3] !== 1 || from[3] !== 1);
  46. ret = (hasAlpha ? 'rgba(' : 'rgb(') +
  47. Math.round(to[0] + (from[0] - to[0]) * (1 - pos)) + ',' +
  48. Math.round(to[1] + (from[1] - to[1]) * (1 - pos)) + ',' +
  49. Math.round(to[2] + (from[2] - to[2]) * (1 - pos)) +
  50. (hasAlpha ? (',' + (to[3] + (from[3] - to[3]) * (1 - pos))) : '') + ')';
  51. }
  52. return ret;
  53. }
  54. /**
  55. * Handle animation of the color attributes directly
  56. */
  57. each(['fill', 'stroke'], function (prop) {
  58. HighchartsAdapter.addAnimSetter(prop, function (fx) {
  59. fx.elem.attr(prop, tweenColors(H.Color(fx.start), H.Color(fx.end), fx.pos));
  60. });
  61. });
  62. // Add language
  63. extend(defaultOptions.lang, {
  64. drillUpText: '◁ Back to {series.name}'
  65. });
  66. defaultOptions.drilldown = {
  67. activeAxisLabelStyle: {
  68. cursor: 'pointer',
  69. color: '#0d233a',
  70. fontWeight: 'bold',
  71. textDecoration: 'underline'
  72. },
  73. activeDataLabelStyle: {
  74. cursor: 'pointer',
  75. color: '#0d233a',
  76. fontWeight: 'bold',
  77. textDecoration: 'underline'
  78. },
  79. animation: {
  80. duration: 500
  81. },
  82. drillUpButton: {
  83. position: {
  84. align: 'right',
  85. x: -10,
  86. y: 10
  87. }
  88. // relativeTo: 'plotBox'
  89. // theme
  90. }
  91. };
  92. /**
  93. * A general fadeIn method
  94. */
  95. H.SVGRenderer.prototype.Element.prototype.fadeIn = function (animation) {
  96. this
  97. .attr({
  98. opacity: 0.1,
  99. visibility: 'inherit'
  100. })
  101. .animate({
  102. opacity: pick(this.newOpacity, 1) // newOpacity used in maps
  103. }, animation || {
  104. duration: 250
  105. });
  106. };
  107. Chart.prototype.addSeriesAsDrilldown = function (point, ddOptions) {
  108. this.addSingleSeriesAsDrilldown(point, ddOptions);
  109. this.applyDrilldown();
  110. };
  111. Chart.prototype.addSingleSeriesAsDrilldown = function (point, ddOptions) {
  112. var oldSeries = point.series,
  113. xAxis = oldSeries.xAxis,
  114. yAxis = oldSeries.yAxis,
  115. newSeries,
  116. color = point.color || oldSeries.color,
  117. pointIndex,
  118. levelSeries = [],
  119. levelSeriesOptions = [],
  120. level,
  121. levelNumber,
  122. last;
  123. if (!this.drilldownLevels) {
  124. this.drilldownLevels = [];
  125. }
  126. levelNumber = oldSeries.options._levelNumber || 0;
  127. // See if we can reuse the registered series from last run
  128. last = this.drilldownLevels[this.drilldownLevels.length - 1];
  129. if (last && last.levelNumber !== levelNumber) {
  130. last = undefined;
  131. }
  132. ddOptions = extend({
  133. color: color,
  134. _ddSeriesId: ddSeriesId++
  135. }, ddOptions);
  136. pointIndex = inArray(point, oldSeries.points);
  137. // Record options for all current series
  138. each(oldSeries.chart.series, function (series) {
  139. if (series.xAxis === xAxis && !series.isDrilling) {
  140. series.options._ddSeriesId = series.options._ddSeriesId || ddSeriesId++;
  141. series.options._colorIndex = series.userOptions._colorIndex;
  142. series.options._levelNumber = series.options._levelNumber || levelNumber; // #3182
  143. if (last) {
  144. levelSeries = last.levelSeries;
  145. levelSeriesOptions = last.levelSeriesOptions;
  146. } else {
  147. levelSeries.push(series);
  148. levelSeriesOptions.push(series.options);
  149. }
  150. }
  151. });
  152. // Add a record of properties for each drilldown level
  153. level = {
  154. levelNumber: levelNumber,
  155. seriesOptions: oldSeries.options,
  156. levelSeriesOptions: levelSeriesOptions,
  157. levelSeries: levelSeries,
  158. shapeArgs: point.shapeArgs,
  159. bBox: point.graphic ? point.graphic.getBBox() : {}, // no graphic in line series with markers disabled
  160. color: color,
  161. lowerSeriesOptions: ddOptions,
  162. pointOptions: oldSeries.options.data[pointIndex],
  163. pointIndex: pointIndex,
  164. oldExtremes: {
  165. xMin: xAxis && xAxis.userMin,
  166. xMax: xAxis && xAxis.userMax,
  167. yMin: yAxis && yAxis.userMin,
  168. yMax: yAxis && yAxis.userMax
  169. }
  170. };
  171. // Push it to the lookup array
  172. this.drilldownLevels.push(level);
  173. newSeries = level.lowerSeries = this.addSeries(ddOptions, false);
  174. newSeries.options._levelNumber = levelNumber + 1;
  175. if (xAxis) {
  176. xAxis.oldPos = xAxis.pos;
  177. xAxis.userMin = xAxis.userMax = null;
  178. yAxis.userMin = yAxis.userMax = null;
  179. }
  180. // Run fancy cross-animation on supported and equal types
  181. if (oldSeries.type === newSeries.type) {
  182. newSeries.animate = newSeries.animateDrilldown || noop;
  183. newSeries.options.animation = true;
  184. }
  185. };
  186. Chart.prototype.applyDrilldown = function () {
  187. var drilldownLevels = this.drilldownLevels,
  188. levelToRemove;
  189. if (drilldownLevels && drilldownLevels.length > 0) { // #3352, async loading
  190. levelToRemove = drilldownLevels[drilldownLevels.length - 1].levelNumber;
  191. each(this.drilldownLevels, function (level) {
  192. if (level.levelNumber === levelToRemove) {
  193. each(level.levelSeries, function (series) {
  194. if (series.options && series.options._levelNumber === levelToRemove) { // Not removed, not added as part of a multi-series drilldown
  195. series.remove(false);
  196. }
  197. });
  198. }
  199. });
  200. }
  201. this.redraw();
  202. this.showDrillUpButton();
  203. };
  204. Chart.prototype.getDrilldownBackText = function () {
  205. var drilldownLevels = this.drilldownLevels,
  206. lastLevel;
  207. if (drilldownLevels && drilldownLevels.length > 0) { // #3352, async loading
  208. lastLevel = drilldownLevels[drilldownLevels.length - 1];
  209. lastLevel.series = lastLevel.seriesOptions;
  210. return format(this.options.lang.drillUpText, lastLevel);
  211. }
  212. };
  213. Chart.prototype.showDrillUpButton = function () {
  214. var chart = this,
  215. backText = this.getDrilldownBackText(),
  216. buttonOptions = chart.options.drilldown.drillUpButton,
  217. attr,
  218. states;
  219. if (!this.drillUpButton) {
  220. attr = buttonOptions.theme;
  221. states = attr && attr.states;
  222. this.drillUpButton = this.renderer.button(
  223. backText,
  224. null,
  225. null,
  226. function () {
  227. chart.drillUp();
  228. },
  229. attr,
  230. states && states.hover,
  231. states && states.select
  232. )
  233. .attr({
  234. align: buttonOptions.position.align,
  235. zIndex: 9
  236. })
  237. .add()
  238. .align(buttonOptions.position, false, buttonOptions.relativeTo || 'plotBox');
  239. } else {
  240. this.drillUpButton.attr({
  241. text: backText
  242. })
  243. .align();
  244. }
  245. };
  246. Chart.prototype.drillUp = function () {
  247. var chart = this,
  248. drilldownLevels = chart.drilldownLevels,
  249. levelNumber = drilldownLevels[drilldownLevels.length - 1].levelNumber,
  250. i = drilldownLevels.length,
  251. chartSeries = chart.series,
  252. seriesI,
  253. level,
  254. oldSeries,
  255. newSeries,
  256. oldExtremes,
  257. addSeries = function (seriesOptions) {
  258. var addedSeries;
  259. each(chartSeries, function (series) {
  260. if (series.options._ddSeriesId === seriesOptions._ddSeriesId) {
  261. addedSeries = series;
  262. }
  263. });
  264. addedSeries = addedSeries || chart.addSeries(seriesOptions, false);
  265. if (addedSeries.type === oldSeries.type && addedSeries.animateDrillupTo) {
  266. addedSeries.animate = addedSeries.animateDrillupTo;
  267. }
  268. if (seriesOptions === level.seriesOptions) {
  269. newSeries = addedSeries;
  270. }
  271. };
  272. while (i--) {
  273. level = drilldownLevels[i];
  274. if (level.levelNumber === levelNumber) {
  275. drilldownLevels.pop();
  276. // Get the lower series by reference or id
  277. oldSeries = level.lowerSeries;
  278. if (!oldSeries.chart) { // #2786
  279. seriesI = chartSeries.length; // #2919
  280. while (seriesI--) {
  281. if (chartSeries[seriesI].options.id === level.lowerSeriesOptions.id &&
  282. chartSeries[seriesI].options._levelNumber === levelNumber + 1) { // #3867
  283. oldSeries = chartSeries[seriesI];
  284. break;
  285. }
  286. }
  287. }
  288. oldSeries.xData = []; // Overcome problems with minRange (#2898)
  289. each(level.levelSeriesOptions, addSeries);
  290. fireEvent(chart, 'drillup', { seriesOptions: level.seriesOptions });
  291. if (newSeries.type === oldSeries.type) {
  292. newSeries.drilldownLevel = level;
  293. newSeries.options.animation = chart.options.drilldown.animation;
  294. if (oldSeries.animateDrillupFrom && oldSeries.chart) { // #2919
  295. oldSeries.animateDrillupFrom(level);
  296. }
  297. }
  298. newSeries.options._levelNumber = levelNumber;
  299. oldSeries.remove(false);
  300. // Reset the zoom level of the upper series
  301. if (newSeries.xAxis) {
  302. oldExtremes = level.oldExtremes;
  303. newSeries.xAxis.setExtremes(oldExtremes.xMin, oldExtremes.xMax, false);
  304. newSeries.yAxis.setExtremes(oldExtremes.yMin, oldExtremes.yMax, false);
  305. }
  306. }
  307. }
  308. this.redraw();
  309. if (this.drilldownLevels.length === 0) {
  310. this.drillUpButton = this.drillUpButton.destroy();
  311. } else {
  312. this.drillUpButton.attr({
  313. text: this.getDrilldownBackText()
  314. })
  315. .align();
  316. }
  317. this.ddDupes.length = []; // #3315
  318. };
  319. ColumnSeries.prototype.supportsDrilldown = true;
  320. /**
  321. * When drilling up, keep the upper series invisible until the lower series has
  322. * moved into place
  323. */
  324. ColumnSeries.prototype.animateDrillupTo = function (init) {
  325. if (!init) {
  326. var newSeries = this,
  327. level = newSeries.drilldownLevel;
  328. each(this.points, function (point) {
  329. if (point.graphic) { // #3407
  330. point.graphic.hide();
  331. }
  332. if (point.dataLabel) {
  333. point.dataLabel.hide();
  334. }
  335. if (point.connector) {
  336. point.connector.hide();
  337. }
  338. });
  339. // Do dummy animation on first point to get to complete
  340. setTimeout(function () {
  341. if (newSeries.points) { // May be destroyed in the meantime, #3389
  342. each(newSeries.points, function (point, i) {
  343. // Fade in other points
  344. var verb = i === (level && level.pointIndex) ? 'show' : 'fadeIn',
  345. inherit = verb === 'show' ? true : undefined;
  346. if (point.graphic) { // #3407
  347. point.graphic[verb](inherit);
  348. }
  349. if (point.dataLabel) {
  350. point.dataLabel[verb](inherit);
  351. }
  352. if (point.connector) {
  353. point.connector[verb](inherit);
  354. }
  355. });
  356. }
  357. }, Math.max(this.chart.options.drilldown.animation.duration - 50, 0));
  358. // Reset
  359. this.animate = noop;
  360. }
  361. };
  362. ColumnSeries.prototype.animateDrilldown = function (init) {
  363. var series = this,
  364. drilldownLevels = this.chart.drilldownLevels,
  365. animateFrom,
  366. animationOptions = this.chart.options.drilldown.animation,
  367. xAxis = this.xAxis;
  368. if (!init) {
  369. each(drilldownLevels, function (level) {
  370. if (series.options._ddSeriesId === level.lowerSeriesOptions._ddSeriesId) {
  371. animateFrom = level.shapeArgs;
  372. animateFrom.fill = level.color;
  373. }
  374. });
  375. animateFrom.x += (pick(xAxis.oldPos, xAxis.pos) - xAxis.pos);
  376. each(this.points, function (point) {
  377. if (point.graphic) {
  378. point.graphic
  379. .attr(animateFrom)
  380. .animate(
  381. extend(point.shapeArgs, { fill: point.color }),
  382. animationOptions
  383. );
  384. }
  385. if (point.dataLabel) {
  386. point.dataLabel.fadeIn(animationOptions);
  387. }
  388. });
  389. this.animate = null;
  390. }
  391. };
  392. /**
  393. * When drilling up, pull out the individual point graphics from the lower series
  394. * and animate them into the origin point in the upper series.
  395. */
  396. ColumnSeries.prototype.animateDrillupFrom = function (level) {
  397. var animationOptions = this.chart.options.drilldown.animation,
  398. group = this.group,
  399. series = this;
  400. // Cancel mouse events on the series group (#2787)
  401. each(series.trackerGroups, function (key) {
  402. if (series[key]) { // we don't always have dataLabelsGroup
  403. series[key].on('mouseover');
  404. }
  405. });
  406. delete this.group;
  407. each(this.points, function (point) {
  408. var graphic = point.graphic,
  409. complete = function () {
  410. graphic.destroy();
  411. if (group) {
  412. group = group.destroy();
  413. }
  414. };
  415. if (graphic) {
  416. delete point.graphic;
  417. if (animationOptions) {
  418. graphic.animate(
  419. extend(level.shapeArgs, { fill: level.color }),
  420. H.merge(animationOptions, { complete: complete })
  421. );
  422. } else {
  423. graphic.attr(level.shapeArgs);
  424. complete();
  425. }
  426. }
  427. });
  428. };
  429. if (PieSeries) {
  430. extend(PieSeries.prototype, {
  431. supportsDrilldown: true,
  432. animateDrillupTo: ColumnSeries.prototype.animateDrillupTo,
  433. animateDrillupFrom: ColumnSeries.prototype.animateDrillupFrom,
  434. animateDrilldown: function (init) {
  435. var level = this.chart.drilldownLevels[this.chart.drilldownLevels.length - 1],
  436. animationOptions = this.chart.options.drilldown.animation,
  437. animateFrom = level.shapeArgs,
  438. start = animateFrom.start,
  439. angle = animateFrom.end - start,
  440. startAngle = angle / this.points.length;
  441. if (!init) {
  442. each(this.points, function (point, i) {
  443. point.graphic
  444. .attr(H.merge(animateFrom, {
  445. start: start + i * startAngle,
  446. end: start + (i + 1) * startAngle,
  447. fill: level.color
  448. }))[animationOptions ? 'animate' : 'attr'](
  449. extend(point.shapeArgs, { fill: point.color }),
  450. animationOptions
  451. );
  452. });
  453. this.animate = null;
  454. }
  455. }
  456. });
  457. }
  458. H.Point.prototype.doDrilldown = function (_holdRedraw, category) {
  459. var series = this.series,
  460. chart = series.chart,
  461. drilldown = chart.options.drilldown,
  462. i = (drilldown.series || []).length,
  463. seriesOptions;
  464. if (!chart.ddDupes) {
  465. chart.ddDupes = [];
  466. }
  467. while (i-- && !seriesOptions) {
  468. if (drilldown.series[i].id === this.drilldown && inArray(this.drilldown, chart.ddDupes) === -1) {
  469. seriesOptions = drilldown.series[i];
  470. chart.ddDupes.push(this.drilldown);
  471. }
  472. }
  473. // Fire the event. If seriesOptions is undefined, the implementer can check for
  474. // seriesOptions, and call addSeriesAsDrilldown async if necessary.
  475. fireEvent(chart, 'drilldown', {
  476. point: this,
  477. seriesOptions: seriesOptions,
  478. category: category,
  479. points: category !== undefined && this.series.xAxis.ddPoints[category].slice(0)
  480. });
  481. if (seriesOptions) {
  482. if (_holdRedraw) {
  483. chart.addSingleSeriesAsDrilldown(this, seriesOptions);
  484. } else {
  485. chart.addSeriesAsDrilldown(this, seriesOptions);
  486. }
  487. }
  488. };
  489. /**
  490. * Drill down to a given category. This is the same as clicking on an axis label.
  491. */
  492. H.Axis.prototype.drilldownCategory = function (x) {
  493. var key,
  494. point,
  495. ddPointsX = this.ddPoints[x];
  496. for (key in ddPointsX) {
  497. point = ddPointsX[key];
  498. if (point && point.series && point.series.visible && point.doDrilldown) { // #3197
  499. point.doDrilldown(true, x);
  500. }
  501. }
  502. this.chart.applyDrilldown();
  503. };
  504. /**
  505. * Create and return a collection of points associated with the X position. Reset it for each level.
  506. */
  507. H.Axis.prototype.getDDPoints = function (x, levelNumber) {
  508. var ddPoints = this.ddPoints;
  509. if (!ddPoints) {
  510. this.ddPoints = ddPoints = {};
  511. }
  512. if (!ddPoints[x]) {
  513. ddPoints[x] = [];
  514. }
  515. if (ddPoints[x].levelNumber !== levelNumber) {
  516. ddPoints[x].length = 0; // reset
  517. }
  518. return ddPoints[x];
  519. };
  520. /**
  521. * Make a tick label drillable, or remove drilling on update
  522. */
  523. Tick.prototype.drillable = function () {
  524. var pos = this.pos,
  525. label = this.label,
  526. axis = this.axis,
  527. ddPointsX = axis.ddPoints && axis.ddPoints[pos];
  528. if (label && ddPointsX && ddPointsX.length) {
  529. if (!label.basicStyles) {
  530. label.basicStyles = H.merge(label.styles);
  531. }
  532. label
  533. .addClass('highcharts-drilldown-axis-label')
  534. .css(axis.chart.options.drilldown.activeAxisLabelStyle)
  535. .on('click', function () {
  536. axis.drilldownCategory(pos);
  537. });
  538. } else if (label && label.basicStyles) {
  539. label.styles = {}; // reset for full overwrite of styles
  540. label.css(label.basicStyles);
  541. label.on('click', null); // #3806
  542. }
  543. };
  544. /**
  545. * Always keep the drillability updated (#3951)
  546. */
  547. wrap(Tick.prototype, 'addLabel', function (proceed) {
  548. proceed.call(this);
  549. this.drillable();
  550. });
  551. /**
  552. * On initialization of each point, identify its label and make it clickable. Also, provide a
  553. * list of points associated to that label.
  554. */
  555. wrap(H.Point.prototype, 'init', function (proceed, series, options, x) {
  556. var point = proceed.call(this, series, options, x),
  557. xAxis = series.xAxis,
  558. tick = xAxis && xAxis.ticks[x],
  559. ddPointsX = xAxis && xAxis.getDDPoints(x, series.options._levelNumber);
  560. if (point.drilldown) {
  561. // Add the click event to the point
  562. H.addEvent(point, 'click', function () {
  563. if (series.xAxis && series.chart.options.drilldown.allowPointDrilldown === false) {
  564. series.xAxis.drilldownCategory(x);
  565. } else {
  566. point.doDrilldown();
  567. }
  568. });
  569. /*wrap(point, 'importEvents', function (proceed) { // wrapping importEvents makes point.click event work
  570. if (!this.hasImportedEvents) {
  571. proceed.call(this);
  572. H.addEvent(this, 'click', function () {
  573. this.doDrilldown();
  574. });
  575. }
  576. });*/
  577. // Register drilldown points on this X value
  578. if (ddPointsX) {
  579. ddPointsX.push(point);
  580. ddPointsX.levelNumber = series.options._levelNumber;
  581. }
  582. }
  583. // Add or remove click handler and style on the tick label
  584. if (tick) {
  585. tick.drillable();
  586. }
  587. return point;
  588. });
  589. wrap(H.Series.prototype, 'drawDataLabels', function (proceed) {
  590. var css = this.chart.options.drilldown.activeDataLabelStyle;
  591. proceed.call(this);
  592. each(this.points, function (point) {
  593. if (point.drilldown && point.dataLabel) {
  594. point.dataLabel
  595. .attr({
  596. 'class': 'highcharts-drilldown-data-label'
  597. })
  598. .css(css);
  599. }
  600. });
  601. });
  602. // Mark the trackers with a pointer
  603. var type,
  604. drawTrackerWrapper = function (proceed) {
  605. proceed.call(this);
  606. each(this.points, function (point) {
  607. if (point.drilldown && point.graphic) {
  608. point.graphic
  609. .attr({
  610. 'class': 'highcharts-drilldown-point'
  611. })
  612. .css({ cursor: 'pointer' });
  613. }
  614. });
  615. };
  616. for (type in seriesTypes) {
  617. if (seriesTypes[type].prototype.supportsDrilldown) {
  618. wrap(seriesTypes[type].prototype, 'drawTracker', drawTrackerWrapper);
  619. }
  620. }
  621. }(Highcharts));