FlagsSeries.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. import BaseSeries from '../Core/Series/Series.js';
  11. import H from '../Core/Globals.js';
  12. var noop = H.noop;
  13. import OnSeriesMixin from '../Mixins/OnSeries.js';
  14. import SVGElement from '../Core/Renderer/SVG/SVGElement.js';
  15. import SVGRenderer from '../Core/Renderer/SVG/SVGRenderer.js';
  16. import U from '../Core/Utilities.js';
  17. var addEvent = U.addEvent, defined = U.defined, isNumber = U.isNumber, merge = U.merge, objectEach = U.objectEach, wrap = U.wrap;
  18. import './ColumnSeries.js';
  19. import '../Core/Interaction.js';
  20. import '../Core/Renderer/SVG/SVGRenderer.js';
  21. var Renderer = H.Renderer, Series = H.Series, TrackerMixin = H.TrackerMixin, // Interaction
  22. VMLRenderer = H.VMLRenderer, symbols = SVGRenderer.prototype.symbols;
  23. /**
  24. * @typedef {"circlepin"|"flag"|"squarepin"} Highcharts.FlagsShapeValue
  25. */
  26. ''; // detach doclets above
  27. /**
  28. * The Flags series.
  29. *
  30. * @private
  31. * @class
  32. * @name Highcharts.seriesTypes.flags
  33. *
  34. * @augments Highcharts.Series
  35. */
  36. BaseSeries.seriesType('flags', 'column'
  37. /**
  38. * Flags are used to mark events in stock charts. They can be added on the
  39. * timeline, or attached to a specific series.
  40. *
  41. * @sample stock/demo/flags-general/
  42. * Flags on a line series
  43. *
  44. * @extends plotOptions.column
  45. * @excluding animation, borderColor, borderRadius, borderWidth,
  46. * colorByPoint, dataGrouping, pointPadding, pointWidth,
  47. * turboThreshold
  48. * @product highstock
  49. * @optionparent plotOptions.flags
  50. */
  51. , {
  52. /**
  53. * In case the flag is placed on a series, on what point key to place
  54. * it. Line and columns have one key, `y`. In range or OHLC-type series,
  55. * however, the flag can optionally be placed on the `open`, `high`,
  56. * `low` or `close` key.
  57. *
  58. * @sample {highstock} stock/plotoptions/flags-onkey/
  59. * Range series, flag on high
  60. *
  61. * @type {string}
  62. * @default y
  63. * @since 4.2.2
  64. * @product highstock
  65. * @validvalue ["y", "open", "high", "low", "close"]
  66. * @apioption plotOptions.flags.onKey
  67. */
  68. /**
  69. * The id of the series that the flags should be drawn on. If no id
  70. * is given, the flags are drawn on the x axis.
  71. *
  72. * @sample {highstock} stock/plotoptions/flags/
  73. * Flags on series and on x axis
  74. *
  75. * @type {string}
  76. * @product highstock
  77. * @apioption plotOptions.flags.onSeries
  78. */
  79. pointRange: 0,
  80. /**
  81. * Whether the flags are allowed to overlap sideways. If `false`, the
  82. * flags are moved sideways using an algorithm that seeks to place every
  83. * flag as close as possible to its original position.
  84. *
  85. * @sample {highstock} stock/plotoptions/flags-allowoverlapx
  86. * Allow sideways overlap
  87. *
  88. * @since 6.0.4
  89. */
  90. allowOverlapX: false,
  91. /**
  92. * The shape of the marker. Can be one of "flag", "circlepin",
  93. * "squarepin", or an image of the format `url(/path-to-image.jpg)`.
  94. * Individual shapes can also be set for each point.
  95. *
  96. * @sample {highstock} stock/plotoptions/flags/
  97. * Different shapes
  98. *
  99. * @type {Highcharts.FlagsShapeValue}
  100. * @product highstock
  101. */
  102. shape: 'flag',
  103. /**
  104. * When multiple flags in the same series fall on the same value, this
  105. * number determines the vertical offset between them.
  106. *
  107. * @sample {highstock} stock/plotoptions/flags-stackdistance/
  108. * A greater stack distance
  109. *
  110. * @product highstock
  111. */
  112. stackDistance: 12,
  113. /**
  114. * Text alignment for the text inside the flag.
  115. *
  116. * @since 5.0.0
  117. * @product highstock
  118. * @validvalue ["left", "center", "right"]
  119. */
  120. textAlign: 'center',
  121. /**
  122. * Specific tooltip options for flag series. Flag series tooltips are
  123. * different from most other types in that a flag doesn't have a data
  124. * value, so the tooltip rather displays the `text` option for each
  125. * point.
  126. *
  127. * @extends plotOptions.series.tooltip
  128. * @excluding changeDecimals, valueDecimals, valuePrefix, valueSuffix
  129. * @product highstock
  130. */
  131. tooltip: {
  132. pointFormat: '{point.text}<br/>'
  133. },
  134. threshold: null,
  135. /**
  136. * The text to display on each flag. This can be defined on series
  137. * level, or individually for each point. Defaults to `"A"`.
  138. *
  139. * @type {string}
  140. * @default A
  141. * @product highstock
  142. * @apioption plotOptions.flags.title
  143. */
  144. /**
  145. * The y position of the top left corner of the flag relative to either
  146. * the series (if onSeries is defined), or the x axis. Defaults to
  147. * `-30`.
  148. *
  149. * @product highstock
  150. */
  151. y: -30,
  152. /**
  153. * Whether to use HTML to render the flag texts. Using HTML allows for
  154. * advanced formatting, images and reliable bi-directional text
  155. * rendering. Note that exported images won't respect the HTML, and that
  156. * HTML won't respect Z-index settings.
  157. *
  158. * @type {boolean}
  159. * @default false
  160. * @since 1.3
  161. * @product highstock
  162. * @apioption plotOptions.flags.useHTML
  163. */
  164. /**
  165. * Fixed width of the flag's shape. By default, width is autocalculated
  166. * according to the flag's title.
  167. *
  168. * @sample {highstock} stock/demo/flags-shapes/
  169. * Flags with fixed width
  170. *
  171. * @type {number}
  172. * @product highstock
  173. * @apioption plotOptions.flags.width
  174. */
  175. /**
  176. * Fixed height of the flag's shape. By default, height is
  177. * autocalculated according to the flag's title.
  178. *
  179. * @type {number}
  180. * @product highstock
  181. * @apioption plotOptions.flags.height
  182. */
  183. /**
  184. * The fill color for the flags.
  185. *
  186. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  187. * @product highstock
  188. */
  189. fillColor: '#ffffff',
  190. /**
  191. * The color of the line/border of the flag.
  192. *
  193. * In styled mode, the stroke is set in the
  194. * `.highcharts-flag-series.highcharts-point` rule.
  195. *
  196. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  197. * @default #000000
  198. * @product highstock
  199. * @apioption plotOptions.flags.lineColor
  200. */
  201. /**
  202. * The pixel width of the flag's line/border.
  203. *
  204. * @product highstock
  205. */
  206. lineWidth: 1,
  207. states: {
  208. /**
  209. * @extends plotOptions.column.states.hover
  210. * @product highstock
  211. */
  212. hover: {
  213. /**
  214. * The color of the line/border of the flag.
  215. *
  216. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  217. * @product highstock
  218. */
  219. lineColor: '#000000',
  220. /**
  221. * The fill or background color of the flag.
  222. *
  223. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  224. * @product highstock
  225. */
  226. fillColor: '#ccd6eb'
  227. }
  228. },
  229. /**
  230. * The text styles of the flag.
  231. *
  232. * In styled mode, the styles are set in the
  233. * `.highcharts-flag-series .highcharts-point` rule.
  234. *
  235. * @type {Highcharts.CSSObject}
  236. * @default {"fontSize": "11px", "fontWeight": "bold"}
  237. * @product highstock
  238. */
  239. style: {
  240. /** @ignore-option */
  241. fontSize: '11px',
  242. /** @ignore-option */
  243. fontWeight: 'bold'
  244. }
  245. },
  246. /**
  247. * @lends seriesTypes.flags.prototype
  248. */
  249. {
  250. sorted: false,
  251. noSharedTooltip: true,
  252. allowDG: false,
  253. takeOrdinalPosition: false,
  254. trackerGroups: ['markerGroup'],
  255. forceCrop: true,
  256. /* eslint-disable no-invalid-this, valid-jsdoc */
  257. /**
  258. * Inherit the initialization from base Series.
  259. *
  260. * @private
  261. * @borrows Highcharts.Series#init as Highcharts.seriesTypes.flags#init
  262. */
  263. init: Series.prototype.init,
  264. /**
  265. * Get presentational attributes
  266. *
  267. * @private
  268. * @function Highcharts.seriesTypes.flags#pointAttribs
  269. *
  270. * @param {Highcharts.Point} point
  271. *
  272. * @param {string} [state]
  273. *
  274. * @return {Highcharts.SVGAttributes}
  275. */
  276. pointAttribs: function (point, state) {
  277. var options = this.options, color = (point && point.color) || this.color, lineColor = options.lineColor, lineWidth = (point && point.lineWidth), fill = (point && point.fillColor) || options.fillColor;
  278. if (state) {
  279. fill = options.states[state].fillColor;
  280. lineColor = options.states[state].lineColor;
  281. lineWidth = options.states[state].lineWidth;
  282. }
  283. return {
  284. fill: fill || color,
  285. stroke: lineColor || color,
  286. 'stroke-width': lineWidth || options.lineWidth || 0
  287. };
  288. },
  289. translate: OnSeriesMixin.translate,
  290. getPlotBox: OnSeriesMixin.getPlotBox,
  291. /**
  292. * Draw the markers.
  293. *
  294. * @private
  295. * @function Highcharts.seriesTypes.flags#drawPoints
  296. * @return {void}
  297. */
  298. drawPoints: function () {
  299. var series = this, points = series.points, chart = series.chart, renderer = chart.renderer, plotX, plotY, inverted = chart.inverted, options = series.options, optionsY = options.y, shape, i, point, graphic, stackIndex, anchorY, attribs, outsideRight, yAxis = series.yAxis, boxesMap = {}, boxes = [], centered;
  300. i = points.length;
  301. while (i--) {
  302. point = points[i];
  303. outsideRight =
  304. (inverted ? point.plotY : point.plotX) >
  305. series.xAxis.len;
  306. plotX = point.plotX;
  307. stackIndex = point.stackIndex;
  308. shape = point.options.shape || options.shape;
  309. plotY = point.plotY;
  310. if (typeof plotY !== 'undefined') {
  311. plotY = point.plotY + optionsY -
  312. (typeof stackIndex !== 'undefined' &&
  313. (stackIndex * options.stackDistance));
  314. }
  315. // skip connectors for higher level stacked points
  316. point.anchorX = stackIndex ? void 0 : point.plotX;
  317. anchorY = stackIndex ? void 0 : point.plotY;
  318. centered = shape !== 'flag';
  319. graphic = point.graphic;
  320. // Only draw the point if y is defined and the flag is within
  321. // the visible area
  322. if (typeof plotY !== 'undefined' &&
  323. plotX >= 0 &&
  324. !outsideRight) {
  325. // Create the flag
  326. if (!graphic) {
  327. graphic = point.graphic = renderer.label('', null, null, shape, null, null, options.useHTML);
  328. if (!chart.styledMode) {
  329. graphic
  330. .attr(series.pointAttribs(point))
  331. .css(merge(options.style, point.style));
  332. }
  333. graphic.attr({
  334. align: centered ? 'center' : 'left',
  335. width: options.width,
  336. height: options.height,
  337. 'text-align': options.textAlign
  338. })
  339. .addClass('highcharts-point')
  340. .add(series.markerGroup);
  341. // Add reference to the point for tracker (#6303)
  342. if (point.graphic.div) {
  343. point.graphic.div.point = point;
  344. }
  345. if (!chart.styledMode) {
  346. graphic.shadow(options.shadow);
  347. }
  348. graphic.isNew = true;
  349. }
  350. if (plotX > 0) { // #3119
  351. plotX -= graphic.strokeWidth() % 2; // #4285
  352. }
  353. // Plant the flag
  354. attribs = {
  355. y: plotY,
  356. anchorY: anchorY
  357. };
  358. if (options.allowOverlapX) {
  359. attribs.x = plotX;
  360. attribs.anchorX = point.anchorX;
  361. }
  362. graphic.attr({
  363. text: point.options.title || options.title || 'A'
  364. })[graphic.isNew ? 'attr' : 'animate'](attribs);
  365. // Rig for the distribute function
  366. if (!options.allowOverlapX) {
  367. if (!boxesMap[point.plotX]) {
  368. boxesMap[point.plotX] = {
  369. align: centered ? 0.5 : 0,
  370. size: graphic.width,
  371. target: plotX,
  372. anchorX: plotX
  373. };
  374. }
  375. else {
  376. boxesMap[point.plotX].size = Math.max(boxesMap[point.plotX].size, graphic.width);
  377. }
  378. }
  379. // Set the tooltip anchor position
  380. point.tooltipPos = [
  381. plotX,
  382. plotY + yAxis.pos - chart.plotTop
  383. ]; // #6327
  384. }
  385. else if (graphic) {
  386. point.graphic = graphic.destroy();
  387. }
  388. }
  389. // Handle X-dimension overlapping
  390. if (!options.allowOverlapX) {
  391. objectEach(boxesMap, function (box) {
  392. box.plotX = box.anchorX;
  393. boxes.push(box);
  394. });
  395. H.distribute(boxes, inverted ? yAxis.len : this.xAxis.len, 100);
  396. points.forEach(function (point) {
  397. var box = point.graphic && boxesMap[point.plotX];
  398. if (box) {
  399. point.graphic[point.graphic.isNew ? 'attr' : 'animate']({
  400. x: box.pos + box.align * box.size,
  401. anchorX: point.anchorX
  402. });
  403. // Hide flag when its box position is not specified
  404. // (#8573, #9299)
  405. if (!defined(box.pos)) {
  406. point.graphic.attr({
  407. x: -9999,
  408. anchorX: -9999
  409. });
  410. point.graphic.isNew = true;
  411. }
  412. else {
  413. point.graphic.isNew = false;
  414. }
  415. }
  416. });
  417. }
  418. // Can be a mix of SVG and HTML and we need events for both (#6303)
  419. if (options.useHTML) {
  420. wrap(series.markerGroup, 'on', function (proceed) {
  421. return SVGElement.prototype.on.apply(
  422. // for HTML
  423. proceed.apply(this, [].slice.call(arguments, 1)),
  424. // and for SVG
  425. [].slice.call(arguments, 1));
  426. });
  427. }
  428. },
  429. /**
  430. * Extend the column trackers with listeners to expand and contract
  431. * stacks.
  432. *
  433. * @private
  434. * @function Highcharts.seriesTypes.flags#drawTracker
  435. * @return {void}
  436. */
  437. drawTracker: function () {
  438. var series = this, points = series.points;
  439. TrackerMixin.drawTrackerPoint.apply(this);
  440. /* *
  441. * Bring each stacked flag up on mouse over, this allows readability
  442. * of vertically stacked elements as well as tight points on the x
  443. * axis. #1924.
  444. */
  445. points.forEach(function (point) {
  446. var graphic = point.graphic;
  447. if (graphic) {
  448. addEvent(graphic.element, 'mouseover', function () {
  449. // Raise this point
  450. if (point.stackIndex > 0 &&
  451. !point.raised) {
  452. point._y = graphic.y;
  453. graphic.attr({
  454. y: point._y - 8
  455. });
  456. point.raised = true;
  457. }
  458. // Revert other raised points
  459. points.forEach(function (otherPoint) {
  460. if (otherPoint !== point &&
  461. otherPoint.raised &&
  462. otherPoint.graphic) {
  463. otherPoint.graphic.attr({
  464. y: otherPoint._y
  465. });
  466. otherPoint.raised = false;
  467. }
  468. });
  469. });
  470. }
  471. });
  472. },
  473. /**
  474. * Disable animation, but keep clipping (#8546).
  475. *
  476. * @private
  477. * @function Highcharts.seriesTypes.flags#animate
  478. * @param {boolean} [init]
  479. * @return {void}
  480. */
  481. animate: function (init) {
  482. if (init) {
  483. this.setClip();
  484. }
  485. },
  486. /**
  487. * @private
  488. * @function Highcharts.seriesTypes.flags#setClip
  489. * @return {void}
  490. */
  491. setClip: function () {
  492. Series.prototype.setClip.apply(this, arguments);
  493. if (this.options.clip !== false && this.sharedClipKey) {
  494. this.markerGroup
  495. .clip(this.chart[this.sharedClipKey]);
  496. }
  497. },
  498. /**
  499. * @private
  500. * @function Highcharts.seriesTypes.flags#buildKDTree
  501. */
  502. buildKDTree: noop,
  503. /**
  504. * Don't invert the flag marker group (#4960).
  505. *
  506. * @private
  507. * @function Highcharts.seriesTypes.flags#invertGroups
  508. */
  509. invertGroups: noop
  510. /* eslint-enable no-invalid-this, valid-jsdoc */
  511. },
  512. /**
  513. * @lends Highcharts.seriesTypes.flag.prototype.pointClass.prototype
  514. */
  515. {
  516. isValid: function () {
  517. // #9233 - Prevent from treating flags as null points (even if
  518. // they have no y values defined).
  519. return isNumber(this.y) || typeof this.y === 'undefined';
  520. }
  521. });
  522. // create the flag icon with anchor
  523. symbols.flag = function (x, y, w, h, options) {
  524. var anchorX = (options && options.anchorX) || x, anchorY = (options && options.anchorY) || y;
  525. // To do: unwanted any cast because symbols.circle has wrong type, it
  526. // actually returns an SVGPathArray
  527. var path = symbols.circle(anchorX - 1, anchorY - 1, 2, 2);
  528. path.push(['M', anchorX, anchorY], ['L', x, y + h], ['L', x, y], ['L', x + w, y], ['L', x + w, y + h], ['L', x, y + h], ['Z']);
  529. return path;
  530. };
  531. /**
  532. * Create the circlepin and squarepin icons with anchor.
  533. * @private
  534. * @param {string} shape - circle or square
  535. * @return {void}
  536. */
  537. function createPinSymbol(shape) {
  538. symbols[shape + 'pin'] = function (x, y, w, h, options) {
  539. var anchorX = options && options.anchorX, anchorY = options && options.anchorY, path;
  540. // For single-letter flags, make sure circular flags are not taller
  541. // than their width
  542. if (shape === 'circle' && h > w) {
  543. x -= Math.round((h - w) / 2);
  544. w = h;
  545. }
  546. path = (symbols[shape])(x, y, w, h);
  547. if (anchorX && anchorY) {
  548. /**
  549. * If the label is below the anchor, draw the connecting line from
  550. * the top edge of the label, otherwise start drawing from the
  551. * bottom edge
  552. */
  553. var labelX = anchorX;
  554. if (shape === 'circle') {
  555. labelX = x + w / 2;
  556. }
  557. else {
  558. var startSeg = path[0];
  559. var endSeg = path[1];
  560. if (startSeg[0] === 'M' && endSeg[0] === 'L') {
  561. labelX = (startSeg[1] + endSeg[1]) / 2;
  562. }
  563. }
  564. var labelY = (y > anchorY) ? y : y + h;
  565. path.push([
  566. 'M',
  567. labelX,
  568. labelY
  569. ], [
  570. 'L',
  571. anchorX,
  572. anchorY
  573. ]);
  574. path = path.concat(symbols.circle(anchorX - 1, anchorY - 1, 2, 2));
  575. }
  576. return path;
  577. };
  578. }
  579. createPinSymbol('circle');
  580. createPinSymbol('square');
  581. /**
  582. * The symbol callbacks are generated on the SVGRenderer object in all browsers.
  583. * Even VML browsers need this in order to generate shapes in export. Now share
  584. * them with the VMLRenderer.
  585. */
  586. if (Renderer === VMLRenderer) {
  587. ['circlepin', 'flag', 'squarepin'].forEach(function (shape) {
  588. VMLRenderer.prototype.symbols[shape] = symbols[shape];
  589. });
  590. }
  591. /**
  592. * A `flags` series. If the [type](#series.flags.type) option is not
  593. * specified, it is inherited from [chart.type](#chart.type).
  594. *
  595. * @extends series,plotOptions.flags
  596. * @excluding animation, borderColor, borderRadius, borderWidth, colorByPoint,
  597. * connectNulls, dashStyle, dataGrouping, dataParser, dataURL,
  598. * gapSize, gapUnit, linecap, lineWidth, marker, pointPadding,
  599. * pointWidth, step, turboThreshold, useOhlcData
  600. * @product highstock
  601. * @apioption series.flags
  602. */
  603. /**
  604. * An array of data points for the series. For the `flags` series type,
  605. * points can be given in the following ways:
  606. *
  607. * 1. An array of objects with named values. The following snippet shows only a
  608. * few settings, see the complete options set below. If the total number of
  609. * data points exceeds the series'
  610. * [turboThreshold](#series.flags.turboThreshold), this option is not
  611. * available.
  612. * ```js
  613. * data: [{
  614. * x: 1,
  615. * title: "A",
  616. * text: "First event"
  617. * }, {
  618. * x: 1,
  619. * title: "B",
  620. * text: "Second event"
  621. * }]
  622. * ```
  623. *
  624. * @type {Array<*>}
  625. * @extends series.line.data
  626. * @excluding dataLabels, marker, name, y
  627. * @product highstock
  628. * @apioption series.flags.data
  629. */
  630. /**
  631. * The fill color of an individual flag. By default it inherits from
  632. * the series color.
  633. *
  634. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  635. * @product highstock
  636. * @apioption series.flags.data.fillColor
  637. */
  638. /**
  639. * The longer text to be shown in the flag's tooltip.
  640. *
  641. * @type {string}
  642. * @product highstock
  643. * @apioption series.flags.data.text
  644. */
  645. /**
  646. * The short text to be shown on the flag.
  647. *
  648. * @type {string}
  649. * @product highstock
  650. * @apioption series.flags.data.title
  651. */
  652. ''; // adds doclets above to transpiled file