7b2fb2727032235fe221c21cf632320faf034dbc.svn-base 7.0 KB

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