Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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