123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- /**
- * @license Highcharts JS v4.1.5 (2015-04-13)
- * Solid angular gauge module
- *
- * (c) 2010-2014 Torstein Honsi
- *
- * License: www.highcharts.com/license
- */
- /*global Highcharts, HighchartsAdapter*/
- (function (H) {
- "use strict";
- var defaultPlotOptions = H.getOptions().plotOptions,
- pInt = H.pInt,
- pick = H.pick,
- each = H.each,
- colorAxisMethods,
- UNDEFINED;
- // The default options
- defaultPlotOptions.solidgauge = H.merge(defaultPlotOptions.gauge, {
- colorByPoint: true
- });
- // These methods are defined in the ColorAxis object, and copied here.
- // If we implement an AMD system we should make ColorAxis a dependency.
- colorAxisMethods = {
- initDataClasses: function (userOptions) {
- var axis = this,
- chart = this.chart,
- dataClasses,
- colorCounter = 0,
- options = this.options;
- this.dataClasses = dataClasses = [];
- each(userOptions.dataClasses, function (dataClass, i) {
- var colors;
- dataClass = H.merge(dataClass);
- dataClasses.push(dataClass);
- if (!dataClass.color) {
- if (options.dataClassColor === 'category') {
- colors = chart.options.colors;
- dataClass.color = colors[colorCounter++];
- // loop back to zero
- if (colorCounter === colors.length) {
- colorCounter = 0;
- }
- } else {
- dataClass.color = axis.tweenColors(H.Color(options.minColor), H.Color(options.maxColor), i / (userOptions.dataClasses.length - 1));
- }
- }
- });
- },
- initStops: function (userOptions) {
- this.stops = userOptions.stops || [
- [0, this.options.minColor],
- [1, this.options.maxColor]
- ];
- each(this.stops, function (stop) {
- stop.color = H.Color(stop[1]);
- });
- },
- /**
- * Translate from a value to a color
- */
- toColor: function (value, point) {
- var pos,
- stops = this.stops,
- from,
- to,
- color,
- dataClasses = this.dataClasses,
- dataClass,
- i;
- if (dataClasses) {
- i = dataClasses.length;
- while (i--) {
- dataClass = dataClasses[i];
- from = dataClass.from;
- to = dataClass.to;
- if ((from === UNDEFINED || value >= from) && (to === UNDEFINED || value <= to)) {
- color = dataClass.color;
- if (point) {
- point.dataClass = i;
- }
- break;
- }
- }
- } else {
- if (this.isLog) {
- value = this.val2lin(value);
- }
- pos = 1 - ((this.max - value) / (this.max - this.min));
- i = stops.length;
- while (i--) {
- if (pos > stops[i][0]) {
- break;
- }
- }
- from = stops[i] || stops[i + 1];
- to = stops[i + 1] || from;
- // The position within the gradient
- pos = 1 - (to[0] - pos) / ((to[0] - from[0]) || 1);
-
- color = this.tweenColors(
- from.color,
- to.color,
- pos
- );
- }
- return color;
- },
- /*
- * Return an intermediate color between two colors, according to pos where 0
- * is the from color and 1 is the to color.
- */
- tweenColors: function (from, to, pos) {
- // Check for has alpha, because rgba colors perform worse due to lack of
- // support in WebKit.
- var hasAlpha,
- ret;
- // Unsupported color, return to-color (#3920)
- if (!to.rgba.length || !from.rgba.length) {
- ret = to.raw || 'none';
- // Interpolate
- } else {
- from = from.rgba;
- to = to.rgba;
- hasAlpha = (to[3] !== 1 || from[3] !== 1);
- ret = (hasAlpha ? 'rgba(' : 'rgb(') +
- Math.round(to[0] + (from[0] - to[0]) * (1 - pos)) + ',' +
- Math.round(to[1] + (from[1] - to[1]) * (1 - pos)) + ',' +
- Math.round(to[2] + (from[2] - to[2]) * (1 - pos)) +
- (hasAlpha ? (',' + (to[3] + (from[3] - to[3]) * (1 - pos))) : '') + ')';
- }
- return ret;
- }
- };
- /**
- * Handle animation of the color attributes directly
- */
- each(['fill', 'stroke'], function (prop) {
- HighchartsAdapter.addAnimSetter(prop, function (fx) {
- fx.elem.attr(prop, colorAxisMethods.tweenColors(H.Color(fx.start), H.Color(fx.end), fx.pos));
- });
- });
- // The series prototype
- H.seriesTypes.solidgauge = H.extendClass(H.seriesTypes.gauge, {
- type: 'solidgauge',
- bindAxes: function () {
- var axis;
- H.seriesTypes.gauge.prototype.bindAxes.call(this);
- axis = this.yAxis;
- H.extend(axis, colorAxisMethods);
- // Prepare data classes
- if (axis.options.dataClasses) {
- axis.initDataClasses(axis.options);
- }
- axis.initStops(axis.options);
- },
- /**
- * Draw the points where each point is one needle
- */
- drawPoints: function () {
- var series = this,
- yAxis = series.yAxis,
- center = yAxis.center,
- options = series.options,
- renderer = series.chart.renderer,
- overshoot = options.overshoot,
- overshootVal = overshoot && typeof overshoot === 'number' ? overshoot / 180 * Math.PI : 0;
- H.each(series.points, function (point) {
- var graphic = point.graphic,
- rotation = yAxis.startAngleRad + yAxis.translate(point.y, null, null, null, true),
- radius = (pInt(pick(point.options.radius, options.radius, 100)) * center[2]) / 200, // docs: series<solidgauge>.data.radius http://jsfiddle.net/highcharts/7nwebu4b/
- innerRadius = (pInt(pick(point.options.innerRadius, options.innerRadius, 60)) * center[2]) / 200, // docs: series<solidgauge>.data.innerRadius
- shapeArgs,
- d,
- toColor = yAxis.toColor(point.y, point),
- fromColor;
- if (toColor === 'none') { // #3708
- toColor = point.color || series.color || 'none';
- }
- if (toColor !== 'none') {
- fromColor = point.color;
- point.color = toColor;
- }
- // Handle overshoot and clipping to axis max/min
- rotation = Math.max(yAxis.startAngleRad - overshootVal, Math.min(yAxis.endAngleRad + overshootVal, rotation));
- // Handle the wrap option
- if (options.wrap === false) {
- rotation = Math.max(yAxis.startAngleRad, Math.min(yAxis.endAngleRad, rotation));
- }
- rotation = rotation * 180 / Math.PI;
- var angle1 = rotation / (180 / Math.PI),
- angle2 = yAxis.startAngleRad,
- minAngle = Math.min(angle1, angle2),
- maxAngle = Math.max(angle1, angle2);
- if (maxAngle - minAngle > 2 * Math.PI) {
- maxAngle = minAngle + 2 * Math.PI;
- }
- point.shapeArgs = shapeArgs = {
- x: center[0],
- y: center[1],
- r: radius,
- innerR: innerRadius,
- start: minAngle,
- end: maxAngle,
- fill: toColor
- };
- point.startR = radius; // For PieSeries.animate
- if (graphic) {
- d = shapeArgs.d;
- graphic.animate(shapeArgs);
- shapeArgs.d = d; // animate alters it
- } else {
- point.graphic = renderer.arc(shapeArgs)
- .attr({
- stroke: options.borderColor || 'none',
- 'stroke-width': options.borderWidth || 0,
- fill: toColor,
- 'sweep-flag': 0
- })
- .add(series.group);
- }
- });
- },
- /**
- * Extend the pie slice animation by animating from start angle and up
- */
- animate: function (init) {
- if (!init) {
- this.startAngleRad = this.yAxis.startAngleRad;
- H.seriesTypes.pie.prototype.animate.call(this, init);
- }
- }
- });
- }(Highcharts));
|