UNPKG

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