windbarb.src.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /**
  2. * @license Highcharts JS v8.2.2 (2020-10-22)
  3. *
  4. * Wind barb series module
  5. *
  6. * (c) 2010-2019 Torstein Honsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/windbarb', ['highcharts'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Mixins/OnSeries.js', [_modules['Core/Globals.js'], _modules['Core/Utilities.js']], function (H, U) {
  32. /* *
  33. *
  34. * (c) 2010-2020 Torstein Honsi
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var defined = U.defined,
  42. stableSort = U.stableSort;
  43. var seriesTypes = H.seriesTypes;
  44. /**
  45. * @private
  46. * @mixin onSeriesMixin
  47. */
  48. var onSeriesMixin = {
  49. /* eslint-disable valid-jsdoc */
  50. /**
  51. * Override getPlotBox. If the onSeries option is valid,
  52. return the plot box
  53. * of the onSeries,
  54. otherwise proceed as usual.
  55. *
  56. * @private
  57. * @function onSeriesMixin.getPlotBox
  58. * @return {Highcharts.SeriesPlotBoxObject}
  59. */
  60. getPlotBox: function () {
  61. return H.Series.prototype.getPlotBox.call((this.options.onSeries &&
  62. this.chart.get(this.options.onSeries)) || this);
  63. },
  64. /**
  65. * Extend the translate method by placing the point on the related series
  66. *
  67. * @private
  68. * @function onSeriesMixin.translate
  69. * @return {void}
  70. */
  71. translate: function () {
  72. seriesTypes.column.prototype.translate.apply(this);
  73. var series = this,
  74. options = series.options,
  75. chart = series.chart,
  76. points = series.points,
  77. cursor = points.length - 1,
  78. point,
  79. lastPoint,
  80. optionsOnSeries = options.onSeries,
  81. onSeries = (optionsOnSeries &&
  82. chart.get(optionsOnSeries)),
  83. onKey = options.onKey || 'y',
  84. step = onSeries && onSeries.options.step,
  85. onData = (onSeries && onSeries.points),
  86. i = onData && onData.length,
  87. inverted = chart.inverted,
  88. xAxis = series.xAxis,
  89. yAxis = series.yAxis,
  90. xOffset = 0,
  91. leftPoint,
  92. lastX,
  93. rightPoint,
  94. currentDataGrouping,
  95. distanceRatio;
  96. // relate to a master series
  97. if (onSeries && onSeries.visible && i) {
  98. xOffset = (onSeries.pointXOffset || 0) + (onSeries.barW || 0) / 2;
  99. currentDataGrouping = onSeries.currentDataGrouping;
  100. lastX = (onData[i - 1].x +
  101. (currentDataGrouping ? currentDataGrouping.totalRange : 0)); // #2374
  102. // sort the data points
  103. stableSort(points, function (a, b) {
  104. return (a.x - b.x);
  105. });
  106. onKey = 'plot' + onKey[0].toUpperCase() + onKey.substr(1);
  107. while (i-- && points[cursor]) {
  108. leftPoint = onData[i];
  109. point = points[cursor];
  110. point.y = leftPoint.y;
  111. if (leftPoint.x <= point.x &&
  112. typeof leftPoint[onKey] !== 'undefined') {
  113. if (point.x <= lastX) { // #803
  114. point.plotY = leftPoint[onKey];
  115. // interpolate between points, #666
  116. if (leftPoint.x < point.x &&
  117. !step) {
  118. rightPoint = onData[i + 1];
  119. if (rightPoint &&
  120. typeof rightPoint[onKey] !== 'undefined') {
  121. // the distance ratio, between 0 and 1
  122. distanceRatio =
  123. (point.x - leftPoint.x) /
  124. (rightPoint.x - leftPoint.x);
  125. point.plotY +=
  126. distanceRatio *
  127. // the plotY distance
  128. (rightPoint[onKey] - leftPoint[onKey]);
  129. point.y +=
  130. distanceRatio *
  131. (rightPoint.y - leftPoint.y);
  132. }
  133. }
  134. }
  135. cursor--;
  136. i++; // check again for points in the same x position
  137. if (cursor < 0) {
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. // Add plotY position and handle stacking
  144. points.forEach(function (point, i) {
  145. var stackIndex;
  146. point.plotX += xOffset; // #2049
  147. // Undefined plotY means the point is either on axis, outside series
  148. // range or hidden series. If the series is outside the range of the
  149. // x axis it should fall through with an undefined plotY, but then
  150. // we must remove the shapeArgs (#847). For inverted charts, we need
  151. // to calculate position anyway, because series.invertGroups is not
  152. // defined
  153. if (typeof point.plotY === 'undefined' || inverted) {
  154. if (point.plotX >= 0 &&
  155. point.plotX <= xAxis.len) {
  156. // We're inside xAxis range
  157. if (inverted) {
  158. point.plotY = xAxis.translate(point.x, 0, 1, 0, 1);
  159. point.plotX = defined(point.y) ?
  160. yAxis.translate(point.y, 0, 0, 0, 1) :
  161. 0;
  162. }
  163. else {
  164. point.plotY = (xAxis.opposite ? 0 : series.yAxis.len) +
  165. xAxis.offset; // For the windbarb demo
  166. }
  167. }
  168. else {
  169. point.shapeArgs = {}; // 847
  170. }
  171. }
  172. // if multiple flags appear at the same x, order them into a stack
  173. lastPoint = points[i - 1];
  174. if (lastPoint && lastPoint.plotX === point.plotX) {
  175. if (typeof lastPoint.stackIndex === 'undefined') {
  176. lastPoint.stackIndex = 0;
  177. }
  178. stackIndex = lastPoint.stackIndex + 1;
  179. }
  180. point.stackIndex = stackIndex; // #3639
  181. });
  182. this.onSeries = onSeries;
  183. }
  184. /* eslint-enable valid-jsdoc */
  185. };
  186. return onSeriesMixin;
  187. });
  188. _registerModule(_modules, 'Series/WindbarbSeries.js', [_modules['Core/Animation/AnimationUtilities.js'], _modules['Core/Series/Series.js'], _modules['Core/Globals.js'], _modules['Mixins/OnSeries.js'], _modules['Core/Utilities.js']], function (A, BaseSeries, H, OnSeriesMixin, U) {
  189. /* *
  190. *
  191. * Wind barb series module
  192. *
  193. * (c) 2010-2020 Torstein Honsi
  194. *
  195. * License: www.highcharts.com/license
  196. *
  197. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  198. *
  199. * */
  200. var animObject = A.animObject;
  201. var noop = H.noop;
  202. var isNumber = U.isNumber,
  203. pick = U.pick;
  204. // eslint-disable-next-line valid-jsdoc
  205. /**
  206. * Once off, register the windbarb approximation for data grouping. This can be
  207. * called anywhere (not necessarily in the translate function), but must happen
  208. * after the data grouping module is loaded and before the wind barb series uses
  209. * it.
  210. * @private
  211. */
  212. function registerApproximation() {
  213. if (H.approximations && !H.approximations.windbarb) {
  214. H.approximations.windbarb = function (values, directions) {
  215. var vectorX = 0,
  216. vectorY = 0,
  217. i,
  218. len = values.length;
  219. for (i = 0; i < len; i++) {
  220. vectorX += values[i] * Math.cos(directions[i] * H.deg2rad);
  221. vectorY += values[i] * Math.sin(directions[i] * H.deg2rad);
  222. }
  223. return [
  224. // Wind speed
  225. values.reduce(function (sum, value) {
  226. return sum + value;
  227. }, 0) / values.length,
  228. // Wind direction
  229. Math.atan2(vectorY, vectorX) / H.deg2rad
  230. ];
  231. };
  232. }
  233. }
  234. registerApproximation();
  235. /**
  236. * @private
  237. * @class
  238. * @name Highcharts.seriesTypes.windbarb
  239. *
  240. * @augments Highcharts.Series
  241. */
  242. BaseSeries.seriesType('windbarb', 'column'
  243. /**
  244. * Wind barbs are a convenient way to represent wind speed and direction in
  245. * one graphical form. Wind direction is given by the stem direction, and
  246. * wind speed by the number and shape of barbs.
  247. *
  248. * @sample {highcharts|highstock} highcharts/demo/windbarb-series/
  249. * Wind barb series
  250. *
  251. * @extends plotOptions.column
  252. * @excluding boostThreshold, marker, connectEnds, connectNulls,
  253. * cropThreshold, dashStyle, dragDrop, gapSize, gapUnit,
  254. * linecap, shadow, stacking, step, boostBlending
  255. * @since 6.0.0
  256. * @product highcharts highstock
  257. * @requires modules/windbarb
  258. * @optionparent plotOptions.windbarb
  259. */
  260. , {
  261. /**
  262. * Data grouping options for the wind barbs. In Highcharts, this
  263. * requires the `modules/datagrouping.js` module to be loaded. In
  264. * Highstock, data grouping is included.
  265. *
  266. * @sample highcharts/plotoptions/windbarb-datagrouping
  267. * Wind barb with data grouping
  268. *
  269. * @since 7.1.0
  270. * @product highcharts highstock
  271. */
  272. dataGrouping: {
  273. /**
  274. * Whether to enable data grouping.
  275. *
  276. * @product highcharts highstock
  277. */
  278. enabled: true,
  279. /**
  280. * Approximation function for the data grouping. The default
  281. * returns an average of wind speed and a vector average direction
  282. * weighted by wind speed.
  283. *
  284. * @product highcharts highstock
  285. *
  286. * @type {string|Function}
  287. */
  288. approximation: 'windbarb',
  289. /**
  290. * The approximate data group width.
  291. *
  292. * @product highcharts highstock
  293. */
  294. groupPixelWidth: 30
  295. },
  296. /**
  297. * The line width of the wind barb symbols.
  298. */
  299. lineWidth: 2,
  300. /**
  301. * The id of another series in the chart that the wind barbs are
  302. * projected on. When `null`, the wind symbols are drawn on the X axis,
  303. * but offset up or down by the `yOffset` setting.
  304. *
  305. * @sample {highcharts|highstock} highcharts/plotoptions/windbarb-onseries
  306. * Projected on area series
  307. *
  308. * @type {string|null}
  309. */
  310. onSeries: null,
  311. states: {
  312. hover: {
  313. lineWidthPlus: 0
  314. }
  315. },
  316. tooltip: {
  317. /**
  318. * The default point format for the wind barb tooltip. Note the
  319. * `point.beaufort` property that refers to the Beaufort wind scale.
  320. * The names can be internationalized by modifying
  321. * `Highcharts.seriesTypes.windbarb.prototype.beaufortNames`.
  322. */
  323. pointFormat: '<span style="color:{point.color}">\u25CF</span> {series.name}: <b>{point.value}</b> ({point.beaufort})<br/>'
  324. },
  325. /**
  326. * Pixel length of the stems.
  327. */
  328. vectorLength: 20,
  329. /**
  330. * @default value
  331. */
  332. colorKey: 'value',
  333. /**
  334. * Vertical offset from the cartesian position, in pixels. The default
  335. * value makes sure the symbols don't overlap the X axis when `onSeries`
  336. * is `null`, and that they don't overlap the linked series when
  337. * `onSeries` is given.
  338. */
  339. yOffset: -20,
  340. /**
  341. * Horizontal offset from the cartesian position, in pixels. When the
  342. * chart is inverted, this option allows translation like
  343. * [yOffset](#plotOptions.windbarb.yOffset) in non inverted charts.
  344. *
  345. * @since 6.1.0
  346. */
  347. xOffset: 0
  348. }, {
  349. pointArrayMap: ['value', 'direction'],
  350. parallelArrays: ['x', 'value', 'direction'],
  351. beaufortName: ['Calm', 'Light air', 'Light breeze',
  352. 'Gentle breeze', 'Moderate breeze', 'Fresh breeze',
  353. 'Strong breeze', 'Near gale', 'Gale', 'Strong gale', 'Storm',
  354. 'Violent storm', 'Hurricane'],
  355. beaufortFloor: [0, 0.3, 1.6, 3.4, 5.5, 8.0, 10.8, 13.9, 17.2, 20.8,
  356. 24.5, 28.5, 32.7],
  357. trackerGroups: ['markerGroup'],
  358. init: function (chart, options) {
  359. registerApproximation();
  360. H.Series.prototype.init.call(this, chart, options);
  361. },
  362. // Get presentational attributes.
  363. pointAttribs: function (point, state) {
  364. var options = this.options,
  365. stroke = point.color || this.color,
  366. strokeWidth = this.options.lineWidth;
  367. if (state) {
  368. stroke = options.states[state].color || stroke;
  369. strokeWidth =
  370. (options.states[state].lineWidth || strokeWidth) +
  371. (options.states[state].lineWidthPlus || 0);
  372. }
  373. return {
  374. 'stroke': stroke,
  375. 'stroke-width': strokeWidth
  376. };
  377. },
  378. markerAttribs: function () {
  379. return;
  380. },
  381. getPlotBox: OnSeriesMixin.getPlotBox,
  382. // Create a single wind arrow. It is later rotated around the zero
  383. // centerpoint.
  384. windArrow: function (point) {
  385. var knots = point.value * 1.943844,
  386. level = point.beaufortLevel,
  387. path,
  388. barbs,
  389. u = this.options.vectorLength / 20,
  390. pos = -10;
  391. if (point.isNull) {
  392. return [];
  393. }
  394. if (level === 0) {
  395. return this.chart.renderer.symbols.circle(-10 * u, -10 * u, 20 * u, 20 * u);
  396. }
  397. // The stem and the arrow head
  398. path = [
  399. ['M', 0, 7 * u],
  400. ['L', -1.5 * u, 7 * u],
  401. ['L', 0, 10 * u],
  402. ['L', 1.5 * u, 7 * u],
  403. ['L', 0, 7 * u],
  404. ['L', 0, -10 * u] // top
  405. ];
  406. // For each full 50 knots, add a pennant
  407. barbs = (knots - knots % 50) / 50; // pennants
  408. if (barbs > 0) {
  409. while (barbs--) {
  410. path.push(pos === -10 ? ['L', 0, pos * u] : ['M', 0, pos * u], ['L', 5 * u, pos * u + 2], ['L', 0, pos * u + 4]);
  411. // Substract from the rest and move position for next
  412. knots -= 50;
  413. pos += 7;
  414. }
  415. }
  416. // For each full 10 knots, add a full barb
  417. barbs = (knots - knots % 10) / 10;
  418. if (barbs > 0) {
  419. while (barbs--) {
  420. path.push(pos === -10 ? ['L', 0, pos * u] : ['M', 0, pos * u], ['L', 7 * u, pos * u]);
  421. knots -= 10;
  422. pos += 3;
  423. }
  424. }
  425. // For each full 5 knots, add a half barb
  426. barbs = (knots - knots % 5) / 5; // half barbs
  427. if (barbs > 0) {
  428. while (barbs--) {
  429. path.push(pos === -10 ? ['L', 0, pos * u] : ['M', 0, pos * u], ['L', 4 * u, pos * u]);
  430. knots -= 5;
  431. pos += 3;
  432. }
  433. }
  434. return path;
  435. },
  436. translate: function () {
  437. var beaufortFloor = this.beaufortFloor,
  438. beaufortName = this.beaufortName;
  439. OnSeriesMixin.translate.call(this);
  440. this.points.forEach(function (point) {
  441. var level = 0;
  442. // Find the beaufort level (zero based)
  443. for (; level < beaufortFloor.length; level++) {
  444. if (beaufortFloor[level] > point.value) {
  445. break;
  446. }
  447. }
  448. point.beaufortLevel = level - 1;
  449. point.beaufort = beaufortName[level - 1];
  450. });
  451. },
  452. drawPoints: function () {
  453. var chart = this.chart,
  454. yAxis = this.yAxis,
  455. inverted = chart.inverted,
  456. shapeOffset = this.options.vectorLength / 2;
  457. this.points.forEach(function (point) {
  458. var plotX = point.plotX,
  459. plotY = point.plotY;
  460. // Check if it's inside the plot area, but only for the X
  461. // dimension.
  462. if (this.options.clip === false ||
  463. chart.isInsidePlot(plotX, 0, false)) {
  464. // Create the graphic the first time
  465. if (!point.graphic) {
  466. point.graphic = this.chart.renderer
  467. .path()
  468. .add(this.markerGroup)
  469. .addClass('highcharts-point ' +
  470. 'highcharts-color-' +
  471. pick(point.colorIndex, point.series.colorIndex));
  472. }
  473. // Position the graphic
  474. point.graphic
  475. .attr({
  476. d: this.windArrow(point),
  477. translateX: plotX + this.options.xOffset,
  478. translateY: plotY + this.options.yOffset,
  479. rotation: point.direction
  480. });
  481. if (!this.chart.styledMode) {
  482. point.graphic
  483. .attr(this.pointAttribs(point));
  484. }
  485. }
  486. else if (point.graphic) {
  487. point.graphic = point.graphic.destroy();
  488. }
  489. // Set the tooltip anchor position
  490. point.tooltipPos = [
  491. plotX + this.options.xOffset +
  492. (inverted && !this.onSeries ? shapeOffset : 0),
  493. plotY + this.options.yOffset -
  494. (inverted ?
  495. 0 :
  496. shapeOffset + yAxis.pos - chart.plotTop)
  497. ]; // #6327
  498. }, this);
  499. },
  500. // Fade in the arrows on initializing series.
  501. animate: function (init) {
  502. if (init) {
  503. this.markerGroup.attr({
  504. opacity: 0.01
  505. });
  506. }
  507. else {
  508. this.markerGroup.animate({
  509. opacity: 1
  510. }, animObject(this.options.animation));
  511. }
  512. },
  513. // Don't invert the marker group (#4960)
  514. invertGroups: noop,
  515. // No data extremes for the Y axis
  516. getExtremes: function () { return ({}); }
  517. }, {
  518. isValid: function () {
  519. return isNumber(this.value) && this.value >= 0;
  520. }
  521. });
  522. /**
  523. * A `windbarb` series. If the [type](#series.windbarb.type) option is not
  524. * specified, it is inherited from [chart.type](#chart.type).
  525. *
  526. * @extends series,plotOptions.windbarb
  527. * @excluding dataParser, dataURL, boostThreshold, boostBlending
  528. * @product highcharts highstock
  529. * @requires modules/windbarb
  530. * @apioption series.windbarb
  531. */
  532. /**
  533. * An array of data points for the series. For the `windbarb` series type,
  534. * points can be given in the following ways:
  535. *
  536. * 1. An array of arrays with 3 values. In this case, the values correspond to
  537. * `x,value,direction`. If the first value is a string, it is applied as the
  538. * name of the point, and the `x` value is inferred.
  539. * ```js
  540. * data: [
  541. * [Date.UTC(2017, 0, 1, 0), 3.3, 90],
  542. * [Date.UTC(2017, 0, 1, 1), 12.1, 180],
  543. * [Date.UTC(2017, 0, 1, 2), 11.1, 270]
  544. * ]
  545. * ```
  546. *
  547. * 2. An array of objects with named values. The following snippet shows only a
  548. * few settings, see the complete options set below. If the total number of
  549. * data points exceeds the series'
  550. * [turboThreshold](#series.area.turboThreshold), this option is not
  551. * available.
  552. * ```js
  553. * data: [{
  554. * x: Date.UTC(2017, 0, 1, 0),
  555. * value: 12.1,
  556. * direction: 90
  557. * }, {
  558. * x: Date.UTC(2017, 0, 1, 1),
  559. * value: 11.1,
  560. * direction: 270
  561. * }]
  562. * ```
  563. *
  564. * @sample {highcharts} highcharts/chart/reflow-true/
  565. * Numerical values
  566. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  567. * Arrays of numeric x and y
  568. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  569. * Arrays of datetime x and y
  570. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  571. * Arrays of point.name and y
  572. * @sample {highcharts} highcharts/series/data-array-of-objects/
  573. * Config objects
  574. *
  575. * @type {Array<Array<(number|string),number,number>|*>}
  576. * @extends series.line.data
  577. * @product highcharts highstock
  578. * @apioption series.windbarb.data
  579. */
  580. /**
  581. * The wind speed in meters per second.
  582. *
  583. * @type {number|null}
  584. * @product highcharts highstock
  585. * @apioption series.windbarb.data.value
  586. */
  587. /**
  588. * The wind direction in degrees, where 0 is north (pointing towards south).
  589. *
  590. * @type {number}
  591. * @product highcharts highstock
  592. * @apioption series.windbarb.data.direction
  593. */
  594. ''; // adds doclets above to transpiled file
  595. });
  596. _registerModule(_modules, 'masters/modules/windbarb.src.js', [], function () {
  597. });
  598. }));