solid-gauge.src.js 15 KB

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