GaugeSeries.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. import BaseSeries from '../Core/Series/Series.js';
  11. import H from '../Core/Globals.js';
  12. var noop = H.noop;
  13. import U from '../Core/Utilities.js';
  14. var clamp = U.clamp, isNumber = U.isNumber, merge = U.merge, pick = U.pick, pInt = U.pInt;
  15. import '../Core/Options.js';
  16. import '../Core/Series/Point.js';
  17. import '../Series/LineSeries.js';
  18. import '../Core/Interaction.js';
  19. var Series = H.Series, TrackerMixin = H.TrackerMixin;
  20. /**
  21. * Gauges are circular plots displaying one or more values with a dial pointing
  22. * to values along the perimeter.
  23. *
  24. * @sample highcharts/demo/gauge-speedometer/
  25. * Gauge chart
  26. *
  27. * @extends plotOptions.line
  28. * @excluding animationLimit, boostThreshold, colorAxis, colorKey,
  29. * connectEnds, connectNulls, cropThreshold, dashStyle, dragDrop,
  30. * findNearestPointBy, getExtremesFromAll, marker, negativeColor,
  31. * pointPlacement, shadow, softThreshold, stacking, states, step,
  32. * threshold, turboThreshold, xAxis, zoneAxis, zones, dataSorting,
  33. * boostBlending
  34. * @product highcharts
  35. * @requires highcharts-more
  36. * @optionparent plotOptions.gauge
  37. */
  38. BaseSeries.seriesType('gauge', 'line', {
  39. /**
  40. * When this option is `true`, the dial will wrap around the axes. For
  41. * instance, in a full-range gauge going from 0 to 360, a value of 400
  42. * will point to 40\. When `wrap` is `false`, the dial stops at 360.
  43. *
  44. * @see [overshoot](#plotOptions.gauge.overshoot)
  45. *
  46. * @type {boolean}
  47. * @default true
  48. * @since 3.0
  49. * @product highcharts
  50. * @apioption plotOptions.gauge.wrap
  51. */
  52. /**
  53. * Data labels for the gauge. For gauges, the data labels are enabled
  54. * by default and shown in a bordered box below the point.
  55. *
  56. * @since 2.3.0
  57. * @product highcharts
  58. */
  59. dataLabels: {
  60. borderColor: '#cccccc',
  61. borderRadius: 3,
  62. borderWidth: 1,
  63. crop: false,
  64. defer: false,
  65. enabled: true,
  66. verticalAlign: 'top',
  67. y: 15,
  68. zIndex: 2
  69. },
  70. /**
  71. * Options for the dial or arrow pointer of the gauge.
  72. *
  73. * In styled mode, the dial is styled with the
  74. * `.highcharts-gauge-series .highcharts-dial` rule.
  75. *
  76. * @sample {highcharts} highcharts/css/gauge/
  77. * Styled mode
  78. *
  79. * @type {*}
  80. * @since 2.3.0
  81. * @product highcharts
  82. */
  83. dial: {},
  84. /**
  85. * The length of the dial's base part, relative to the total radius
  86. * or length of the dial.
  87. *
  88. * @sample {highcharts} highcharts/plotoptions/gauge-dial/
  89. * Dial options demonstrated
  90. *
  91. * @type {string}
  92. * @default 70%
  93. * @since 2.3.0
  94. * @product highcharts
  95. * @apioption plotOptions.gauge.dial.baseLength
  96. */
  97. /**
  98. * The pixel width of the base of the gauge dial. The base is the part
  99. * closest to the pivot, defined by baseLength.
  100. *
  101. * @sample {highcharts} highcharts/plotoptions/gauge-dial/
  102. * Dial options demonstrated
  103. *
  104. * @type {number}
  105. * @default 3
  106. * @since 2.3.0
  107. * @product highcharts
  108. * @apioption plotOptions.gauge.dial.baseWidth
  109. */
  110. /**
  111. * The radius or length of the dial, in percentages relative to the
  112. * radius of the gauge itself.
  113. *
  114. * @sample {highcharts} highcharts/plotoptions/gauge-dial/
  115. * Dial options demonstrated
  116. *
  117. * @type {string}
  118. * @default 80%
  119. * @since 2.3.0
  120. * @product highcharts
  121. * @apioption plotOptions.gauge.dial.radius
  122. */
  123. /**
  124. * The length of the dial's rear end, the part that extends out on the
  125. * other side of the pivot. Relative to the dial's length.
  126. *
  127. * @sample {highcharts} highcharts/plotoptions/gauge-dial/
  128. * Dial options demonstrated
  129. *
  130. * @type {string}
  131. * @default 10%
  132. * @since 2.3.0
  133. * @product highcharts
  134. * @apioption plotOptions.gauge.dial.rearLength
  135. */
  136. /**
  137. * The width of the top of the dial, closest to the perimeter. The pivot
  138. * narrows in from the base to the top.
  139. *
  140. * @sample {highcharts} highcharts/plotoptions/gauge-dial/
  141. * Dial options demonstrated
  142. *
  143. * @type {number}
  144. * @default 1
  145. * @since 2.3.0
  146. * @product highcharts
  147. * @apioption plotOptions.gauge.dial.topWidth
  148. */
  149. /**
  150. * The background or fill color of the gauge's dial.
  151. *
  152. * @sample {highcharts} highcharts/plotoptions/gauge-dial/
  153. * Dial options demonstrated
  154. *
  155. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  156. * @default #000000
  157. * @since 2.3.0
  158. * @product highcharts
  159. * @apioption plotOptions.gauge.dial.backgroundColor
  160. */
  161. /**
  162. * The border color or stroke of the gauge's dial. By default, the
  163. * borderWidth is 0, so this must be set in addition to a custom border
  164. * color.
  165. *
  166. * @sample {highcharts} highcharts/plotoptions/gauge-dial/
  167. * Dial options demonstrated
  168. *
  169. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  170. * @default #cccccc
  171. * @since 2.3.0
  172. * @product highcharts
  173. * @apioption plotOptions.gauge.dial.borderColor
  174. */
  175. /**
  176. * The width of the gauge dial border in pixels.
  177. *
  178. * @sample {highcharts} highcharts/plotoptions/gauge-dial/
  179. * Dial options demonstrated
  180. *
  181. * @type {number}
  182. * @default 0
  183. * @since 2.3.0
  184. * @product highcharts
  185. * @apioption plotOptions.gauge.dial.borderWidth
  186. */
  187. /**
  188. * Allow the dial to overshoot the end of the perimeter axis by this
  189. * many degrees. Say if the gauge axis goes from 0 to 60, a value of
  190. * 100, or 1000, will show 5 degrees beyond the end of the axis when this
  191. * option is set to 5.
  192. *
  193. * @see [wrap](#plotOptions.gauge.wrap)
  194. *
  195. * @sample {highcharts} highcharts/plotoptions/gauge-overshoot/
  196. * Allow 5 degrees overshoot
  197. *
  198. * @type {number}
  199. * @since 3.0.10
  200. * @product highcharts
  201. * @apioption plotOptions.gauge.overshoot
  202. */
  203. /**
  204. * Options for the pivot or the center point of the gauge.
  205. *
  206. * In styled mode, the pivot is styled with the
  207. * `.highcharts-gauge-series .highcharts-pivot` rule.
  208. *
  209. * @sample {highcharts} highcharts/css/gauge/
  210. * Styled mode
  211. *
  212. * @type {*}
  213. * @since 2.3.0
  214. * @product highcharts
  215. */
  216. pivot: {},
  217. /**
  218. * The pixel radius of the pivot.
  219. *
  220. * @sample {highcharts} highcharts/plotoptions/gauge-pivot/
  221. * Pivot options demonstrated
  222. *
  223. * @type {number}
  224. * @default 5
  225. * @since 2.3.0
  226. * @product highcharts
  227. * @apioption plotOptions.gauge.pivot.radius
  228. */
  229. /**
  230. * The border or stroke width of the pivot.
  231. *
  232. * @sample {highcharts} highcharts/plotoptions/gauge-pivot/
  233. * Pivot options demonstrated
  234. *
  235. * @type {number}
  236. * @default 0
  237. * @since 2.3.0
  238. * @product highcharts
  239. * @apioption plotOptions.gauge.pivot.borderWidth
  240. */
  241. /**
  242. * The border or stroke color of the pivot. In able to change this,
  243. * the borderWidth must also be set to something other than the default
  244. * 0.
  245. *
  246. * @sample {highcharts} highcharts/plotoptions/gauge-pivot/
  247. * Pivot options demonstrated
  248. *
  249. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  250. * @default #cccccc
  251. * @since 2.3.0
  252. * @product highcharts
  253. * @apioption plotOptions.gauge.pivot.borderColor
  254. */
  255. /**
  256. * The background color or fill of the pivot.
  257. *
  258. * @sample {highcharts} highcharts/plotoptions/gauge-pivot/
  259. * Pivot options demonstrated
  260. *
  261. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  262. * @default #000000
  263. * @since 2.3.0
  264. * @product highcharts
  265. * @apioption plotOptions.gauge.pivot.backgroundColor
  266. */
  267. tooltip: {
  268. headerFormat: ''
  269. },
  270. /**
  271. * Whether to display this particular series or series type in the
  272. * legend. Defaults to false for gauge series.
  273. *
  274. * @since 2.3.0
  275. * @product highcharts
  276. */
  277. showInLegend: false
  278. // Prototype members
  279. }, {
  280. // chart.angular will be set to true when a gauge series is present,
  281. // and this will be used on the axes
  282. angular: true,
  283. directTouch: true,
  284. drawGraph: noop,
  285. fixedBox: true,
  286. forceDL: true,
  287. noSharedTooltip: true,
  288. trackerGroups: ['group', 'dataLabelsGroup'],
  289. /* eslint-disable valid-jsdoc */
  290. /**
  291. * Calculate paths etc
  292. * @private
  293. */
  294. translate: function () {
  295. var series = this, yAxis = series.yAxis, options = series.options, center = yAxis.center;
  296. series.generatePoints();
  297. series.points.forEach(function (point) {
  298. var dialOptions = merge(options.dial, point.dial), radius = ((pInt(pick(dialOptions.radius, '80%')) * center[2]) /
  299. 200), baseLength = ((pInt(pick(dialOptions.baseLength, '70%')) * radius) /
  300. 100), rearLength = ((pInt(pick(dialOptions.rearLength, '10%')) * radius) /
  301. 100), baseWidth = dialOptions.baseWidth || 3, topWidth = dialOptions.topWidth || 1, overshoot = options.overshoot, rotation = yAxis.startAngleRad + yAxis.translate(point.y, null, null, null, true);
  302. // Handle the wrap and overshoot options
  303. if (isNumber(overshoot) || options.wrap === false) {
  304. overshoot = isNumber(overshoot) ?
  305. (overshoot / 180 * Math.PI) : 0;
  306. rotation = clamp(rotation, yAxis.startAngleRad - overshoot, yAxis.endAngleRad + overshoot);
  307. }
  308. rotation = rotation * 180 / Math.PI;
  309. point.shapeType = 'path';
  310. var d = dialOptions.path || [
  311. ['M', -rearLength, -baseWidth / 2],
  312. ['L', baseLength, -baseWidth / 2],
  313. ['L', radius, -topWidth / 2],
  314. ['L', radius, topWidth / 2],
  315. ['L', baseLength, baseWidth / 2],
  316. ['L', -rearLength, baseWidth / 2],
  317. ['Z']
  318. ];
  319. point.shapeArgs = {
  320. d: d,
  321. translateX: center[0],
  322. translateY: center[1],
  323. rotation: rotation
  324. };
  325. // Positions for data label
  326. point.plotX = center[0];
  327. point.plotY = center[1];
  328. });
  329. },
  330. /**
  331. * Draw the points where each point is one needle
  332. * @private
  333. */
  334. drawPoints: function () {
  335. var series = this, chart = series.chart, center = series.yAxis.center, pivot = series.pivot, options = series.options, pivotOptions = options.pivot, renderer = chart.renderer;
  336. series.points.forEach(function (point) {
  337. var graphic = point.graphic, shapeArgs = point.shapeArgs, d = shapeArgs.d, dialOptions = merge(options.dial, point.dial); // #1233
  338. if (graphic) {
  339. graphic.animate(shapeArgs);
  340. shapeArgs.d = d; // animate alters it
  341. }
  342. else {
  343. point.graphic =
  344. renderer[point.shapeType](shapeArgs)
  345. .attr({
  346. // required by VML when animation is false
  347. rotation: shapeArgs.rotation,
  348. zIndex: 1
  349. })
  350. .addClass('highcharts-dial')
  351. .add(series.group);
  352. }
  353. // Presentational attributes
  354. if (!chart.styledMode) {
  355. point.graphic[graphic ? 'animate' : 'attr']({
  356. stroke: dialOptions.borderColor || 'none',
  357. 'stroke-width': dialOptions.borderWidth || 0,
  358. fill: dialOptions.backgroundColor ||
  359. '#000000'
  360. });
  361. }
  362. });
  363. // Add or move the pivot
  364. if (pivot) {
  365. pivot.animate({
  366. translateX: center[0],
  367. translateY: center[1]
  368. });
  369. }
  370. else {
  371. series.pivot =
  372. renderer.circle(0, 0, pick(pivotOptions.radius, 5))
  373. .attr({
  374. zIndex: 2
  375. })
  376. .addClass('highcharts-pivot')
  377. .translate(center[0], center[1])
  378. .add(series.group);
  379. // Presentational attributes
  380. if (!chart.styledMode) {
  381. series.pivot.attr({
  382. 'stroke-width': pivotOptions.borderWidth || 0,
  383. stroke: pivotOptions.borderColor ||
  384. '#cccccc',
  385. fill: pivotOptions.backgroundColor ||
  386. '#000000'
  387. });
  388. }
  389. }
  390. },
  391. /**
  392. * Animate the arrow up from startAngle
  393. * @private
  394. */
  395. animate: function (init) {
  396. var series = this;
  397. if (!init) {
  398. series.points.forEach(function (point) {
  399. var graphic = point.graphic;
  400. if (graphic) {
  401. // start value
  402. graphic.attr({
  403. rotation: series.yAxis.startAngleRad * 180 / Math.PI
  404. });
  405. // animate
  406. graphic.animate({
  407. rotation: point.shapeArgs.rotation
  408. }, series.options.animation);
  409. }
  410. });
  411. }
  412. },
  413. /**
  414. * @private
  415. */
  416. render: function () {
  417. this.group = this.plotGroup('group', 'series', this.visible ? 'visible' : 'hidden', this.options.zIndex, this.chart.seriesGroup);
  418. Series.prototype.render.call(this);
  419. this.group.clip(this.chart.clipRect);
  420. },
  421. /**
  422. * Extend the basic setData method by running processData and generatePoints
  423. * immediately, in order to access the points from the legend.
  424. * @private
  425. */
  426. setData: function (data, redraw) {
  427. Series.prototype.setData.call(this, data, false);
  428. this.processData();
  429. this.generatePoints();
  430. if (pick(redraw, true)) {
  431. this.chart.redraw();
  432. }
  433. },
  434. /**
  435. * Define hasData function for non-cartesian series.
  436. * Returns true if the series has points at all.
  437. * @private
  438. */
  439. hasData: function () {
  440. return !!this.points.length; // != 0
  441. },
  442. // If the tracking module is loaded, add the point tracker
  443. drawTracker: TrackerMixin && TrackerMixin.drawTrackerPoint
  444. /* eslint-enable valid-jsdoc */
  445. }, {
  446. // Point members
  447. /* eslint-disable valid-jsdoc */
  448. /**
  449. * Don't do any hover colors or anything
  450. * @private
  451. */
  452. setState: function (state) {
  453. this.state = state;
  454. }
  455. /* eslint-enable valid-jsdoc */
  456. });
  457. /**
  458. * A `gauge` series. If the [type](#series.gauge.type) option is not
  459. * specified, it is inherited from [chart.type](#chart.type).
  460. *
  461. * @extends series,plotOptions.gauge
  462. * @excluding animationLimit, boostThreshold, connectEnds, connectNulls,
  463. * cropThreshold, dashStyle, dataParser, dataURL, findNearestPointBy,
  464. * getExtremesFromAll, marker, negativeColor, pointPlacement, shadow,
  465. * softThreshold, stack, stacking, states, step, threshold,
  466. * turboThreshold, zoneAxis, zones, dataSorting, boostBlending
  467. * @product highcharts
  468. * @requires highcharts-more
  469. * @apioption series.gauge
  470. */
  471. /**
  472. * An array of data points for the series. For the `gauge` series type,
  473. * points can be given in the following ways:
  474. *
  475. * 1. An array of numerical values. In this case, the numerical values will be
  476. * interpreted as `y` options. Example:
  477. * ```js
  478. * data: [0, 5, 3, 5]
  479. * ```
  480. *
  481. * 2. An array of objects with named values. The following snippet shows only a
  482. * few settings, see the complete options set below. If the total number of
  483. * data points exceeds the series'
  484. * [turboThreshold](#series.gauge.turboThreshold), this option is not
  485. * available.
  486. * ```js
  487. * data: [{
  488. * y: 6,
  489. * name: "Point2",
  490. * color: "#00FF00"
  491. * }, {
  492. * y: 8,
  493. * name: "Point1",
  494. * color: "#FF00FF"
  495. * }]
  496. * ```
  497. *
  498. * The typical gauge only contains a single data value.
  499. *
  500. * @sample {highcharts} highcharts/chart/reflow-true/
  501. * Numerical values
  502. * @sample {highcharts} highcharts/series/data-array-of-objects/
  503. * Config objects
  504. *
  505. * @type {Array<number|null|*>}
  506. * @extends series.line.data
  507. * @excluding drilldown, marker, x
  508. * @product highcharts
  509. * @apioption series.gauge.data
  510. */
  511. ''; // adds the doclets above in the transpiled file