WindbarbSeries.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* *
  2. *
  3. * Wind barb series 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 A from '../Core/Animation/AnimationUtilities.js';
  14. var animObject = A.animObject;
  15. import BaseSeries from '../Core/Series/Series.js';
  16. import H from '../Core/Globals.js';
  17. var noop = H.noop;
  18. import OnSeriesMixin from '../Mixins/OnSeries.js';
  19. import U from '../Core/Utilities.js';
  20. var isNumber = U.isNumber, pick = U.pick;
  21. import './ColumnSeries.js';
  22. // eslint-disable-next-line valid-jsdoc
  23. /**
  24. * Once off, register the windbarb approximation for data grouping. This can be
  25. * called anywhere (not necessarily in the translate function), but must happen
  26. * after the data grouping module is loaded and before the wind barb series uses
  27. * it.
  28. * @private
  29. */
  30. function registerApproximation() {
  31. if (H.approximations && !H.approximations.windbarb) {
  32. H.approximations.windbarb = function (values, directions) {
  33. var vectorX = 0, vectorY = 0, i, len = values.length;
  34. for (i = 0; i < len; i++) {
  35. vectorX += values[i] * Math.cos(directions[i] * H.deg2rad);
  36. vectorY += values[i] * Math.sin(directions[i] * H.deg2rad);
  37. }
  38. return [
  39. // Wind speed
  40. values.reduce(function (sum, value) {
  41. return sum + value;
  42. }, 0) / values.length,
  43. // Wind direction
  44. Math.atan2(vectorY, vectorX) / H.deg2rad
  45. ];
  46. };
  47. }
  48. }
  49. registerApproximation();
  50. /**
  51. * @private
  52. * @class
  53. * @name Highcharts.seriesTypes.windbarb
  54. *
  55. * @augments Highcharts.Series
  56. */
  57. BaseSeries.seriesType('windbarb', 'column'
  58. /**
  59. * Wind barbs are a convenient way to represent wind speed and direction in
  60. * one graphical form. Wind direction is given by the stem direction, and
  61. * wind speed by the number and shape of barbs.
  62. *
  63. * @sample {highcharts|highstock} highcharts/demo/windbarb-series/
  64. * Wind barb series
  65. *
  66. * @extends plotOptions.column
  67. * @excluding boostThreshold, marker, connectEnds, connectNulls,
  68. * cropThreshold, dashStyle, dragDrop, gapSize, gapUnit,
  69. * linecap, shadow, stacking, step, boostBlending
  70. * @since 6.0.0
  71. * @product highcharts highstock
  72. * @requires modules/windbarb
  73. * @optionparent plotOptions.windbarb
  74. */
  75. , {
  76. /**
  77. * Data grouping options for the wind barbs. In Highcharts, this
  78. * requires the `modules/datagrouping.js` module to be loaded. In
  79. * Highstock, data grouping is included.
  80. *
  81. * @sample highcharts/plotoptions/windbarb-datagrouping
  82. * Wind barb with data grouping
  83. *
  84. * @since 7.1.0
  85. * @product highcharts highstock
  86. */
  87. dataGrouping: {
  88. /**
  89. * Whether to enable data grouping.
  90. *
  91. * @product highcharts highstock
  92. */
  93. enabled: true,
  94. /**
  95. * Approximation function for the data grouping. The default
  96. * returns an average of wind speed and a vector average direction
  97. * weighted by wind speed.
  98. *
  99. * @product highcharts highstock
  100. *
  101. * @type {string|Function}
  102. */
  103. approximation: 'windbarb',
  104. /**
  105. * The approximate data group width.
  106. *
  107. * @product highcharts highstock
  108. */
  109. groupPixelWidth: 30
  110. },
  111. /**
  112. * The line width of the wind barb symbols.
  113. */
  114. lineWidth: 2,
  115. /**
  116. * The id of another series in the chart that the wind barbs are
  117. * projected on. When `null`, the wind symbols are drawn on the X axis,
  118. * but offset up or down by the `yOffset` setting.
  119. *
  120. * @sample {highcharts|highstock} highcharts/plotoptions/windbarb-onseries
  121. * Projected on area series
  122. *
  123. * @type {string|null}
  124. */
  125. onSeries: null,
  126. states: {
  127. hover: {
  128. lineWidthPlus: 0
  129. }
  130. },
  131. tooltip: {
  132. /**
  133. * The default point format for the wind barb tooltip. Note the
  134. * `point.beaufort` property that refers to the Beaufort wind scale.
  135. * The names can be internationalized by modifying
  136. * `Highcharts.seriesTypes.windbarb.prototype.beaufortNames`.
  137. */
  138. pointFormat: '<span style="color:{point.color}">\u25CF</span> {series.name}: <b>{point.value}</b> ({point.beaufort})<br/>'
  139. },
  140. /**
  141. * Pixel length of the stems.
  142. */
  143. vectorLength: 20,
  144. /**
  145. * @default value
  146. */
  147. colorKey: 'value',
  148. /**
  149. * Vertical offset from the cartesian position, in pixels. The default
  150. * value makes sure the symbols don't overlap the X axis when `onSeries`
  151. * is `null`, and that they don't overlap the linked series when
  152. * `onSeries` is given.
  153. */
  154. yOffset: -20,
  155. /**
  156. * Horizontal offset from the cartesian position, in pixels. When the
  157. * chart is inverted, this option allows translation like
  158. * [yOffset](#plotOptions.windbarb.yOffset) in non inverted charts.
  159. *
  160. * @since 6.1.0
  161. */
  162. xOffset: 0
  163. }, {
  164. pointArrayMap: ['value', 'direction'],
  165. parallelArrays: ['x', 'value', 'direction'],
  166. beaufortName: ['Calm', 'Light air', 'Light breeze',
  167. 'Gentle breeze', 'Moderate breeze', 'Fresh breeze',
  168. 'Strong breeze', 'Near gale', 'Gale', 'Strong gale', 'Storm',
  169. 'Violent storm', 'Hurricane'],
  170. beaufortFloor: [0, 0.3, 1.6, 3.4, 5.5, 8.0, 10.8, 13.9, 17.2, 20.8,
  171. 24.5, 28.5, 32.7],
  172. trackerGroups: ['markerGroup'],
  173. init: function (chart, options) {
  174. registerApproximation();
  175. H.Series.prototype.init.call(this, chart, options);
  176. },
  177. // Get presentational attributes.
  178. pointAttribs: function (point, state) {
  179. var options = this.options, stroke = point.color || this.color, strokeWidth = this.options.lineWidth;
  180. if (state) {
  181. stroke = options.states[state].color || stroke;
  182. strokeWidth =
  183. (options.states[state].lineWidth || strokeWidth) +
  184. (options.states[state].lineWidthPlus || 0);
  185. }
  186. return {
  187. 'stroke': stroke,
  188. 'stroke-width': strokeWidth
  189. };
  190. },
  191. markerAttribs: function () {
  192. return;
  193. },
  194. getPlotBox: OnSeriesMixin.getPlotBox,
  195. // Create a single wind arrow. It is later rotated around the zero
  196. // centerpoint.
  197. windArrow: function (point) {
  198. var knots = point.value * 1.943844, level = point.beaufortLevel, path, barbs, u = this.options.vectorLength / 20, pos = -10;
  199. if (point.isNull) {
  200. return [];
  201. }
  202. if (level === 0) {
  203. return this.chart.renderer.symbols.circle(-10 * u, -10 * u, 20 * u, 20 * u);
  204. }
  205. // The stem and the arrow head
  206. path = [
  207. ['M', 0, 7 * u],
  208. ['L', -1.5 * u, 7 * u],
  209. ['L', 0, 10 * u],
  210. ['L', 1.5 * u, 7 * u],
  211. ['L', 0, 7 * u],
  212. ['L', 0, -10 * u] // top
  213. ];
  214. // For each full 50 knots, add a pennant
  215. barbs = (knots - knots % 50) / 50; // pennants
  216. if (barbs > 0) {
  217. while (barbs--) {
  218. path.push(pos === -10 ? ['L', 0, pos * u] : ['M', 0, pos * u], ['L', 5 * u, pos * u + 2], ['L', 0, pos * u + 4]);
  219. // Substract from the rest and move position for next
  220. knots -= 50;
  221. pos += 7;
  222. }
  223. }
  224. // For each full 10 knots, add a full barb
  225. barbs = (knots - knots % 10) / 10;
  226. if (barbs > 0) {
  227. while (barbs--) {
  228. path.push(pos === -10 ? ['L', 0, pos * u] : ['M', 0, pos * u], ['L', 7 * u, pos * u]);
  229. knots -= 10;
  230. pos += 3;
  231. }
  232. }
  233. // For each full 5 knots, add a half barb
  234. barbs = (knots - knots % 5) / 5; // half barbs
  235. if (barbs > 0) {
  236. while (barbs--) {
  237. path.push(pos === -10 ? ['L', 0, pos * u] : ['M', 0, pos * u], ['L', 4 * u, pos * u]);
  238. knots -= 5;
  239. pos += 3;
  240. }
  241. }
  242. return path;
  243. },
  244. translate: function () {
  245. var beaufortFloor = this.beaufortFloor, beaufortName = this.beaufortName;
  246. OnSeriesMixin.translate.call(this);
  247. this.points.forEach(function (point) {
  248. var level = 0;
  249. // Find the beaufort level (zero based)
  250. for (; level < beaufortFloor.length; level++) {
  251. if (beaufortFloor[level] > point.value) {
  252. break;
  253. }
  254. }
  255. point.beaufortLevel = level - 1;
  256. point.beaufort = beaufortName[level - 1];
  257. });
  258. },
  259. drawPoints: function () {
  260. var chart = this.chart, yAxis = this.yAxis, inverted = chart.inverted, shapeOffset = this.options.vectorLength / 2;
  261. this.points.forEach(function (point) {
  262. var plotX = point.plotX, plotY = point.plotY;
  263. // Check if it's inside the plot area, but only for the X
  264. // dimension.
  265. if (this.options.clip === false ||
  266. chart.isInsidePlot(plotX, 0, false)) {
  267. // Create the graphic the first time
  268. if (!point.graphic) {
  269. point.graphic = this.chart.renderer
  270. .path()
  271. .add(this.markerGroup)
  272. .addClass('highcharts-point ' +
  273. 'highcharts-color-' +
  274. pick(point.colorIndex, point.series.colorIndex));
  275. }
  276. // Position the graphic
  277. point.graphic
  278. .attr({
  279. d: this.windArrow(point),
  280. translateX: plotX + this.options.xOffset,
  281. translateY: plotY + this.options.yOffset,
  282. rotation: point.direction
  283. });
  284. if (!this.chart.styledMode) {
  285. point.graphic
  286. .attr(this.pointAttribs(point));
  287. }
  288. }
  289. else if (point.graphic) {
  290. point.graphic = point.graphic.destroy();
  291. }
  292. // Set the tooltip anchor position
  293. point.tooltipPos = [
  294. plotX + this.options.xOffset +
  295. (inverted && !this.onSeries ? shapeOffset : 0),
  296. plotY + this.options.yOffset -
  297. (inverted ?
  298. 0 :
  299. shapeOffset + yAxis.pos - chart.plotTop)
  300. ]; // #6327
  301. }, this);
  302. },
  303. // Fade in the arrows on initializing series.
  304. animate: function (init) {
  305. if (init) {
  306. this.markerGroup.attr({
  307. opacity: 0.01
  308. });
  309. }
  310. else {
  311. this.markerGroup.animate({
  312. opacity: 1
  313. }, animObject(this.options.animation));
  314. }
  315. },
  316. // Don't invert the marker group (#4960)
  317. invertGroups: noop,
  318. // No data extremes for the Y axis
  319. getExtremes: function () { return ({}); }
  320. }, {
  321. isValid: function () {
  322. return isNumber(this.value) && this.value >= 0;
  323. }
  324. });
  325. /**
  326. * A `windbarb` series. If the [type](#series.windbarb.type) option is not
  327. * specified, it is inherited from [chart.type](#chart.type).
  328. *
  329. * @extends series,plotOptions.windbarb
  330. * @excluding dataParser, dataURL, boostThreshold, boostBlending
  331. * @product highcharts highstock
  332. * @requires modules/windbarb
  333. * @apioption series.windbarb
  334. */
  335. /**
  336. * An array of data points for the series. For the `windbarb` series type,
  337. * points can be given in the following ways:
  338. *
  339. * 1. An array of arrays with 3 values. In this case, the values correspond to
  340. * `x,value,direction`. If the first value is a string, it is applied as the
  341. * name of the point, and the `x` value is inferred.
  342. * ```js
  343. * data: [
  344. * [Date.UTC(2017, 0, 1, 0), 3.3, 90],
  345. * [Date.UTC(2017, 0, 1, 1), 12.1, 180],
  346. * [Date.UTC(2017, 0, 1, 2), 11.1, 270]
  347. * ]
  348. * ```
  349. *
  350. * 2. An array of objects with named values. The following snippet shows only a
  351. * few settings, see the complete options set below. If the total number of
  352. * data points exceeds the series'
  353. * [turboThreshold](#series.area.turboThreshold), this option is not
  354. * available.
  355. * ```js
  356. * data: [{
  357. * x: Date.UTC(2017, 0, 1, 0),
  358. * value: 12.1,
  359. * direction: 90
  360. * }, {
  361. * x: Date.UTC(2017, 0, 1, 1),
  362. * value: 11.1,
  363. * direction: 270
  364. * }]
  365. * ```
  366. *
  367. * @sample {highcharts} highcharts/chart/reflow-true/
  368. * Numerical values
  369. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  370. * Arrays of numeric x and y
  371. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  372. * Arrays of datetime x and y
  373. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  374. * Arrays of point.name and y
  375. * @sample {highcharts} highcharts/series/data-array-of-objects/
  376. * Config objects
  377. *
  378. * @type {Array<Array<(number|string),number,number>|*>}
  379. * @extends series.line.data
  380. * @product highcharts highstock
  381. * @apioption series.windbarb.data
  382. */
  383. /**
  384. * The wind speed in meters per second.
  385. *
  386. * @type {number|null}
  387. * @product highcharts highstock
  388. * @apioption series.windbarb.data.value
  389. */
  390. /**
  391. * The wind direction in degrees, where 0 is north (pointing towards south).
  392. *
  393. * @type {number}
  394. * @product highcharts highstock
  395. * @apioption series.windbarb.data.direction
  396. */
  397. ''; // adds doclets above to transpiled file