Chart.js 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027
  1. /* *
  2. *
  3. * (c) 2010-2019 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import H from './Globals.js';
  12. /**
  13. * Callback for chart constructors.
  14. *
  15. * @callback Highcharts.ChartCallbackFunction
  16. *
  17. * @param {Highcharts.Chart} chart
  18. * Created chart.
  19. */
  20. /**
  21. * Format a number and return a string based on input settings.
  22. *
  23. * @callback Highcharts.NumberFormatterCallbackFunction
  24. *
  25. * @param {number} number
  26. * The input number to format.
  27. *
  28. * @param {number} decimals
  29. * The amount of decimals. A value of -1 preserves the amount in the
  30. * input number.
  31. *
  32. * @param {string} [decimalPoint]
  33. * The decimal point, defaults to the one given in the lang options, or
  34. * a dot.
  35. *
  36. * @param {string} [thousandsSep]
  37. * The thousands separator, defaults to the one given in the lang
  38. * options, or a space character.
  39. *
  40. * @return {string} The formatted number.
  41. */
  42. /**
  43. * The chart title. The title has an `update` method that allows modifying the
  44. * options directly or indirectly via `chart.update`.
  45. *
  46. * @interface Highcharts.TitleObject
  47. * @extends Highcharts.SVGElement
  48. */ /**
  49. * Modify options for the title.
  50. *
  51. * @function Highcharts.TitleObject#update
  52. *
  53. * @param {Highcharts.TitleOptions} titleOptions
  54. * Options to modify.
  55. *
  56. * @param {boolean} [redraw=true]
  57. * Whether to redraw the chart after the title is altered. If doing more
  58. * operations on the chart, it is a good idea to set redraw to false and
  59. * call {@link Chart#redraw} after.
  60. */
  61. /**
  62. * The chart subtitle. The subtitle has an `update` method that
  63. * allows modifying the options directly or indirectly via
  64. * `chart.update`.
  65. *
  66. * @interface Highcharts.SubtitleObject
  67. * @extends Highcharts.SVGElement
  68. */ /**
  69. * Modify options for the subtitle.
  70. *
  71. * @function Highcharts.SubtitleObject#update
  72. *
  73. * @param {Highcharts.SubtitleOptions} subtitleOptions
  74. * Options to modify.
  75. *
  76. * @param {boolean} [redraw=true]
  77. * Whether to redraw the chart after the subtitle is altered. If doing
  78. * more operations on the chart, it is a good idea to set redraw to false
  79. * and call {@link Chart#redraw} after.
  80. */
  81. /**
  82. * The chart caption. The caption has an `update` method that
  83. * allows modifying the options directly or indirectly via
  84. * `chart.update`.
  85. *
  86. * @interface Highcharts.CaptionObject
  87. * @extends Highcharts.SVGElement
  88. */ /**
  89. * Modify options for the caption.
  90. *
  91. * @function Highcharts.CaptionObject#update
  92. *
  93. * @param {Highcharts.CaptionOptions} captionOptions
  94. * Options to modify.
  95. *
  96. * @param {boolean} [redraw=true]
  97. * Whether to redraw the chart after the caption is altered. If doing
  98. * more operations on the chart, it is a good idea to set redraw to false
  99. * and call {@link Chart#redraw} after.
  100. */
  101. import U from './Utilities.js';
  102. var animObject = U.animObject, attr = U.attr, defined = U.defined, discardElement = U.discardElement, erase = U.erase, extend = U.extend, isArray = U.isArray, isNumber = U.isNumber, isObject = U.isObject, isString = U.isString, numberFormat = U.numberFormat, objectEach = U.objectEach, pick = U.pick, pInt = U.pInt, relativeLength = U.relativeLength, setAnimation = U.setAnimation, splat = U.splat, syncTimeout = U.syncTimeout;
  103. import './Axis.js';
  104. import './Legend.js';
  105. import './Options.js';
  106. import './Pointer.js';
  107. var addEvent = H.addEvent, animate = H.animate, doc = H.doc, Axis = H.Axis, // @todo add as requirement
  108. createElement = H.createElement, defaultOptions = H.defaultOptions, charts = H.charts, css = H.css, find = H.find, fireEvent = H.fireEvent, Legend = H.Legend, // @todo add as requirement
  109. marginNames = H.marginNames, merge = H.merge, Pointer = H.Pointer, // @todo add as requirement
  110. removeEvent = H.removeEvent, seriesTypes = H.seriesTypes, win = H.win;
  111. /* eslint-disable no-invalid-this, valid-jsdoc */
  112. /**
  113. * The Chart class. The recommended constructor is {@link Highcharts#chart}.
  114. *
  115. * @example
  116. * var chart = Highcharts.chart('container', {
  117. * title: {
  118. * text: 'My chart'
  119. * },
  120. * series: [{
  121. * data: [1, 3, 2, 4]
  122. * }]
  123. * })
  124. *
  125. * @class
  126. * @name Highcharts.Chart
  127. *
  128. * @param {string|Highcharts.HTMLDOMElement} [renderTo]
  129. * The DOM element to render to, or its id.
  130. *
  131. * @param {Highcharts.Options} options
  132. * The chart options structure.
  133. *
  134. * @param {Highcharts.ChartCallbackFunction} [callback]
  135. * Function to run when the chart has loaded and and all external images
  136. * are loaded. Defining a
  137. * [chart.events.load](https://api.highcharts.com/highcharts/chart.events.load)
  138. * handler is equivalent.
  139. */
  140. var Chart = H.Chart = function () {
  141. this.getArgs.apply(this, arguments);
  142. };
  143. /**
  144. * Factory function for basic charts.
  145. *
  146. * @example
  147. * // Render a chart in to div#container
  148. * var chart = Highcharts.chart('container', {
  149. * title: {
  150. * text: 'My chart'
  151. * },
  152. * series: [{
  153. * data: [1, 3, 2, 4]
  154. * }]
  155. * });
  156. *
  157. * @function Highcharts.chart
  158. *
  159. * @param {string|Highcharts.HTMLDOMElement} [renderTo]
  160. * The DOM element to render to, or its id.
  161. *
  162. * @param {Highcharts.Options} options
  163. * The chart options structure.
  164. *
  165. * @param {Highcharts.ChartCallbackFunction} [callback]
  166. * Function to run when the chart has loaded and and all external images
  167. * are loaded. Defining a
  168. * [chart.events.load](https://api.highcharts.com/highcharts/chart.events.load)
  169. * handler is equivalent.
  170. *
  171. * @return {Highcharts.Chart}
  172. * Returns the Chart object.
  173. */
  174. H.chart = function (a, b, c) {
  175. return new Chart(a, b, c);
  176. };
  177. extend(Chart.prototype, /** @lends Highcharts.Chart.prototype */ {
  178. // Hook for adding callbacks in modules
  179. callbacks: [],
  180. /**
  181. * Handle the arguments passed to the constructor.
  182. *
  183. * @private
  184. * @function Highcharts.Chart#getArgs
  185. *
  186. * @param {...Array<*>} arguments
  187. * All arguments for the constructor.
  188. *
  189. * @return {Array<*>}
  190. * Passed arguments without renderTo.
  191. *
  192. * @fires Highcharts.Chart#event:init
  193. * @fires Highcharts.Chart#event:afterInit
  194. */
  195. getArgs: function () {
  196. var args = [].slice.call(arguments);
  197. // Remove the optional first argument, renderTo, and
  198. // set it on this.
  199. if (isString(args[0]) || args[0].nodeName) {
  200. this.renderTo = args.shift();
  201. }
  202. this.init(args[0], args[1]);
  203. },
  204. /**
  205. * Overridable function that initializes the chart. The constructor's
  206. * arguments are passed on directly.
  207. *
  208. * @function Highcharts.Chart#init
  209. *
  210. * @param {Highcharts.Options} userOptions
  211. * Custom options.
  212. *
  213. * @param {Function} [callback]
  214. * Function to run when the chart has loaded and and all external
  215. * images are loaded.
  216. *
  217. * @return {void}
  218. *
  219. * @fires Highcharts.Chart#event:init
  220. * @fires Highcharts.Chart#event:afterInit
  221. */
  222. init: function (userOptions, callback) {
  223. // Handle regular options
  224. var options,
  225. // skip merging data points to increase performance
  226. seriesOptions = userOptions.series, userPlotOptions = userOptions.plotOptions || {};
  227. // Fire the event with a default function
  228. fireEvent(this, 'init', { args: arguments }, function () {
  229. userOptions.series = null;
  230. options = merge(defaultOptions, userOptions); // do the merge
  231. // Override (by copy of user options) or clear tooltip options
  232. // in chart.options.plotOptions (#6218)
  233. objectEach(options.plotOptions, function (typeOptions, type) {
  234. if (isObject(typeOptions)) { // #8766
  235. typeOptions.tooltip = (userPlotOptions[type] && // override by copy:
  236. merge(userPlotOptions[type].tooltip)) || void 0; // or clear
  237. }
  238. });
  239. // User options have higher priority than default options
  240. // (#6218). In case of exporting: path is changed
  241. options.tooltip.userOptions = (userOptions.chart &&
  242. userOptions.chart.forExport &&
  243. userOptions.tooltip.userOptions) || userOptions.tooltip;
  244. // set back the series data
  245. options.series = userOptions.series = seriesOptions;
  246. /**
  247. * The original options given to the constructor or a chart factory
  248. * like {@link Highcharts.chart} and {@link Highcharts.stockChart}.
  249. *
  250. * @name Highcharts.Chart#userOptions
  251. * @type {Highcharts.Options}
  252. */
  253. this.userOptions = userOptions;
  254. var optionsChart = options.chart;
  255. var chartEvents = optionsChart.events;
  256. this.margin = [];
  257. this.spacing = [];
  258. // Pixel data bounds for touch zoom
  259. this.bounds = { h: {}, v: {} };
  260. // An array of functions that returns labels that should be
  261. // considered for anti-collision
  262. this.labelCollectors = [];
  263. this.callback = callback;
  264. this.isResizing = 0;
  265. /**
  266. * The options structure for the chart after merging
  267. * {@link #defaultOptions} and {@link #userOptions}. It contains
  268. * members for the sub elements like series, legend, tooltip etc.
  269. *
  270. * @name Highcharts.Chart#options
  271. * @type {Highcharts.Options}
  272. */
  273. this.options = options;
  274. /**
  275. * All the axes in the chart.
  276. *
  277. * @see Highcharts.Chart.xAxis
  278. * @see Highcharts.Chart.yAxis
  279. *
  280. * @name Highcharts.Chart#axes
  281. * @type {Array<Highcharts.Axis>}
  282. */
  283. this.axes = [];
  284. /**
  285. * All the current series in the chart.
  286. *
  287. * @name Highcharts.Chart#series
  288. * @type {Array<Highcharts.Series>}
  289. */
  290. this.series = [];
  291. /**
  292. * The `Time` object associated with the chart. Since v6.0.5,
  293. * time settings can be applied individually for each chart. If
  294. * no individual settings apply, the `Time` object is shared by
  295. * all instances.
  296. *
  297. * @name Highcharts.Chart#time
  298. * @type {Highcharts.Time}
  299. */
  300. this.time =
  301. userOptions.time && Object.keys(userOptions.time).length ?
  302. new H.Time(userOptions.time) :
  303. H.time;
  304. /**
  305. * Callback function to override the default function that formats
  306. * all the numbers in the chart. Returns a string with the formatted
  307. * number.
  308. *
  309. * @name Highcharts.Chart#numberFormatter
  310. * @type {Highcharts.NumberFormatterCallbackFunction}
  311. */
  312. this.numberFormatter = optionsChart.numberFormatter || numberFormat;
  313. /**
  314. * Whether the chart is in styled mode, meaning all presentatinoal
  315. * attributes are avoided.
  316. *
  317. * @name Highcharts.Chart#styledMode
  318. * @type {boolean}
  319. */
  320. this.styledMode = optionsChart.styledMode;
  321. this.hasCartesianSeries = optionsChart.showAxes;
  322. var chart = this;
  323. /**
  324. * Index position of the chart in the {@link Highcharts#charts}
  325. * property.
  326. *
  327. * @name Highcharts.Chart#index
  328. * @type {number}
  329. * @readonly
  330. */
  331. chart.index = charts.length; // Add the chart to the global lookup
  332. charts.push(chart);
  333. H.chartCount++;
  334. // Chart event handlers
  335. if (chartEvents) {
  336. objectEach(chartEvents, function (event, eventType) {
  337. if (H.isFunction(event)) {
  338. addEvent(chart, eventType, event);
  339. }
  340. });
  341. }
  342. /**
  343. * A collection of the X axes in the chart.
  344. *
  345. * @name Highcharts.Chart#xAxis
  346. * @type {Array<Highcharts.Axis>}
  347. */
  348. chart.xAxis = [];
  349. /**
  350. * A collection of the Y axes in the chart.
  351. *
  352. * @name Highcharts.Chart#yAxis
  353. * @type {Array<Highcharts.Axis>}
  354. *
  355. * @todo
  356. * Make events official: Fire the event `afterInit`.
  357. */
  358. chart.yAxis = [];
  359. chart.pointCount = chart.colorCounter = chart.symbolCounter = 0;
  360. // Fire after init but before first render, before axes and series
  361. // have been initialized.
  362. fireEvent(chart, 'afterInit');
  363. chart.firstRender();
  364. });
  365. },
  366. /**
  367. * Internal function to unitialize an individual series.
  368. *
  369. * @private
  370. * @function Highcharts.Chart#initSeries
  371. *
  372. * @param {Highcharts.SeriesOptions} options
  373. *
  374. * @return {Highcharts.Series}
  375. */
  376. initSeries: function (options) {
  377. var chart = this, optionsChart = chart.options.chart, type = (options.type ||
  378. optionsChart.type ||
  379. optionsChart.defaultSeriesType), series, Constr = seriesTypes[type];
  380. // No such series type
  381. if (!Constr) {
  382. H.error(17, true, chart, { missingModuleFor: type });
  383. }
  384. series = new Constr();
  385. series.init(this, options);
  386. return series;
  387. },
  388. /**
  389. * Internal function to set data for all series with enabled sorting.
  390. *
  391. * @private
  392. * @function Highcharts.Chart#setSeriesData
  393. *
  394. * @param {Highcharts.SeriesOptions} options
  395. *
  396. * @return {void}
  397. */
  398. setSeriesData: function () {
  399. this.getSeriesOrderByLinks().forEach(function (series) {
  400. // We need to set data for series with sorting after series init
  401. if (!series.points && !series.data && series.enabledDataSorting) {
  402. series.setData(series.options.data, false);
  403. }
  404. });
  405. },
  406. /**
  407. * Sort and return chart series in order depending on the number of linked
  408. * series.
  409. *
  410. * @private
  411. * @function Highcharts.Series#getSeriesOrderByLinks
  412. *
  413. * @return {Array<Highcharts.Series>}
  414. */
  415. getSeriesOrderByLinks: function () {
  416. return this.series.concat().sort(function (a, b) {
  417. if (a.linkedSeries.length || b.linkedSeries.length) {
  418. return b.linkedSeries.length - a.linkedSeries.length;
  419. }
  420. return 0;
  421. });
  422. },
  423. /**
  424. * Order all series above a given index. When series are added and ordered
  425. * by configuration, only the last series is handled (#248, #1123, #2456,
  426. * #6112). This function is called on series initialization and destroy.
  427. *
  428. * @private
  429. * @function Highcharts.Series#orderSeries
  430. * @param {number} [fromIndex]
  431. * If this is given, only the series above this index are handled.
  432. * @return {void}
  433. */
  434. orderSeries: function (fromIndex) {
  435. var series = this.series, i = fromIndex || 0;
  436. for (; i < series.length; i++) {
  437. if (series[i]) {
  438. /**
  439. * Contains the series' index in the `Chart.series` array.
  440. *
  441. * @name Highcharts.Series#index
  442. * @type {number}
  443. * @readonly
  444. */
  445. series[i].index = i;
  446. series[i].name = series[i].getName();
  447. }
  448. }
  449. },
  450. /**
  451. * Check whether a given point is within the plot area.
  452. *
  453. * @function Highcharts.Chart#isInsidePlot
  454. *
  455. * @param {number} plotX
  456. * Pixel x relative to the plot area.
  457. *
  458. * @param {number} plotY
  459. * Pixel y relative to the plot area.
  460. *
  461. * @param {boolean} [inverted]
  462. * Whether the chart is inverted.
  463. *
  464. * @return {boolean}
  465. * Returns true if the given point is inside the plot area.
  466. */
  467. isInsidePlot: function (plotX, plotY, inverted) {
  468. var x = inverted ? plotY : plotX, y = inverted ? plotX : plotY;
  469. return x >= 0 &&
  470. x <= this.plotWidth &&
  471. y >= 0 &&
  472. y <= this.plotHeight;
  473. },
  474. /**
  475. * Redraw the chart after changes have been done to the data, axis extremes
  476. * chart size or chart elements. All methods for updating axes, series or
  477. * points have a parameter for redrawing the chart. This is `true` by
  478. * default. But in many cases you want to do more than one operation on the
  479. * chart before redrawing, for example add a number of points. In those
  480. * cases it is a waste of resources to redraw the chart for each new point
  481. * added. So you add the points and call `chart.redraw()` after.
  482. *
  483. * @function Highcharts.Chart#redraw
  484. *
  485. * @param {boolean|Highcharts.AnimationOptionsObject} [animation]
  486. * If or how to apply animation to the redraw.
  487. *
  488. * @return {void}
  489. *
  490. * @fires Highcharts.Chart#event:afterSetExtremes
  491. * @fires Highcharts.Chart#event:beforeRedraw
  492. * @fires Highcharts.Chart#event:predraw
  493. * @fires Highcharts.Chart#event:redraw
  494. * @fires Highcharts.Chart#event:render
  495. * @fires Highcharts.Chart#event:updatedData
  496. */
  497. redraw: function (animation) {
  498. fireEvent(this, 'beforeRedraw');
  499. var chart = this, axes = chart.axes, series = chart.series, pointer = chart.pointer, legend = chart.legend, legendUserOptions = chart.userOptions.legend, redrawLegend = chart.isDirtyLegend, hasStackedSeries, hasDirtyStacks, hasCartesianSeries = chart.hasCartesianSeries, isDirtyBox = chart.isDirtyBox, i, serie, renderer = chart.renderer, isHiddenChart = renderer.isHidden(), afterRedraw = [];
  500. // Handle responsive rules, not only on resize (#6130)
  501. if (chart.setResponsive) {
  502. chart.setResponsive(false);
  503. }
  504. setAnimation(animation, chart);
  505. if (isHiddenChart) {
  506. chart.temporaryDisplay();
  507. }
  508. // Adjust title layout (reflow multiline text)
  509. chart.layOutTitles();
  510. // link stacked series
  511. i = series.length;
  512. while (i--) {
  513. serie = series[i];
  514. if (serie.options.stacking) {
  515. hasStackedSeries = true;
  516. if (serie.isDirty) {
  517. hasDirtyStacks = true;
  518. break;
  519. }
  520. }
  521. }
  522. if (hasDirtyStacks) { // mark others as dirty
  523. i = series.length;
  524. while (i--) {
  525. serie = series[i];
  526. if (serie.options.stacking) {
  527. serie.isDirty = true;
  528. }
  529. }
  530. }
  531. // Handle updated data in the series
  532. series.forEach(function (serie) {
  533. if (serie.isDirty) {
  534. if (serie.options.legendType === 'point') {
  535. if (serie.updateTotals) {
  536. serie.updateTotals();
  537. }
  538. redrawLegend = true;
  539. }
  540. else if (legendUserOptions &&
  541. (legendUserOptions.labelFormatter ||
  542. legendUserOptions.labelFormat)) {
  543. redrawLegend = true; // #2165
  544. }
  545. }
  546. if (serie.isDirtyData) {
  547. fireEvent(serie, 'updatedData');
  548. }
  549. });
  550. // handle added or removed series
  551. if (redrawLegend && legend && legend.options.enabled) {
  552. // draw legend graphics
  553. legend.render();
  554. chart.isDirtyLegend = false;
  555. }
  556. // reset stacks
  557. if (hasStackedSeries) {
  558. chart.getStacks();
  559. }
  560. if (hasCartesianSeries) {
  561. // set axes scales
  562. axes.forEach(function (axis) {
  563. axis.updateNames();
  564. axis.setScale();
  565. });
  566. }
  567. chart.getMargins(); // #3098
  568. if (hasCartesianSeries) {
  569. // If one axis is dirty, all axes must be redrawn (#792, #2169)
  570. axes.forEach(function (axis) {
  571. if (axis.isDirty) {
  572. isDirtyBox = true;
  573. }
  574. });
  575. // redraw axes
  576. axes.forEach(function (axis) {
  577. // Fire 'afterSetExtremes' only if extremes are set
  578. var key = axis.min + ',' + axis.max;
  579. if (axis.extKey !== key) { // #821, #4452
  580. axis.extKey = key;
  581. // prevent a recursive call to chart.redraw() (#1119)
  582. afterRedraw.push(function () {
  583. fireEvent(axis, 'afterSetExtremes', extend(axis.eventArgs, axis.getExtremes())); // #747, #751
  584. delete axis.eventArgs;
  585. });
  586. }
  587. if (isDirtyBox || hasStackedSeries) {
  588. axis.redraw();
  589. }
  590. });
  591. }
  592. // the plot areas size has changed
  593. if (isDirtyBox) {
  594. chart.drawChartBox();
  595. }
  596. // Fire an event before redrawing series, used by the boost module to
  597. // clear previous series renderings.
  598. fireEvent(chart, 'predraw');
  599. // redraw affected series
  600. series.forEach(function (serie) {
  601. if ((isDirtyBox || serie.isDirty) && serie.visible) {
  602. serie.redraw();
  603. }
  604. // Set it here, otherwise we will have unlimited 'updatedData' calls
  605. // for a hidden series after setData(). Fixes #6012
  606. serie.isDirtyData = false;
  607. });
  608. // move tooltip or reset
  609. if (pointer) {
  610. pointer.reset(true);
  611. }
  612. // redraw if canvas
  613. renderer.draw();
  614. // Fire the events
  615. fireEvent(chart, 'redraw');
  616. fireEvent(chart, 'render');
  617. if (isHiddenChart) {
  618. chart.temporaryDisplay(true);
  619. }
  620. // Fire callbacks that are put on hold until after the redraw
  621. afterRedraw.forEach(function (callback) {
  622. callback.call();
  623. });
  624. },
  625. /**
  626. * Get an axis, series or point object by `id` as given in the configuration
  627. * options. Returns `undefined` if no item is found.
  628. *
  629. * @sample highcharts/plotoptions/series-id/
  630. * Get series by id
  631. *
  632. * @function Highcharts.Chart#get
  633. *
  634. * @param {string} id
  635. * The id as given in the configuration options.
  636. *
  637. * @return {Highcharts.Axis|Highcharts.Series|Highcharts.Point|undefined}
  638. * The retrieved item.
  639. */
  640. get: function (id) {
  641. var ret, series = this.series, i;
  642. /**
  643. * @private
  644. * @param {Highcharts.Axis|Highcharts.Series} item
  645. * @return {boolean}
  646. */
  647. function itemById(item) {
  648. return (item.id === id ||
  649. (item.options && item.options.id === id));
  650. }
  651. ret =
  652. // Search axes
  653. find(this.axes, itemById) ||
  654. // Search series
  655. find(this.series, itemById);
  656. // Search points
  657. for (i = 0; !ret && i < series.length; i++) {
  658. ret = find(series[i].points || [], itemById);
  659. }
  660. return ret;
  661. },
  662. /**
  663. * Create the Axis instances based on the config options.
  664. *
  665. * @private
  666. * @function Highcharts.Chart#getAxes
  667. *
  668. * @return {void}
  669. *
  670. * @fires Highcharts.Chart#event:afterGetAxes
  671. * @fires Highcharts.Chart#event:getAxes
  672. */
  673. getAxes: function () {
  674. var chart = this, options = this.options, xAxisOptions = options.xAxis = splat(options.xAxis || {}), yAxisOptions = options.yAxis = splat(options.yAxis || {}), optionsArray;
  675. fireEvent(this, 'getAxes');
  676. // make sure the options are arrays and add some members
  677. xAxisOptions.forEach(function (axis, i) {
  678. axis.index = i;
  679. axis.isX = true;
  680. });
  681. yAxisOptions.forEach(function (axis, i) {
  682. axis.index = i;
  683. });
  684. // concatenate all axis options into one array
  685. optionsArray = xAxisOptions.concat(yAxisOptions);
  686. optionsArray.forEach(function (axisOptions) {
  687. new Axis(chart, axisOptions); // eslint-disable-line no-new
  688. });
  689. fireEvent(this, 'afterGetAxes');
  690. },
  691. /**
  692. * Returns an array of all currently selected points in the chart. Points
  693. * can be selected by clicking or programmatically by the
  694. * {@link Highcharts.Point#select}
  695. * function.
  696. *
  697. * @sample highcharts/plotoptions/series-allowpointselect-line/
  698. * Get selected points
  699. *
  700. * @function Highcharts.Chart#getSelectedPoints
  701. *
  702. * @return {Array<Highcharts.Point>}
  703. * The currently selected points.
  704. */
  705. getSelectedPoints: function () {
  706. var points = [];
  707. this.series.forEach(function (serie) {
  708. // For one-to-one points inspect series.data in order to retrieve
  709. // points outside the visible range (#6445). For grouped data,
  710. // inspect the generated series.points.
  711. points = points.concat((serie[serie.hasGroupedData ? 'points' : 'data'] || []).filter(function (point) {
  712. return pick(point.selectedStaging, point.selected);
  713. }));
  714. });
  715. return points;
  716. },
  717. /**
  718. * Returns an array of all currently selected series in the chart. Series
  719. * can be selected either programmatically by the
  720. * {@link Highcharts.Series#select}
  721. * function or by checking the checkbox next to the legend item if
  722. * [series.showCheckBox](https://api.highcharts.com/highcharts/plotOptions.series.showCheckbox)
  723. * is true.
  724. *
  725. * @sample highcharts/members/chart-getselectedseries/
  726. * Get selected series
  727. *
  728. * @function Highcharts.Chart#getSelectedSeries
  729. *
  730. * @return {Array<Highcharts.Series>}
  731. * The currently selected series.
  732. */
  733. getSelectedSeries: function () {
  734. return this.series.filter(function (serie) {
  735. return serie.selected;
  736. });
  737. },
  738. /**
  739. * Set a new title or subtitle for the chart.
  740. *
  741. * @sample highcharts/members/chart-settitle/
  742. * Set title text and styles
  743. *
  744. * @function Highcharts.Chart#setTitle
  745. *
  746. * @param {Highcharts.TitleOptions} [titleOptions]
  747. * New title options. The title text itself is set by the
  748. * `titleOptions.text` property.
  749. *
  750. * @param {Highcharts.SubtitleOptions} [subtitleOptions]
  751. * New subtitle options. The subtitle text itself is set by the
  752. * `subtitleOptions.text` property.
  753. *
  754. * @param {boolean} [redraw]
  755. * Whether to redraw the chart or wait for a later call to
  756. * `chart.redraw()`.
  757. *
  758. * @return {void}
  759. */
  760. setTitle: function (titleOptions, subtitleOptions, redraw) {
  761. this.applyDescription('title', titleOptions);
  762. this.applyDescription('subtitle', subtitleOptions);
  763. // The initial call also adds the caption. On update, chart.update will
  764. // relay to Chart.setCaption.
  765. this.applyDescription('caption', void 0);
  766. this.layOutTitles(redraw);
  767. },
  768. /**
  769. * Apply a title, subtitle or caption for the chart
  770. *
  771. * @private
  772. * @function Highcharts.Chart#applyDescription
  773. *
  774. * @param name {string}
  775. * Either title, subtitle or caption
  776. * @param {Highcharts.TitleOptions|Highcharts.SubtitleOptions|Highcharts.CaptionOptions|undefined} explicitOptions
  777. * The options to set, will be merged with default options.
  778. *
  779. * @return {void}
  780. */
  781. applyDescription: function (name, explicitOptions) {
  782. var chart = this;
  783. // Default style
  784. var style = name === 'title' ? {
  785. color: '#333333',
  786. fontSize: this.options.isStock ? '16px' : '18px' // #2944
  787. } : {
  788. color: '#666666'
  789. };
  790. // Merge default options with explicit options
  791. var options = this.options[name] = merge(
  792. // Default styles
  793. (!this.styledMode && { style: style }), this.options[name], explicitOptions);
  794. var elem = this[name];
  795. if (elem && explicitOptions) {
  796. this[name] = elem = elem.destroy(); // remove old
  797. }
  798. if (options && !elem) {
  799. elem = this.renderer.text(options.text, 0, 0, options.useHTML)
  800. .attr({
  801. align: options.align,
  802. 'class': 'highcharts-' + name,
  803. zIndex: options.zIndex || 4
  804. })
  805. .add();
  806. // Update methods, shortcut to Chart.setTitle, Chart.setSubtitle and
  807. // Chart.setCaption
  808. elem.update = function (updateOptions) {
  809. var fn = {
  810. title: 'setTitle',
  811. subtitle: 'setSubtitle',
  812. caption: 'setCaption'
  813. }[name];
  814. chart[fn](updateOptions);
  815. };
  816. // Presentational
  817. if (!this.styledMode) {
  818. elem.css(options.style);
  819. }
  820. /**
  821. * The chart title. The title has an `update` method that allows
  822. * modifying the options directly or indirectly via
  823. * `chart.update`.
  824. *
  825. * @sample highcharts/members/title-update/
  826. * Updating titles
  827. *
  828. * @name Highcharts.Chart#title
  829. * @type {Highcharts.TitleObject}
  830. */
  831. /**
  832. * The chart subtitle. The subtitle has an `update` method that
  833. * allows modifying the options directly or indirectly via
  834. * `chart.update`.
  835. *
  836. * @name Highcharts.Chart#subtitle
  837. * @type {Highcharts.SubtitleObject}
  838. */
  839. this[name] = elem;
  840. }
  841. },
  842. /**
  843. * Internal function to lay out the chart title, subtitle and caption, and
  844. * cache the full offset height for use in `getMargins`. The result is
  845. * stored in `this.titleOffset`.
  846. *
  847. * @private
  848. * @function Highcharts.Chart#layOutTitles
  849. *
  850. * @param {boolean} [redraw=true]
  851. *
  852. * @return {void}
  853. *
  854. * @fires Highcharts.Chart#event:afterLayOutTitles
  855. */
  856. layOutTitles: function (redraw) {
  857. var titleOffset = [0, 0, 0], requiresDirtyBox, renderer = this.renderer, spacingBox = this.spacingBox;
  858. // Lay out the title and the subtitle respectively
  859. ['title', 'subtitle', 'caption'].forEach(function (key) {
  860. var title = this[key], titleOptions = this.options[key], verticalAlign = titleOptions.verticalAlign || 'top', offset = key === 'title' ? -3 :
  861. // Floating subtitle (#6574)
  862. verticalAlign === 'top' ? titleOffset[0] + 2 : 0, titleSize, height;
  863. if (title) {
  864. if (!this.styledMode) {
  865. titleSize = titleOptions.style.fontSize;
  866. }
  867. titleSize = renderer.fontMetrics(titleSize, title).b;
  868. title
  869. .css({
  870. width: (titleOptions.width ||
  871. spacingBox.width + (titleOptions.widthAdjust || 0)) + 'px'
  872. });
  873. // Skip the cache for HTML (#3481, #11666)
  874. height = Math.round(title.getBBox(titleOptions.useHTML).height);
  875. title.align(extend({
  876. y: verticalAlign === 'bottom' ?
  877. titleSize :
  878. offset + titleSize,
  879. height: height
  880. }, titleOptions), false, 'spacingBox');
  881. if (!titleOptions.floating) {
  882. if (verticalAlign === 'top') {
  883. titleOffset[0] = Math.ceil(titleOffset[0] +
  884. height);
  885. }
  886. else if (verticalAlign === 'bottom') {
  887. titleOffset[2] = Math.ceil(titleOffset[2] +
  888. height);
  889. }
  890. }
  891. }
  892. }, this);
  893. // Handle title.margin and caption.margin
  894. if (titleOffset[0] &&
  895. (this.options.title.verticalAlign || 'top') === 'top') {
  896. titleOffset[0] += this.options.title.margin;
  897. }
  898. if (titleOffset[2] &&
  899. this.options.caption.verticalAlign === 'bottom') {
  900. titleOffset[2] += this.options.caption.margin;
  901. }
  902. requiresDirtyBox = (!this.titleOffset ||
  903. this.titleOffset.join(',') !== titleOffset.join(','));
  904. // Used in getMargins
  905. this.titleOffset = titleOffset;
  906. fireEvent(this, 'afterLayOutTitles');
  907. if (!this.isDirtyBox && requiresDirtyBox) {
  908. this.isDirtyBox = this.isDirtyLegend = requiresDirtyBox;
  909. // Redraw if necessary (#2719, #2744)
  910. if (this.hasRendered && pick(redraw, true) && this.isDirtyBox) {
  911. this.redraw();
  912. }
  913. }
  914. },
  915. /**
  916. * Internal function to get the chart width and height according to options
  917. * and container size. Sets {@link Chart.chartWidth} and
  918. * {@link Chart.chartHeight}.
  919. *
  920. * @private
  921. * @function Highcharts.Chart#getChartSize
  922. *
  923. * @return {void}
  924. */
  925. getChartSize: function () {
  926. var chart = this, optionsChart = chart.options.chart, widthOption = optionsChart.width, heightOption = optionsChart.height, renderTo = chart.renderTo;
  927. // Get inner width and height
  928. if (!defined(widthOption)) {
  929. chart.containerWidth = H.getStyle(renderTo, 'width');
  930. }
  931. if (!defined(heightOption)) {
  932. chart.containerHeight = H.getStyle(renderTo, 'height');
  933. }
  934. /**
  935. * The current pixel width of the chart.
  936. *
  937. * @name Highcharts.Chart#chartWidth
  938. * @type {number}
  939. */
  940. chart.chartWidth = Math.max(// #1393
  941. 0, widthOption || chart.containerWidth || 600 // #1460
  942. );
  943. /**
  944. * The current pixel height of the chart.
  945. *
  946. * @name Highcharts.Chart#chartHeight
  947. * @type {number}
  948. */
  949. chart.chartHeight = Math.max(0, relativeLength(heightOption, chart.chartWidth) ||
  950. (chart.containerHeight > 1 ?
  951. chart.containerHeight :
  952. 400));
  953. },
  954. /**
  955. * If the renderTo element has no offsetWidth, most likely one or more of
  956. * its parents are hidden. Loop up the DOM tree to temporarily display the
  957. * parents, then save the original display properties, and when the true
  958. * size is retrieved, reset them. Used on first render and on redraws.
  959. *
  960. * @private
  961. * @function Highcharts.Chart#temporaryDisplay
  962. *
  963. * @param {boolean} [revert]
  964. * Revert to the saved original styles.
  965. *
  966. * @return {void}
  967. */
  968. temporaryDisplay: function (revert) {
  969. var node = this.renderTo, tempStyle;
  970. if (!revert) {
  971. while (node && node.style) {
  972. // When rendering to a detached node, it needs to be temporarily
  973. // attached in order to read styling and bounding boxes (#5783,
  974. // #7024).
  975. if (!doc.body.contains(node) && !node.parentNode) {
  976. node.hcOrigDetached = true;
  977. doc.body.appendChild(node);
  978. }
  979. if (H.getStyle(node, 'display', false) === 'none' ||
  980. node.hcOricDetached) {
  981. node.hcOrigStyle = {
  982. display: node.style.display,
  983. height: node.style.height,
  984. overflow: node.style.overflow
  985. };
  986. tempStyle = {
  987. display: 'block',
  988. overflow: 'hidden'
  989. };
  990. if (node !== this.renderTo) {
  991. tempStyle.height = 0;
  992. }
  993. H.css(node, tempStyle);
  994. // If it still doesn't have an offset width after setting
  995. // display to block, it probably has an !important priority
  996. // #2631, 6803
  997. if (!node.offsetWidth) {
  998. node.style.setProperty('display', 'block', 'important');
  999. }
  1000. }
  1001. node = node.parentNode;
  1002. if (node === doc.body) {
  1003. break;
  1004. }
  1005. }
  1006. }
  1007. else {
  1008. while (node && node.style) {
  1009. if (node.hcOrigStyle) {
  1010. H.css(node, node.hcOrigStyle);
  1011. delete node.hcOrigStyle;
  1012. }
  1013. if (node.hcOrigDetached) {
  1014. doc.body.removeChild(node);
  1015. node.hcOrigDetached = false;
  1016. }
  1017. node = node.parentNode;
  1018. }
  1019. }
  1020. },
  1021. /**
  1022. * Set the {@link Chart.container|chart container's} class name, in
  1023. * addition to `highcharts-container`.
  1024. *
  1025. * @function Highcharts.Chart#setClassName
  1026. *
  1027. * @param {string} [className]
  1028. *
  1029. * @return {void}
  1030. */
  1031. setClassName: function (className) {
  1032. this.container.className = 'highcharts-container ' + (className || '');
  1033. },
  1034. /**
  1035. * Get the containing element, determine the size and create the inner
  1036. * container div to hold the chart.
  1037. *
  1038. * @private
  1039. * @function Highcharts.Chart#afterGetContainer
  1040. *
  1041. * @return {void}
  1042. *
  1043. * @fires Highcharts.Chart#event:afterGetContainer
  1044. */
  1045. getContainer: function () {
  1046. var chart = this, container, options = chart.options, optionsChart = options.chart, chartWidth, chartHeight, renderTo = chart.renderTo, indexAttrName = 'data-highcharts-chart', oldChartIndex, Ren, containerId = H.uniqueKey(), containerStyle, key;
  1047. if (!renderTo) {
  1048. chart.renderTo = renderTo =
  1049. optionsChart.renderTo;
  1050. }
  1051. if (isString(renderTo)) {
  1052. chart.renderTo = renderTo =
  1053. doc.getElementById(renderTo);
  1054. }
  1055. // Display an error if the renderTo is wrong
  1056. if (!renderTo) {
  1057. H.error(13, true, chart);
  1058. }
  1059. // If the container already holds a chart, destroy it. The check for
  1060. // hasRendered is there because web pages that are saved to disk from
  1061. // the browser, will preserve the data-highcharts-chart attribute and
  1062. // the SVG contents, but not an interactive chart. So in this case,
  1063. // charts[oldChartIndex] will point to the wrong chart if any (#2609).
  1064. oldChartIndex = pInt(attr(renderTo, indexAttrName));
  1065. if (isNumber(oldChartIndex) &&
  1066. charts[oldChartIndex] &&
  1067. charts[oldChartIndex].hasRendered) {
  1068. charts[oldChartIndex].destroy();
  1069. }
  1070. // Make a reference to the chart from the div
  1071. attr(renderTo, indexAttrName, chart.index);
  1072. // remove previous chart
  1073. renderTo.innerHTML = '';
  1074. // If the container doesn't have an offsetWidth, it has or is a child of
  1075. // a node that has display:none. We need to temporarily move it out to a
  1076. // visible state to determine the size, else the legend and tooltips
  1077. // won't render properly. The skipClone option is used in sparklines as
  1078. // a micro optimization, saving about 1-2 ms each chart.
  1079. if (!optionsChart.skipClone && !renderTo.offsetWidth) {
  1080. chart.temporaryDisplay();
  1081. }
  1082. // get the width and height
  1083. chart.getChartSize();
  1084. chartWidth = chart.chartWidth;
  1085. chartHeight = chart.chartHeight;
  1086. // Allow table cells and flex-boxes to shrink without the chart blocking
  1087. // them out (#6427)
  1088. css(renderTo, { overflow: 'hidden' });
  1089. // Create the inner container
  1090. if (!chart.styledMode) {
  1091. containerStyle = extend({
  1092. position: 'relative',
  1093. // needed for context menu (avoidscrollbars) and content
  1094. // overflow in IE
  1095. overflow: 'hidden',
  1096. width: chartWidth + 'px',
  1097. height: chartHeight + 'px',
  1098. textAlign: 'left',
  1099. lineHeight: 'normal',
  1100. zIndex: 0,
  1101. '-webkit-tap-highlight-color': 'rgba(0,0,0,0)'
  1102. }, optionsChart.style);
  1103. }
  1104. /**
  1105. * The containing HTML element of the chart. The container is
  1106. * dynamically inserted into the element given as the `renderTo`
  1107. * parameter in the {@link Highcharts#chart} constructor.
  1108. *
  1109. * @name Highcharts.Chart#container
  1110. * @type {Highcharts.HTMLDOMElement}
  1111. */
  1112. container = createElement('div', {
  1113. id: containerId
  1114. }, containerStyle, renderTo);
  1115. chart.container = container;
  1116. // cache the cursor (#1650)
  1117. chart._cursor = container.style.cursor;
  1118. // Initialize the renderer
  1119. Ren = H[optionsChart.renderer] || H.Renderer;
  1120. /**
  1121. * The renderer instance of the chart. Each chart instance has only one
  1122. * associated renderer.
  1123. *
  1124. * @name Highcharts.Chart#renderer
  1125. * @type {Highcharts.SVGRenderer}
  1126. */
  1127. chart.renderer = new Ren(container, chartWidth, chartHeight, null, optionsChart.forExport, options.exporting && options.exporting.allowHTML, chart.styledMode);
  1128. chart.setClassName(optionsChart.className);
  1129. if (!chart.styledMode) {
  1130. chart.renderer.setStyle(optionsChart.style);
  1131. }
  1132. else {
  1133. // Initialize definitions
  1134. for (key in options.defs) { // eslint-disable-line guard-for-in
  1135. this.renderer.definition(options.defs[key]);
  1136. }
  1137. }
  1138. // Add a reference to the charts index
  1139. chart.renderer.chartIndex = chart.index;
  1140. fireEvent(this, 'afterGetContainer');
  1141. },
  1142. /**
  1143. * Calculate margins by rendering axis labels in a preliminary position.
  1144. * Title, subtitle and legend have already been rendered at this stage, but
  1145. * will be moved into their final positions.
  1146. *
  1147. * @private
  1148. * @function Highcharts.Chart#getMargins
  1149. * @param {boolean} skipAxes
  1150. * @return {void}
  1151. * @fires Highcharts.Chart#event:getMargins
  1152. */
  1153. getMargins: function (skipAxes) {
  1154. var _a = this, spacing = _a.spacing, margin = _a.margin, titleOffset = _a.titleOffset;
  1155. this.resetMargins();
  1156. // Adjust for title and subtitle
  1157. if (titleOffset[0] && !defined(margin[0])) {
  1158. this.plotTop = Math.max(this.plotTop, titleOffset[0] + spacing[0]);
  1159. }
  1160. if (titleOffset[2] && !defined(margin[2])) {
  1161. this.marginBottom = Math.max(this.marginBottom, titleOffset[2] + spacing[2]);
  1162. }
  1163. // Adjust for legend
  1164. if (this.legend && this.legend.display) {
  1165. this.legend.adjustMargins(margin, spacing);
  1166. }
  1167. fireEvent(this, 'getMargins');
  1168. if (!skipAxes) {
  1169. this.getAxisMargins();
  1170. }
  1171. },
  1172. /**
  1173. * @private
  1174. * @function Highcharts.Chart#getAxisMargins
  1175. * @return {void}
  1176. */
  1177. getAxisMargins: function () {
  1178. var chart = this,
  1179. // [top, right, bottom, left]
  1180. axisOffset = chart.axisOffset = [0, 0, 0, 0], colorAxis = chart.colorAxis, margin = chart.margin, getOffset = function (axes) {
  1181. axes.forEach(function (axis) {
  1182. if (axis.visible) {
  1183. axis.getOffset();
  1184. }
  1185. });
  1186. };
  1187. // pre-render axes to get labels offset width
  1188. if (chart.hasCartesianSeries) {
  1189. getOffset(chart.axes);
  1190. }
  1191. else if (colorAxis && colorAxis.length) {
  1192. getOffset(colorAxis);
  1193. }
  1194. // Add the axis offsets
  1195. marginNames.forEach(function (m, side) {
  1196. if (!defined(margin[side])) {
  1197. chart[m] += axisOffset[side];
  1198. }
  1199. });
  1200. chart.setChartSize();
  1201. },
  1202. /**
  1203. * Reflows the chart to its container. By default, the chart reflows
  1204. * automatically to its container following a `window.resize` event, as per
  1205. * the [chart.reflow](https://api.highcharts.com/highcharts/chart.reflow)
  1206. * option. However, there are no reliable events for div resize, so if the
  1207. * container is resized without a window resize event, this must be called
  1208. * explicitly.
  1209. *
  1210. * @sample highcharts/members/chart-reflow/
  1211. * Resize div and reflow
  1212. * @sample highcharts/chart/events-container/
  1213. * Pop up and reflow
  1214. *
  1215. * @function Highcharts.Chart#reflow
  1216. *
  1217. * @param {global.Event} [e]
  1218. * Event arguments. Used primarily when the function is called
  1219. * internally as a response to window resize.
  1220. */
  1221. reflow: function (e) {
  1222. var chart = this, optionsChart = chart.options.chart, renderTo = chart.renderTo, hasUserSize = (defined(optionsChart.width) &&
  1223. defined(optionsChart.height)), width = optionsChart.width || H.getStyle(renderTo, 'width'), height = optionsChart.height || H.getStyle(renderTo, 'height'), target = e ? e.target : win;
  1224. // Width and height checks for display:none. Target is doc in IE8 and
  1225. // Opera, win in Firefox, Chrome and IE9.
  1226. if (!hasUserSize &&
  1227. !chart.isPrinting &&
  1228. width &&
  1229. height &&
  1230. (target === win || target === doc)) {
  1231. if (width !== chart.containerWidth ||
  1232. height !== chart.containerHeight) {
  1233. H.clearTimeout(chart.reflowTimeout);
  1234. // When called from window.resize, e is set, else it's called
  1235. // directly (#2224)
  1236. chart.reflowTimeout = syncTimeout(function () {
  1237. // Set size, it may have been destroyed in the meantime
  1238. // (#1257)
  1239. if (chart.container) {
  1240. chart.setSize(void 0, void 0, false);
  1241. }
  1242. }, e ? 100 : 0);
  1243. }
  1244. chart.containerWidth = width;
  1245. chart.containerHeight = height;
  1246. }
  1247. },
  1248. /**
  1249. * Toggle the event handlers necessary for auto resizing, depending on the
  1250. * `chart.reflow` option.
  1251. *
  1252. * @private
  1253. * @function Highcharts.Chart#setReflow
  1254. * @param {boolean} [reflow]
  1255. * @return {void}
  1256. */
  1257. setReflow: function (reflow) {
  1258. var chart = this;
  1259. if (reflow !== false && !this.unbindReflow) {
  1260. this.unbindReflow = addEvent(win, 'resize', function (e) {
  1261. // a removed event listener still runs in Edge and IE if the
  1262. // listener was removed while the event runs, so check if the
  1263. // chart is not destroyed (#11609)
  1264. if (chart.options) {
  1265. chart.reflow(e);
  1266. }
  1267. });
  1268. addEvent(this, 'destroy', this.unbindReflow);
  1269. }
  1270. else if (reflow === false && this.unbindReflow) {
  1271. // Unbind and unset
  1272. this.unbindReflow = this.unbindReflow();
  1273. }
  1274. // The following will add listeners to re-fit the chart before and after
  1275. // printing (#2284). However it only works in WebKit. Should have worked
  1276. // in Firefox, but not supported in IE.
  1277. /*
  1278. if (win.matchMedia) {
  1279. win.matchMedia('print').addListener(function reflow() {
  1280. chart.reflow();
  1281. });
  1282. }
  1283. //*/
  1284. },
  1285. /**
  1286. * Resize the chart to a given width and height. In order to set the width
  1287. * only, the height argument may be skipped. To set the height only, pass
  1288. * `undefined` for the width.
  1289. *
  1290. * @sample highcharts/members/chart-setsize-button/
  1291. * Test resizing from buttons
  1292. * @sample highcharts/members/chart-setsize-jquery-resizable/
  1293. * Add a jQuery UI resizable
  1294. * @sample stock/members/chart-setsize/
  1295. * Highstock with UI resizable
  1296. *
  1297. * @function Highcharts.Chart#setSize
  1298. *
  1299. * @param {number|null} [width]
  1300. * The new pixel width of the chart. Since v4.2.6, the argument can
  1301. * be `undefined` in order to preserve the current value (when
  1302. * setting height only), or `null` to adapt to the width of the
  1303. * containing element.
  1304. *
  1305. * @param {number|null} [height]
  1306. * The new pixel height of the chart. Since v4.2.6, the argument can
  1307. * be `undefined` in order to preserve the current value, or `null`
  1308. * in order to adapt to the height of the containing element.
  1309. *
  1310. * @param {boolean|Highcharts.AnimationOptionsObject} [animation=true]
  1311. * Whether and how to apply animation.
  1312. *
  1313. * @return {void}
  1314. *
  1315. * @fires Highcharts.Chart#event:endResize
  1316. * @fires Highcharts.Chart#event:resize
  1317. */
  1318. setSize: function (width, height, animation) {
  1319. var chart = this, renderer = chart.renderer, globalAnimation;
  1320. // Handle the isResizing counter
  1321. chart.isResizing += 1;
  1322. // set the animation for the current process
  1323. setAnimation(animation, chart);
  1324. chart.oldChartHeight = chart.chartHeight;
  1325. chart.oldChartWidth = chart.chartWidth;
  1326. if (typeof width !== 'undefined') {
  1327. chart.options.chart.width = width;
  1328. }
  1329. if (typeof height !== 'undefined') {
  1330. chart.options.chart.height = height;
  1331. }
  1332. chart.getChartSize();
  1333. // Resize the container with the global animation applied if enabled
  1334. // (#2503)
  1335. if (!chart.styledMode) {
  1336. globalAnimation = renderer.globalAnimation;
  1337. (globalAnimation ? animate : css)(chart.container, {
  1338. width: chart.chartWidth + 'px',
  1339. height: chart.chartHeight + 'px'
  1340. }, globalAnimation);
  1341. }
  1342. chart.setChartSize(true);
  1343. renderer.setSize(chart.chartWidth, chart.chartHeight, animation);
  1344. // handle axes
  1345. chart.axes.forEach(function (axis) {
  1346. axis.isDirty = true;
  1347. axis.setScale();
  1348. });
  1349. chart.isDirtyLegend = true; // force legend redraw
  1350. chart.isDirtyBox = true; // force redraw of plot and chart border
  1351. chart.layOutTitles(); // #2857
  1352. chart.getMargins();
  1353. chart.redraw(animation);
  1354. chart.oldChartHeight = null;
  1355. fireEvent(chart, 'resize');
  1356. // Fire endResize and set isResizing back. If animation is disabled,
  1357. // fire without delay
  1358. syncTimeout(function () {
  1359. if (chart) {
  1360. fireEvent(chart, 'endResize', null, function () {
  1361. chart.isResizing -= 1;
  1362. });
  1363. }
  1364. }, animObject(globalAnimation).duration || 0);
  1365. },
  1366. /**
  1367. * Set the public chart properties. This is done before and after the
  1368. * pre-render to determine margin sizes.
  1369. *
  1370. * @private
  1371. * @function Highcharts.Chart#setChartSize
  1372. *
  1373. * @param {boolean} skipAxes
  1374. *
  1375. * @return {void}
  1376. *
  1377. * @fires Highcharts.Chart#event:afterSetChartSize
  1378. */
  1379. setChartSize: function (skipAxes) {
  1380. var chart = this, inverted = chart.inverted, renderer = chart.renderer, chartWidth = chart.chartWidth, chartHeight = chart.chartHeight, optionsChart = chart.options.chart, spacing = chart.spacing, clipOffset = chart.clipOffset, clipX, clipY, plotLeft, plotTop, plotWidth, plotHeight, plotBorderWidth;
  1381. /**
  1382. * The current left position of the plot area in pixels.
  1383. *
  1384. * @name Highcharts.Chart#plotLeft
  1385. * @type {number}
  1386. */
  1387. chart.plotLeft = plotLeft = Math.round(chart.plotLeft);
  1388. /**
  1389. * The current top position of the plot area in pixels.
  1390. *
  1391. * @name Highcharts.Chart#plotTop
  1392. * @type {number}
  1393. */
  1394. chart.plotTop = plotTop = Math.round(chart.plotTop);
  1395. /**
  1396. * The current width of the plot area in pixels.
  1397. *
  1398. * @name Highcharts.Chart#plotWidth
  1399. * @type {number}
  1400. */
  1401. chart.plotWidth = plotWidth = Math.max(0, Math.round(chartWidth - plotLeft - chart.marginRight));
  1402. /**
  1403. * The current height of the plot area in pixels.
  1404. *
  1405. * @name Highcharts.Chart#plotHeight
  1406. * @type {number}
  1407. */
  1408. chart.plotHeight = plotHeight = Math.max(0, Math.round(chartHeight - plotTop - chart.marginBottom));
  1409. chart.plotSizeX = inverted ? plotHeight : plotWidth;
  1410. chart.plotSizeY = inverted ? plotWidth : plotHeight;
  1411. chart.plotBorderWidth = optionsChart.plotBorderWidth || 0;
  1412. // Set boxes used for alignment
  1413. chart.spacingBox = renderer.spacingBox = {
  1414. x: spacing[3],
  1415. y: spacing[0],
  1416. width: chartWidth - spacing[3] - spacing[1],
  1417. height: chartHeight - spacing[0] - spacing[2]
  1418. };
  1419. chart.plotBox = renderer.plotBox = {
  1420. x: plotLeft,
  1421. y: plotTop,
  1422. width: plotWidth,
  1423. height: plotHeight
  1424. };
  1425. plotBorderWidth = 2 * Math.floor(chart.plotBorderWidth / 2);
  1426. clipX = Math.ceil(Math.max(plotBorderWidth, clipOffset[3]) / 2);
  1427. clipY = Math.ceil(Math.max(plotBorderWidth, clipOffset[0]) / 2);
  1428. chart.clipBox = {
  1429. x: clipX,
  1430. y: clipY,
  1431. width: Math.floor(chart.plotSizeX -
  1432. Math.max(plotBorderWidth, clipOffset[1]) / 2 -
  1433. clipX),
  1434. height: Math.max(0, Math.floor(chart.plotSizeY -
  1435. Math.max(plotBorderWidth, clipOffset[2]) / 2 -
  1436. clipY))
  1437. };
  1438. if (!skipAxes) {
  1439. chart.axes.forEach(function (axis) {
  1440. axis.setAxisSize();
  1441. axis.setAxisTranslation();
  1442. });
  1443. }
  1444. fireEvent(chart, 'afterSetChartSize', { skipAxes: skipAxes });
  1445. },
  1446. /**
  1447. * Initial margins before auto size margins are applied.
  1448. *
  1449. * @private
  1450. * @function Highcharts.Chart#resetMargins
  1451. * @return {void}
  1452. */
  1453. resetMargins: function () {
  1454. fireEvent(this, 'resetMargins');
  1455. var chart = this, chartOptions = chart.options.chart;
  1456. // Create margin and spacing array
  1457. ['margin', 'spacing'].forEach(function splashArrays(target) {
  1458. var value = chartOptions[target], values = isObject(value) ? value : [value, value, value, value];
  1459. [
  1460. 'Top',
  1461. 'Right',
  1462. 'Bottom',
  1463. 'Left'
  1464. ].forEach(function (sideName, side) {
  1465. chart[target][side] = pick(chartOptions[target + sideName], values[side]);
  1466. });
  1467. });
  1468. // Set margin names like chart.plotTop, chart.plotLeft,
  1469. // chart.marginRight, chart.marginBottom.
  1470. marginNames.forEach(function (m, side) {
  1471. chart[m] = pick(chart.margin[side], chart.spacing[side]);
  1472. });
  1473. chart.axisOffset = [0, 0, 0, 0]; // top, right, bottom, left
  1474. chart.clipOffset = [0, 0, 0, 0];
  1475. },
  1476. /**
  1477. * Internal function to draw or redraw the borders and backgrounds for chart
  1478. * and plot area.
  1479. *
  1480. * @private
  1481. * @function Highcharts.Chart#drawChartBox
  1482. * @return {void}
  1483. * @fires Highcharts.Chart#event:afterDrawChartBox
  1484. */
  1485. drawChartBox: function () {
  1486. var chart = this, optionsChart = chart.options.chart, renderer = chart.renderer, chartWidth = chart.chartWidth, chartHeight = chart.chartHeight, chartBackground = chart.chartBackground, plotBackground = chart.plotBackground, plotBorder = chart.plotBorder, chartBorderWidth, styledMode = chart.styledMode, plotBGImage = chart.plotBGImage, chartBackgroundColor = optionsChart.backgroundColor, plotBackgroundColor = optionsChart.plotBackgroundColor, plotBackgroundImage = optionsChart.plotBackgroundImage, mgn, bgAttr, plotLeft = chart.plotLeft, plotTop = chart.plotTop, plotWidth = chart.plotWidth, plotHeight = chart.plotHeight, plotBox = chart.plotBox, clipRect = chart.clipRect, clipBox = chart.clipBox, verb = 'animate';
  1487. // Chart area
  1488. if (!chartBackground) {
  1489. chart.chartBackground = chartBackground = renderer.rect()
  1490. .addClass('highcharts-background')
  1491. .add();
  1492. verb = 'attr';
  1493. }
  1494. if (!styledMode) {
  1495. // Presentational
  1496. chartBorderWidth = optionsChart.borderWidth || 0;
  1497. mgn = chartBorderWidth + (optionsChart.shadow ? 8 : 0);
  1498. bgAttr = {
  1499. fill: chartBackgroundColor || 'none'
  1500. };
  1501. if (chartBorderWidth || chartBackground['stroke-width']) { // #980
  1502. bgAttr.stroke = optionsChart.borderColor;
  1503. bgAttr['stroke-width'] = chartBorderWidth;
  1504. }
  1505. chartBackground
  1506. .attr(bgAttr)
  1507. .shadow(optionsChart.shadow);
  1508. }
  1509. else {
  1510. chartBorderWidth = mgn = chartBackground.strokeWidth();
  1511. }
  1512. chartBackground[verb]({
  1513. x: mgn / 2,
  1514. y: mgn / 2,
  1515. width: chartWidth - mgn - chartBorderWidth % 2,
  1516. height: chartHeight - mgn - chartBorderWidth % 2,
  1517. r: optionsChart.borderRadius
  1518. });
  1519. // Plot background
  1520. verb = 'animate';
  1521. if (!plotBackground) {
  1522. verb = 'attr';
  1523. chart.plotBackground = plotBackground = renderer.rect()
  1524. .addClass('highcharts-plot-background')
  1525. .add();
  1526. }
  1527. plotBackground[verb](plotBox);
  1528. if (!styledMode) {
  1529. // Presentational attributes for the background
  1530. plotBackground
  1531. .attr({
  1532. fill: plotBackgroundColor || 'none'
  1533. })
  1534. .shadow(optionsChart.plotShadow);
  1535. // Create the background image
  1536. if (plotBackgroundImage) {
  1537. if (!plotBGImage) {
  1538. chart.plotBGImage = renderer.image(plotBackgroundImage, plotLeft, plotTop, plotWidth, plotHeight).add();
  1539. }
  1540. else {
  1541. if (plotBackgroundImage !== plotBGImage.attr('href')) {
  1542. plotBGImage.attr('href', plotBackgroundImage);
  1543. }
  1544. plotBGImage.animate(plotBox);
  1545. }
  1546. }
  1547. }
  1548. // Plot clip
  1549. if (!clipRect) {
  1550. chart.clipRect = renderer.clipRect(clipBox);
  1551. }
  1552. else {
  1553. clipRect.animate({
  1554. width: clipBox.width,
  1555. height: clipBox.height
  1556. });
  1557. }
  1558. // Plot area border
  1559. verb = 'animate';
  1560. if (!plotBorder) {
  1561. verb = 'attr';
  1562. chart.plotBorder = plotBorder = renderer.rect()
  1563. .addClass('highcharts-plot-border')
  1564. .attr({
  1565. zIndex: 1 // Above the grid
  1566. })
  1567. .add();
  1568. }
  1569. if (!styledMode) {
  1570. // Presentational
  1571. plotBorder.attr({
  1572. stroke: optionsChart.plotBorderColor,
  1573. 'stroke-width': optionsChart.plotBorderWidth || 0,
  1574. fill: 'none'
  1575. });
  1576. }
  1577. plotBorder[verb](plotBorder.crisp({
  1578. x: plotLeft,
  1579. y: plotTop,
  1580. width: plotWidth,
  1581. height: plotHeight
  1582. }, -plotBorder.strokeWidth())); // #3282 plotBorder should be negative;
  1583. // reset
  1584. chart.isDirtyBox = false;
  1585. fireEvent(this, 'afterDrawChartBox');
  1586. },
  1587. /**
  1588. * Detect whether a certain chart property is needed based on inspecting its
  1589. * options and series. This mainly applies to the chart.inverted property,
  1590. * and in extensions to the chart.angular and chart.polar properties.
  1591. *
  1592. * @private
  1593. * @function Highcharts.Chart#propFromSeries
  1594. * @return {void}
  1595. */
  1596. propFromSeries: function () {
  1597. var chart = this, optionsChart = chart.options.chart, klass, seriesOptions = chart.options.series, i, value;
  1598. /**
  1599. * The flag is set to `true` if a series of the chart is inverted.
  1600. *
  1601. * @name Highcharts.Chart#inverted
  1602. * @type {boolean|undefined}
  1603. */
  1604. ['inverted', 'angular', 'polar'].forEach(function (key) {
  1605. // The default series type's class
  1606. klass = seriesTypes[(optionsChart.type ||
  1607. optionsChart.defaultSeriesType)];
  1608. // Get the value from available chart-wide properties
  1609. value =
  1610. // It is set in the options:
  1611. optionsChart[key] ||
  1612. // The default series class:
  1613. (klass && klass.prototype[key]);
  1614. // requires it
  1615. // 4. Check if any the chart's series require it
  1616. i = seriesOptions && seriesOptions.length;
  1617. while (!value && i--) {
  1618. klass = seriesTypes[seriesOptions[i].type];
  1619. if (klass && klass.prototype[key]) {
  1620. value = true;
  1621. }
  1622. }
  1623. // Set the chart property
  1624. chart[key] = value;
  1625. });
  1626. },
  1627. /**
  1628. * Internal function to link two or more series together, based on the
  1629. * `linkedTo` option. This is done from `Chart.render`, and after
  1630. * `Chart.addSeries` and `Series.remove`.
  1631. *
  1632. * @private
  1633. * @function Highcharts.Chart#linkSeries
  1634. * @return {void}
  1635. * @fires Highcharts.Chart#event:afterLinkSeries
  1636. */
  1637. linkSeries: function () {
  1638. var chart = this, chartSeries = chart.series;
  1639. // Reset links
  1640. chartSeries.forEach(function (series) {
  1641. series.linkedSeries.length = 0;
  1642. });
  1643. // Apply new links
  1644. chartSeries.forEach(function (series) {
  1645. var linkedTo = series.options.linkedTo;
  1646. if (isString(linkedTo)) {
  1647. if (linkedTo === ':previous') {
  1648. linkedTo = chart.series[series.index - 1];
  1649. }
  1650. else {
  1651. linkedTo = chart.get(linkedTo);
  1652. }
  1653. // #3341 avoid mutual linking
  1654. if (linkedTo && linkedTo.linkedParent !== series) {
  1655. linkedTo.linkedSeries.push(series);
  1656. series.linkedParent = linkedTo;
  1657. if (linkedTo.enabledDataSorting) {
  1658. series.setDataSortingOptions();
  1659. }
  1660. series.visible = pick(series.options.visible, linkedTo.options.visible, series.visible); // #3879
  1661. }
  1662. }
  1663. });
  1664. fireEvent(this, 'afterLinkSeries');
  1665. },
  1666. /**
  1667. * Render series for the chart.
  1668. *
  1669. * @private
  1670. * @function Highcharts.Chart#renderSeries
  1671. * @return {void}
  1672. */
  1673. renderSeries: function () {
  1674. this.series.forEach(function (serie) {
  1675. serie.translate();
  1676. serie.render();
  1677. });
  1678. },
  1679. /**
  1680. * Render labels for the chart.
  1681. *
  1682. * @private
  1683. * @function Highcharts.Chart#renderLabels
  1684. * @return {void}
  1685. */
  1686. renderLabels: function () {
  1687. var chart = this, labels = chart.options.labels;
  1688. if (labels.items) {
  1689. labels.items.forEach(function (label) {
  1690. var style = extend(labels.style, label.style), x = pInt(style.left) + chart.plotLeft, y = pInt(style.top) + chart.plotTop + 12;
  1691. // delete to prevent rewriting in IE
  1692. delete style.left;
  1693. delete style.top;
  1694. chart.renderer.text(label.html, x, y)
  1695. .attr({ zIndex: 2 })
  1696. .css(style)
  1697. .add();
  1698. });
  1699. }
  1700. },
  1701. /**
  1702. * Render all graphics for the chart. Runs internally on initialization.
  1703. *
  1704. * @private
  1705. * @function Highcharts.Chart#render
  1706. * @return {void}
  1707. */
  1708. render: function () {
  1709. var chart = this, axes = chart.axes, colorAxis = chart.colorAxis, renderer = chart.renderer, options = chart.options, correction = 0, // correction for X axis labels
  1710. tempWidth, tempHeight, redoHorizontal, redoVertical, renderAxes = function (axes) {
  1711. axes.forEach(function (axis) {
  1712. if (axis.visible) {
  1713. axis.render();
  1714. }
  1715. });
  1716. };
  1717. // Title
  1718. chart.setTitle();
  1719. /**
  1720. * The overview of the chart's series.
  1721. *
  1722. * @name Highcharts.Chart#legend
  1723. * @type {Highcharts.Legend}
  1724. */
  1725. chart.legend = new Legend(chart, options.legend);
  1726. // Get stacks
  1727. if (chart.getStacks) {
  1728. chart.getStacks();
  1729. }
  1730. // Get chart margins
  1731. chart.getMargins(true);
  1732. chart.setChartSize();
  1733. // Record preliminary dimensions for later comparison
  1734. tempWidth = chart.plotWidth;
  1735. axes.some(function (axis) {
  1736. if (axis.horiz &&
  1737. axis.visible &&
  1738. axis.options.labels.enabled &&
  1739. axis.series.length) {
  1740. // 21 is the most common correction for X axis labels
  1741. correction = 21;
  1742. return true;
  1743. }
  1744. });
  1745. // use Math.max to prevent negative plotHeight
  1746. chart.plotHeight = Math.max(chart.plotHeight - correction, 0);
  1747. tempHeight = chart.plotHeight;
  1748. // Get margins by pre-rendering axes
  1749. axes.forEach(function (axis) {
  1750. axis.setScale();
  1751. });
  1752. chart.getAxisMargins();
  1753. // If the plot area size has changed significantly, calculate tick
  1754. // positions again
  1755. redoHorizontal = tempWidth / chart.plotWidth > 1.1;
  1756. // Height is more sensitive, use lower threshold
  1757. redoVertical = tempHeight / chart.plotHeight > 1.05;
  1758. if (redoHorizontal || redoVertical) {
  1759. axes.forEach(function (axis) {
  1760. if ((axis.horiz && redoHorizontal) ||
  1761. (!axis.horiz && redoVertical)) {
  1762. // update to reflect the new margins
  1763. axis.setTickInterval(true);
  1764. }
  1765. });
  1766. chart.getMargins(); // second pass to check for new labels
  1767. }
  1768. // Draw the borders and backgrounds
  1769. chart.drawChartBox();
  1770. // Axes
  1771. if (chart.hasCartesianSeries) {
  1772. renderAxes(axes);
  1773. }
  1774. else if (colorAxis && colorAxis.length) {
  1775. renderAxes(colorAxis);
  1776. }
  1777. // The series
  1778. if (!chart.seriesGroup) {
  1779. chart.seriesGroup = renderer.g('series-group')
  1780. .attr({ zIndex: 3 })
  1781. .add();
  1782. }
  1783. chart.renderSeries();
  1784. // Labels
  1785. chart.renderLabels();
  1786. // Credits
  1787. chart.addCredits();
  1788. // Handle responsiveness
  1789. if (chart.setResponsive) {
  1790. chart.setResponsive();
  1791. }
  1792. // Handle scaling
  1793. chart.updateContainerScaling();
  1794. // Set flag
  1795. chart.hasRendered = true;
  1796. },
  1797. /**
  1798. * Set a new credits label for the chart.
  1799. *
  1800. * @sample highcharts/credits/credits-update/
  1801. * Add and update credits
  1802. *
  1803. * @function Highcharts.Chart#addCredits
  1804. *
  1805. * @param {Highcharts.CreditsOptions} credits
  1806. * A configuration object for the new credits.
  1807. *
  1808. * @return {void}
  1809. */
  1810. addCredits: function (credits) {
  1811. var chart = this;
  1812. credits = merge(true, this.options.credits, credits);
  1813. if (credits.enabled && !this.credits) {
  1814. /**
  1815. * The chart's credits label. The label has an `update` method that
  1816. * allows setting new options as per the
  1817. * [credits options set](https://api.highcharts.com/highcharts/credits).
  1818. *
  1819. * @name Highcharts.Chart#credits
  1820. * @type {Highcharts.SVGElement}
  1821. */
  1822. this.credits = this.renderer.text(credits.text + (this.mapCredits || ''), 0, 0)
  1823. .addClass('highcharts-credits')
  1824. .on('click', function () {
  1825. if (credits.href) {
  1826. win.location.href = credits.href;
  1827. }
  1828. })
  1829. .attr({
  1830. align: credits.position.align,
  1831. zIndex: 8
  1832. });
  1833. if (!chart.styledMode) {
  1834. this.credits.css(credits.style);
  1835. }
  1836. this.credits
  1837. .add()
  1838. .align(credits.position);
  1839. // Dynamically update
  1840. this.credits.update = function (options) {
  1841. chart.credits = chart.credits.destroy();
  1842. chart.addCredits(options);
  1843. };
  1844. }
  1845. },
  1846. /**
  1847. * Handle scaling, #11329 - when there is scaling/transform on the container
  1848. * or on a parent element, we need to take this into account. We calculate
  1849. * the scaling once here and it is picked up where we need to use it
  1850. * (Pointer, Tooltip).
  1851. *
  1852. * @private
  1853. * @function Highcharts.Chart#updateContainerScaling
  1854. * @return {void}
  1855. */
  1856. updateContainerScaling: function () {
  1857. var container = this.container;
  1858. if (container.offsetWidth &&
  1859. container.offsetHeight &&
  1860. container.getBoundingClientRect) {
  1861. var bb = container.getBoundingClientRect(), scaleX = bb.width / container.offsetWidth, scaleY = bb.height / container.offsetHeight;
  1862. if (scaleX !== 1 || scaleY !== 1) {
  1863. this.containerScaling = { scaleX: scaleX, scaleY: scaleY };
  1864. }
  1865. else {
  1866. delete this.containerScaling;
  1867. }
  1868. }
  1869. },
  1870. /**
  1871. * Remove the chart and purge memory. This method is called internally
  1872. * before adding a second chart into the same container, as well as on
  1873. * window unload to prevent leaks.
  1874. *
  1875. * @sample highcharts/members/chart-destroy/
  1876. * Destroy the chart from a button
  1877. * @sample stock/members/chart-destroy/
  1878. * Destroy with Highstock
  1879. *
  1880. * @function Highcharts.Chart#destroy
  1881. *
  1882. * @return {void}
  1883. *
  1884. * @fires Highcharts.Chart#event:destroy
  1885. */
  1886. destroy: function () {
  1887. var chart = this, axes = chart.axes, series = chart.series, container = chart.container, i, parentNode = container && container.parentNode;
  1888. // fire the chart.destoy event
  1889. fireEvent(chart, 'destroy');
  1890. // Delete the chart from charts lookup array
  1891. if (chart.renderer.forExport) {
  1892. erase(charts, chart); // #6569
  1893. }
  1894. else {
  1895. charts[chart.index] = void 0;
  1896. }
  1897. H.chartCount--;
  1898. chart.renderTo.removeAttribute('data-highcharts-chart');
  1899. // remove events
  1900. removeEvent(chart);
  1901. // ==== Destroy collections:
  1902. // Destroy axes
  1903. i = axes.length;
  1904. while (i--) {
  1905. axes[i] = axes[i].destroy();
  1906. }
  1907. // Destroy scroller & scroller series before destroying base series
  1908. if (this.scroller && this.scroller.destroy) {
  1909. this.scroller.destroy();
  1910. }
  1911. // Destroy each series
  1912. i = series.length;
  1913. while (i--) {
  1914. series[i] = series[i].destroy();
  1915. }
  1916. // ==== Destroy chart properties:
  1917. [
  1918. 'title', 'subtitle', 'chartBackground', 'plotBackground',
  1919. 'plotBGImage', 'plotBorder', 'seriesGroup', 'clipRect', 'credits',
  1920. 'pointer', 'rangeSelector', 'legend', 'resetZoomButton', 'tooltip',
  1921. 'renderer'
  1922. ].forEach(function (name) {
  1923. var prop = chart[name];
  1924. if (prop && prop.destroy) {
  1925. chart[name] = prop.destroy();
  1926. }
  1927. });
  1928. // Remove container and all SVG, check container as it can break in IE
  1929. // when destroyed before finished loading
  1930. if (container) {
  1931. container.innerHTML = '';
  1932. removeEvent(container);
  1933. if (parentNode) {
  1934. discardElement(container);
  1935. }
  1936. }
  1937. // clean it all up
  1938. objectEach(chart, function (val, key) {
  1939. delete chart[key];
  1940. });
  1941. },
  1942. /**
  1943. * Prepare for first rendering after all data are loaded.
  1944. *
  1945. * @private
  1946. * @function Highcharts.Chart#firstRender
  1947. * @return {void}
  1948. *
  1949. * @fires Highcharts.Chart#event:beforeRender
  1950. */
  1951. firstRender: function () {
  1952. var chart = this, options = chart.options;
  1953. // Hook for oldIE to check whether the chart is ready to render
  1954. if (chart.isReadyToRender && !chart.isReadyToRender()) {
  1955. return;
  1956. }
  1957. // Create the container
  1958. chart.getContainer();
  1959. chart.resetMargins();
  1960. chart.setChartSize();
  1961. // Set the common chart properties (mainly invert) from the given series
  1962. chart.propFromSeries();
  1963. // get axes
  1964. chart.getAxes();
  1965. // Initialize the series
  1966. (isArray(options.series) ? options.series : []).forEach(
  1967. // #9680
  1968. function (serieOptions) {
  1969. chart.initSeries(serieOptions);
  1970. });
  1971. chart.linkSeries();
  1972. chart.setSeriesData();
  1973. // Run an event after axes and series are initialized, but before
  1974. // render. At this stage, the series data is indexed and cached in the
  1975. // xData and yData arrays, so we can access those before rendering. Used
  1976. // in Highstock.
  1977. fireEvent(chart, 'beforeRender');
  1978. // depends on inverted and on margins being set
  1979. if (Pointer) {
  1980. /**
  1981. * The Pointer that keeps track of mouse and touch interaction.
  1982. *
  1983. * @memberof Highcharts.Chart
  1984. * @name pointer
  1985. * @type {Highcharts.Pointer}
  1986. * @instance
  1987. */
  1988. chart.pointer = new Pointer(chart, options);
  1989. }
  1990. chart.render();
  1991. // Fire the load event if there are no external images
  1992. if (!chart.renderer.imgCount && chart.onload) {
  1993. chart.onload();
  1994. }
  1995. // If the chart was rendered outside the top container, put it back in
  1996. // (#3679)
  1997. chart.temporaryDisplay(true);
  1998. },
  1999. /**
  2000. * Internal function that runs on chart load, async if any images are loaded
  2001. * in the chart. Runs the callbacks and triggers the `load` and `render`
  2002. * events.
  2003. *
  2004. * @private
  2005. * @function Highcharts.Chart#onload
  2006. * @return {void}
  2007. * @fires Highcharts.Chart#event:load
  2008. * @fires Highcharts.Chart#event:render
  2009. */
  2010. onload: function () {
  2011. // Run callbacks, first the ones registered by modules, then user's one
  2012. this.callbacks.concat([this.callback]).forEach(function (fn) {
  2013. // Chart destroyed in its own callback (#3600)
  2014. if (fn && typeof this.index !== 'undefined') {
  2015. fn.apply(this, [this]);
  2016. }
  2017. }, this);
  2018. fireEvent(this, 'load');
  2019. fireEvent(this, 'render');
  2020. // Set up auto resize, check for not destroyed (#6068)
  2021. if (defined(this.index)) {
  2022. this.setReflow(this.options.chart.reflow);
  2023. }
  2024. // Don't run again
  2025. this.onload = null;
  2026. }
  2027. }); // end Chart