SolidGaugeSeries.js 16 KB

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