solid-gauge.src.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**
  2. * @license Highcharts JS v5.0.6 (2016-12-07)
  3. * Solid angular gauge module
  4. *
  5. * (c) 2010-2016 Torstein Honsi
  6. *
  7. * License: www.highcharts.com/license
  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. * Solid angular gauge module
  19. *
  20. * (c) 2010-2016 Torstein Honsi
  21. *
  22. * License: www.highcharts.com/license
  23. */
  24. 'use strict';
  25. var pInt = H.pInt,
  26. pick = H.pick,
  27. each = H.each,
  28. isNumber = H.isNumber,
  29. colorAxisMethods;
  30. // These methods are defined in the ColorAxis object, and copied here.
  31. // If we implement an AMD system we should make ColorAxis a dependency.
  32. colorAxisMethods = {
  33. initDataClasses: function(userOptions) {
  34. var axis = this,
  35. chart = this.chart,
  36. dataClasses,
  37. colorCounter = 0,
  38. options = this.options;
  39. this.dataClasses = dataClasses = [];
  40. each(userOptions.dataClasses, function(dataClass, i) {
  41. var colors;
  42. dataClass = H.merge(dataClass);
  43. dataClasses.push(dataClass);
  44. if (!dataClass.color) {
  45. if (options.dataClassColor === 'category') {
  46. colors = chart.options.colors;
  47. dataClass.color = colors[colorCounter++];
  48. // loop back to zero
  49. if (colorCounter === colors.length) {
  50. colorCounter = 0;
  51. }
  52. } else {
  53. dataClass.color = axis.tweenColors(H.color(options.minColor), H.color(options.maxColor), i / (userOptions.dataClasses.length - 1));
  54. }
  55. }
  56. });
  57. },
  58. initStops: function(userOptions) {
  59. this.stops = userOptions.stops || [
  60. [0, this.options.minColor],
  61. [1, this.options.maxColor]
  62. ];
  63. each(this.stops, function(stop) {
  64. stop.color = H.color(stop[1]);
  65. });
  66. },
  67. /**
  68. * Translate from a value to a color
  69. */
  70. toColor: function(value, point) {
  71. var pos,
  72. stops = this.stops,
  73. from,
  74. to,
  75. color,
  76. dataClasses = this.dataClasses,
  77. dataClass,
  78. i;
  79. if (dataClasses) {
  80. i = dataClasses.length;
  81. while (i--) {
  82. dataClass = dataClasses[i];
  83. from = dataClass.from;
  84. to = dataClass.to;
  85. if ((from === undefined || value >= from) && (to === undefined || value <= to)) {
  86. color = dataClass.color;
  87. if (point) {
  88. point.dataClass = i;
  89. }
  90. break;
  91. }
  92. }
  93. } else {
  94. if (this.isLog) {
  95. value = this.val2lin(value);
  96. }
  97. pos = 1 - ((this.max - value) / (this.max - this.min));
  98. i = stops.length;
  99. while (i--) {
  100. if (pos > stops[i][0]) {
  101. break;
  102. }
  103. }
  104. from = stops[i] || stops[i + 1];
  105. to = stops[i + 1] || from;
  106. // The position within the gradient
  107. pos = 1 - (to[0] - pos) / ((to[0] - from[0]) || 1);
  108. color = this.tweenColors(
  109. from.color,
  110. to.color,
  111. pos
  112. );
  113. }
  114. return color;
  115. },
  116. /*
  117. * Return an intermediate color between two colors, according to pos where 0
  118. * is the from color and 1 is the to color.
  119. */
  120. tweenColors: function(from, to, pos) {
  121. // Check for has alpha, because rgba colors perform worse due to lack of
  122. // support in WebKit.
  123. var hasAlpha,
  124. ret;
  125. // Unsupported color, return to-color (#3920)
  126. if (!to.rgba.length || !from.rgba.length) {
  127. ret = to.input || 'none';
  128. // Interpolate
  129. } else {
  130. from = from.rgba;
  131. to = to.rgba;
  132. hasAlpha = (to[3] !== 1 || from[3] !== 1);
  133. ret = (hasAlpha ? 'rgba(' : 'rgb(') +
  134. Math.round(to[0] + (from[0] - to[0]) * (1 - pos)) + ',' +
  135. Math.round(to[1] + (from[1] - to[1]) * (1 - pos)) + ',' +
  136. Math.round(to[2] + (from[2] - to[2]) * (1 - pos)) +
  137. (hasAlpha ? (',' + (to[3] + (from[3] - to[3]) * (1 - pos))) : '') + ')';
  138. }
  139. return ret;
  140. }
  141. };
  142. /**
  143. * Handle animation of the color attributes directly
  144. */
  145. each(['fill', 'stroke'], function(prop) {
  146. H.Fx.prototype[prop + 'Setter'] = function() {
  147. this.elem.attr(
  148. prop,
  149. colorAxisMethods.tweenColors(
  150. H.color(this.start),
  151. H.color(this.end),
  152. this.pos
  153. ),
  154. null,
  155. true
  156. );
  157. };
  158. });
  159. // The solidgauge series type
  160. H.seriesType('solidgauge', 'gauge', {
  161. colorByPoint: true
  162. }, {
  163. /**
  164. * Extend the translate function to extend the Y axis with the necessary
  165. * decoration (#5895).
  166. */
  167. translate: function() {
  168. var axis = this.yAxis;
  169. H.extend(axis, colorAxisMethods);
  170. // Prepare data classes
  171. if (!axis.dataClasses && axis.options.dataClasses) {
  172. axis.initDataClasses(axis.options);
  173. }
  174. axis.initStops(axis.options);
  175. // Generate points and inherit data label position
  176. H.seriesTypes.gauge.prototype.translate.call(this);
  177. },
  178. /**
  179. * Draw the points where each point is one needle
  180. */
  181. drawPoints: function() {
  182. var series = this,
  183. yAxis = series.yAxis,
  184. center = yAxis.center,
  185. options = series.options,
  186. renderer = series.chart.renderer,
  187. overshoot = options.overshoot,
  188. overshootVal = isNumber(overshoot) ? overshoot / 180 * Math.PI : 0,
  189. thresholdAngleRad;
  190. // Handle the threshold option
  191. if (isNumber(options.threshold)) {
  192. thresholdAngleRad = yAxis.startAngleRad + yAxis.translate(
  193. options.threshold,
  194. null,
  195. null,
  196. null,
  197. true
  198. );
  199. }
  200. this.thresholdAngleRad = pick(thresholdAngleRad, yAxis.startAngleRad);
  201. each(series.points, function(point) {
  202. var graphic = point.graphic,
  203. rotation = yAxis.startAngleRad + yAxis.translate(point.y, null, null, null, true),
  204. radius = (pInt(pick(point.options.radius, options.radius, 100)) * center[2]) / 200,
  205. innerRadius = (pInt(pick(point.options.innerRadius, options.innerRadius, 60)) * center[2]) / 200,
  206. shapeArgs,
  207. d,
  208. toColor = yAxis.toColor(point.y, point),
  209. axisMinAngle = Math.min(yAxis.startAngleRad, yAxis.endAngleRad),
  210. axisMaxAngle = Math.max(yAxis.startAngleRad, yAxis.endAngleRad),
  211. minAngle,
  212. maxAngle;
  213. if (toColor === 'none') { // #3708
  214. toColor = point.color || series.color || 'none';
  215. }
  216. if (toColor !== 'none') {
  217. point.color = toColor;
  218. }
  219. // Handle overshoot and clipping to axis max/min
  220. rotation = Math.max(axisMinAngle - overshootVal, Math.min(axisMaxAngle + overshootVal, rotation));
  221. // Handle the wrap option
  222. if (options.wrap === false) {
  223. rotation = Math.max(axisMinAngle, Math.min(axisMaxAngle, rotation));
  224. }
  225. minAngle = Math.min(rotation, series.thresholdAngleRad);
  226. maxAngle = Math.max(rotation, series.thresholdAngleRad);
  227. if (maxAngle - minAngle > 2 * Math.PI) {
  228. maxAngle = minAngle + 2 * Math.PI;
  229. }
  230. point.shapeArgs = shapeArgs = {
  231. x: center[0],
  232. y: center[1],
  233. r: radius,
  234. innerR: innerRadius,
  235. start: minAngle,
  236. end: maxAngle,
  237. fill: toColor
  238. };
  239. point.startR = radius; // For PieSeries.animate
  240. if (graphic) {
  241. d = shapeArgs.d;
  242. graphic.animate(shapeArgs);
  243. if (d) {
  244. shapeArgs.d = d; // animate alters it
  245. }
  246. } else {
  247. point.graphic = renderer.arc(shapeArgs)
  248. .addClass('highcharts-point')
  249. .attr({
  250. fill: toColor,
  251. 'sweep-flag': 0
  252. })
  253. .add(series.group);
  254. }
  255. });
  256. },
  257. /**
  258. * Extend the pie slice animation by animating from start angle and up
  259. */
  260. animate: function(init) {
  261. if (!init) {
  262. this.startAngleRad = this.thresholdAngleRad;
  263. H.seriesTypes.pie.prototype.animate.call(this, init);
  264. }
  265. }
  266. });
  267. }(Highcharts));
  268. }));