Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
776 lars 1
/**
2
 * @license Highmaps JS v1.1.9 (2015-10-07)
3
 *
4
 * (c) 2011-2014 Torstein Honsi
5
 *
6
 * License: www.highcharts.com/license
7
 */
8
 
9
/*global HighchartsAdapter*/
10
(function (Highcharts) {
11
 
12
 
13
var UNDEFINED,
14
	Axis = Highcharts.Axis,
15
	Chart = Highcharts.Chart,
16
	Color = Highcharts.Color,
17
	Legend = Highcharts.Legend,
18
	LegendSymbolMixin = Highcharts.LegendSymbolMixin,
19
	Series = Highcharts.Series,
20
	Point = Highcharts.Point,
21
 
22
	defaultOptions = Highcharts.getOptions(),
23
	each = Highcharts.each,
24
	extend = Highcharts.extend,
25
	extendClass = Highcharts.extendClass,
26
	merge = Highcharts.merge,
27
	pick = Highcharts.pick,
28
	seriesTypes = Highcharts.seriesTypes,
29
	wrap = Highcharts.wrap,
30
	noop = function () {};
31
 
32
 
33
 
34
 
35
/**
36
 * The ColorAxis object for inclusion in gradient legends
37
 */
38
var ColorAxis = Highcharts.ColorAxis = function () {
39
	this.isColorAxis = true;
40
	this.init.apply(this, arguments);
41
};
42
extend(ColorAxis.prototype, Axis.prototype);
43
extend(ColorAxis.prototype, {
44
	defaultColorAxisOptions: {
45
		lineWidth: 0,
46
		minPadding: 0,
47
		maxPadding: 0,
48
		gridLineWidth: 1,
49
		tickPixelInterval: 72,
50
		startOnTick: true,
51
		endOnTick: true,
52
		offset: 0,
53
		marker: {
54
			animation: {
55
				duration: 50
56
			},
57
			color: 'gray',
58
			width: 0.01
59
		},
60
		labels: {
61
			overflow: 'justify'
62
		},
63
		minColor: '#EFEFFF',
64
		maxColor: '#003875',
65
		tickLength: 5
66
	},
67
	init: function (chart, userOptions) {
68
		var horiz = chart.options.legend.layout !== 'vertical',
69
			options;
70
 
71
		// Build the options
72
		options = merge(this.defaultColorAxisOptions, {
73
			side: horiz ? 2 : 1,
74
			reversed: !horiz
75
		}, userOptions, {
76
			opposite: !horiz,
77
			showEmpty: false,
78
			title: null,
79
			isColor: true
80
		});
81
 
82
		Axis.prototype.init.call(this, chart, options);
83
 
84
		// Base init() pushes it to the xAxis array, now pop it again
85
		//chart[this.isXAxis ? 'xAxis' : 'yAxis'].pop();
86
 
87
		// Prepare data classes
88
		if (userOptions.dataClasses) {
89
			this.initDataClasses(userOptions);
90
		}
91
		this.initStops(userOptions);
92
 
93
		// Override original axis properties
94
		this.horiz = horiz;
95
		this.zoomEnabled = false;
96
	},
97
 
98
	/*
99
	 * Return an intermediate color between two colors, according to pos where 0
100
	 * is the from color and 1 is the to color.
101
	 * NOTE: Changes here should be copied
102
	 * to the same function in drilldown.src.js and solid-gauge-src.js.
103
	 */
104
	tweenColors: function (from, to, pos) {
105
		// Check for has alpha, because rgba colors perform worse due to lack of
106
		// support in WebKit.
107
		var hasAlpha,
108
			ret;
109
 
110
		// Unsupported color, return to-color (#3920)
111
		if (!to.rgba.length || !from.rgba.length) {
112
			ret = to.raw || 'none';
113
 
114
		// Interpolate
115
		} else {
116
			from = from.rgba;
117
			to = to.rgba;
118
			hasAlpha = (to[3] !== 1 || from[3] !== 1);
119
			ret = (hasAlpha ? 'rgba(' : 'rgb(') +
120
				Math.round(to[0] + (from[0] - to[0]) * (1 - pos)) + ',' +
121
				Math.round(to[1] + (from[1] - to[1]) * (1 - pos)) + ',' +
122
				Math.round(to[2] + (from[2] - to[2]) * (1 - pos)) +
123
				(hasAlpha ? (',' + (to[3] + (from[3] - to[3]) * (1 - pos))) : '') + ')';
124
		}
125
		return ret;
126
	},
127
 
128
	initDataClasses: function (userOptions) {
129
		var axis = this,
130
			chart = this.chart,
131
			dataClasses,
132
			colorCounter = 0,
133
			options = this.options,
134
			len = userOptions.dataClasses.length;
135
		this.dataClasses = dataClasses = [];
136
		this.legendItems = [];
137
 
138
		each(userOptions.dataClasses, function (dataClass, i) {
139
			var colors;
140
 
141
			dataClass = merge(dataClass);
142
			dataClasses.push(dataClass);
143
			if (!dataClass.color) {
144
				if (options.dataClassColor === 'category') {
145
					colors = chart.options.colors;
146
					dataClass.color = colors[colorCounter++];
147
					// loop back to zero
148
					if (colorCounter === colors.length) {
149
						colorCounter = 0;
150
					}
151
				} else {
152
					dataClass.color = axis.tweenColors(
153
						Color(options.minColor),
154
						Color(options.maxColor),
155
						len < 2 ? 0.5 : i / (len - 1) // #3219
156
					);
157
				}
158
			}
159
		});
160
	},
161
 
162
	initStops: function (userOptions) {
163
		this.stops = userOptions.stops || [
164
			[0, this.options.minColor],
165
			[1, this.options.maxColor]
166
		];
167
		each(this.stops, function (stop) {
168
			stop.color = Color(stop[1]);
169
		});
170
	},
171
 
172
	/**
173
	 * Extend the setOptions method to process extreme colors and color
174
	 * stops.
175
	 */
176
	setOptions: function (userOptions) {
177
		Axis.prototype.setOptions.call(this, userOptions);
178
 
179
		this.options.crosshair = this.options.marker;
180
		this.coll = 'colorAxis';
181
	},
182
 
183
	setAxisSize: function () {
184
		var symbol = this.legendSymbol,
185
			chart = this.chart,
186
			x,
187
			y,
188
			width,
189
			height;
190
 
191
		if (symbol) {
192
			this.left = x = symbol.attr('x');
193
			this.top = y = symbol.attr('y');
194
			this.width = width = symbol.attr('width');
195
			this.height = height = symbol.attr('height');
196
			this.right = chart.chartWidth - x - width;
197
			this.bottom = chart.chartHeight - y - height;
198
 
199
			this.len = this.horiz ? width : height;
200
			this.pos = this.horiz ? x : y;
201
		}
202
	},
203
 
204
	/**
205
	 * Translate from a value to a color
206
	 */
207
	toColor: function (value, point) {
208
		var pos,
209
			stops = this.stops,
210
			from,
211
			to,
212
			color,
213
			dataClasses = this.dataClasses,
214
			dataClass,
215
			i;
216
 
217
		if (dataClasses) {
218
			i = dataClasses.length;
219
			while (i--) {
220
				dataClass = dataClasses[i];
221
				from = dataClass.from;
222
				to = dataClass.to;
223
				if ((from === UNDEFINED || value >= from) && (to === UNDEFINED || value <= to)) {
224
					color = dataClass.color;
225
					if (point) {
226
						point.dataClass = i;
227
					}
228
					break;
229
				}
230
			}
231
 
232
		} else {
233
 
234
			if (this.isLog) {
235
				value = this.val2lin(value);
236
			}
237
			pos = 1 - ((this.max - value) / ((this.max - this.min) || 1));
238
			i = stops.length;
239
			while (i--) {
240
				if (pos > stops[i][0]) {
241
					break;
242
				}
243
			}
244
			from = stops[i] || stops[i + 1];
245
			to = stops[i + 1] || from;
246
 
247
			// The position within the gradient
248
			pos = 1 - (to[0] - pos) / ((to[0] - from[0]) || 1);
249
 
250
			color = this.tweenColors(
251
				from.color,
252
				to.color,
253
				pos
254
			);
255
		}
256
		return color;
257
	},
258
 
259
	/**
260
	 * Override the getOffset method to add the whole axis groups inside the legend.
261
	 */
262
	getOffset: function () {
263
		var group = this.legendGroup,
264
			sideOffset = this.chart.axisOffset[this.side];
265
 
266
		if (group) {
267
 
268
			// Hook for the getOffset method to add groups to this parent group
269
			this.axisParent = group;
270
 
271
			// Call the base
272
			Axis.prototype.getOffset.call(this);
273
 
274
			// First time only
275
			if (!this.added) {
276
 
277
				this.added = true;
278
 
279
				this.labelLeft = 0;
280
				this.labelRight = this.width;
281
			}
282
			// Reset it to avoid color axis reserving space
283
			this.chart.axisOffset[this.side] = sideOffset;
284
		}
285
	},
286
 
287
	/**
288
	 * Create the color gradient
289
	 */
290
	setLegendColor: function () {
291
		var grad,
292
			horiz = this.horiz,
293
			options = this.options,
294
			reversed = this.reversed;
295
 
296
		grad = horiz ? [+reversed, 0, +!reversed, 0] : [0, +!reversed, 0, +reversed]; // #3190
297
		this.legendColor = {
298
			linearGradient: { x1: grad[0], y1: grad[1], x2: grad[2], y2: grad[3] },
299
			stops: options.stops || [
300
				[0, options.minColor],
301
				[1, options.maxColor]
302
			]
303
		};
304
	},
305
 
306
	/**
307
	 * The color axis appears inside the legend and has its own legend symbol
308
	 */
309
	drawLegendSymbol: function (legend, item) {
310
		var padding = legend.padding,
311
			legendOptions = legend.options,
312
			horiz = this.horiz,
313
			box,
314
			width = pick(legendOptions.symbolWidth, horiz ? 200 : 12),
315
			height = pick(legendOptions.symbolHeight, horiz ? 12 : 200),
316
			labelPadding = pick(legendOptions.labelPadding, horiz ? 16 : 30),
317
			itemDistance = pick(legendOptions.itemDistance, 10);
318
 
319
		this.setLegendColor();
320
 
321
		// Create the gradient
322
		item.legendSymbol = this.chart.renderer.rect(
323
			0,
324
			legend.baseline - 11,
325
			width,
326
			height
327
		).attr({
328
			zIndex: 1
329
		}).add(item.legendGroup);
330
		box = item.legendSymbol.getBBox();
331
 
332
		// Set how much space this legend item takes up
333
		this.legendItemWidth = width + padding + (horiz ? itemDistance : labelPadding);
334
		this.legendItemHeight = height + padding + (horiz ? labelPadding : 0);
335
	},
336
	/**
337
	 * Fool the legend
338
	 */
339
	setState: noop,
340
	visible: true,
341
	setVisible: noop,
342
	getSeriesExtremes: function () {
343
		var series;
344
		if (this.series.length) {
345
			series = this.series[0];
346
			this.dataMin = series.valueMin;
347
			this.dataMax = series.valueMax;
348
		}
349
	},
350
	drawCrosshair: function (e, point) {
351
		var plotX = point && point.plotX,
352
			plotY = point && point.plotY,
353
			crossPos,
354
			axisPos = this.pos,
355
			axisLen = this.len;
356
 
357
		if (point) {
358
			crossPos = this.toPixels(point[point.series.colorKey]);
359
			if (crossPos < axisPos) {
360
				crossPos = axisPos - 2;
361
			} else if (crossPos > axisPos + axisLen) {
362
				crossPos = axisPos + axisLen + 2;
363
			}
364
 
365
			point.plotX = crossPos;
366
			point.plotY = this.len - crossPos;
367
			Axis.prototype.drawCrosshair.call(this, e, point);
368
			point.plotX = plotX;
369
			point.plotY = plotY;
370
 
371
			if (this.cross) {
372
				this.cross
373
					.attr({
374
						fill: this.crosshair.color
375
					})
376
					.add(this.legendGroup);
377
			}
378
		}
379
	},
380
	getPlotLinePath: function (a, b, c, d, pos) {
381
		if (typeof pos === 'number') { // crosshairs only // #3969 pos can be 0 !!
382
			return this.horiz ?
383
				['M', pos - 4, this.top - 6, 'L', pos + 4, this.top - 6, pos, this.top, 'Z'] :
384
				['M', this.left, pos, 'L', this.left - 6, pos + 6, this.left - 6, pos - 6, 'Z'];
385
		} else {
386
			return Axis.prototype.getPlotLinePath.call(this, a, b, c, d);
387
		}
388
	},
389
 
390
	update: function (newOptions, redraw) {
391
		var chart = this.chart,
392
			legend = chart.legend;
393
 
394
		each(this.series, function (series) {
395
			series.isDirtyData = true; // Needed for Axis.update when choropleth colors change
396
		});
397
 
398
		// When updating data classes, destroy old items and make sure new ones are created (#3207)
399
		if (newOptions.dataClasses && legend.allItems) {
400
			each(legend.allItems, function (item) {
401
				if (item.isDataClass) {
402
					item.legendGroup.destroy();
403
				}
404
			});
405
			chart.isDirtyLegend = true;
406
		}
407
 
408
		// Keep the options structure updated for export. Unlike xAxis and yAxis, the colorAxis is
409
		// not an array. (#3207)
410
		chart.options[this.coll] = merge(this.userOptions, newOptions);
411
 
412
		Axis.prototype.update.call(this, newOptions, redraw);
413
		if (this.legendItem) {
414
			this.setLegendColor();
415
			legend.colorizeItem(this, true);
416
		}
417
	},
418
 
419
	/**
420
	 * Get the legend item symbols for data classes
421
	 */
422
	getDataClassLegendSymbols: function () {
423
		var axis = this,
424
			chart = this.chart,
425
			legendItems = this.legendItems,
426
			legendOptions = chart.options.legend,
427
			valueDecimals = legendOptions.valueDecimals,
428
			valueSuffix = legendOptions.valueSuffix || '',
429
			name;
430
 
431
		if (!legendItems.length) {
432
			each(this.dataClasses, function (dataClass, i) {
433
				var vis = true,
434
					from = dataClass.from,
435
					to = dataClass.to;
436
 
437
				// Assemble the default name. This can be overridden by legend.options.labelFormatter
438
				name = '';
439
				if (from === UNDEFINED) {
440
					name = '< ';
441
				} else if (to === UNDEFINED) {
442
					name = '> ';
443
				}
444
				if (from !== UNDEFINED) {
445
					name += Highcharts.numberFormat(from, valueDecimals) + valueSuffix;
446
				}
447
				if (from !== UNDEFINED && to !== UNDEFINED) {
448
					name += ' - ';
449
				}
450
				if (to !== UNDEFINED) {
451
					name += Highcharts.numberFormat(to, valueDecimals) + valueSuffix;
452
				}
453
 
454
				// Add a mock object to the legend items
455
				legendItems.push(extend({
456
					chart: chart,
457
					name: name,
458
					options: {},
459
					drawLegendSymbol: LegendSymbolMixin.drawRectangle,
460
					visible: true,
461
					setState: noop,
462
					isDataClass: true,
463
					setVisible: function () {
464
						vis = this.visible = !vis;
465
						each(axis.series, function (series) {
466
							each(series.points, function (point) {
467
								if (point.dataClass === i) {
468
									point.setVisible(vis);
469
								}
470
							});
471
						});
472
 
473
						chart.legend.colorizeItem(this, vis);
474
					}
475
				}, dataClass));
476
			});
477
		}
478
		return legendItems;
479
	},
480
	name: '' // Prevents 'undefined' in legend in IE8
481
});
482
 
483
/**
484
 * Handle animation of the color attributes directly
485
 */
486
each(['fill', 'stroke'], function (prop) {
487
	HighchartsAdapter.addAnimSetter(prop, function (fx) {
488
		fx.elem.attr(prop, ColorAxis.prototype.tweenColors(Color(fx.start), Color(fx.end), fx.pos));
489
	});
490
});
491
 
492
/**
493
 * Extend the chart getAxes method to also get the color axis
494
 */
495
wrap(Chart.prototype, 'getAxes', function (proceed) {
496
 
497
	var options = this.options,
498
		colorAxisOptions = options.colorAxis;
499
 
500
	proceed.call(this);
501
 
502
	this.colorAxis = [];
503
	if (colorAxisOptions) {
504
		proceed = new ColorAxis(this, colorAxisOptions); // Fake assignment for jsLint
505
	}
506
});
507
 
508
 
509
/**
510
 * Wrap the legend getAllItems method to add the color axis. This also removes the
511
 * axis' own series to prevent them from showing up individually.
512
 */
513
wrap(Legend.prototype, 'getAllItems', function (proceed) {
514
	var allItems = [],
515
		colorAxis = this.chart.colorAxis[0];
516
 
517
	if (colorAxis) {
518
 
519
		// Data classes
520
		if (colorAxis.options.dataClasses) {
521
			allItems = allItems.concat(colorAxis.getDataClassLegendSymbols());
522
		// Gradient legend
523
		} else {
524
			// Add this axis on top
525
			allItems.push(colorAxis);
526
		}
527
 
528
		// Don't add the color axis' series
529
		each(colorAxis.series, function (series) {
530
			series.options.showInLegend = false;
531
		});
532
	}
533
 
534
	return allItems.concat(proceed.call(this));
535
});/**
536
 * Mixin for maps and heatmaps
537
 */
538
var colorPointMixin = {
539
	/**
540
	 * Set the visibility of a single point
541
	 */
542
	setVisible: function (vis) {
543
		var point = this,
544
			method = vis ? 'show' : 'hide';
545
 
546
		// Show and hide associated elements
547
		each(['graphic', 'dataLabel'], function (key) {
548
			if (point[key]) {
549
				point[key][method]();
550
			}
551
		});
552
	}
553
};
554
var colorSeriesMixin = {
555
 
556
	pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
557
		stroke: 'borderColor',
558
		'stroke-width': 'borderWidth',
559
		fill: 'color',
560
		dashstyle: 'dashStyle'
561
	},
562
	pointArrayMap: ['value'],
563
	axisTypes: ['xAxis', 'yAxis', 'colorAxis'],
564
	optionalAxis: 'colorAxis',
565
	trackerGroups: ['group', 'markerGroup', 'dataLabelsGroup'],
566
	getSymbol: noop,
567
	parallelArrays: ['x', 'y', 'value'],
568
	colorKey: 'value',
569
 
570
	/**
571
	 * In choropleth maps, the color is a result of the value, so this needs translation too
572
	 */
573
	translateColors: function () {
574
		var series = this,
575
			nullColor = this.options.nullColor,
576
			colorAxis = this.colorAxis,
577
			colorKey = this.colorKey;
578
 
579
		each(this.data, function (point) {
580
			var value = point[colorKey],
581
				color;
582
 
583
			color = point.options.color ||
584
				(value === null ? nullColor : (colorAxis && value !== undefined) ? colorAxis.toColor(value, point) : point.color || series.color);
585
 
586
			if (color) {
587
				point.color = color;
588
			}
589
		});
590
	}
591
};
592
/**
593
 * Extend the default options with map options
594
 */
595
defaultOptions.plotOptions.heatmap = merge(defaultOptions.plotOptions.scatter, {
596
	animation: false,
597
	borderWidth: 0,
598
	nullColor: '#F8F8F8',
599
	dataLabels: {
600
		formatter: function () { // #2945
601
			return this.point.value;
602
		},
603
		inside: true,
604
		verticalAlign: 'middle',
605
		crop: false,
606
		overflow: false,
607
		padding: 0 // #3837
608
	},
609
	marker: null,
610
	pointRange: null, // dynamically set to colsize by default
611
	tooltip: {
612
		pointFormat: '{point.x}, {point.y}: {point.value}<br/>'
613
	},
614
	states: {
615
		normal: {
616
			animation: true
617
		},
618
		hover: {
619
			halo: false,  // #3406, halo is not required on heatmaps
620
			brightness: 0.2
621
		}
622
	}
623
});
624
 
625
// The Heatmap series type
626
seriesTypes.heatmap = extendClass(seriesTypes.scatter, merge(colorSeriesMixin, {
627
	type: 'heatmap',
628
	pointArrayMap: ['y', 'value'],
629
	hasPointSpecificOptions: true,
630
	pointClass: extendClass(Point, colorPointMixin),
631
	supportsDrilldown: true,
632
	getExtremesFromAll: true,
633
	directTouch: true,
634
 
635
	/**
636
	 * Override the init method to add point ranges on both axes.
637
	 */
638
	init: function () {
639
		var options;
640
		seriesTypes.scatter.prototype.init.apply(this, arguments);
641
 
642
		options = this.options;
643
		this.pointRange = options.pointRange = pick(options.pointRange, options.colsize || 1); // #3758, prevent resetting in setData
644
		this.yAxis.axisPointRange = options.rowsize || 1; // general point range
645
	},
646
	translate: function () {
647
		var series = this,
648
			options = series.options,
649
			xAxis = series.xAxis,
650
			yAxis = series.yAxis,
651
			between = function (x, a, b) {
652
				return Math.min(Math.max(a, x), b);
653
			};
654
 
655
		series.generatePoints();
656
 
657
		each(series.points, function (point) {
658
			var xPad = (options.colsize || 1) / 2,
659
				yPad = (options.rowsize || 1) / 2,
660
				x1 = between(Math.round(xAxis.len - xAxis.translate(point.x - xPad, 0, 1, 0, 1)), 0, xAxis.len),
661
				x2 = between(Math.round(xAxis.len - xAxis.translate(point.x + xPad, 0, 1, 0, 1)), 0, xAxis.len),
662
				y1 = between(Math.round(yAxis.translate(point.y - yPad, 0, 1, 0, 1)), 0, yAxis.len),
663
				y2 = between(Math.round(yAxis.translate(point.y + yPad, 0, 1, 0, 1)), 0, yAxis.len);
664
 
665
			// Set plotX and plotY for use in K-D-Tree and more
666
			point.plotX = point.clientX = (x1 + x2) / 2;
667
			point.plotY = (y1 + y2) / 2;
668
 
669
			point.shapeType = 'rect';
670
			point.shapeArgs = {
671
				x: Math.min(x1, x2),
672
				y: Math.min(y1, y2),
673
				width: Math.abs(x2 - x1),
674
				height: Math.abs(y2 - y1)
675
			};
676
		});
677
 
678
		series.translateColors();
679
 
680
		// Make sure colors are updated on colorAxis update (#2893)
681
		if (this.chart.hasRendered) {
682
			each(series.points, function (point) {
683
				point.shapeArgs.fill = point.options.color || point.color; // #3311
684
			});
685
		}
686
	},
687
	drawPoints: seriesTypes.column.prototype.drawPoints,
688
	animate: noop,
689
	getBox: noop,
690
	drawLegendSymbol: LegendSymbolMixin.drawRectangle,
691
 
692
	getExtremes: function () {
693
		// Get the extremes from the value data
694
		Series.prototype.getExtremes.call(this, this.valueData);
695
		this.valueMin = this.dataMin;
696
		this.valueMax = this.dataMax;
697
 
698
		// Get the extremes from the y data
699
		Series.prototype.getExtremes.call(this);
700
	}
701
 
702
}));
703
 
704
 
705
}(Highcharts));