solid-gauge.src.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * @license Highcharts JS v4.0.3 (2014-07-03)
  3. * Solid angular gauge module
  4. *
  5. * (c) 2010-2014 Torstein Honsi
  6. *
  7. * License: www.highcharts.com/license
  8. */
  9. /*global Highcharts*/
  10. (function (H) {
  11. "use strict";
  12. var defaultPlotOptions = H.getOptions().plotOptions,
  13. pInt = H.pInt,
  14. pick = H.pick,
  15. each = H.each,
  16. colorAxisMethods,
  17. UNDEFINED;
  18. // The default options
  19. defaultPlotOptions.solidgauge = H.merge(defaultPlotOptions.gauge, {
  20. colorByPoint: true
  21. });
  22. // These methods are defined in the ColorAxis object, and copied here.
  23. // If we implement an AMD system we should make ColorAxis a dependency.
  24. colorAxisMethods = {
  25. initDataClasses: function (userOptions) {
  26. var axis = this,
  27. chart = this.chart,
  28. dataClasses,
  29. colorCounter = 0,
  30. options = this.options;
  31. this.dataClasses = dataClasses = [];
  32. each(userOptions.dataClasses, function (dataClass, i) {
  33. var colors;
  34. dataClass = H.merge(dataClass);
  35. dataClasses.push(dataClass);
  36. if (!dataClass.color) {
  37. if (options.dataClassColor === 'category') {
  38. colors = chart.options.colors;
  39. dataClass.color = colors[colorCounter++];
  40. // loop back to zero
  41. if (colorCounter === colors.length) {
  42. colorCounter = 0;
  43. }
  44. } else {
  45. dataClass.color = axis.tweenColors(H.Color(options.minColor), H.Color(options.maxColor), i / (userOptions.dataClasses.length - 1));
  46. }
  47. }
  48. });
  49. },
  50. initStops: function (userOptions) {
  51. this.stops = userOptions.stops || [
  52. [0, this.options.minColor],
  53. [1, this.options.maxColor]
  54. ];
  55. each(this.stops, function (stop) {
  56. stop.color = H.Color(stop[1]);
  57. });
  58. },
  59. /**
  60. * Translate from a value to a color
  61. */
  62. toColor: function (value, point) {
  63. var pos,
  64. stops = this.stops,
  65. from,
  66. to,
  67. color,
  68. dataClasses = this.dataClasses,
  69. dataClass,
  70. i;
  71. if (dataClasses) {
  72. i = dataClasses.length;
  73. while (i--) {
  74. dataClass = dataClasses[i];
  75. from = dataClass.from;
  76. to = dataClass.to;
  77. if ((from === UNDEFINED || value >= from) && (to === UNDEFINED || value <= to)) {
  78. color = dataClass.color;
  79. if (point) {
  80. point.dataClass = i;
  81. }
  82. break;
  83. }
  84. }
  85. } else {
  86. if (this.isLog) {
  87. value = this.val2lin(value);
  88. }
  89. pos = 1 - ((this.max - value) / (this.max - this.min));
  90. i = stops.length;
  91. while (i--) {
  92. if (pos > stops[i][0]) {
  93. break;
  94. }
  95. }
  96. from = stops[i] || stops[i + 1];
  97. to = stops[i + 1] || from;
  98. // The position within the gradient
  99. pos = 1 - (to[0] - pos) / ((to[0] - from[0]) || 1);
  100. color = this.tweenColors(
  101. from.color,
  102. to.color,
  103. pos
  104. );
  105. }
  106. return color;
  107. },
  108. tweenColors: function (from, to, pos) {
  109. // Check for has alpha, because rgba colors perform worse due to lack of
  110. // support in WebKit.
  111. var hasAlpha = (to.rgba[3] !== 1 || from.rgba[3] !== 1);
  112. if (from.rgba.length === 0 || to.rgba.length === 0) {
  113. return 'none';
  114. }
  115. return (hasAlpha ? 'rgba(' : 'rgb(') +
  116. Math.round(to.rgba[0] + (from.rgba[0] - to.rgba[0]) * (1 - pos)) + ',' +
  117. Math.round(to.rgba[1] + (from.rgba[1] - to.rgba[1]) * (1 - pos)) + ',' +
  118. Math.round(to.rgba[2] + (from.rgba[2] - to.rgba[2]) * (1 - pos)) +
  119. (hasAlpha ? (',' + (to.rgba[3] + (from.rgba[3] - to.rgba[3]) * (1 - pos))) : '') + ')';
  120. }
  121. };
  122. // The series prototype
  123. H.seriesTypes.solidgauge = H.extendClass(H.seriesTypes.gauge, {
  124. type: 'solidgauge',
  125. bindAxes: function () {
  126. var axis;
  127. H.seriesTypes.gauge.prototype.bindAxes.call(this);
  128. axis = this.yAxis;
  129. H.extend(axis, colorAxisMethods);
  130. // Prepare data classes
  131. if (axis.options.dataClasses) {
  132. axis.initDataClasses(axis.options);
  133. }
  134. axis.initStops(axis.options);
  135. },
  136. /**
  137. * Draw the points where each point is one needle
  138. */
  139. drawPoints: function () {
  140. var series = this,
  141. yAxis = series.yAxis,
  142. center = yAxis.center,
  143. options = series.options,
  144. renderer = series.chart.renderer;
  145. H.each(series.points, function (point) {
  146. var graphic = point.graphic,
  147. rotation = yAxis.startAngleRad + yAxis.translate(point.y, null, null, null, true),
  148. radius = (pInt(pick(options.radius, 100)) * center[2]) / 200,
  149. innerRadius = (pInt(pick(options.innerRadius, 60)) * center[2]) / 200,
  150. shapeArgs,
  151. d,
  152. toColor = yAxis.toColor(point.y, point),
  153. fromColor;
  154. if (toColor !== 'none') {
  155. fromColor = point.color;
  156. point.color = toColor;
  157. }
  158. // Handle the wrap option
  159. if (options.wrap === false) {
  160. rotation = Math.max(yAxis.startAngleRad, Math.min(yAxis.endAngleRad, rotation));
  161. }
  162. rotation = rotation * 180 / Math.PI;
  163. var angle1 = rotation / (180 / Math.PI),
  164. angle2 = yAxis.startAngleRad,
  165. minAngle = Math.min(angle1, angle2),
  166. maxAngle = Math.max(angle1, angle2);
  167. if (maxAngle - minAngle > 2 * Math.PI) {
  168. maxAngle = minAngle + 2 * Math.PI;
  169. }
  170. shapeArgs = {
  171. x: center[0],
  172. y: center[1],
  173. r: radius,
  174. innerR: innerRadius,
  175. start: minAngle,
  176. end: maxAngle
  177. };
  178. if (graphic) {
  179. d = shapeArgs.d;
  180. /*jslint unparam: true*/
  181. graphic.attr({
  182. fill: point.color
  183. }).animate(shapeArgs, {
  184. step: function (value, fx) {
  185. graphic.attr('fill', colorAxisMethods.tweenColors(H.Color(fromColor), H.Color(toColor), fx.pos));
  186. }
  187. });
  188. /*jslint unparam: false*/
  189. shapeArgs.d = d; // animate alters it
  190. } else {
  191. point.graphic = renderer.arc(shapeArgs)
  192. .attr({
  193. stroke: options.borderColor || 'none',
  194. 'stroke-width': options.borderWidth || 0,
  195. fill: point.color,
  196. 'sweep-flag': 0
  197. })
  198. .add(series.group);
  199. }
  200. });
  201. },
  202. animate: null
  203. });
  204. }(Highcharts));