PlotLineOrBand.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. 'use strict';
  11. import Axis from './Axis.js';
  12. import H from '../Globals.js';
  13. /**
  14. * Options for plot bands on axes.
  15. *
  16. * @typedef {Highcharts.XAxisPlotBandsOptions|Highcharts.YAxisPlotBandsOptions|Highcharts.ZAxisPlotBandsOptions} Highcharts.AxisPlotBandsOptions
  17. */
  18. /**
  19. * Options for plot band labels on axes.
  20. *
  21. * @typedef {Highcharts.XAxisPlotBandsLabelOptions|Highcharts.YAxisPlotBandsLabelOptions|Highcharts.ZAxisPlotBandsLabelOptions} Highcharts.AxisPlotBandsLabelOptions
  22. */
  23. /**
  24. * Options for plot lines on axes.
  25. *
  26. * @typedef {Highcharts.XAxisPlotLinesOptions|Highcharts.YAxisPlotLinesOptions|Highcharts.ZAxisPlotLinesOptions} Highcharts.AxisPlotLinesOptions
  27. */
  28. /**
  29. * Options for plot line labels on axes.
  30. *
  31. * @typedef {Highcharts.XAxisPlotLinesLabelOptions|Highcharts.YAxisPlotLinesLabelOptions|Highcharts.ZAxisPlotLinesLabelOptions} Highcharts.AxisPlotLinesLabelOptions
  32. */
  33. import U from '../Utilities.js';
  34. var arrayMax = U.arrayMax, arrayMin = U.arrayMin, defined = U.defined, destroyObjectProperties = U.destroyObjectProperties, erase = U.erase, extend = U.extend, merge = U.merge, objectEach = U.objectEach, pick = U.pick;
  35. /* eslint-disable no-invalid-this, valid-jsdoc */
  36. /**
  37. * The object wrapper for plot lines and plot bands
  38. *
  39. * @class
  40. * @name Highcharts.PlotLineOrBand
  41. *
  42. * @param {Highcharts.Axis} axis
  43. *
  44. * @param {Highcharts.AxisPlotLinesOptions|Highcharts.AxisPlotBandsOptions} [options]
  45. */
  46. var PlotLineOrBand = /** @class */ (function () {
  47. function PlotLineOrBand(axis, options) {
  48. this.axis = axis;
  49. if (options) {
  50. this.options = options;
  51. this.id = options.id;
  52. }
  53. }
  54. /**
  55. * Render the plot line or plot band. If it is already existing,
  56. * move it.
  57. *
  58. * @private
  59. * @function Highcharts.PlotLineOrBand#render
  60. * @return {Highcharts.PlotLineOrBand|undefined}
  61. */
  62. PlotLineOrBand.prototype.render = function () {
  63. H.fireEvent(this, 'render');
  64. var plotLine = this, axis = plotLine.axis, horiz = axis.horiz, log = axis.logarithmic, options = plotLine.options, optionsLabel = options.label, label = plotLine.label, to = options.to, from = options.from, value = options.value, isBand = defined(from) && defined(to), isLine = defined(value), svgElem = plotLine.svgElem, isNew = !svgElem, path = [], color = options.color, zIndex = pick(options.zIndex, 0), events = options.events, attribs = {
  65. 'class': 'highcharts-plot-' + (isBand ? 'band ' : 'line ') +
  66. (options.className || '')
  67. }, groupAttribs = {}, renderer = axis.chart.renderer, groupName = isBand ? 'bands' : 'lines', group;
  68. // logarithmic conversion
  69. if (log) {
  70. from = log.log2lin(from);
  71. to = log.log2lin(to);
  72. value = log.log2lin(value);
  73. }
  74. // Set the presentational attributes
  75. if (!axis.chart.styledMode) {
  76. if (isLine) {
  77. attribs.stroke = color || '#999999';
  78. attribs['stroke-width'] = pick(options.width, 1);
  79. if (options.dashStyle) {
  80. attribs.dashstyle =
  81. options.dashStyle;
  82. }
  83. }
  84. else if (isBand) { // plot band
  85. attribs.fill = color || '#e6ebf5';
  86. if (options.borderWidth) {
  87. attribs.stroke = options.borderColor;
  88. attribs['stroke-width'] = options.borderWidth;
  89. }
  90. }
  91. }
  92. // Grouping and zIndex
  93. groupAttribs.zIndex = zIndex;
  94. groupName += '-' + zIndex;
  95. group = axis.plotLinesAndBandsGroups[groupName];
  96. if (!group) {
  97. axis.plotLinesAndBandsGroups[groupName] = group =
  98. renderer.g('plot-' + groupName)
  99. .attr(groupAttribs).add();
  100. }
  101. // Create the path
  102. if (isNew) {
  103. /**
  104. * SVG element of the plot line or band.
  105. *
  106. * @name Highcharts.PlotLineOrBand#svgElement
  107. * @type {Highcharts.SVGElement}
  108. */
  109. plotLine.svgElem = svgElem = renderer
  110. .path()
  111. .attr(attribs)
  112. .add(group);
  113. }
  114. // Set the path or return
  115. if (isLine) {
  116. path = axis.getPlotLinePath({
  117. value: value,
  118. lineWidth: svgElem.strokeWidth(),
  119. acrossPanes: options.acrossPanes
  120. });
  121. }
  122. else if (isBand) { // plot band
  123. path = axis.getPlotBandPath(from, to, options);
  124. }
  125. else {
  126. return;
  127. }
  128. // common for lines and bands
  129. // Add events only if they were not added before.
  130. if (!plotLine.eventsAdded && events) {
  131. objectEach(events, function (event, eventType) {
  132. svgElem.on(eventType, function (e) {
  133. events[eventType].apply(plotLine, [e]);
  134. });
  135. });
  136. plotLine.eventsAdded = true;
  137. }
  138. if ((isNew || !svgElem.d) && path && path.length) {
  139. svgElem.attr({ d: path });
  140. }
  141. else if (svgElem) {
  142. if (path) {
  143. svgElem.show(true);
  144. svgElem.animate({ d: path });
  145. }
  146. else if (svgElem.d) {
  147. svgElem.hide();
  148. if (label) {
  149. plotLine.label = label = label.destroy();
  150. }
  151. }
  152. }
  153. // the plot band/line label
  154. if (optionsLabel &&
  155. (defined(optionsLabel.text) || defined(optionsLabel.formatter)) &&
  156. path &&
  157. path.length &&
  158. axis.width > 0 &&
  159. axis.height > 0 &&
  160. !path.isFlat) {
  161. // apply defaults
  162. optionsLabel = merge({
  163. align: horiz && isBand && 'center',
  164. x: horiz ? !isBand && 4 : 10,
  165. verticalAlign: !horiz && isBand && 'middle',
  166. y: horiz ? isBand ? 16 : 10 : isBand ? 6 : -4,
  167. rotation: horiz && !isBand && 90
  168. }, optionsLabel);
  169. this.renderLabel(optionsLabel, path, isBand, zIndex);
  170. }
  171. else if (label) { // move out of sight
  172. label.hide();
  173. }
  174. // chainable
  175. return plotLine;
  176. };
  177. /**
  178. * Render and align label for plot line or band.
  179. *
  180. * @private
  181. * @function Highcharts.PlotLineOrBand#renderLabel
  182. * @param {Highcharts.AxisPlotLinesLabelOptions|Highcharts.AxisPlotBandsLabelOptions} optionsLabel
  183. * @param {Highcharts.SVGPathArray} path
  184. * @param {boolean} [isBand]
  185. * @param {number} [zIndex]
  186. * @return {void}
  187. */
  188. PlotLineOrBand.prototype.renderLabel = function (optionsLabel, path, isBand, zIndex) {
  189. var plotLine = this, label = plotLine.label, renderer = plotLine.axis.chart.renderer, attribs, xBounds, yBounds, x, y, labelText;
  190. // add the SVG element
  191. if (!label) {
  192. attribs = {
  193. align: optionsLabel.textAlign || optionsLabel.align,
  194. rotation: optionsLabel.rotation,
  195. 'class': 'highcharts-plot-' + (isBand ? 'band' : 'line') +
  196. '-label ' + (optionsLabel.className || '')
  197. };
  198. attribs.zIndex = zIndex;
  199. labelText = this.getLabelText(optionsLabel);
  200. /**
  201. * SVG element of the label.
  202. *
  203. * @name Highcharts.PlotLineOrBand#label
  204. * @type {Highcharts.SVGElement}
  205. */
  206. plotLine.label = label = renderer
  207. .text(labelText, 0, 0, optionsLabel.useHTML)
  208. .attr(attribs)
  209. .add();
  210. if (!this.axis.chart.styledMode) {
  211. label.css(optionsLabel.style);
  212. }
  213. }
  214. // get the bounding box and align the label
  215. // #3000 changed to better handle choice between plotband or plotline
  216. xBounds = path.xBounds ||
  217. [path[0][1], path[1][1], (isBand ? path[2][1] : path[0][1])];
  218. yBounds = path.yBounds ||
  219. [path[0][2], path[1][2], (isBand ? path[2][2] : path[0][2])];
  220. x = arrayMin(xBounds);
  221. y = arrayMin(yBounds);
  222. label.align(optionsLabel, false, {
  223. x: x,
  224. y: y,
  225. width: arrayMax(xBounds) - x,
  226. height: arrayMax(yBounds) - y
  227. });
  228. label.show(true);
  229. };
  230. /**
  231. * Get label's text content.
  232. *
  233. * @private
  234. * @function Highcharts.PlotLineOrBand#getLabelText
  235. * @param {Highcharts.AxisPlotLinesLabelOptions|Highcharts.AxisPlotBandsLabelOptions} optionsLabel
  236. * @return {string}
  237. */
  238. PlotLineOrBand.prototype.getLabelText = function (optionsLabel) {
  239. return defined(optionsLabel.formatter) ?
  240. optionsLabel.formatter
  241. .call(this) :
  242. optionsLabel.text;
  243. };
  244. /**
  245. * Remove the plot line or band.
  246. *
  247. * @function Highcharts.PlotLineOrBand#destroy
  248. * @return {void}
  249. */
  250. PlotLineOrBand.prototype.destroy = function () {
  251. // remove it from the lookup
  252. erase(this.axis.plotLinesAndBands, this);
  253. delete this.axis;
  254. destroyObjectProperties(this);
  255. };
  256. return PlotLineOrBand;
  257. }());
  258. /* eslint-enable no-invalid-this, valid-jsdoc */
  259. // Object with members for extending the Axis prototype
  260. extend(Axis.prototype, /** @lends Highcharts.Axis.prototype */ {
  261. /**
  262. * An array of colored bands stretching across the plot area marking an
  263. * interval on the axis.
  264. *
  265. * In styled mode, the plot bands are styled by the `.highcharts-plot-band`
  266. * class in addition to the `className` option.
  267. *
  268. * @productdesc {highcharts}
  269. * In a gauge, a plot band on the Y axis (value axis) will stretch along the
  270. * perimeter of the gauge.
  271. *
  272. * @type {Array<*>}
  273. * @product highcharts highstock gantt
  274. * @apioption xAxis.plotBands
  275. */
  276. /**
  277. * Flag to decide if plotBand should be rendered across all panes.
  278. *
  279. * @since 7.1.2
  280. * @product highstock
  281. * @type {boolean}
  282. * @default true
  283. * @apioption xAxis.plotBands.acrossPanes
  284. */
  285. /**
  286. * Border color for the plot band. Also requires `borderWidth` to be set.
  287. *
  288. * @type {Highcharts.ColorString}
  289. * @apioption xAxis.plotBands.borderColor
  290. */
  291. /**
  292. * Border width for the plot band. Also requires `borderColor` to be set.
  293. *
  294. * @type {number}
  295. * @default 0
  296. * @apioption xAxis.plotBands.borderWidth
  297. */
  298. /**
  299. * A custom class name, in addition to the default `highcharts-plot-band`,
  300. * to apply to each individual band.
  301. *
  302. * @type {string}
  303. * @since 5.0.0
  304. * @apioption xAxis.plotBands.className
  305. */
  306. /**
  307. * The color of the plot band.
  308. *
  309. * @sample {highcharts} highcharts/xaxis/plotbands-color/
  310. * Color band
  311. * @sample {highstock} stock/xaxis/plotbands/
  312. * Plot band on Y axis
  313. *
  314. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  315. * @default #e6ebf5
  316. * @apioption xAxis.plotBands.color
  317. */
  318. /**
  319. * An object defining mouse events for the plot band. Supported properties
  320. * are `click`, `mouseover`, `mouseout`, `mousemove`.
  321. *
  322. * @sample {highcharts} highcharts/xaxis/plotbands-events/
  323. * Mouse events demonstrated
  324. *
  325. * @since 1.2
  326. * @apioption xAxis.plotBands.events
  327. */
  328. /**
  329. * Click event on a plot band.
  330. *
  331. * @type {Highcharts.EventCallbackFunction<Highcharts.PlotLineOrBand>}
  332. * @apioption xAxis.plotBands.events.click
  333. */
  334. /**
  335. * Mouse move event on a plot band.
  336. *
  337. * @type {Highcharts.EventCallbackFunction<Highcharts.PlotLineOrBand>}
  338. * @apioption xAxis.plotBands.events.mousemove
  339. */
  340. /**
  341. * Mouse out event on the corner of a plot band.
  342. *
  343. * @type {Highcharts.EventCallbackFunction<Highcharts.PlotLineOrBand>}
  344. * @apioption xAxis.plotBands.events.mouseout
  345. */
  346. /**
  347. * Mouse over event on a plot band.
  348. *
  349. * @type {Highcharts.EventCallbackFunction<Highcharts.PlotLineOrBand>}
  350. * @apioption xAxis.plotBands.events.mouseover
  351. */
  352. /**
  353. * The start position of the plot band in axis units.
  354. *
  355. * @sample {highcharts} highcharts/xaxis/plotbands-color/
  356. * Datetime axis
  357. * @sample {highcharts} highcharts/xaxis/plotbands-from/
  358. * Categorized axis
  359. * @sample {highstock} stock/xaxis/plotbands/
  360. * Plot band on Y axis
  361. *
  362. * @type {number}
  363. * @apioption xAxis.plotBands.from
  364. */
  365. /**
  366. * An id used for identifying the plot band in Axis.removePlotBand.
  367. *
  368. * @sample {highcharts} highcharts/xaxis/plotbands-id/
  369. * Remove plot band by id
  370. * @sample {highstock} highcharts/xaxis/plotbands-id/
  371. * Remove plot band by id
  372. *
  373. * @type {string}
  374. * @apioption xAxis.plotBands.id
  375. */
  376. /**
  377. * The end position of the plot band in axis units.
  378. *
  379. * @sample {highcharts} highcharts/xaxis/plotbands-color/
  380. * Datetime axis
  381. * @sample {highcharts} highcharts/xaxis/plotbands-from/
  382. * Categorized axis
  383. * @sample {highstock} stock/xaxis/plotbands/
  384. * Plot band on Y axis
  385. *
  386. * @type {number}
  387. * @apioption xAxis.plotBands.to
  388. */
  389. /**
  390. * The z index of the plot band within the chart, relative to other
  391. * elements. Using the same z index as another element may give
  392. * unpredictable results, as the last rendered element will be on top.
  393. * Values from 0 to 20 make sense.
  394. *
  395. * @sample {highcharts} highcharts/xaxis/plotbands-color/
  396. * Behind plot lines by default
  397. * @sample {highcharts} highcharts/xaxis/plotbands-zindex/
  398. * Above plot lines
  399. * @sample {highcharts} highcharts/xaxis/plotbands-zindex-above-series/
  400. * Above plot lines and series
  401. *
  402. * @type {number}
  403. * @since 1.2
  404. * @apioption xAxis.plotBands.zIndex
  405. */
  406. /**
  407. * Text labels for the plot bands
  408. *
  409. * @product highcharts highstock gantt
  410. * @apioption xAxis.plotBands.label
  411. */
  412. /**
  413. * Horizontal alignment of the label. Can be one of "left", "center" or
  414. * "right".
  415. *
  416. * @sample {highcharts} highcharts/xaxis/plotbands-label-align/
  417. * Aligned to the right
  418. * @sample {highstock} stock/xaxis/plotbands-label/
  419. * Plot band with labels
  420. *
  421. * @type {Highcharts.AlignValue}
  422. * @default center
  423. * @since 2.1
  424. * @apioption xAxis.plotBands.label.align
  425. */
  426. /**
  427. * Rotation of the text label in degrees .
  428. *
  429. * @sample {highcharts} highcharts/xaxis/plotbands-label-rotation/
  430. * Vertical text
  431. *
  432. * @type {number}
  433. * @default 0
  434. * @since 2.1
  435. * @apioption xAxis.plotBands.label.rotation
  436. */
  437. /**
  438. * CSS styles for the text label.
  439. *
  440. * In styled mode, the labels are styled by the
  441. * `.highcharts-plot-band-label` class.
  442. *
  443. * @sample {highcharts} highcharts/xaxis/plotbands-label-style/
  444. * Blue and bold label
  445. *
  446. * @type {Highcharts.CSSObject}
  447. * @since 2.1
  448. * @apioption xAxis.plotBands.label.style
  449. */
  450. /**
  451. * The string text itself. A subset of HTML is supported.
  452. *
  453. * @type {string}
  454. * @since 2.1
  455. * @apioption xAxis.plotBands.label.text
  456. */
  457. /**
  458. * The text alignment for the label. While `align` determines where the
  459. * texts anchor point is placed within the plot band, `textAlign` determines
  460. * how the text is aligned against its anchor point. Possible values are
  461. * "left", "center" and "right". Defaults to the same as the `align` option.
  462. *
  463. * @sample {highcharts} highcharts/xaxis/plotbands-label-rotation/
  464. * Vertical text in center position but text-aligned left
  465. *
  466. * @type {Highcharts.AlignValue}
  467. * @since 2.1
  468. * @apioption xAxis.plotBands.label.textAlign
  469. */
  470. /**
  471. * Whether to [use HTML](https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting#html)
  472. * to render the labels.
  473. *
  474. * @type {boolean}
  475. * @default false
  476. * @since 3.0.3
  477. * @apioption xAxis.plotBands.label.useHTML
  478. */
  479. /**
  480. * Vertical alignment of the label relative to the plot band. Can be one of
  481. * "top", "middle" or "bottom".
  482. *
  483. * @sample {highcharts} highcharts/xaxis/plotbands-label-verticalalign/
  484. * Vertically centered label
  485. * @sample {highstock} stock/xaxis/plotbands-label/
  486. * Plot band with labels
  487. *
  488. * @type {Highcharts.VerticalAlignValue}
  489. * @default top
  490. * @since 2.1
  491. * @apioption xAxis.plotBands.label.verticalAlign
  492. */
  493. /**
  494. * Horizontal position relative the alignment. Default varies by
  495. * orientation.
  496. *
  497. * @sample {highcharts} highcharts/xaxis/plotbands-label-align/
  498. * Aligned 10px from the right edge
  499. * @sample {highstock} stock/xaxis/plotbands-label/
  500. * Plot band with labels
  501. *
  502. * @type {number}
  503. * @since 2.1
  504. * @apioption xAxis.plotBands.label.x
  505. */
  506. /**
  507. * Vertical position of the text baseline relative to the alignment. Default
  508. * varies by orientation.
  509. *
  510. * @sample {highcharts} highcharts/xaxis/plotbands-label-y/
  511. * Label on x axis
  512. * @sample {highstock} stock/xaxis/plotbands-label/
  513. * Plot band with labels
  514. *
  515. * @type {number}
  516. * @since 2.1
  517. * @apioption xAxis.plotBands.label.y
  518. */
  519. /**
  520. * An array of lines stretching across the plot area, marking a specific
  521. * value on one of the axes.
  522. *
  523. * In styled mode, the plot lines are styled by the
  524. * `.highcharts-plot-line` class in addition to the `className` option.
  525. *
  526. * @type {Array<*>}
  527. * @product highcharts highstock gantt
  528. * @sample {highcharts} highcharts/xaxis/plotlines-color/
  529. * Basic plot line
  530. * @sample {highcharts} highcharts/series-solidgauge/labels-auto-aligned/
  531. * Solid gauge plot line
  532. * @apioption xAxis.plotLines
  533. */
  534. /**
  535. * Flag to decide if plotLine should be rendered across all panes.
  536. *
  537. * @sample {highstock} stock/xaxis/plotlines-acrosspanes/
  538. * Plot lines on different panes
  539. *
  540. * @since 7.1.2
  541. * @product highstock
  542. * @type {boolean}
  543. * @default true
  544. * @apioption xAxis.plotLines.acrossPanes
  545. */
  546. /**
  547. * A custom class name, in addition to the default `highcharts-plot-line`,
  548. * to apply to each individual line.
  549. *
  550. * @type {string}
  551. * @since 5.0.0
  552. * @apioption xAxis.plotLines.className
  553. */
  554. /**
  555. * The color of the line.
  556. *
  557. * @sample {highcharts} highcharts/xaxis/plotlines-color/
  558. * A red line from X axis
  559. * @sample {highstock} stock/xaxis/plotlines/
  560. * Plot line on Y axis
  561. *
  562. * @type {Highcharts.ColorString}
  563. * @default #999999
  564. * @apioption xAxis.plotLines.color
  565. */
  566. /**
  567. * The dashing or dot style for the plot line. For possible values see
  568. * [this overview](https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/plotoptions/series-dashstyle-all/).
  569. *
  570. * @sample {highcharts} highcharts/xaxis/plotlines-dashstyle/
  571. * Dash and dot pattern
  572. * @sample {highstock} stock/xaxis/plotlines/
  573. * Plot line on Y axis
  574. *
  575. * @type {Highcharts.DashStyleValue}
  576. * @default Solid
  577. * @since 1.2
  578. * @apioption xAxis.plotLines.dashStyle
  579. */
  580. /**
  581. * An object defining mouse events for the plot line. Supported
  582. * properties are `click`, `mouseover`, `mouseout`, `mousemove`.
  583. *
  584. * @sample {highcharts} highcharts/xaxis/plotlines-events/
  585. * Mouse events demonstrated
  586. *
  587. * @since 1.2
  588. * @apioption xAxis.plotLines.events
  589. */
  590. /**
  591. * Click event on a plot band.
  592. *
  593. * @type {Highcharts.EventCallbackFunction<Highcharts.PlotLineOrBand>}
  594. * @apioption xAxis.plotLines.events.click
  595. */
  596. /**
  597. * Mouse move event on a plot band.
  598. *
  599. * @type {Highcharts.EventCallbackFunction<Highcharts.PlotLineOrBand>}
  600. * @apioption xAxis.plotLines.events.mousemove
  601. */
  602. /**
  603. * Mouse out event on the corner of a plot band.
  604. *
  605. * @type {Highcharts.EventCallbackFunction<Highcharts.PlotLineOrBand>}
  606. * @apioption xAxis.plotLines.events.mouseout
  607. */
  608. /**
  609. * Mouse over event on a plot band.
  610. *
  611. * @type {Highcharts.EventCallbackFunction<Highcharts.PlotLineOrBand>}
  612. * @apioption xAxis.plotLines.events.mouseover
  613. */
  614. /**
  615. * An id used for identifying the plot line in Axis.removePlotLine.
  616. *
  617. * @sample {highcharts} highcharts/xaxis/plotlines-id/
  618. * Remove plot line by id
  619. *
  620. * @type {string}
  621. * @apioption xAxis.plotLines.id
  622. */
  623. /**
  624. * The position of the line in axis units.
  625. *
  626. * @sample {highcharts} highcharts/xaxis/plotlines-color/
  627. * Between two categories on X axis
  628. * @sample {highstock} stock/xaxis/plotlines/
  629. * Plot line on Y axis
  630. *
  631. * @type {number}
  632. * @apioption xAxis.plotLines.value
  633. */
  634. /**
  635. * The width or thickness of the plot line.
  636. *
  637. * @sample {highcharts} highcharts/xaxis/plotlines-color/
  638. * 2px wide line from X axis
  639. * @sample {highstock} stock/xaxis/plotlines/
  640. * Plot line on Y axis
  641. *
  642. * @type {number}
  643. * @default 2
  644. * @apioption xAxis.plotLines.width
  645. */
  646. /**
  647. * The z index of the plot line within the chart.
  648. *
  649. * @sample {highcharts} highcharts/xaxis/plotlines-zindex-behind/
  650. * Behind plot lines by default
  651. * @sample {highcharts} highcharts/xaxis/plotlines-zindex-above/
  652. * Above plot lines
  653. * @sample {highcharts} highcharts/xaxis/plotlines-zindex-above-all/
  654. * Above plot lines and series
  655. *
  656. * @type {number}
  657. * @since 1.2
  658. * @apioption xAxis.plotLines.zIndex
  659. */
  660. /**
  661. * Text labels for the plot bands
  662. *
  663. * @apioption xAxis.plotLines.label
  664. */
  665. /**
  666. * Horizontal alignment of the label. Can be one of "left", "center" or
  667. * "right".
  668. *
  669. * @sample {highcharts} highcharts/xaxis/plotlines-label-align-right/
  670. * Aligned to the right
  671. * @sample {highstock} stock/xaxis/plotlines/
  672. * Plot line on Y axis
  673. *
  674. * @type {Highcharts.AlignValue}
  675. * @default left
  676. * @since 2.1
  677. * @apioption xAxis.plotLines.label.align
  678. */
  679. /**
  680. * Callback JavaScript function to format the label. Useful properties like
  681. * the value of plot line or the range of plot band (`from` & `to`
  682. * properties) can be found in `this.options` object.
  683. *
  684. * @sample {highcharts} highcharts/xaxis/plotlines-plotbands-label-formatter
  685. * Label formatters for plot line and plot band.
  686. * @type {Highcharts.FormatterCallbackFunction<Highcharts.PlotLineOrBand>}
  687. * @apioption xAxis.plotLines.label.formatter
  688. */
  689. /**
  690. * Rotation of the text label in degrees. Defaults to 0 for horizontal plot
  691. * lines and 90 for vertical lines.
  692. *
  693. * @sample {highcharts} highcharts/xaxis/plotlines-label-verticalalign-middle/
  694. * Slanted text
  695. *
  696. * @type {number}
  697. * @since 2.1
  698. * @apioption xAxis.plotLines.label.rotation
  699. */
  700. /**
  701. * CSS styles for the text label.
  702. *
  703. * In styled mode, the labels are styled by the
  704. * `.highcharts-plot-line-label` class.
  705. *
  706. * @sample {highcharts} highcharts/xaxis/plotlines-label-style/
  707. * Blue and bold label
  708. *
  709. * @type {Highcharts.CSSObject}
  710. * @since 2.1
  711. * @apioption xAxis.plotLines.label.style
  712. */
  713. /**
  714. * The text itself. A subset of HTML is supported.
  715. *
  716. * @type {string}
  717. * @since 2.1
  718. * @apioption xAxis.plotLines.label.text
  719. */
  720. /**
  721. * The text alignment for the label. While `align` determines where the
  722. * texts anchor point is placed within the plot band, `textAlign` determines
  723. * how the text is aligned against its anchor point. Possible values are
  724. * "left", "center" and "right". Defaults to the same as the `align` option.
  725. *
  726. * @sample {highcharts} highcharts/xaxis/plotlines-label-textalign/
  727. * Text label in bottom position
  728. *
  729. * @type {Highcharts.AlignValue}
  730. * @since 2.1
  731. * @apioption xAxis.plotLines.label.textAlign
  732. */
  733. /**
  734. * Whether to [use HTML](https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting#html)
  735. * to render the labels.
  736. *
  737. * @type {boolean}
  738. * @default false
  739. * @since 3.0.3
  740. * @apioption xAxis.plotLines.label.useHTML
  741. */
  742. /**
  743. * Vertical alignment of the label relative to the plot line. Can be
  744. * one of "top", "middle" or "bottom".
  745. *
  746. * @sample {highcharts} highcharts/xaxis/plotlines-label-verticalalign-middle/
  747. * Vertically centered label
  748. *
  749. * @type {Highcharts.VerticalAlignValue}
  750. * @default {highcharts} top
  751. * @default {highstock} top
  752. * @since 2.1
  753. * @apioption xAxis.plotLines.label.verticalAlign
  754. */
  755. /**
  756. * Horizontal position relative the alignment. Default varies by
  757. * orientation.
  758. *
  759. * @sample {highcharts} highcharts/xaxis/plotlines-label-align-right/
  760. * Aligned 10px from the right edge
  761. * @sample {highstock} stock/xaxis/plotlines/
  762. * Plot line on Y axis
  763. *
  764. * @type {number}
  765. * @since 2.1
  766. * @apioption xAxis.plotLines.label.x
  767. */
  768. /**
  769. * Vertical position of the text baseline relative to the alignment. Default
  770. * varies by orientation.
  771. *
  772. * @sample {highcharts} highcharts/xaxis/plotlines-label-y/
  773. * Label below the plot line
  774. * @sample {highstock} stock/xaxis/plotlines/
  775. * Plot line on Y axis
  776. *
  777. * @type {number}
  778. * @since 2.1
  779. * @apioption xAxis.plotLines.label.y
  780. */
  781. /**
  782. *
  783. * @type {Array<*>}
  784. * @extends xAxis.plotBands
  785. * @apioption yAxis.plotBands
  786. */
  787. /**
  788. * In a gauge chart, this option determines the inner radius of the
  789. * plot band that stretches along the perimeter. It can be given as
  790. * a percentage string, like `"100%"`, or as a pixel number, like `100`.
  791. * By default, the inner radius is controlled by the [thickness](
  792. * #yAxis.plotBands.thickness) option.
  793. *
  794. * @sample {highcharts} highcharts/xaxis/plotbands-gauge
  795. * Gauge plot band
  796. *
  797. * @type {number|string}
  798. * @since 2.3
  799. * @product highcharts
  800. * @apioption yAxis.plotBands.innerRadius
  801. */
  802. /**
  803. * In a gauge chart, this option determines the outer radius of the
  804. * plot band that stretches along the perimeter. It can be given as
  805. * a percentage string, like `"100%"`, or as a pixel number, like `100`.
  806. *
  807. * @sample {highcharts} highcharts/xaxis/plotbands-gauge
  808. * Gauge plot band
  809. *
  810. * @type {number|string}
  811. * @default 100%
  812. * @since 2.3
  813. * @product highcharts
  814. * @apioption yAxis.plotBands.outerRadius
  815. */
  816. /**
  817. * In a gauge chart, this option sets the width of the plot band
  818. * stretching along the perimeter. It can be given as a percentage
  819. * string, like `"10%"`, or as a pixel number, like `10`. The default
  820. * value 10 is the same as the default [tickLength](#yAxis.tickLength),
  821. * thus making the plot band act as a background for the tick markers.
  822. *
  823. * @sample {highcharts} highcharts/xaxis/plotbands-gauge
  824. * Gauge plot band
  825. *
  826. * @type {number|string}
  827. * @default 10
  828. * @since 2.3
  829. * @product highcharts
  830. * @apioption yAxis.plotBands.thickness
  831. */
  832. /**
  833. * @type {Array<*>}
  834. * @extends xAxis.plotLines
  835. * @apioption yAxis.plotLines
  836. */
  837. /* eslint-disable no-invalid-this, valid-jsdoc */
  838. /**
  839. * Internal function to create the SVG path definition for a plot band.
  840. *
  841. * @function Highcharts.Axis#getPlotBandPath
  842. *
  843. * @param {number} from
  844. * The axis value to start from.
  845. *
  846. * @param {number} to
  847. * The axis value to end on.
  848. *
  849. * @return {Highcharts.SVGPathArray}
  850. * The SVG path definition in array form.
  851. */
  852. getPlotBandPath: function (from, to) {
  853. var toPath = this.getPlotLinePath({
  854. value: to,
  855. force: true,
  856. acrossPanes: this.options.acrossPanes
  857. }), path = this.getPlotLinePath({
  858. value: from,
  859. force: true,
  860. acrossPanes: this.options.acrossPanes
  861. }), result = [], i,
  862. // #4964 check if chart is inverted or plotband is on yAxis
  863. horiz = this.horiz, plus = 1, isFlat, outside = (from < this.min && to < this.min) ||
  864. (from > this.max && to > this.max);
  865. if (path && toPath) {
  866. // Flat paths don't need labels (#3836)
  867. if (outside) {
  868. isFlat = path.toString() === toPath.toString();
  869. plus = 0;
  870. }
  871. // Go over each subpath - for panes in Highstock
  872. for (i = 0; i < path.length; i += 2) {
  873. var pathStart = path[i], pathEnd = path[i + 1], toPathStart = toPath[i], toPathEnd = toPath[i + 1];
  874. // Type checking all affected path segments. Consider something
  875. // smarter.
  876. if ((pathStart[0] === 'M' || pathStart[0] === 'L') &&
  877. (pathEnd[0] === 'M' || pathEnd[0] === 'L') &&
  878. (toPathStart[0] === 'M' || toPathStart[0] === 'L') &&
  879. (toPathEnd[0] === 'M' || toPathEnd[0] === 'L')) {
  880. // Add 1 pixel when coordinates are the same
  881. if (horiz && toPathStart[1] === pathStart[1]) {
  882. toPathStart[1] += plus;
  883. toPathEnd[1] += plus;
  884. }
  885. else if (!horiz && toPathStart[2] === pathStart[2]) {
  886. toPathStart[2] += plus;
  887. toPathEnd[2] += plus;
  888. }
  889. result.push(['M', pathStart[1], pathStart[2]], ['L', pathEnd[1], pathEnd[2]], ['L', toPathEnd[1], toPathEnd[2]], ['L', toPathStart[1], toPathStart[2]], ['Z']);
  890. }
  891. result.isFlat = isFlat;
  892. }
  893. }
  894. else { // outside the axis area
  895. path = null;
  896. }
  897. return result;
  898. },
  899. /**
  900. * Add a plot band after render time.
  901. *
  902. * @sample highcharts/members/axis-addplotband/
  903. * Toggle the plot band from a button
  904. *
  905. * @function Highcharts.Axis#addPlotBand
  906. *
  907. * @param {Highcharts.AxisPlotBandsOptions} options
  908. * A configuration object for the plot band, as defined in
  909. * [xAxis.plotBands](https://api.highcharts.com/highcharts/xAxis.plotBands).
  910. *
  911. * @return {Highcharts.PlotLineOrBand|undefined}
  912. * The added plot band.
  913. */
  914. addPlotBand: function (options) {
  915. return this.addPlotBandOrLine(options, 'plotBands');
  916. },
  917. /**
  918. * Add a plot line after render time.
  919. *
  920. * @sample highcharts/members/axis-addplotline/
  921. * Toggle the plot line from a button
  922. *
  923. * @function Highcharts.Axis#addPlotLine
  924. *
  925. * @param {Highcharts.AxisPlotLinesOptions} options
  926. * A configuration object for the plot line, as defined in
  927. * [xAxis.plotLines](https://api.highcharts.com/highcharts/xAxis.plotLines).
  928. *
  929. * @return {Highcharts.PlotLineOrBand|undefined}
  930. * The added plot line.
  931. */
  932. addPlotLine: function (options) {
  933. return this.addPlotBandOrLine(options, 'plotLines');
  934. },
  935. /**
  936. * Add a plot band or plot line after render time. Called from addPlotBand
  937. * and addPlotLine internally.
  938. *
  939. * @private
  940. * @function Highcharts.Axis#addPlotBandOrLine
  941. *
  942. * @param {Highcharts.AxisPlotBandsOptions|Highcharts.AxisPlotLinesOptions} options
  943. * The plotBand or plotLine configuration object.
  944. *
  945. * @param {"plotBands"|"plotLines"} [coll]
  946. *
  947. * @return {Highcharts.PlotLineOrBand|undefined}
  948. */
  949. addPlotBandOrLine: function (options, coll) {
  950. var obj = new PlotLineOrBand(this, options).render(), userOptions = this.userOptions;
  951. if (obj) { // #2189
  952. // Add it to the user options for exporting and Axis.update
  953. if (coll) {
  954. // Workaround Microsoft/TypeScript issue #32693
  955. var updatedOptions = (userOptions[coll] || []);
  956. updatedOptions.push(options);
  957. userOptions[coll] = updatedOptions;
  958. }
  959. this.plotLinesAndBands.push(obj);
  960. this._addedPlotLB = true;
  961. }
  962. return obj;
  963. },
  964. /**
  965. * Remove a plot band or plot line from the chart by id. Called internally
  966. * from `removePlotBand` and `removePlotLine`.
  967. *
  968. * @private
  969. * @function Highcharts.Axis#removePlotBandOrLine
  970. * @param {string} id
  971. * @return {void}
  972. */
  973. removePlotBandOrLine: function (id) {
  974. var plotLinesAndBands = this.plotLinesAndBands, options = this.options, userOptions = this.userOptions, i = plotLinesAndBands.length;
  975. while (i--) {
  976. if (plotLinesAndBands[i].id === id) {
  977. plotLinesAndBands[i].destroy();
  978. }
  979. }
  980. ([
  981. options.plotLines || [],
  982. userOptions.plotLines || [],
  983. options.plotBands || [],
  984. userOptions.plotBands || []
  985. ]).forEach(function (arr) {
  986. i = arr.length;
  987. while (i--) {
  988. if ((arr[i] || {}).id === id) {
  989. erase(arr, arr[i]);
  990. }
  991. }
  992. });
  993. },
  994. /**
  995. * Remove a plot band by its id.
  996. *
  997. * @sample highcharts/members/axis-removeplotband/
  998. * Remove plot band by id
  999. * @sample highcharts/members/axis-addplotband/
  1000. * Toggle the plot band from a button
  1001. *
  1002. * @function Highcharts.Axis#removePlotBand
  1003. *
  1004. * @param {string} id
  1005. * The plot band's `id` as given in the original configuration
  1006. * object or in the `addPlotBand` option.
  1007. *
  1008. * @return {void}
  1009. */
  1010. removePlotBand: function (id) {
  1011. this.removePlotBandOrLine(id);
  1012. },
  1013. /**
  1014. * Remove a plot line by its id.
  1015. *
  1016. * @sample highcharts/xaxis/plotlines-id/
  1017. * Remove plot line by id
  1018. * @sample highcharts/members/axis-addplotline/
  1019. * Toggle the plot line from a button
  1020. *
  1021. * @function Highcharts.Axis#removePlotLine
  1022. *
  1023. * @param {string} id
  1024. * The plot line's `id` as given in the original configuration
  1025. * object or in the `addPlotLine` option.
  1026. */
  1027. removePlotLine: function (id) {
  1028. this.removePlotBandOrLine(id);
  1029. }
  1030. });
  1031. H.PlotLineOrBand = PlotLineOrBand;
  1032. export default H.PlotLineOrBand;