solid-gauge.src.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* *
  2. * Solid angular gauge module
  3. *
  4. * (c) 2010-2019 Torstein Honsi
  5. *
  6. * License: www.highcharts.com/license
  7. */
  8. /**
  9. * Additional options, depending on the actual symbol drawn.
  10. *
  11. * @interface Highcharts.SymbolOptionsObject
  12. *//**
  13. * Whether to draw rounded edges.
  14. * @name Highcharts.SymbolOptionsObject#rounded
  15. * @type {boolean|undefined}
  16. */
  17. 'use strict';
  18. import H from '../parts/Globals.js';
  19. import '../parts/Utilities.js';
  20. import '../parts/Options.js';
  21. import '../parts-more/GaugeSeries.js';
  22. var pInt = H.pInt,
  23. pick = H.pick,
  24. isNumber = H.isNumber,
  25. wrap = H.wrap,
  26. Renderer = H.Renderer,
  27. colorAxisMethods;
  28. /**
  29. * Symbol definition of an arc with round edges.
  30. *
  31. * @private
  32. * @function Highcharts.Renderer#symbols.arc
  33. *
  34. * @param {number} x
  35. * The X coordinate for the top left position.
  36. *
  37. * @param {number} y
  38. * The Y coordinate for the top left position.
  39. *
  40. * @param {number} w
  41. * The pixel width.
  42. *
  43. * @param {number} h
  44. * The pixel height.
  45. *
  46. * @param {Highcharts.SymbolOptionsObject} [options]
  47. * Additional options, depending on the actual symbol drawn.
  48. *
  49. * @return {Highcharts.SVGPathArray}
  50. * Path of the created arc.
  51. */
  52. wrap(
  53. Renderer.prototype.symbols,
  54. 'arc',
  55. function (proceed, x, y, w, h, options) {
  56. var arc = proceed,
  57. path = arc(x, y, w, h, options);
  58. if (options.rounded) {
  59. var r = options.r || w,
  60. smallR = (r - options.innerR) / 2,
  61. x1 = path[1],
  62. y1 = path[2],
  63. x2 = path[12],
  64. y2 = path[13],
  65. roundStart = ['A', smallR, smallR, 0, 1, 1, x1, y1],
  66. roundEnd = ['A', smallR, smallR, 0, 1, 1, x2, y2];
  67. // Insert rounded edge on end, and remove line.
  68. path.splice.apply(path, [path.length - 1, 0].concat(roundStart));
  69. // Insert rounded edge on end, and remove line.
  70. path.splice.apply(path, [11, 3].concat(roundEnd));
  71. }
  72. return path;
  73. }
  74. );
  75. // These methods are defined in the ColorAxis object, and copied here.
  76. // If we implement an AMD system we should make ColorAxis a dependency.
  77. colorAxisMethods = {
  78. initDataClasses: function (userOptions) {
  79. var chart = this.chart,
  80. dataClasses,
  81. colorCounter = 0,
  82. options = this.options;
  83. this.dataClasses = dataClasses = [];
  84. userOptions.dataClasses.forEach(function (dataClass, i) {
  85. var colors;
  86. dataClass = H.merge(dataClass);
  87. dataClasses.push(dataClass);
  88. if (!dataClass.color) {
  89. if (options.dataClassColor === 'category') {
  90. colors = chart.options.colors;
  91. dataClass.color = colors[colorCounter++];
  92. // loop back to zero
  93. if (colorCounter === colors.length) {
  94. colorCounter = 0;
  95. }
  96. } else {
  97. dataClass.color = H.color(options.minColor).tweenTo(
  98. H.color(options.maxColor),
  99. i / (userOptions.dataClasses.length - 1)
  100. );
  101. }
  102. }
  103. });
  104. },
  105. initStops: function (userOptions) {
  106. this.stops = userOptions.stops || [
  107. [0, this.options.minColor],
  108. [1, this.options.maxColor]
  109. ];
  110. this.stops.forEach(function (stop) {
  111. stop.color = H.color(stop[1]);
  112. });
  113. },
  114. // Translate from a value to a color
  115. toColor: function (value, point) {
  116. var pos,
  117. stops = this.stops,
  118. from,
  119. to,
  120. color,
  121. dataClasses = this.dataClasses,
  122. dataClass,
  123. i;
  124. if (dataClasses) {
  125. i = dataClasses.length;
  126. while (i--) {
  127. dataClass = dataClasses[i];
  128. from = dataClass.from;
  129. to = dataClass.to;
  130. if (
  131. (from === undefined || value >= from) &&
  132. (to === undefined || value <= to)
  133. ) {
  134. color = dataClass.color;
  135. if (point) {
  136. point.dataClass = i;
  137. }
  138. break;
  139. }
  140. }
  141. } else {
  142. if (this.isLog) {
  143. value = this.val2lin(value);
  144. }
  145. pos = 1 - ((this.max - value) / (this.max - this.min));
  146. i = stops.length;
  147. while (i--) {
  148. if (pos > stops[i][0]) {
  149. break;
  150. }
  151. }
  152. from = stops[i] || stops[i + 1];
  153. to = stops[i + 1] || from;
  154. // The position within the gradient
  155. pos = 1 - (to[0] - pos) / ((to[0] - from[0]) || 1);
  156. color = from.color.tweenTo(
  157. to.color,
  158. pos
  159. );
  160. }
  161. return color;
  162. }
  163. };
  164. /**
  165. * A solid gauge is a circular gauge where the value is indicated by a filled
  166. * arc, and the color of the arc may variate with the value.
  167. *
  168. * @sample highcharts/demo/gauge-solid/
  169. * Solid gauges
  170. *
  171. * @extends plotOptions.gauge
  172. * @excluding dial, pivot, wrap
  173. * @product highcharts
  174. * @optionparent plotOptions.solidgauge
  175. */
  176. var solidGaugeOptions = {
  177. /**
  178. * Whether the strokes of the solid gauge should be `round` or `square`.
  179. *
  180. * @sample {highcharts} highcharts/demo/gauge-activity/
  181. * Rounded gauge
  182. *
  183. * @type {string}
  184. * @default round
  185. * @since 4.2.2
  186. * @product highcharts
  187. * @validvalue ["square", "round"]
  188. * @apioption plotOptions.solidgauge.linecap
  189. */
  190. /**
  191. * Allow the gauge to overshoot the end of the perimeter axis by this
  192. * many degrees. Say if the gauge axis goes from 0 to 60, a value of
  193. * 100, or 1000, will show 5 degrees beyond the end of the axis when this
  194. * option is set to 5.
  195. *
  196. * @type {number}
  197. * @default 0
  198. * @since 3.0.10
  199. * @product highcharts
  200. * @apioption plotOptions.solidgauge.overshoot
  201. */
  202. /**
  203. * Wether to draw rounded edges on the gauge.
  204. *
  205. * @sample {highcharts} highcharts/demo/gauge-activity/
  206. * Activity Gauge
  207. *
  208. * @type {boolean}
  209. * @default false
  210. * @since 5.0.8
  211. * @product highcharts
  212. * @apioption plotOptions.solidgauge.rounded
  213. */
  214. /**
  215. * The threshold or base level for the gauge.
  216. *
  217. * @sample {highcharts} highcharts/plotoptions/solidgauge-threshold/
  218. * Zero threshold with negative and positive values
  219. *
  220. * @type {number}
  221. * @since 5.0.3
  222. * @product highcharts
  223. * @apioption plotOptions.solidgauge.threshold
  224. */
  225. /**
  226. * Whether to give each point an individual color.
  227. */
  228. colorByPoint: true
  229. };
  230. // The solidgauge series type
  231. H.seriesType('solidgauge', 'gauge', solidGaugeOptions, {
  232. // Extend the translate function to extend the Y axis with the necessary
  233. // decoration (#5895).
  234. translate: function () {
  235. var axis = this.yAxis;
  236. H.extend(axis, colorAxisMethods);
  237. // Prepare data classes
  238. if (!axis.dataClasses && axis.options.dataClasses) {
  239. axis.initDataClasses(axis.options);
  240. }
  241. axis.initStops(axis.options);
  242. // Generate points and inherit data label position
  243. H.seriesTypes.gauge.prototype.translate.call(this);
  244. },
  245. // Draw the points where each point is one needle.
  246. drawPoints: function () {
  247. var series = this,
  248. yAxis = series.yAxis,
  249. center = yAxis.center,
  250. options = series.options,
  251. renderer = series.chart.renderer,
  252. overshoot = options.overshoot,
  253. overshootVal = isNumber(overshoot) ? overshoot / 180 * Math.PI : 0,
  254. thresholdAngleRad;
  255. // Handle the threshold option
  256. if (isNumber(options.threshold)) {
  257. thresholdAngleRad = yAxis.startAngleRad + yAxis.translate(
  258. options.threshold,
  259. null,
  260. null,
  261. null,
  262. true
  263. );
  264. }
  265. this.thresholdAngleRad = pick(thresholdAngleRad, yAxis.startAngleRad);
  266. series.points.forEach(function (point) {
  267. var graphic = point.graphic,
  268. rotation = yAxis.startAngleRad +
  269. yAxis.translate(point.y, null, null, null, true),
  270. radius = (
  271. pInt(
  272. pick(point.options.radius, options.radius, 100)
  273. ) * center[2]
  274. ) / 200,
  275. innerRadius = (
  276. pInt(
  277. pick(point.options.innerRadius, options.innerRadius, 60)
  278. ) * center[2]
  279. ) / 200,
  280. shapeArgs,
  281. d,
  282. toColor = yAxis.toColor(point.y, point),
  283. axisMinAngle = Math.min(yAxis.startAngleRad, yAxis.endAngleRad),
  284. axisMaxAngle = Math.max(yAxis.startAngleRad, yAxis.endAngleRad),
  285. minAngle,
  286. maxAngle;
  287. if (toColor === 'none') { // #3708
  288. toColor = point.color || series.color || 'none';
  289. }
  290. if (toColor !== 'none') {
  291. point.color = toColor;
  292. }
  293. // Handle overshoot and clipping to axis max/min
  294. rotation = Math.max(
  295. axisMinAngle - overshootVal,
  296. Math.min(axisMaxAngle + overshootVal, rotation)
  297. );
  298. // Handle the wrap option
  299. if (options.wrap === false) {
  300. rotation = Math.max(
  301. axisMinAngle,
  302. Math.min(axisMaxAngle, rotation)
  303. );
  304. }
  305. minAngle = Math.min(rotation, series.thresholdAngleRad);
  306. maxAngle = Math.max(rotation, series.thresholdAngleRad);
  307. if (maxAngle - minAngle > 2 * Math.PI) {
  308. maxAngle = minAngle + 2 * Math.PI;
  309. }
  310. point.shapeArgs = shapeArgs = {
  311. x: center[0],
  312. y: center[1],
  313. r: radius,
  314. innerR: innerRadius,
  315. start: minAngle,
  316. end: maxAngle,
  317. rounded: options.rounded
  318. };
  319. point.startR = radius; // For PieSeries.animate
  320. if (graphic) {
  321. d = shapeArgs.d;
  322. graphic.animate(H.extend({ fill: toColor }, shapeArgs));
  323. if (d) {
  324. shapeArgs.d = d; // animate alters it
  325. }
  326. } else {
  327. point.graphic = graphic = renderer.arc(shapeArgs)
  328. .attr({
  329. fill: toColor,
  330. 'sweep-flag': 0
  331. })
  332. .add(series.group);
  333. if (!series.chart.styledMode) {
  334. if (options.linecap !== 'square') {
  335. graphic.attr({
  336. 'stroke-linecap': 'round',
  337. 'stroke-linejoin': 'round'
  338. });
  339. }
  340. graphic.attr({
  341. stroke: options.borderColor || 'none',
  342. 'stroke-width': options.borderWidth || 0
  343. });
  344. }
  345. }
  346. if (graphic) {
  347. graphic.addClass(point.getClassName(), true);
  348. }
  349. });
  350. },
  351. // Extend the pie slice animation by animating from start angle and up.
  352. animate: function (init) {
  353. if (!init) {
  354. this.startAngleRad = this.thresholdAngleRad;
  355. H.seriesTypes.pie.prototype.animate.call(this, init);
  356. }
  357. }
  358. });
  359. /**
  360. * A `solidgauge` series. If the [type](#series.solidgauge.type) option is not
  361. * specified, it is inherited from [chart.type](#chart.type).
  362. *
  363. *
  364. * @extends series,plotOptions.solidgauge
  365. * @excluding animationLimit, boostThreshold, connectEnds, connectNulls,
  366. * cropThreshold, dashStyle, dataParser, dataURL, dial,
  367. * findNearestPointBy, getExtremesFromAll, marker, negativeColor,
  368. * pointPlacement, pivot, shadow, softThreshold, stack, stacking,
  369. * states, step, threshold, turboThreshold, wrap, zoneAxis, zones
  370. * @product highcharts
  371. * @apioption series.solidgauge
  372. */
  373. /**
  374. * An array of data points for the series. For the `solidgauge` series
  375. * type, points can be given in the following ways:
  376. *
  377. * 1. An array of numerical values. In this case, the numerical values will be
  378. * interpreted as `y` options. Example:
  379. * ```js
  380. * data: [0, 5, 3, 5]
  381. * ```
  382. *
  383. * 2. An array of objects with named values. The following snippet shows only a
  384. * few settings, see the complete options set below. If the total number of
  385. * data points exceeds the series'
  386. * [turboThreshold](#series.solidgauge.turboThreshold), this option is not
  387. * available.
  388. * ```js
  389. * data: [{
  390. * y: 5,
  391. * name: "Point2",
  392. * color: "#00FF00"
  393. * }, {
  394. * y: 7,
  395. * name: "Point1",
  396. * color: "#FF00FF"
  397. * }]
  398. * ```
  399. *
  400. * The typical gauge only contains a single data value.
  401. *
  402. * @sample {highcharts} highcharts/chart/reflow-true/
  403. * Numerical values
  404. * @sample {highcharts} highcharts/series/data-array-of-objects/
  405. * Config objects
  406. *
  407. * @type {Array<number|*>}
  408. * @extends series.gauge.data
  409. * @product highcharts
  410. * @apioption series.solidgauge.data
  411. */
  412. /**
  413. * The inner radius of an individual point in a solid gauge. Can be given as a
  414. * number (pixels) or percentage string.
  415. *
  416. * @sample {highcharts} highcharts/plotoptions/solidgauge-radius/
  417. * Individual radius and innerRadius
  418. *
  419. * @type {number|string}
  420. * @since 4.1.6
  421. * @product highcharts
  422. * @apioption series.solidgauge.data.innerRadius
  423. */
  424. /**
  425. * The outer radius of an individual point in a solid gauge. Can be
  426. * given as a number (pixels) or percentage string.
  427. *
  428. * @sample {highcharts} highcharts/plotoptions/solidgauge-radius/
  429. * Individual radius and innerRadius
  430. *
  431. * @type {number|string}
  432. * @since 4.1.6
  433. * @product highcharts
  434. * @apioption series.solidgauge.data.radius
  435. */