drilldown.src.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. point.doDrilldown();
  564. });
  565. /*wrap(point, 'importEvents', function (proceed) { // wrapping importEvents makes point.click event work
  566. if (!this.hasImportedEvents) {
  567. proceed.call(this);
  568. H.addEvent(this, 'click', function () {
  569. this.doDrilldown();
  570. });
  571. }
  572. });*/
  573. // Register drilldown points on this X value
  574. if (ddPointsX) {
  575. ddPointsX.push(point);
  576. ddPointsX.levelNumber = series.options._levelNumber;
  577. }
  578. }
  579. // Add or remove click handler and style on the tick label
  580. if (tick) {
  581. tick.drillable();
  582. }
  583. return point;
  584. });
  585. wrap(H.Series.prototype, 'drawDataLabels', function (proceed) {
  586. var css = this.chart.options.drilldown.activeDataLabelStyle;
  587. proceed.call(this);
  588. each(this.points, function (point) {
  589. if (point.drilldown && point.dataLabel) {
  590. point.dataLabel
  591. .attr({
  592. 'class': 'highcharts-drilldown-data-label'
  593. })
  594. .css(css);
  595. }
  596. });
  597. });
  598. // Mark the trackers with a pointer
  599. var type,
  600. drawTrackerWrapper = function (proceed) {
  601. proceed.call(this);
  602. each(this.points, function (point) {
  603. if (point.drilldown && point.graphic) {
  604. point.graphic
  605. .attr({
  606. 'class': 'highcharts-drilldown-point'
  607. })
  608. .css({ cursor: 'pointer' });
  609. }
  610. });
  611. };
  612. for (type in seriesTypes) {
  613. if (seriesTypes[type].prototype.supportsDrilldown) {
  614. wrap(seriesTypes[type].prototype, 'drawTracker', drawTrackerWrapper);
  615. }
  616. }
  617. }(Highcharts));