UNPKG

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