UNPKG

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