DumbbellSeries.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* *
  2. *
  3. * (c) 2010-2020 Sebastian Bochan, Rafal Sebestjanski
  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 SVGRenderer from '../Core/Renderer/SVG/SVGRenderer.js';
  12. import H from '../Core/Globals.js';
  13. var noop = H.noop;
  14. import U from '../Core/Utilities.js';
  15. var extend = U.extend, pick = U.pick;
  16. import './AreaRangeSeries.js';
  17. import './ColumnRangeSeries.js';
  18. import './ColumnSeries.js';
  19. import '../Core/Interaction.js';
  20. var seriesProto = H.Series.prototype, seriesTypes = BaseSeries.seriesTypes, areaRangeProto = seriesTypes.arearange.prototype, columnRangeProto = seriesTypes.columnrange.prototype, colProto = seriesTypes.column.prototype, areaRangePointProto = areaRangeProto.pointClass.prototype, TrackerMixin = H.TrackerMixin; // Interaction
  21. /**
  22. * The dumbbell series is a cartesian series with higher and lower values for
  23. * each point along an X axis, connected with a line between the values.
  24. * Requires `highcharts-more.js` and `modules/dumbbell.js`.
  25. *
  26. * @sample {highcharts} highcharts/demo/dumbbell/
  27. * Dumbbell chart
  28. * @sample {highcharts} highcharts/series-dumbbell/styled-mode-dumbbell/
  29. * Styled mode
  30. *
  31. * @extends plotOptions.arearange
  32. * @product highcharts highstock
  33. * @excluding fillColor, fillOpacity, lineWidth, stack, stacking,
  34. * stickyTracking, trackByArea, boostThreshold, boostBlending
  35. * @since 8.0.0
  36. * @optionparent plotOptions.dumbbell
  37. */
  38. BaseSeries.seriesType('dumbbell', 'arearange', {
  39. /** @ignore-option */
  40. trackByArea: false,
  41. /** @ignore-option */
  42. fillColor: 'none',
  43. /** @ignore-option */
  44. lineWidth: 0,
  45. pointRange: 1,
  46. /**
  47. * Pixel width of the line that connects the dumbbell point's values.
  48. *
  49. * @since 8.0.0
  50. * @product highcharts highstock
  51. */
  52. connectorWidth: 1,
  53. /** @ignore-option */
  54. stickyTracking: false,
  55. groupPadding: 0.2,
  56. crisp: false,
  57. pointPadding: 0.1,
  58. /**
  59. * Color of the start markers in a dumbbell graph.
  60. *
  61. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  62. * @since 8.0.0
  63. * @product highcharts highstock
  64. */
  65. lowColor: '#333333',
  66. /**
  67. * Color of the line that connects the dumbbell point's values.
  68. * By default it is the series' color.
  69. *
  70. * @type {string}
  71. * @product highcharts highstock
  72. * @since 8.0.0
  73. * @apioption plotOptions.dumbbell.connectorColor
  74. */
  75. states: {
  76. hover: {
  77. /** @ignore-option */
  78. lineWidthPlus: 0,
  79. /**
  80. * The additional connector line width for a hovered point.
  81. *
  82. * @since 8.0.0
  83. * @product highcharts highstock
  84. */
  85. connectorWidthPlus: 1,
  86. /** @ignore-option */
  87. halo: false
  88. }
  89. }
  90. }, {
  91. trackerGroups: ['group', 'markerGroup', 'dataLabelsGroup'],
  92. drawTracker: TrackerMixin.drawTrackerPoint,
  93. drawGraph: noop,
  94. crispCol: colProto.crispCol,
  95. /**
  96. * Get connector line path and styles that connects dumbbell point's low and
  97. * high values.
  98. * @private
  99. *
  100. * @param {Highcharts.Series} this The series of points.
  101. * @param {Highcharts.Point} point The point to inspect.
  102. *
  103. * @return {Highcharts.SVGAttributes} attribs The path and styles.
  104. */
  105. getConnectorAttribs: function (point) {
  106. var series = this, chart = series.chart, pointOptions = point.options, seriesOptions = series.options, xAxis = series.xAxis, yAxis = series.yAxis, connectorWidth = pick(pointOptions.connectorWidth, seriesOptions.connectorWidth), connectorColor = pick(pointOptions.connectorColor, seriesOptions.connectorColor, pointOptions.color, point.zone ? point.zone.color : void 0, point.color), connectorWidthPlus = pick(seriesOptions.states &&
  107. seriesOptions.states.hover &&
  108. seriesOptions.states.hover.connectorWidthPlus, 1), dashStyle = pick(pointOptions.dashStyle, seriesOptions.dashStyle), pointTop = pick(point.plotLow, point.plotY), pxThreshold = yAxis.toPixels(seriesOptions.threshold || 0, true), pointHeight = chart.inverted ?
  109. yAxis.len - pxThreshold : pxThreshold, pointBottom = pick(point.plotHigh, pointHeight), attribs, origProps;
  110. if (point.state) {
  111. connectorWidth = connectorWidth + connectorWidthPlus;
  112. }
  113. if (pointTop < 0) {
  114. pointTop = 0;
  115. }
  116. else if (pointTop >= yAxis.len) {
  117. pointTop = yAxis.len;
  118. }
  119. if (pointBottom < 0) {
  120. pointBottom = 0;
  121. }
  122. else if (pointBottom >= yAxis.len) {
  123. pointBottom = yAxis.len;
  124. }
  125. if (point.plotX < 0 || point.plotX > xAxis.len) {
  126. connectorWidth = 0;
  127. }
  128. // Connector should reflect upper marker's zone color
  129. if (point.upperGraphic) {
  130. origProps = {
  131. y: point.y,
  132. zone: point.zone
  133. };
  134. point.y = point.high;
  135. point.zone = point.zone ? point.getZone() : void 0;
  136. connectorColor = pick(pointOptions.connectorColor, seriesOptions.connectorColor, pointOptions.color, point.zone ? point.zone.color : void 0, point.color);
  137. extend(point, origProps);
  138. }
  139. attribs = {
  140. d: SVGRenderer.prototype.crispLine([[
  141. 'M',
  142. point.plotX,
  143. pointTop
  144. ], [
  145. 'L',
  146. point.plotX,
  147. pointBottom
  148. ]], connectorWidth, 'ceil')
  149. };
  150. if (!chart.styledMode) {
  151. attribs.stroke = connectorColor;
  152. attribs['stroke-width'] = connectorWidth;
  153. if (dashStyle) {
  154. attribs.dashstyle = dashStyle;
  155. }
  156. }
  157. return attribs;
  158. },
  159. /**
  160. * Draw connector line that connects dumbbell point's low and high values.
  161. * @private
  162. *
  163. * @param {Highcharts.Series} this The series of points.
  164. * @param {Highcharts.Point} point The point to inspect.
  165. *
  166. * @return {void}
  167. */
  168. drawConnector: function (point) {
  169. var series = this, animationLimit = pick(series.options.animationLimit, 250), verb = point.connector && series.chart.pointCount < animationLimit ?
  170. 'animate' : 'attr';
  171. if (!point.connector) {
  172. point.connector = series.chart.renderer.path()
  173. .addClass('highcharts-lollipop-stem')
  174. .attr({
  175. zIndex: -1
  176. })
  177. .add(series.markerGroup);
  178. }
  179. point.connector[verb](this.getConnectorAttribs(point));
  180. },
  181. /**
  182. * Return the width and x offset of the dumbbell adjusted for grouping,
  183. * groupPadding, pointPadding, pointWidth etc.
  184. *
  185. * @private
  186. *
  187. * @function Highcharts.seriesTypes.column#getColumnMetrics
  188. *
  189. * @param {Highcharts.Series} this The series of points.
  190. *
  191. * @return {Highcharts.ColumnMetricsObject} metrics shapeArgs
  192. *
  193. */
  194. getColumnMetrics: function () {
  195. var metrics = colProto.getColumnMetrics.apply(this, arguments);
  196. metrics.offset += metrics.width / 2;
  197. return metrics;
  198. },
  199. translatePoint: areaRangeProto.translate,
  200. setShapeArgs: columnRangeProto.translate,
  201. /**
  202. * Translate each point to the plot area coordinate system and find
  203. * shape positions
  204. *
  205. * @private
  206. *
  207. * @function Highcharts.seriesTypes.dumbbell#translate
  208. *
  209. * @param {Highcharts.Series} this The series of points.
  210. *
  211. * @return {void}
  212. */
  213. translate: function () {
  214. // Calculate shapeargs
  215. this.setShapeArgs.apply(this);
  216. // Calculate point low / high values
  217. this.translatePoint.apply(this, arguments);
  218. // Correct x position
  219. this.points.forEach(function (point) {
  220. var shapeArgs = point.shapeArgs, pointWidth = point.pointWidth;
  221. point.plotX = shapeArgs.x;
  222. shapeArgs.x = point.plotX - pointWidth / 2;
  223. point.tooltipPos = null;
  224. });
  225. this.columnMetrics.offset -= this.columnMetrics.width / 2;
  226. },
  227. seriesDrawPoints: areaRangeProto.drawPoints,
  228. /**
  229. * Extend the arearange series' drawPoints method by applying a connector
  230. * and coloring markers.
  231. * @private
  232. *
  233. * @function Highcharts.Series#drawPoints
  234. *
  235. * @param {Highcharts.Series} this The series of points.
  236. *
  237. * @return {void}
  238. */
  239. drawPoints: function () {
  240. var series = this, chart = series.chart, pointLength = series.points.length, seriesLowColor = series.lowColor = series.options.lowColor, i = 0, lowerGraphicColor, point, zoneColor;
  241. this.seriesDrawPoints.apply(series, arguments);
  242. // Draw connectors and color upper markers
  243. while (i < pointLength) {
  244. point = series.points[i];
  245. series.drawConnector(point);
  246. if (point.upperGraphic) {
  247. point.upperGraphic.element.point = point;
  248. point.upperGraphic.addClass('highcharts-lollipop-high');
  249. }
  250. point.connector.element.point = point;
  251. if (point.lowerGraphic) {
  252. zoneColor = point.zone && point.zone.color;
  253. lowerGraphicColor = pick(point.options.lowColor, seriesLowColor, point.options.color, zoneColor, point.color, series.color);
  254. if (!chart.styledMode) {
  255. point.lowerGraphic.attr({
  256. fill: lowerGraphicColor
  257. });
  258. }
  259. point.lowerGraphic.addClass('highcharts-lollipop-low');
  260. }
  261. i++;
  262. }
  263. },
  264. /**
  265. * Get non-presentational attributes for a point. Used internally for
  266. * both styled mode and classic. Set correct position in link with connector
  267. * line.
  268. *
  269. * @see Series#pointAttribs
  270. *
  271. * @function Highcharts.Series#markerAttribs
  272. *
  273. * @param {Highcharts.Series} this The series of points.
  274. *
  275. * @return {Highcharts.SVGAttributes}
  276. * A hash containing those attributes that are not settable from
  277. * CSS.
  278. */
  279. markerAttribs: function () {
  280. var ret = areaRangeProto.markerAttribs.apply(this, arguments);
  281. ret.x = Math.floor(ret.x);
  282. ret.y = Math.floor(ret.y);
  283. return ret;
  284. },
  285. /**
  286. * Get presentational attributes
  287. *
  288. * @private
  289. * @function Highcharts.seriesTypes.column#pointAttribs
  290. *
  291. * @param {Highcharts.Series} this The series of points.
  292. * @param {Highcharts.Point} point The point to inspect.
  293. * @param {string} state current state of point (normal, hover, select)
  294. *
  295. * @return {Highcharts.SVGAttributes} pointAttribs SVGAttributes
  296. */
  297. pointAttribs: function (point, state) {
  298. var pointAttribs;
  299. pointAttribs = seriesProto.pointAttribs.apply(this, arguments);
  300. if (state === 'hover') {
  301. delete pointAttribs.fill;
  302. }
  303. return pointAttribs;
  304. }
  305. }, {
  306. // seriesTypes doesn't inherit from arearange point proto so put below
  307. // methods rigidly.
  308. destroyElements: areaRangePointProto.destroyElements,
  309. isValid: areaRangePointProto.isValid,
  310. pointSetState: areaRangePointProto.setState,
  311. /**
  312. * Set the point's state extended by have influence on the connector
  313. * (between low and high value).
  314. *
  315. * @private
  316. * @param {Highcharts.Point} this The point to inspect.
  317. *
  318. * @return {void}
  319. */
  320. setState: function () {
  321. var point = this, series = point.series, chart = series.chart, seriesLowColor = series.options.lowColor, seriesMarker = series.options.marker, pointOptions = point.options, pointLowColor = pointOptions.lowColor, zoneColor = point.zone && point.zone.color, lowerGraphicColor = pick(pointLowColor, seriesLowColor, pointOptions.color, zoneColor, point.color, series.color), verb = 'attr', upperGraphicColor, origProps;
  322. this.pointSetState.apply(this, arguments);
  323. if (!point.state) {
  324. verb = 'animate';
  325. if (point.lowerGraphic && !chart.styledMode) {
  326. point.lowerGraphic.attr({
  327. fill: lowerGraphicColor
  328. });
  329. if (point.upperGraphic) {
  330. origProps = {
  331. y: point.y,
  332. zone: point.zone
  333. };
  334. point.y = point.high;
  335. point.zone = point.zone ? point.getZone() : void 0;
  336. upperGraphicColor = pick(point.marker ? point.marker.fillColor : void 0, seriesMarker ? seriesMarker.fillColor : void 0, pointOptions.color, point.zone ? point.zone.color : void 0, point.color);
  337. point.upperGraphic.attr({
  338. fill: upperGraphicColor
  339. });
  340. extend(point, origProps);
  341. }
  342. }
  343. }
  344. point.connector[verb](series.getConnectorAttribs(point));
  345. }
  346. });
  347. /**
  348. * The `dumbbell` series. If the [type](#series.dumbbell.type) option is
  349. * not specified, it is inherited from [chart.type](#chart.type).
  350. *
  351. * @extends series,plotOptions.dumbbell
  352. * @excluding boostThreshold, boostBlending
  353. * @product highcharts highstock
  354. * @requires highcharts-more
  355. * @requires modules/dumbbell
  356. * @apioption series.dumbbell
  357. */
  358. /**
  359. * An array of data points for the series. For the `dumbbell` series
  360. * type, points can be given in the following ways:
  361. *
  362. * 1. An array of arrays with 3 or 2 values. In this case, the values correspond
  363. * to `x,low,high`. If the first value is a string, it is applied as the name
  364. * of the point, and the `x` value is inferred. The `x` value can also be
  365. * omitted, in which case the inner arrays should be of length 2\. Then the
  366. * `x` value is automatically calculated, either starting at 0 and
  367. * incremented by 1, or from `pointStart` and `pointInterval` given in the
  368. * series options.
  369. * ```js
  370. * data: [
  371. * [0, 4, 2],
  372. * [1, 2, 1],
  373. * [2, 9, 10]
  374. * ]
  375. * ```
  376. *
  377. * 2. An array of objects with named values. The following snippet shows only a
  378. * few settings, see the complete options set below. If the total number of
  379. * data points exceeds the series'
  380. * [turboThreshold](#series.dumbbell.turboThreshold), this option is not
  381. * available.
  382. * ```js
  383. * data: [{
  384. * x: 1,
  385. * low: 0,
  386. * high: 4,
  387. * name: "Point2",
  388. * color: "#00FF00",
  389. * lowColor: "#00FFFF",
  390. * connectorWidth: 3,
  391. * connectorColor: "#FF00FF"
  392. * }, {
  393. * x: 1,
  394. * low: 5,
  395. * high: 3,
  396. * name: "Point1",
  397. * color: "#FF00FF"
  398. * }]
  399. * ```
  400. *
  401. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  402. * Arrays of numeric x and y
  403. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  404. * Arrays of datetime x and y
  405. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  406. * Arrays of point.name and y
  407. * @sample {highcharts} highcharts/series/data-array-of-objects/
  408. * Config objects
  409. *
  410. * @type {Array<Array<(number|string),number>|Array<(number|string),number,number>|*>}
  411. * @extends series.arearange.data
  412. * @product highcharts highstock
  413. * @apioption series.dumbbell.data
  414. */
  415. /**
  416. * Color of the line that connects the dumbbell point's values.
  417. * By default it is the series' color.
  418. *
  419. * @type {string}
  420. * @since 8.0.0
  421. * @product highcharts highstock
  422. * @apioption series.dumbbell.data.connectorColor
  423. */
  424. /**
  425. * Pixel width of the line that connects the dumbbell point's values.
  426. *
  427. * @type {number}
  428. * @since 8.0.0
  429. * @default 1
  430. * @product highcharts highstock
  431. * @apioption series.dumbbell.data.connectorWidth
  432. */
  433. /**
  434. * Color of the start markers in a dumbbell graph.
  435. *
  436. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  437. * @since 8.0.0
  438. * @default #333333
  439. * @product highcharts highstock
  440. * @apioption series.dumbbell.data.lowColor
  441. */
  442. ''; // adds doclets above to transpiled file