annotations.src.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. /**
  2. * @license Highcharts JS v6.1.0 (2018-04-13)
  3. * Annotations module
  4. *
  5. * (c) 2009-2017 Torstein Honsi
  6. *
  7. * License: www.highcharts.com/license
  8. */
  9. 'use strict';
  10. (function (factory) {
  11. if (typeof module === 'object' && module.exports) {
  12. module.exports = factory;
  13. } else {
  14. factory(Highcharts);
  15. }
  16. }(function (Highcharts) {
  17. (function (H) {
  18. /**
  19. * (c) 2009-2017 Highsoft, Black Label
  20. *
  21. * License: www.highcharts.com/license
  22. */
  23. var merge = H.merge,
  24. addEvent = H.addEvent,
  25. extend = H.extend,
  26. each = H.each,
  27. isString = H.isString,
  28. isNumber = H.isNumber,
  29. defined = H.defined,
  30. isObject = H.isObject,
  31. inArray = H.inArray,
  32. erase = H.erase,
  33. find = H.find,
  34. format = H.format,
  35. pick = H.pick,
  36. objectEach = H.objectEach,
  37. uniqueKey = H.uniqueKey,
  38. doc = H.doc,
  39. splat = H.splat,
  40. destroyObjectProperties = H.destroyObjectProperties,
  41. grep = H.grep,
  42. tooltipPrototype = H.Tooltip.prototype,
  43. seriesPrototype = H.Series.prototype,
  44. chartPrototype = H.Chart.prototype;
  45. /* ***************************************************************************
  46. *
  47. * MARKER SECTION
  48. * Contains objects and functions for adding a marker element to a path element
  49. *
  50. **************************************************************************** */
  51. /**
  52. * Options for configuring markers for annotations.
  53. *
  54. * An example of the arrow marker:
  55. * <pre>
  56. * {
  57. * arrow: {
  58. * id: 'arrow',
  59. * tagName: 'marker',
  60. * refY: 5,
  61. * refX: 5,
  62. * markerWidth: 10,
  63. * markerHeight: 10,
  64. * children: [{
  65. * tagName: 'path',
  66. * attrs: {
  67. * d: 'M 0 0 L 10 5 L 0 10 Z',
  68. * strokeWidth: 0
  69. * }
  70. * }]
  71. * }
  72. * }
  73. * </pre>
  74. * @type {Object}
  75. * @sample highcharts/annotations/custom-markers/
  76. * Define a custom marker for annotations
  77. * @sample highcharts/css/annotations-markers/
  78. * Define markers in a styled mode
  79. * @since 6.0.0
  80. * @apioption defs
  81. */
  82. var defaultMarkers = {
  83. arrow: {
  84. tagName: 'marker',
  85. render: false,
  86. id: 'arrow',
  87. refY: 5,
  88. refX: 5,
  89. markerWidth: 10,
  90. markerHeight: 10,
  91. children: [{
  92. tagName: 'path',
  93. d: 'M 0 0 L 10 5 L 0 10 Z', // triangle (used as an arrow)
  94. strokeWidth: 0
  95. }]
  96. }
  97. };
  98. var MarkerMixin = {
  99. markerSetter: function (markerType) {
  100. return function (value) {
  101. this.attr(markerType, 'url(#' + value + ')');
  102. };
  103. }
  104. };
  105. extend(MarkerMixin, {
  106. markerEndSetter: MarkerMixin.markerSetter('marker-end'),
  107. markerStartSetter: MarkerMixin.markerSetter('marker-start')
  108. });
  109. // In a styled mode definition is implemented
  110. H.SVGRenderer.prototype.definition = function (def) {
  111. var ren = this;
  112. function recurse(config, parent) {
  113. var ret;
  114. each(splat(config), function (item) {
  115. var node = ren.createElement(item.tagName),
  116. attr = {};
  117. // Set attributes
  118. objectEach(item, function (val, key) {
  119. if (
  120. key !== 'tagName' &&
  121. key !== 'children' &&
  122. key !== 'textContent'
  123. ) {
  124. attr[key] = val;
  125. }
  126. });
  127. node.attr(attr);
  128. // Add to the tree
  129. node.add(parent || ren.defs);
  130. // Add text content
  131. if (item.textContent) {
  132. node.element.appendChild(
  133. doc.createTextNode(item.textContent)
  134. );
  135. }
  136. // Recurse
  137. recurse(item.children || [], node);
  138. ret = node;
  139. });
  140. // Return last node added (on top level it's the only one)
  141. return ret;
  142. }
  143. return recurse(def);
  144. };
  145. H.SVGRenderer.prototype.addMarker = function (id, markerOptions) {
  146. var options = { id: id };
  147. var attrs = {
  148. stroke: markerOptions.color || 'none',
  149. fill: markerOptions.color || 'rgba(0, 0, 0, 0.75)'
  150. };
  151. options.children = H.map(markerOptions.children, function (child) {
  152. return merge(attrs, child);
  153. });
  154. var marker = this.definition(merge({
  155. markerWidth: 20,
  156. markerHeight: 20,
  157. refX: 0,
  158. refY: 0,
  159. orient: 'auto'
  160. }, markerOptions, options));
  161. marker.id = id;
  162. return marker;
  163. };
  164. /* ***************************************************************************
  165. *
  166. * MOCK POINT
  167. *
  168. **************************************************************************** */
  169. /**
  170. * A mock point configuration.
  171. *
  172. * @typedef {Object} MockPointOptions
  173. * @property {Number} x - x value for the point in xAxis scale or pixels
  174. * @property {Number} y - y value for the point in yAxis scale or pixels
  175. * @property {String|Number} [xAxis] - xAxis index or id
  176. * @property {String|Number} [yAxis] - yAxis index or id
  177. */
  178. /**
  179. * A trimmed point object which imitates {@link Highchart.Point} class.
  180. * It is created when there is a need of pointing to some chart's position
  181. * using axis values or pixel values
  182. *
  183. * @class MockPoint
  184. * @memberOf Highcharts
  185. * @private
  186. *
  187. * @param {Highcharts.Chart} - the chart object
  188. * @param {MockPointOptions} - the options object
  189. */
  190. var MockPoint = H.MockPoint = function (chart, options) {
  191. this.mock = true;
  192. this.series = {
  193. visible: true,
  194. chart: chart,
  195. getPlotBox: seriesPrototype.getPlotBox
  196. };
  197. // this.plotX
  198. // this.plotY
  199. /* Those might not exist if a specific axis was not found/defined */
  200. // this.x?
  201. // this.y?
  202. this.init(chart, options);
  203. };
  204. /**
  205. * A factory function for creating a mock point object
  206. *
  207. * @function #mockPoint
  208. * @memberOf Highcharts
  209. *
  210. * @param {MockPointOptions} mockPointOptions
  211. * @return {MockPoint} a mock point
  212. */
  213. var mockPoint = H.mockPoint = function (chart, mockPointOptions) {
  214. return new MockPoint(chart, mockPointOptions);
  215. };
  216. MockPoint.prototype = {
  217. /**
  218. * Initialisation of the mock point
  219. *
  220. * @function init
  221. * @memberOf Highcharts.MockPoint#
  222. *
  223. * @param {Highcharts.Chart} chart - a chart object to which the mock point
  224. * is attached
  225. * @param {MockPointOptions} options - a config for the mock point
  226. */
  227. init: function (chart, options) {
  228. var xAxisId = options.xAxis,
  229. xAxis = defined(xAxisId) ?
  230. chart.xAxis[xAxisId] || chart.get(xAxisId) :
  231. null,
  232. yAxisId = options.yAxis,
  233. yAxis = defined(yAxisId) ?
  234. chart.yAxis[yAxisId] || chart.get(yAxisId) :
  235. null;
  236. if (xAxis) {
  237. this.x = options.x;
  238. this.series.xAxis = xAxis;
  239. } else {
  240. this.plotX = options.x;
  241. }
  242. if (yAxis) {
  243. this.y = options.y;
  244. this.series.yAxis = yAxis;
  245. } else {
  246. this.plotY = options.y;
  247. }
  248. },
  249. /**
  250. * Update of the point's coordinates (plotX/plotY)
  251. *
  252. * @function translate
  253. * @memberOf Highcharts.MockPoint#
  254. *
  255. * @return {undefined}
  256. */
  257. translate: function () {
  258. var series = this.series,
  259. xAxis = series.xAxis,
  260. yAxis = series.yAxis;
  261. if (xAxis) {
  262. this.plotX = xAxis.toPixels(this.x, true);
  263. }
  264. if (yAxis) {
  265. this.plotY = yAxis.toPixels(this.y, true);
  266. }
  267. this.isInside = this.isInsidePane();
  268. },
  269. /**
  270. * Returns a box to which an item can be aligned to
  271. *
  272. * @function #alignToBox
  273. * @memberOf Highcharts.MockPoint#
  274. *
  275. * @param {Boolean} [forceTranslate=false] - whether to update the point's
  276. * coordinates
  277. * @return {Array.<Number>} A quadruple of numbers which denotes x, y,
  278. * width and height of the box
  279. **/
  280. alignToBox: function (forceTranslate) {
  281. if (forceTranslate) {
  282. this.translate();
  283. }
  284. var x = this.plotX,
  285. y = this.plotY,
  286. temp;
  287. if (this.series.chart.inverted) {
  288. temp = x;
  289. x = y;
  290. y = temp;
  291. }
  292. return [x, y, 0, 0];
  293. },
  294. /**
  295. * Returns a label config object -
  296. * the same as Highcharts.Point.prototype.getLabelConfig
  297. *
  298. * @function getLabelConfig
  299. * @memberOf Highcharts.MockPoint#
  300. *
  301. * @return {Object} labelConfig - label config object
  302. * @return {Number|undefined} labelConfig.x
  303. * X value translated to x axis scale
  304. * @return {Number|undefined} labelConfig.y
  305. * Y value translated to y axis scale
  306. * @return {MockPoint} labelConfig.point
  307. * The instance of the point
  308. */
  309. getLabelConfig: function () {
  310. return {
  311. x: this.x,
  312. y: this.y,
  313. point: this
  314. };
  315. },
  316. isInsidePane: function () {
  317. var plotX = this.plotX,
  318. plotY = this.plotY,
  319. xAxis = this.series.xAxis,
  320. yAxis = this.series.yAxis,
  321. isInside = true;
  322. if (xAxis) {
  323. isInside = defined(plotX) && plotX >= 0 && plotX <= xAxis.len;
  324. }
  325. if (yAxis) {
  326. isInside =
  327. isInside &&
  328. defined(plotY) &&
  329. plotY >= 0 && plotY <= yAxis.len;
  330. }
  331. return isInside;
  332. }
  333. };
  334. /* ***************************************************************************
  335. *
  336. * ANNOTATION
  337. *
  338. **************************************************************************** */
  339. H.defaultOptions.annotations = [];
  340. /**
  341. * An annotation class which serves as a container for items like labels or
  342. * shapes. Created items are positioned on the chart either by linking them to
  343. * existing points or created mock points
  344. *
  345. * @class Annotation
  346. * @memberOf Highcharts
  347. *
  348. * @param {Chart} - the chart object
  349. * @param {AnnotationOptions} - the options object
  350. */
  351. var Annotation = H.Annotation = function (chart, userOptions) {
  352. /**
  353. * The chart that the annotation belongs to.
  354. *
  355. * @name chart
  356. * @memberOf Highcharts.Annotation#
  357. * @type {Chart}
  358. */
  359. this.chart = chart;
  360. /**
  361. * The array of labels which belong to the annotation.
  362. *
  363. * @name labels
  364. * @memberOf Highcharts.Annotation#
  365. * @type {Array<Highcharts.SVGElement>}
  366. */
  367. this.labels = [];
  368. /**
  369. * The array of shapes which belong to the annotation.
  370. *
  371. * @name shapes
  372. * @memberOf Highcharts.Annotation#
  373. * @type {Array<Highcharts.SVGElement>}
  374. */
  375. this.shapes = [];
  376. /**
  377. * The options for the annotations. It containers user defined options
  378. * merged with the default options.
  379. *
  380. * @name options
  381. * @memberOf Highcharts.Annotation#
  382. * @type {AnnotationOptions}
  383. */
  384. this.options = merge(this.defaultOptions, userOptions);
  385. /**
  386. * The callback that reports to the overlapping-labels module which
  387. * labels it should account for.
  388. *
  389. * @name labelCollector
  390. * @memberOf Highcharts.Annotation#
  391. * @type {Function}
  392. * @private
  393. */
  394. /**
  395. * The group element of the annotation.
  396. *
  397. * @name group
  398. * @memberOf Highcharts.Annotation#
  399. * @type {Highcharts.SVGElement}
  400. * @private
  401. */
  402. /**
  403. * The group element of the annotation's shapes.
  404. *
  405. * @name shapesGroup
  406. * @memberOf Highcharts.Annotation#
  407. * @type {Highcharts.SVGElement}
  408. * @private
  409. */
  410. /**
  411. * The group element of the annotation's labels.
  412. *
  413. * @name labelsGroup
  414. * @memberOf Highcharts.Annotation#
  415. * @type {Highcharts.SVGElement}
  416. * @private
  417. */
  418. this.init(chart, userOptions);
  419. };
  420. Annotation.prototype = /** @lends Highcharts.Annotation# */ {
  421. /**
  422. * Shapes which do not have background - the object is used for proper
  423. * setting of the contrast color
  424. *
  425. * @type {Array.<String>}
  426. * @private
  427. */
  428. shapesWithoutBackground: ['connector'],
  429. /**
  430. * A map object which allows to map options attributes to element
  431. * attributes.
  432. *
  433. * @type {Object}
  434. * @private
  435. */
  436. attrsMap: {
  437. backgroundColor: 'fill',
  438. borderColor: 'stroke',
  439. borderWidth: 'stroke-width',
  440. dashStyle: 'dashstyle',
  441. strokeWidth: 'stroke-width',
  442. stroke: 'stroke',
  443. fill: 'fill',
  444. zIndex: 'zIndex',
  445. width: 'width',
  446. height: 'height',
  447. borderRadius: 'r',
  448. r: 'r',
  449. padding: 'padding'
  450. },
  451. /**
  452. * Options for configuring annotations, for example labels, arrows or
  453. * shapes. Annotations can be tied to points, axis coordinates or chart
  454. * pixel coordinates.
  455. *
  456. * @private
  457. * @type {Array<Object>}
  458. * @sample highcharts/annotations/basic/
  459. * Basic annotations
  460. * @sample highcharts/demo/annotations/
  461. * Advanced annotations
  462. * @sample highcharts/css/annotations
  463. * Styled mode
  464. * @sample {highstock} stock/annotations/fibonacci-retracements
  465. * Custom annotation, Fibonacci retracement
  466. * @since 6.0.0
  467. * @optionparent annotations
  468. */
  469. defaultOptions: {
  470. /**
  471. * Whether the annotation is visible.
  472. *
  473. * @sample highcharts/annotations/visible/
  474. * Set annotation visibility
  475. */
  476. visible: true,
  477. /**
  478. * Options for annotation's labels. Each label inherits options
  479. * from the labelOptions object. An option from the labelOptions can be
  480. * overwritten by config for a specific label.
  481. */
  482. labelOptions: {
  483. /**
  484. * The alignment of the annotation's label. If right,
  485. * the right side of the label should be touching the point.
  486. *
  487. * @validvalue ["left", "center", "right"]
  488. * @sample highcharts/annotations/label-position/
  489. * Set labels position
  490. */
  491. align: 'center',
  492. /**
  493. * Whether to allow the annotation's labels to overlap.
  494. * To make the labels less sensitive for overlapping,
  495. * the can be set to 0.
  496. *
  497. * @sample highcharts/annotations/tooltip-like/
  498. * Hide overlapping labels
  499. */
  500. allowOverlap: false,
  501. /**
  502. * The background color or gradient for the annotation's label.
  503. *
  504. * @type {Color}
  505. * @sample highcharts/annotations/label-presentation/
  506. * Set labels graphic options
  507. */
  508. backgroundColor: 'rgba(0, 0, 0, 0.75)',
  509. /**
  510. * The border color for the annotation's label.
  511. *
  512. * @type {Color}
  513. * @sample highcharts/annotations/label-presentation/
  514. * Set labels graphic options
  515. */
  516. borderColor: 'black',
  517. /**
  518. * The border radius in pixels for the annotaiton's label.
  519. *
  520. * @sample highcharts/annotations/label-presentation/
  521. * Set labels graphic options
  522. */
  523. borderRadius: 3,
  524. /**
  525. * The border width in pixels for the annotation's label
  526. *
  527. * @sample highcharts/annotations/label-presentation/
  528. * Set labels graphic options
  529. */
  530. borderWidth: 1,
  531. /**
  532. * A class name for styling by CSS.
  533. *
  534. * @sample highcharts/css/annotations
  535. * Styled mode annotations
  536. * @since 6.0.5
  537. */
  538. className: '',
  539. /**
  540. * Whether to hide the annotation's label that is outside the plot
  541. * area.
  542. *
  543. * @sample highcharts/annotations/label-crop-overflow/
  544. * Crop or justify labels
  545. */
  546. crop: false,
  547. /**
  548. * The label's pixel distance from the point.
  549. *
  550. * @type {Number}
  551. * @sample highcharts/annotations/label-position/
  552. * Set labels position
  553. * @default undefined
  554. * @apioption annotations.labelOptions.distance
  555. */
  556. /**
  557. * A [format](https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting) string for the data label.
  558. *
  559. * @type {String}
  560. * @see [plotOptions.series.dataLabels.format](
  561. * plotOptions.series.dataLabels.format.html)
  562. * @sample highcharts/annotations/label-text/
  563. * Set labels text
  564. * @default undefined
  565. * @apioption annotations.labelOptions.format
  566. */
  567. /**
  568. * Alias for the format option.
  569. *
  570. * @type {String}
  571. * @see [format](annotations.labelOptions.format.html)
  572. * @sample highcharts/annotations/label-text/
  573. * Set labels text
  574. * @default undefined
  575. * @apioption annotations.labelOptions.text
  576. */
  577. /**
  578. * Callback JavaScript function to format the annotation's label.
  579. * Note that if a `format` or `text` are defined, the format or text
  580. * take precedence and the formatter is ignored. `This` refers to a
  581. * point object.
  582. *
  583. * @type {Function}
  584. * @sample highcharts/annotations/label-text/
  585. * Set labels text
  586. * @default function () {
  587. * return defined(this.y) ? this.y : 'Annotation label';
  588. * }
  589. */
  590. formatter: function () {
  591. return defined(this.y) ? this.y : 'Annotation label';
  592. },
  593. /**
  594. * How to handle the annotation's label that flow outside the plot
  595. * area. The justify option aligns the label inside the plot area.
  596. *
  597. * @validvalue ["none", "justify"]
  598. * @sample highcharts/annotations/label-crop-overflow/
  599. * Crop or justify labels
  600. **/
  601. overflow: 'justify',
  602. /**
  603. * When either the borderWidth or the backgroundColor is set,
  604. * this is the padding within the box.
  605. *
  606. * @sample highcharts/annotations/label-presentation/
  607. * Set labels graphic options
  608. */
  609. padding: 5,
  610. /**
  611. * The shadow of the box. The shadow can be an object configuration
  612. * containing `color`, `offsetX`, `offsetY`, `opacity` and `width`.
  613. *
  614. * @type {Boolean|Object}
  615. * @sample highcharts/annotations/label-presentation/
  616. * Set labels graphic options
  617. */
  618. shadow: false,
  619. /**
  620. * The name of a symbol to use for the border around the label.
  621. * Symbols are predefined functions on the Renderer object.
  622. *
  623. * @type {String}
  624. * @sample highcharts/annotations/shapes/
  625. * Available shapes for labels
  626. */
  627. shape: 'callout',
  628. /**
  629. * Styles for the annotation's label.
  630. *
  631. * @type {CSSObject}
  632. * @sample highcharts/annotations/label-presentation/
  633. * Set labels graphic options
  634. * @see [plotOptions.series.dataLabels.style](
  635. * plotOptions.series.dataLabels.style.html)
  636. */
  637. style: {
  638. fontSize: '11px',
  639. fontWeight: 'normal',
  640. color: 'contrast'
  641. },
  642. /**
  643. * Whether to [use HTML](http://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting#html)
  644. * to render the annotation's label.
  645. *
  646. * @type {Boolean}
  647. * @default false
  648. */
  649. useHTML: false,
  650. /**
  651. * The vertical alignment of the annotation's label.
  652. *
  653. * @type {String}
  654. * @validvalue ["top", "middle", "bottom"]
  655. * @sample highcharts/annotations/label-position/
  656. * Set labels position
  657. */
  658. verticalAlign: 'bottom',
  659. /**
  660. * The x position offset of the label relative to the point.
  661. * Note that if a `distance` is defined, the distance takes
  662. * precedence over `x` and `y` options.
  663. *
  664. * @sample highcharts/annotations/label-position/
  665. * Set labels position
  666. */
  667. x: 0,
  668. /**
  669. * The y position offset of the label relative to the point.
  670. * Note that if a `distance` is defined, the distance takes
  671. * precedence over `x` and `y` options.
  672. *
  673. * @sample highcharts/annotations/label-position/
  674. * Set labels position
  675. */
  676. y: -16
  677. },
  678. /**
  679. * An array of labels for the annotation. For options that apply to
  680. * multiple labels, they can be added to the
  681. * [labelOptions](annotations.labelOptions.html).
  682. *
  683. * @type {Array<Object>}
  684. * @extends annotations.labelOptions
  685. * @apioption annotations.labels
  686. */
  687. /**
  688. * This option defines the point to which the label will be connected.
  689. * It can be either the point which exists in the series - it is
  690. * referenced by the point's id - or a new point with defined x, y
  691. * properies and optionally axes.
  692. *
  693. * @type {String|Object}
  694. * @sample highcharts/annotations/mock-point/
  695. * Attach annotation to a mock point
  696. * @apioption annotations.labels.point
  697. */
  698. /**
  699. * The x position of the point. Units can be either in axis
  700. * or chart pixel coordinates.
  701. *
  702. * @type {Number}
  703. * @apioption annotations.labels.point.x
  704. */
  705. /**
  706. * The y position of the point. Units can be either in axis
  707. * or chart pixel coordinates.
  708. *
  709. * @type {Number}
  710. * @apioption annotations.labels.point.y
  711. */
  712. /**
  713. * This number defines which xAxis the point is connected to. It refers
  714. * to either the axis id or the index of the axis in the xAxis array.
  715. * If the option is not configured or the axis is not found the point's
  716. * x coordinate refers to the chart pixels.
  717. *
  718. * @type {Number|String}
  719. * @apioption annotations.labels.point.xAxis
  720. */
  721. /**
  722. * This number defines which yAxis the point is connected to. It refers
  723. * to either the axis id or the index of the axis in the yAxis array.
  724. * If the option is not configured or the axis is not found the point's
  725. * y coordinate refers to the chart pixels.
  726. *
  727. * @type {Number|String}
  728. * @apioption annotations.labels.point.yAxis
  729. */
  730. /**
  731. * An array of shapes for the annotation. For options that apply to
  732. * multiple shapes, then can be added to the
  733. * [shapeOptions](annotations.shapeOptions.html).
  734. *
  735. * @type {Array<Object>}
  736. * @extends annotations.shapeOptions
  737. * @apioption annotations.shapes
  738. */
  739. /**
  740. * This option defines the point to which the shape will be connected.
  741. * It can be either the point which exists in the series - it is
  742. * referenced by the point's id - or a new point with defined x, y
  743. * properties and optionally axes.
  744. *
  745. * @type {String|Object}
  746. * @extends annotations.labels.point
  747. * @apioption annotations.shapes.point
  748. */
  749. /**
  750. * An array of points for the shape. This option is available for shapes
  751. * which can use multiple points such as path. A point can be either
  752. * a point object or a point's id.
  753. *
  754. * @type {Array}
  755. * @see [annotations.shapes.point](annotations.shapes.point.html)
  756. * @apioption annotations.shapes.points
  757. */
  758. /**
  759. * Id of the marker which will be drawn at the final vertex of the path.
  760. * Custom markers can be defined in defs property.
  761. *
  762. * @type {String}
  763. * @see [defs.markers](defs.markers.html)
  764. * @sample highcharts/annotations/custom-markers/
  765. * Define a custom marker for annotations
  766. * @apioption annotations.shapes.markerEnd
  767. */
  768. /**
  769. * Id of the marker which will be drawn at the first vertex of the path.
  770. * Custom markers can be defined in defs property.
  771. *
  772. * @type {String}
  773. * @see [defs.markers](defs.markers.html)
  774. * @sample {highcharts} highcharts/annotations/custom-markers/
  775. * Define a custom marker for annotations
  776. * @apioption annotations.shapes.markerStart
  777. */
  778. /**
  779. * Options for annotation's shapes. Each shape inherits options
  780. * from the shapeOptions object. An option from the shapeOptions can be
  781. * overwritten by config for a specific shape.
  782. *
  783. * @type {Object}
  784. */
  785. shapeOptions: {
  786. /**
  787. * The width of the shape.
  788. *
  789. * @type {Number}
  790. * @sample highcharts/annotations/shape/
  791. * Basic shape annotation
  792. * @apioption annotations.shapeOptions.width
  793. **/
  794. /**
  795. * The height of the shape.
  796. *
  797. * @type {Number}
  798. * @sample highcharts/annotations/shape/
  799. * Basic shape annotation
  800. * @apioption annotations.shapeOptions.height
  801. */
  802. /**
  803. * The color of the shape's stroke.
  804. *
  805. * @type {Color}
  806. * @sample highcharts/annotations/shape/
  807. * Basic shape annotation
  808. */
  809. stroke: 'rgba(0, 0, 0, 0.75)',
  810. /**
  811. * The pixel stroke width of the shape.
  812. *
  813. * @sample highcharts/annotations/shape/
  814. * Basic shape annotation
  815. */
  816. strokeWidth: 1,
  817. /**
  818. * The color of the shape's fill.
  819. *
  820. * @type {Color}
  821. * @sample highcharts/annotations/shape/
  822. * Basic shape annotation
  823. */
  824. fill: 'rgba(0, 0, 0, 0.75)',
  825. /**
  826. * The type of the shape, e.g. circle or rectangle.
  827. *
  828. * @type {String}
  829. * @sample highcharts/annotations/shape/
  830. * Basic shape annotation
  831. * @default 'rect'
  832. * @apioption annotations.shapeOptions.type
  833. */
  834. /**
  835. * The radius of the shape.
  836. *
  837. * @sample highcharts/annotations/shape/
  838. * Basic shape annotation
  839. */
  840. r: 0
  841. },
  842. /**
  843. * The Z index of the annotation.
  844. *
  845. * @type {Number}
  846. * @default 6
  847. */
  848. zIndex: 6
  849. },
  850. /**
  851. * Initialize the annotation.
  852. *
  853. * @param {Chart} - the chart
  854. * @param {AnnotationOptions} - the user options for the annotation
  855. */
  856. init: function () {
  857. var anno = this;
  858. each(this.options.labels || [], this.initLabel, this);
  859. each(this.options.shapes || [], this.initShape, this);
  860. this.labelCollector = function () {
  861. return grep(anno.labels, function (label) {
  862. return !label.options.allowOverlap;
  863. });
  864. };
  865. this.chart.labelCollectors.push(this.labelCollector);
  866. },
  867. /**
  868. * Main method for drawing an annotation.
  869. **/
  870. redraw: function () {
  871. if (!this.group) {
  872. this.render();
  873. }
  874. this.redrawItems(this.shapes);
  875. this.redrawItems(this.labels);
  876. },
  877. /**
  878. * @private
  879. * @param {Array<Object>} items
  880. **/
  881. redrawItems: function (items) {
  882. var i = items.length;
  883. // needs a backward loop
  884. // labels/shapes array might be modified due to destruction of the item
  885. while (i--) {
  886. this.redrawItem(items[i]);
  887. }
  888. },
  889. /**
  890. * Render the annotation.
  891. **/
  892. render: function () {
  893. var renderer = this.chart.renderer;
  894. var group = this.group = renderer.g('annotation')
  895. .attr({
  896. zIndex: this.options.zIndex,
  897. visibility: this.options.visible ? 'visible' : 'hidden'
  898. })
  899. .add();
  900. this.shapesGroup = renderer.g('annotation-shapes').add(group);
  901. this.labelsGroup = renderer.g('annotation-labels').attr({
  902. // hideOverlappingLabels requires translation
  903. translateX: 0,
  904. translateY: 0
  905. }).add(group);
  906. this.shapesGroup.clip(this.chart.plotBoxClip);
  907. },
  908. /**
  909. * Set the annotation's visibility.
  910. *
  911. * @param {Boolean} [visibility] - Whether to show or hide an annotation.
  912. * If the param is omitted, the annotation's visibility is toggled.
  913. **/
  914. setVisible: function (visibility) {
  915. var options = this.options,
  916. visible = pick(visibility, !options.visible);
  917. this.group.attr({
  918. visibility: visible ? 'visible' : 'hidden'
  919. });
  920. options.visible = visible;
  921. },
  922. /**
  923. * Destroy the annotation. This function does not touch the chart
  924. * that the annotation belongs to (all annotations are kept in
  925. * the chart.annotations array) - it is recommended to use
  926. * {@link Highcharts.Chart#removeAnnotation} instead.
  927. **/
  928. destroy: function () {
  929. var chart = this.chart;
  930. erase(this.chart.labelCollectors, this.labelCollector);
  931. each(this.labels, function (label) {
  932. label.destroy();
  933. });
  934. each(this.shapes, function (shape) {
  935. shape.destroy();
  936. });
  937. destroyObjectProperties(this, chart);
  938. },
  939. /* ***********************************************************************
  940. * ITEM SECTION
  941. * Contains methods for handling a single item in an annotation
  942. *********************************************************************** */
  943. /**
  944. * Initialisation of a single shape
  945. *
  946. * @private
  947. * @param {Object} shapeOptions - a confg object for a single shape
  948. **/
  949. initShape: function (shapeOptions) {
  950. var renderer = this.chart.renderer,
  951. options = merge(this.options.shapeOptions, shapeOptions),
  952. attr = this.attrsFromOptions(options),
  953. type = renderer[options.type] ? options.type : 'rect',
  954. shape = renderer[type](0, -9e9, 0, 0);
  955. shape.points = [];
  956. shape.type = type;
  957. shape.options = options;
  958. shape.itemType = 'shape';
  959. if (type === 'path') {
  960. extend(shape, {
  961. markerStartSetter: MarkerMixin.markerStartSetter,
  962. markerEndSetter: MarkerMixin.markerEndSetter,
  963. markerStart: MarkerMixin.markerStart,
  964. markerEnd: MarkerMixin.markerEnd
  965. });
  966. }
  967. shape.attr(attr);
  968. if (options.className) {
  969. shape.addClass(options.className);
  970. }
  971. this.shapes.push(shape);
  972. },
  973. /**
  974. * Initialisation of a single label
  975. *
  976. * @private
  977. * @param {Object} labelOptions
  978. **/
  979. initLabel: function (labelOptions) {
  980. var options = merge(this.options.labelOptions, labelOptions),
  981. attr = this.attrsFromOptions(options),
  982. label = this.chart.renderer.label(
  983. '',
  984. 0, -9e9,
  985. options.shape,
  986. null,
  987. null,
  988. options.useHTML,
  989. null,
  990. 'annotation-label'
  991. );
  992. label.points = [];
  993. label.options = options;
  994. label.itemType = 'label';
  995. // Labelrank required for hideOverlappingLabels()
  996. label.labelrank = options.labelrank;
  997. label.annotation = this;
  998. label.attr(attr);
  999. var style = options.style;
  1000. if (style.color === 'contrast') {
  1001. style.color = this.chart.renderer.getContrast(
  1002. inArray(options.shape, this.shapesWithoutBackground) > -1 ?
  1003. '#FFFFFF' :
  1004. options.backgroundColor
  1005. );
  1006. }
  1007. label.css(style).shadow(options.shadow);
  1008. if (options.className) {
  1009. label.addClass(options.className);
  1010. }
  1011. this.labels.push(label);
  1012. },
  1013. /**
  1014. * Redrawing a single item
  1015. *
  1016. * @private
  1017. * @param {SVGElement} item
  1018. */
  1019. redrawItem: function (item) {
  1020. var points = this.linkPoints(item),
  1021. itemOptions = item.options,
  1022. text,
  1023. time = this.chart.time;
  1024. if (!points.length) {
  1025. this.destroyItem(item);
  1026. } else {
  1027. if (!item.parentGroup) {
  1028. this.renderItem(item);
  1029. }
  1030. if (item.itemType === 'label') {
  1031. text = itemOptions.format || itemOptions.text;
  1032. item.attr({
  1033. text: text ?
  1034. format(text, points[0].getLabelConfig(), time) :
  1035. itemOptions.formatter.call(points[0])
  1036. });
  1037. }
  1038. if (item.type === 'path') {
  1039. this.redrawPath(item);
  1040. } else {
  1041. this.alignItem(item, !item.placed);
  1042. }
  1043. }
  1044. },
  1045. /**
  1046. * Destroing a single item
  1047. *
  1048. * @private
  1049. * @param {SVGElement} item
  1050. */
  1051. destroyItem: function (item) {
  1052. // erase from shapes or labels array
  1053. erase(this[item.itemType + 's'], item);
  1054. item.destroy();
  1055. },
  1056. /**
  1057. * Returns a point object
  1058. *
  1059. * @private
  1060. * @param {Object} pointOptions
  1061. * @param {Highcharts.MockPoint|Highcharts.Point} point
  1062. * @return {Highcharts.MockPoint|Highcharts.Point|null} if the point is
  1063. * found/exists returns this point, otherwise null
  1064. */
  1065. pointItem: function (pointOptions, point) {
  1066. if (!point || point.series === null) {
  1067. if (isObject(pointOptions)) {
  1068. point = mockPoint(this.chart, pointOptions);
  1069. } else if (isString(pointOptions)) {
  1070. point = this.chart.get(pointOptions) || null;
  1071. }
  1072. }
  1073. return point;
  1074. },
  1075. /**
  1076. * Linking item with the point or points and returning an array of linked
  1077. * points.
  1078. *
  1079. * @private
  1080. * @param {SVGElement} item
  1081. * @return {
  1082. * Highcharts.Point|
  1083. * Highcharts.MockPoint|
  1084. * Array<Highcharts.Point|Highcharts.MockPoint>
  1085. * }
  1086. */
  1087. linkPoints: function (item) {
  1088. var pointsOptions = (
  1089. item.options.points ||
  1090. (item.options.point && H.splat(item.options.point))
  1091. ),
  1092. points = item.points,
  1093. len = pointsOptions && pointsOptions.length,
  1094. i,
  1095. point;
  1096. for (i = 0; i < len; i++) {
  1097. point = this.pointItem(pointsOptions[i], points[i]);
  1098. if (!point) {
  1099. return (item.points = []);
  1100. }
  1101. points[i] = point;
  1102. }
  1103. return points;
  1104. },
  1105. /**
  1106. * Aligning the item and setting its anchor
  1107. *
  1108. * @private
  1109. * @param {SVGElement} item
  1110. * @param {Boolean} isNew
  1111. * If the label is re-positioned (is not new) it is animated
  1112. * @return {undefined}
  1113. */
  1114. alignItem: function (item, isNew) {
  1115. var anchor = this.itemAnchor(item, item.points[0]),
  1116. attrs = this.itemPosition(item, anchor);
  1117. if (attrs) {
  1118. item.alignAttr = attrs;
  1119. item.placed = true;
  1120. attrs.anchorX = anchor.absolutePosition.x;
  1121. attrs.anchorY = anchor.absolutePosition.y;
  1122. item[isNew ? 'attr' : 'animate'](attrs);
  1123. } else {
  1124. item.placed = false;
  1125. item.attr({
  1126. x: 0,
  1127. y: -9e9
  1128. });
  1129. }
  1130. },
  1131. /**
  1132. * @private
  1133. */
  1134. redrawPath: function (pathItem, isNew) {
  1135. var points = pathItem.points,
  1136. strokeWidth = pathItem['stroke-width'] || 1,
  1137. d = ['M'],
  1138. pointIndex = 0,
  1139. dIndex = 0,
  1140. len = points && points.length,
  1141. crispSegmentIndex,
  1142. anchor,
  1143. point,
  1144. showPath;
  1145. if (len) {
  1146. do {
  1147. point = points[pointIndex];
  1148. anchor = this.itemAnchor(pathItem, point).absolutePosition;
  1149. d[++dIndex] = anchor.x;
  1150. d[++dIndex] = anchor.y;
  1151. // Crisping line, it might be replaced with
  1152. // Renderer.prototype.crispLine but it requires creating many
  1153. // temporary arrays
  1154. crispSegmentIndex = dIndex % 5;
  1155. if (crispSegmentIndex === 0) {
  1156. if (d[crispSegmentIndex + 1] === d[crispSegmentIndex + 4]) {
  1157. d[crispSegmentIndex + 1] = d[crispSegmentIndex + 4] =
  1158. Math.round(d[crispSegmentIndex + 1]) -
  1159. (strokeWidth % 2 / 2);
  1160. }
  1161. if (d[crispSegmentIndex + 2] === d[crispSegmentIndex + 5]) {
  1162. d[crispSegmentIndex + 2] = d[crispSegmentIndex + 5] =
  1163. Math.round(d[crispSegmentIndex + 2]) +
  1164. (strokeWidth % 2 / 2);
  1165. }
  1166. }
  1167. if (pointIndex < len - 1) {
  1168. d[++dIndex] = 'L';
  1169. }
  1170. showPath = point.series.visible;
  1171. } while (++pointIndex < len && showPath);
  1172. }
  1173. if (showPath) {
  1174. pathItem[isNew ? 'attr' : 'animate']({
  1175. d: d
  1176. });
  1177. } else {
  1178. pathItem.attr({
  1179. d: 'M 0 ' + -9e9
  1180. });
  1181. }
  1182. pathItem.placed = showPath;
  1183. },
  1184. /*
  1185. * @private
  1186. */
  1187. renderItem: function (item) {
  1188. item.add(
  1189. item.itemType === 'label' ?
  1190. this.labelsGroup :
  1191. this.shapesGroup
  1192. );
  1193. this.setItemMarkers(item);
  1194. },
  1195. /*
  1196. * @private
  1197. */
  1198. setItemMarkers: function (item) {
  1199. var itemOptions = item.options,
  1200. chart = this.chart,
  1201. defs = chart.options.defs,
  1202. fill = itemOptions.fill,
  1203. color = defined(fill) && fill !== 'none' ?
  1204. fill :
  1205. itemOptions.stroke,
  1206. setMarker = function (markerType) {
  1207. var markerId = itemOptions[markerType],
  1208. def,
  1209. predefinedMarker,
  1210. key,
  1211. marker;
  1212. if (markerId) {
  1213. for (key in defs) {
  1214. def = defs[key];
  1215. if (markerId === def.id && def.tagName === 'marker') {
  1216. predefinedMarker = def;
  1217. break;
  1218. }
  1219. }
  1220. if (predefinedMarker) {
  1221. marker = item[markerType] = chart.renderer.addMarker(
  1222. (itemOptions.id || uniqueKey()) + '-' +
  1223. predefinedMarker.id,
  1224. merge(predefinedMarker, { color: color })
  1225. );
  1226. item.attr(markerType, marker.attr('id'));
  1227. }
  1228. }
  1229. };
  1230. each(['markerStart', 'markerEnd'], setMarker);
  1231. },
  1232. /**
  1233. * An object which denotes an anchor position
  1234. *
  1235. * @typedef {Object} AnchorPosition
  1236. * @property {Number} AnchorPosition.x
  1237. * @property {Number} AnchorPosition.y
  1238. * @property {Number} AnchorPosition.height
  1239. * @property {Number} AnchorPosition.width
  1240. */
  1241. /**
  1242. * Returns object which denotes anchor position - relative and absolute
  1243. *
  1244. * @private
  1245. * @param {SVGElement} item
  1246. * @param {Highcharts.Point|Highcharts.MockPoint} point
  1247. * @return {Object} anchor
  1248. * @return {AnchorPosition} anchor.relativePosition
  1249. * Relative to the plot area position
  1250. * @return {AnchorPosition} anchor.absolutePosition
  1251. * Absolute position
  1252. */
  1253. itemAnchor: function (item, point) {
  1254. var plotBox = point.series.getPlotBox(),
  1255. box = point.mock ?
  1256. point.alignToBox(true) :
  1257. tooltipPrototype.getAnchor.call({
  1258. chart: this.chart
  1259. }, point),
  1260. anchor = {
  1261. x: box[0],
  1262. y: box[1],
  1263. height: box[2] || 0,
  1264. width: box[3] || 0
  1265. };
  1266. return {
  1267. relativePosition: anchor,
  1268. absolutePosition: merge(anchor, {
  1269. x: anchor.x + plotBox.translateX,
  1270. y: anchor.y + plotBox.translateY
  1271. })
  1272. };
  1273. },
  1274. /**
  1275. * Returns the item position
  1276. *
  1277. * @private
  1278. * @param {SVGElement} item
  1279. * @param {AnchorPosition} anchor
  1280. * @return {Object|null} position
  1281. * @return {Number} position.x
  1282. * @return {Number} position.y
  1283. */
  1284. itemPosition: function (item, anchor) {
  1285. var chart = this.chart,
  1286. point = item.points[0],
  1287. itemOptions = item.options,
  1288. anchorAbsolutePosition = anchor.absolutePosition,
  1289. anchorRelativePosition = anchor.relativePosition,
  1290. itemPosition,
  1291. alignTo,
  1292. itemPosRelativeX,
  1293. itemPosRelativeY,
  1294. showItem =
  1295. point.series.visible &&
  1296. MockPoint.prototype.isInsidePane.call(point);
  1297. if (showItem) {
  1298. if (defined(itemOptions.distance) || itemOptions.positioner) {
  1299. itemPosition = (
  1300. itemOptions.positioner ||
  1301. tooltipPrototype.getPosition
  1302. ).call(
  1303. {
  1304. chart: chart,
  1305. distance: pick(itemOptions.distance, 16)
  1306. },
  1307. item.width,
  1308. item.height,
  1309. {
  1310. plotX: anchorRelativePosition.x,
  1311. plotY: anchorRelativePosition.y,
  1312. negative: point.negative,
  1313. ttBelow: point.ttBelow,
  1314. h: anchorRelativePosition.height ||
  1315. anchorRelativePosition.width
  1316. }
  1317. );
  1318. } else {
  1319. alignTo = {
  1320. x: anchorAbsolutePosition.x,
  1321. y: anchorAbsolutePosition.y,
  1322. width: 0,
  1323. height: 0
  1324. };
  1325. itemPosition = this.alignedPosition(
  1326. extend(itemOptions, {
  1327. width: item.width,
  1328. height: item.height
  1329. }),
  1330. alignTo
  1331. );
  1332. if (item.options.overflow === 'justify') {
  1333. itemPosition = this.alignedPosition(
  1334. this.justifiedOptions(item, itemOptions, itemPosition),
  1335. alignTo
  1336. );
  1337. }
  1338. }
  1339. if (itemOptions.crop) {
  1340. itemPosRelativeX = itemPosition.x - chart.plotLeft;
  1341. itemPosRelativeY = itemPosition.y - chart.plotTop;
  1342. showItem =
  1343. chart.isInsidePlot(itemPosRelativeX, itemPosRelativeY) &&
  1344. chart.isInsidePlot(
  1345. itemPosRelativeX + item.width,
  1346. itemPosRelativeY + item.height
  1347. );
  1348. }
  1349. }
  1350. return showItem ? itemPosition : null;
  1351. },
  1352. /**
  1353. * Returns new aligned position based alignment options and box to align to.
  1354. * It is almost a one-to-one copy from SVGElement.prototype.align
  1355. * except it does not use and mutate an element
  1356. *
  1357. * @private
  1358. * @param {Object} alignOptions
  1359. * @param {Object} box
  1360. * @return {Object} aligned position
  1361. **/
  1362. alignedPosition: function (alignOptions, box) {
  1363. var align = alignOptions.align,
  1364. vAlign = alignOptions.verticalAlign,
  1365. x = (box.x || 0) + (alignOptions.x || 0),
  1366. y = (box.y || 0) + (alignOptions.y || 0),
  1367. alignFactor,
  1368. vAlignFactor;
  1369. if (align === 'right') {
  1370. alignFactor = 1;
  1371. } else if (align === 'center') {
  1372. alignFactor = 2;
  1373. }
  1374. if (alignFactor) {
  1375. x += (box.width - (alignOptions.width || 0)) / alignFactor;
  1376. }
  1377. if (vAlign === 'bottom') {
  1378. vAlignFactor = 1;
  1379. } else if (vAlign === 'middle') {
  1380. vAlignFactor = 2;
  1381. }
  1382. if (vAlignFactor) {
  1383. y += (box.height - (alignOptions.height || 0)) / vAlignFactor;
  1384. }
  1385. return {
  1386. x: Math.round(x),
  1387. y: Math.round(y)
  1388. };
  1389. },
  1390. /**
  1391. * Returns new alignment options for a label if the label is outside the
  1392. * plot area. It is almost a one-to-one copy from
  1393. * Series.prototype.justifyDataLabel except it does not mutate the label and
  1394. * it works with absolute instead of relative position.
  1395. *
  1396. * @private
  1397. * @param {Object} label
  1398. * @param {Object} alignOptions
  1399. * @param {Object} alignAttr
  1400. * @return {Object} justified options
  1401. **/
  1402. justifiedOptions: function (label, alignOptions, alignAttr) {
  1403. var chart = this.chart,
  1404. align = alignOptions.align,
  1405. verticalAlign = alignOptions.verticalAlign,
  1406. padding = label.box ? 0 : (label.padding || 0),
  1407. bBox = label.getBBox(),
  1408. off,
  1409. options = {
  1410. align: align,
  1411. verticalAlign: verticalAlign,
  1412. x: alignOptions.x,
  1413. y: alignOptions.y,
  1414. width: label.width,
  1415. height: label.height
  1416. },
  1417. x = alignAttr.x - chart.plotLeft,
  1418. y = alignAttr.y - chart.plotTop;
  1419. // Off left
  1420. off = x + padding;
  1421. if (off < 0) {
  1422. if (align === 'right') {
  1423. options.align = 'left';
  1424. } else {
  1425. options.x = -off;
  1426. }
  1427. }
  1428. // Off right
  1429. off = x + bBox.width - padding;
  1430. if (off > chart.plotWidth) {
  1431. if (align === 'left') {
  1432. options.align = 'right';
  1433. } else {
  1434. options.x = chart.plotWidth - off;
  1435. }
  1436. }
  1437. // Off top
  1438. off = y + padding;
  1439. if (off < 0) {
  1440. if (verticalAlign === 'bottom') {
  1441. options.verticalAlign = 'top';
  1442. } else {
  1443. options.y = -off;
  1444. }
  1445. }
  1446. // Off bottom
  1447. off = y + bBox.height - padding;
  1448. if (off > chart.plotHeight) {
  1449. if (verticalAlign === 'top') {
  1450. options.verticalAlign = 'bottom';
  1451. } else {
  1452. options.y = chart.plotHeight - off;
  1453. }
  1454. }
  1455. return options;
  1456. },
  1457. /**
  1458. * Utility function for mapping item's options to element's attribute
  1459. *
  1460. * @private
  1461. * @param {Object} options
  1462. * @return {Object} mapped options
  1463. **/
  1464. attrsFromOptions: function (options) {
  1465. var map = this.attrsMap,
  1466. attrs = {},
  1467. key,
  1468. mappedKey;
  1469. for (key in options) {
  1470. mappedKey = map[key];
  1471. if (mappedKey) {
  1472. attrs[mappedKey] = options[key];
  1473. }
  1474. }
  1475. return attrs;
  1476. }
  1477. };
  1478. /* ***************************************************************************
  1479. *
  1480. * EXTENDING CHART PROTOTYPE
  1481. *
  1482. **************************************************************************** */
  1483. H.extend(chartPrototype, /** @lends Chart# */ {
  1484. /**
  1485. * Add an annotation to the chart after render time.
  1486. *
  1487. * @param {AnnotationOptions} options
  1488. * The series options for the new, detailed series.
  1489. *
  1490. * @return {Highcharts.Annotation} - The newly generated annotation.
  1491. */
  1492. addAnnotation: function (userOptions, redraw) {
  1493. var annotation = new Annotation(this, userOptions);
  1494. this.annotations.push(annotation);
  1495. if (pick(redraw, true)) {
  1496. annotation.redraw();
  1497. }
  1498. return annotation;
  1499. },
  1500. /**
  1501. * Remove an annotation from the chart.
  1502. *
  1503. * @param {String} id - The annotation's id.
  1504. */
  1505. removeAnnotation: function (id) {
  1506. var annotations = this.annotations,
  1507. annotation = find(annotations, function (annotation) {
  1508. return annotation.options.id === id;
  1509. });
  1510. if (annotation) {
  1511. erase(annotations, annotation);
  1512. annotation.destroy();
  1513. }
  1514. },
  1515. /**
  1516. * @private
  1517. * @memberOf Highcharts.Chart#
  1518. * @function drawAnnotations
  1519. */
  1520. drawAnnotations: function () {
  1521. var clip = this.plotBoxClip,
  1522. plotBox = this.plotBox;
  1523. if (clip) {
  1524. clip.attr(plotBox);
  1525. } else {
  1526. this.plotBoxClip = this.renderer.clipRect(plotBox);
  1527. }
  1528. each(this.annotations, function (annotation) {
  1529. annotation.redraw();
  1530. });
  1531. }
  1532. });
  1533. chartPrototype.callbacks.push(function (chart) {
  1534. chart.annotations = [];
  1535. each(chart.options.annotations, function (annotationOptions) {
  1536. chart.addAnnotation(annotationOptions, false);
  1537. });
  1538. chart.drawAnnotations();
  1539. addEvent(chart, 'redraw', chart.drawAnnotations);
  1540. addEvent(chart, 'destroy', function () {
  1541. var plotBoxClip = chart.plotBoxClip;
  1542. if (plotBoxClip && plotBoxClip.destroy) {
  1543. plotBoxClip.destroy();
  1544. }
  1545. });
  1546. });
  1547. addEvent(H.Chart, 'afterGetContainer', function () {
  1548. this.options.defs = merge(defaultMarkers, this.options.defs || {});
  1549. objectEach(this.options.defs, function (def) {
  1550. if (def.tagName === 'marker' && def.render !== false) {
  1551. this.renderer.addMarker(def.id, def);
  1552. }
  1553. }, this);
  1554. });
  1555. /* ************************************************************************* */
  1556. /**
  1557. * General symbol definition for labels with connector
  1558. */
  1559. H.SVGRenderer.prototype.symbols.connector = function (x, y, w, h, options) {
  1560. var anchorX = options && options.anchorX,
  1561. anchorY = options && options.anchorY,
  1562. path,
  1563. yOffset,
  1564. lateral = w / 2;
  1565. if (isNumber(anchorX) && isNumber(anchorY)) {
  1566. path = ['M', anchorX, anchorY];
  1567. // Prefer 45 deg connectors
  1568. yOffset = y - anchorY;
  1569. if (yOffset < 0) {
  1570. yOffset = -h - yOffset;
  1571. }
  1572. if (yOffset < w) {
  1573. lateral = anchorX < x + (w / 2) ? yOffset : w - yOffset;
  1574. }
  1575. // Anchor below label
  1576. if (anchorY > y + h) {
  1577. path.push('L', x + lateral, y + h);
  1578. // Anchor above label
  1579. } else if (anchorY < y) {
  1580. path.push('L', x + lateral, y);
  1581. // Anchor left of label
  1582. } else if (anchorX < x) {
  1583. path.push('L', x, y + h / 2);
  1584. // Anchor right of label
  1585. } else if (anchorX > x + w) {
  1586. path.push('L', x + w, y + h / 2);
  1587. }
  1588. }
  1589. return path || [];
  1590. };
  1591. }(Highcharts));
  1592. }));