PlotLineOrBand.js 31 KB

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