RangeSelector.js 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import Axis from './Axis.js';
  12. import Chart from './Chart.js';
  13. import H from './Globals.js';
  14. import O from './Options.js';
  15. var defaultOptions = O.defaultOptions;
  16. import SVGElement from './SVGElement.js';
  17. import U from './Utilities.js';
  18. var addEvent = U.addEvent, createElement = U.createElement, css = U.css, defined = U.defined, destroyObjectProperties = U.destroyObjectProperties, discardElement = U.discardElement, extend = U.extend, fireEvent = U.fireEvent, isNumber = U.isNumber, merge = U.merge, objectEach = U.objectEach, pick = U.pick, pInt = U.pInt, splat = U.splat;
  19. /**
  20. * Define the time span for the button
  21. *
  22. * @typedef {"all"|"day"|"hour"|"millisecond"|"minute"|"month"|"second"|"week"|"year"|"ytd"} Highcharts.RangeSelectorButtonTypeValue
  23. */
  24. /**
  25. * Callback function to react on button clicks.
  26. *
  27. * @callback Highcharts.RangeSelectorClickCallbackFunction
  28. *
  29. * @param {global.Event} e
  30. * Event arguments.
  31. *
  32. * @param {boolean|undefined}
  33. * Return false to cancel the default button event.
  34. */
  35. /**
  36. * Callback function to parse values entered in the input boxes and return a
  37. * valid JavaScript time as milliseconds since 1970.
  38. *
  39. * @callback Highcharts.RangeSelectorParseCallbackFunction
  40. *
  41. * @param {string} value
  42. * Input value to parse.
  43. *
  44. * @return {number}
  45. * Parsed JavaScript time value.
  46. */
  47. /* ************************************************************************** *
  48. * Start Range Selector code *
  49. * ************************************************************************** */
  50. extend(defaultOptions, {
  51. /**
  52. * The range selector is a tool for selecting ranges to display within
  53. * the chart. It provides buttons to select preconfigured ranges in
  54. * the chart, like 1 day, 1 week, 1 month etc. It also provides input
  55. * boxes where min and max dates can be manually input.
  56. *
  57. * @product highstock gantt
  58. * @optionparent rangeSelector
  59. */
  60. rangeSelector: {
  61. /**
  62. * Whether to enable all buttons from the start. By default buttons are
  63. * only enabled if the corresponding time range exists on the X axis,
  64. * but enabling all buttons allows for dynamically loading different
  65. * time ranges.
  66. *
  67. * @sample {highstock} stock/rangeselector/allbuttonsenabled-true/
  68. * All buttons enabled
  69. *
  70. * @type {boolean}
  71. * @default false
  72. * @since 2.0.3
  73. * @apioption rangeSelector.allButtonsEnabled
  74. */
  75. /**
  76. * An array of configuration objects for the buttons.
  77. *
  78. * Defaults to:
  79. * ```js
  80. * buttons: [{
  81. * type: 'month',
  82. * count: 1,
  83. * text: '1m'
  84. * }, {
  85. * type: 'month',
  86. * count: 3,
  87. * text: '3m'
  88. * }, {
  89. * type: 'month',
  90. * count: 6,
  91. * text: '6m'
  92. * }, {
  93. * type: 'ytd',
  94. * text: 'YTD'
  95. * }, {
  96. * type: 'year',
  97. * count: 1,
  98. * text: '1y'
  99. * }, {
  100. * type: 'all',
  101. * text: 'All'
  102. * }]
  103. * ```
  104. *
  105. * @sample {highstock} stock/rangeselector/datagrouping/
  106. * Data grouping by buttons
  107. *
  108. * @type {Array<*>}
  109. * @apioption rangeSelector.buttons
  110. */
  111. /**
  112. * How many units of the defined type the button should span. If `type`
  113. * is "month" and `count` is 3, the button spans three months.
  114. *
  115. * @type {number}
  116. * @default 1
  117. * @apioption rangeSelector.buttons.count
  118. */
  119. /**
  120. * Fires when clicking on the rangeSelector button. One parameter,
  121. * event, is passed to the function, containing common event
  122. * information.
  123. *
  124. * ```js
  125. * click: function(e) {
  126. * console.log(this);
  127. * }
  128. * ```
  129. *
  130. * Return false to stop default button's click action.
  131. *
  132. * @sample {highstock} stock/rangeselector/button-click/
  133. * Click event on the button
  134. *
  135. * @type {Highcharts.RangeSelectorClickCallbackFunction}
  136. * @apioption rangeSelector.buttons.events.click
  137. */
  138. /**
  139. * Additional range (in milliseconds) added to the end of the calculated
  140. * time span.
  141. *
  142. * @sample {highstock} stock/rangeselector/min-max-offsets/
  143. * Button offsets
  144. *
  145. * @type {number}
  146. * @default 0
  147. * @since 6.0.0
  148. * @apioption rangeSelector.buttons.offsetMax
  149. */
  150. /**
  151. * Additional range (in milliseconds) added to the start of the
  152. * calculated time span.
  153. *
  154. * @sample {highstock} stock/rangeselector/min-max-offsets/
  155. * Button offsets
  156. *
  157. * @type {number}
  158. * @default 0
  159. * @since 6.0.0
  160. * @apioption rangeSelector.buttons.offsetMin
  161. */
  162. /**
  163. * When buttons apply dataGrouping on a series, by default zooming
  164. * in/out will deselect buttons and unset dataGrouping. Enable this
  165. * option to keep buttons selected when extremes change.
  166. *
  167. * @sample {highstock} stock/rangeselector/preserve-datagrouping/
  168. * Different preserveDataGrouping settings
  169. *
  170. * @type {boolean}
  171. * @default false
  172. * @since 6.1.2
  173. * @apioption rangeSelector.buttons.preserveDataGrouping
  174. */
  175. /**
  176. * A custom data grouping object for each button.
  177. *
  178. * @see [series.dataGrouping](#plotOptions.series.dataGrouping)
  179. *
  180. * @sample {highstock} stock/rangeselector/datagrouping/
  181. * Data grouping by range selector buttons
  182. *
  183. * @type {*}
  184. * @extends plotOptions.series.dataGrouping
  185. * @apioption rangeSelector.buttons.dataGrouping
  186. */
  187. /**
  188. * The text for the button itself.
  189. *
  190. * @type {string}
  191. * @apioption rangeSelector.buttons.text
  192. */
  193. /**
  194. * Defined the time span for the button. Can be one of `millisecond`,
  195. * `second`, `minute`, `hour`, `day`, `week`, `month`, `year`, `ytd`,
  196. * and `all`.
  197. *
  198. * @type {Highcharts.RangeSelectorButtonTypeValue}
  199. * @apioption rangeSelector.buttons.type
  200. */
  201. /**
  202. * The space in pixels between the buttons in the range selector.
  203. *
  204. * @type {number}
  205. * @default 0
  206. * @apioption rangeSelector.buttonSpacing
  207. */
  208. /**
  209. * Enable or disable the range selector.
  210. *
  211. * @sample {highstock} stock/rangeselector/enabled/
  212. * Disable the range selector
  213. *
  214. * @type {boolean}
  215. * @default true
  216. * @apioption rangeSelector.enabled
  217. */
  218. /**
  219. * The vertical alignment of the rangeselector box. Allowed properties
  220. * are `top`, `middle`, `bottom`.
  221. *
  222. * @sample {highstock} stock/rangeselector/vertical-align-middle/
  223. * Middle
  224. * @sample {highstock} stock/rangeselector/vertical-align-bottom/
  225. * Bottom
  226. *
  227. * @type {Highcharts.VerticalAlignValue}
  228. * @since 6.0.0
  229. */
  230. verticalAlign: 'top',
  231. /**
  232. * A collection of attributes for the buttons. The object takes SVG
  233. * attributes like `fill`, `stroke`, `stroke-width`, as well as `style`,
  234. * a collection of CSS properties for the text.
  235. *
  236. * The object can also be extended with states, so you can set
  237. * presentational options for `hover`, `select` or `disabled` button
  238. * states.
  239. *
  240. * CSS styles for the text label.
  241. *
  242. * In styled mode, the buttons are styled by the
  243. * `.highcharts-range-selector-buttons .highcharts-button` rule with its
  244. * different states.
  245. *
  246. * @sample {highstock} stock/rangeselector/styling/
  247. * Styling the buttons and inputs
  248. *
  249. * @type {Highcharts.SVGAttributes}
  250. */
  251. buttonTheme: {
  252. /** @ignore */
  253. width: 28,
  254. /** @ignore */
  255. height: 18,
  256. /** @ignore */
  257. padding: 2,
  258. /** @ignore */
  259. zIndex: 7 // #484, #852
  260. },
  261. /**
  262. * When the rangeselector is floating, the plot area does not reserve
  263. * space for it. This opens for positioning anywhere on the chart.
  264. *
  265. * @sample {highstock} stock/rangeselector/floating/
  266. * Placing the range selector between the plot area and the
  267. * navigator
  268. *
  269. * @since 6.0.0
  270. */
  271. floating: false,
  272. /**
  273. * The x offset of the range selector relative to its horizontal
  274. * alignment within `chart.spacingLeft` and `chart.spacingRight`.
  275. *
  276. * @since 6.0.0
  277. */
  278. x: 0,
  279. /**
  280. * The y offset of the range selector relative to its horizontal
  281. * alignment within `chart.spacingLeft` and `chart.spacingRight`.
  282. *
  283. * @since 6.0.0
  284. */
  285. y: 0,
  286. /**
  287. * Deprecated. The height of the range selector. Currently it is
  288. * calculated dynamically.
  289. *
  290. * @deprecated
  291. * @type {number|undefined}
  292. * @since 2.1.9
  293. */
  294. height: void 0,
  295. /**
  296. * The border color of the date input boxes.
  297. *
  298. * @sample {highstock} stock/rangeselector/styling/
  299. * Styling the buttons and inputs
  300. *
  301. * @type {Highcharts.ColorString}
  302. * @default #cccccc
  303. * @since 1.3.7
  304. * @apioption rangeSelector.inputBoxBorderColor
  305. */
  306. /**
  307. * The pixel height of the date input boxes.
  308. *
  309. * @sample {highstock} stock/rangeselector/styling/
  310. * Styling the buttons and inputs
  311. *
  312. * @type {number}
  313. * @default 17
  314. * @since 1.3.7
  315. * @apioption rangeSelector.inputBoxHeight
  316. */
  317. /**
  318. * CSS for the container DIV holding the input boxes. Deprecated as
  319. * of 1.2.5\. Use [inputPosition](#rangeSelector.inputPosition) instead.
  320. *
  321. * @sample {highstock} stock/rangeselector/styling/
  322. * Styling the buttons and inputs
  323. *
  324. * @deprecated
  325. * @type {Highcharts.CSSObject}
  326. * @apioption rangeSelector.inputBoxStyle
  327. */
  328. /**
  329. * The pixel width of the date input boxes.
  330. *
  331. * @sample {highstock} stock/rangeselector/styling/
  332. * Styling the buttons and inputs
  333. *
  334. * @type {number}
  335. * @default 90
  336. * @since 1.3.7
  337. * @apioption rangeSelector.inputBoxWidth
  338. */
  339. /**
  340. * The date format in the input boxes when not selected for editing.
  341. * Defaults to `%b %e, %Y`.
  342. *
  343. * @sample {highstock} stock/rangeselector/input-format/
  344. * Milliseconds in the range selector
  345. *
  346. * @type {string}
  347. * @default %b %e, %Y
  348. * @apioption rangeSelector.inputDateFormat
  349. */
  350. /**
  351. * A custom callback function to parse values entered in the input boxes
  352. * and return a valid JavaScript time as milliseconds since 1970.
  353. *
  354. * @sample {highstock} stock/rangeselector/input-format/
  355. * Milliseconds in the range selector
  356. *
  357. * @type {Highcharts.RangeSelectorParseCallbackFunction}
  358. * @since 1.3.3
  359. * @apioption rangeSelector.inputDateParser
  360. */
  361. /**
  362. * The date format in the input boxes when they are selected for
  363. * editing. This must be a format that is recognized by JavaScript
  364. * Date.parse.
  365. *
  366. * @sample {highstock} stock/rangeselector/input-format/
  367. * Milliseconds in the range selector
  368. *
  369. * @type {string}
  370. * @default %Y-%m-%d
  371. * @apioption rangeSelector.inputEditDateFormat
  372. */
  373. /**
  374. * Enable or disable the date input boxes. Defaults to enabled when
  375. * there is enough space, disabled if not (typically mobile).
  376. *
  377. * @sample {highstock} stock/rangeselector/input-datepicker/
  378. * Extending the input with a jQuery UI datepicker
  379. *
  380. * @type {boolean}
  381. * @default true
  382. * @apioption rangeSelector.inputEnabled
  383. */
  384. /**
  385. * Positioning for the input boxes. Allowed properties are `align`,
  386. * `x` and `y`.
  387. *
  388. * @since 1.2.4
  389. */
  390. inputPosition: {
  391. /**
  392. * The alignment of the input box. Allowed properties are `left`,
  393. * `center`, `right`.
  394. *
  395. * @sample {highstock} stock/rangeselector/input-button-position/
  396. * Alignment
  397. *
  398. * @type {Highcharts.AlignValue}
  399. * @since 6.0.0
  400. */
  401. align: 'right',
  402. /**
  403. * X offset of the input row.
  404. */
  405. x: 0,
  406. /**
  407. * Y offset of the input row.
  408. */
  409. y: 0
  410. },
  411. /**
  412. * The index of the button to appear pre-selected.
  413. *
  414. * @type {number}
  415. * @apioption rangeSelector.selected
  416. */
  417. /**
  418. * Positioning for the button row.
  419. *
  420. * @since 1.2.4
  421. */
  422. buttonPosition: {
  423. /**
  424. * The alignment of the input box. Allowed properties are `left`,
  425. * `center`, `right`.
  426. *
  427. * @sample {highstock} stock/rangeselector/input-button-position/
  428. * Alignment
  429. *
  430. * @type {Highcharts.AlignValue}
  431. * @since 6.0.0
  432. */
  433. align: 'left',
  434. /**
  435. * X offset of the button row.
  436. */
  437. x: 0,
  438. /**
  439. * Y offset of the button row.
  440. */
  441. y: 0
  442. },
  443. /**
  444. * CSS for the HTML inputs in the range selector.
  445. *
  446. * In styled mode, the inputs are styled by the
  447. * `.highcharts-range-input text` rule in SVG mode, and
  448. * `input.highcharts-range-selector` when active.
  449. *
  450. * @sample {highstock} stock/rangeselector/styling/
  451. * Styling the buttons and inputs
  452. *
  453. * @type {Highcharts.CSSObject}
  454. * @apioption rangeSelector.inputStyle
  455. */
  456. /**
  457. * CSS styles for the labels - the Zoom, From and To texts.
  458. *
  459. * In styled mode, the labels are styled by the
  460. * `.highcharts-range-label` class.
  461. *
  462. * @sample {highstock} stock/rangeselector/styling/
  463. * Styling the buttons and inputs
  464. *
  465. * @type {Highcharts.CSSObject}
  466. */
  467. labelStyle: {
  468. /** @ignore */
  469. color: '#666666'
  470. }
  471. }
  472. });
  473. defaultOptions.lang = merge(defaultOptions.lang,
  474. /**
  475. * Language object. The language object is global and it can't be set
  476. * on each chart initialization. Instead, use `Highcharts.setOptions` to
  477. * set it before any chart is initialized.
  478. *
  479. * ```js
  480. * Highcharts.setOptions({
  481. * lang: {
  482. * months: [
  483. * 'Janvier', 'Février', 'Mars', 'Avril',
  484. * 'Mai', 'Juin', 'Juillet', 'Août',
  485. * 'Septembre', 'Octobre', 'Novembre', 'Décembre'
  486. * ],
  487. * weekdays: [
  488. * 'Dimanche', 'Lundi', 'Mardi', 'Mercredi',
  489. * 'Jeudi', 'Vendredi', 'Samedi'
  490. * ]
  491. * }
  492. * });
  493. * ```
  494. *
  495. * @optionparent lang
  496. */
  497. {
  498. /**
  499. * The text for the label for the range selector buttons.
  500. *
  501. * @product highstock gantt
  502. */
  503. rangeSelectorZoom: 'Zoom',
  504. /**
  505. * The text for the label for the "from" input box in the range
  506. * selector.
  507. *
  508. * @product highstock gantt
  509. */
  510. rangeSelectorFrom: 'From',
  511. /**
  512. * The text for the label for the "to" input box in the range selector.
  513. *
  514. * @product highstock gantt
  515. */
  516. rangeSelectorTo: 'To'
  517. });
  518. /* eslint-disable no-invalid-this, valid-jsdoc */
  519. /**
  520. * The range selector.
  521. *
  522. * @private
  523. * @class
  524. * @name Highcharts.RangeSelector
  525. * @param {Highcharts.Chart} chart
  526. */
  527. var RangeSelector = /** @class */ (function () {
  528. function RangeSelector(chart) {
  529. /* *
  530. *
  531. * Properties
  532. *
  533. * */
  534. this.buttons = void 0;
  535. this.buttonOptions = RangeSelector.prototype.defaultButtons;
  536. this.options = void 0;
  537. this.chart = chart;
  538. // Run RangeSelector
  539. this.init(chart);
  540. }
  541. /**
  542. * The method to run when one of the buttons in the range selectors is
  543. * clicked
  544. *
  545. * @private
  546. * @function Highcharts.RangeSelector#clickButton
  547. * @param {number} i
  548. * The index of the button
  549. * @param {boolean} [redraw]
  550. * @return {void}
  551. */
  552. RangeSelector.prototype.clickButton = function (i, redraw) {
  553. var rangeSelector = this, chart = rangeSelector.chart, rangeOptions = rangeSelector.buttonOptions[i], baseAxis = chart.xAxis[0], unionExtremes = (chart.scroller && chart.scroller.getUnionExtremes()) || baseAxis || {}, dataMin = unionExtremes.dataMin, dataMax = unionExtremes.dataMax, newMin, newMax = baseAxis && Math.round(Math.min(baseAxis.max, pick(dataMax, baseAxis.max))), // #1568
  554. type = rangeOptions.type, baseXAxisOptions, range = rangeOptions._range, rangeMin, minSetting, rangeSetting, ctx, ytdExtremes, dataGrouping = rangeOptions.dataGrouping;
  555. // chart has no data, base series is removed
  556. if (dataMin === null || dataMax === null) {
  557. return;
  558. }
  559. // Set the fixed range before range is altered
  560. chart.fixedRange = range;
  561. // Apply dataGrouping associated to button
  562. if (dataGrouping) {
  563. this.forcedDataGrouping = true;
  564. Axis.prototype.setDataGrouping.call(baseAxis || { chart: this.chart }, dataGrouping, false);
  565. this.frozenStates = rangeOptions.preserveDataGrouping;
  566. }
  567. // Apply range
  568. if (type === 'month' || type === 'year') {
  569. if (!baseAxis) {
  570. // This is set to the user options and picked up later when the
  571. // axis is instantiated so that we know the min and max.
  572. range = rangeOptions;
  573. }
  574. else {
  575. ctx = {
  576. range: rangeOptions,
  577. max: newMax,
  578. chart: chart,
  579. dataMin: dataMin,
  580. dataMax: dataMax
  581. };
  582. newMin = baseAxis.minFromRange.call(ctx);
  583. if (isNumber(ctx.newMax)) {
  584. newMax = ctx.newMax;
  585. }
  586. }
  587. // Fixed times like minutes, hours, days
  588. }
  589. else if (range) {
  590. newMin = Math.max(newMax - range, dataMin);
  591. newMax = Math.min(newMin + range, dataMax);
  592. }
  593. else if (type === 'ytd') {
  594. // On user clicks on the buttons, or a delayed action running from
  595. // the beforeRender event (below), the baseAxis is defined.
  596. if (baseAxis) {
  597. // When "ytd" is the pre-selected button for the initial view,
  598. // its calculation is delayed and rerun in the beforeRender
  599. // event (below). When the series are initialized, but before
  600. // the chart is rendered, we have access to the xData array
  601. // (#942).
  602. if (typeof dataMax === 'undefined') {
  603. dataMin = Number.MAX_VALUE;
  604. dataMax = Number.MIN_VALUE;
  605. chart.series.forEach(function (series) {
  606. // reassign it to the last item
  607. var xData = series.xData;
  608. dataMin = Math.min(xData[0], dataMin);
  609. dataMax = Math.max(xData[xData.length - 1], dataMax);
  610. });
  611. redraw = false;
  612. }
  613. ytdExtremes = rangeSelector.getYTDExtremes(dataMax, dataMin, chart.time.useUTC);
  614. newMin = rangeMin = ytdExtremes.min;
  615. newMax = ytdExtremes.max;
  616. // "ytd" is pre-selected. We don't yet have access to processed
  617. // point and extremes data (things like pointStart and pointInterval
  618. // are missing), so we delay the process (#942)
  619. }
  620. else {
  621. rangeSelector.deferredYTDClick = i;
  622. return;
  623. }
  624. }
  625. else if (type === 'all' && baseAxis) {
  626. newMin = dataMin;
  627. newMax = dataMax;
  628. }
  629. newMin += rangeOptions._offsetMin;
  630. newMax += rangeOptions._offsetMax;
  631. rangeSelector.setSelected(i);
  632. // Update the chart
  633. if (!baseAxis) {
  634. // Axis not yet instanciated. Temporarily set min and range
  635. // options and remove them on chart load (#4317).
  636. baseXAxisOptions = splat(chart.options.xAxis)[0];
  637. rangeSetting = baseXAxisOptions.range;
  638. baseXAxisOptions.range = range;
  639. minSetting = baseXAxisOptions.min;
  640. baseXAxisOptions.min = rangeMin;
  641. addEvent(chart, 'load', function resetMinAndRange() {
  642. baseXAxisOptions.range = rangeSetting;
  643. baseXAxisOptions.min = minSetting;
  644. });
  645. }
  646. else {
  647. // Existing axis object. Set extremes after render time.
  648. baseAxis.setExtremes(newMin, newMax, pick(redraw, 1), null, // auto animation
  649. {
  650. trigger: 'rangeSelectorButton',
  651. rangeSelectorButton: rangeOptions
  652. });
  653. }
  654. };
  655. /**
  656. * Set the selected option. This method only sets the internal flag, it
  657. * doesn't update the buttons or the actual zoomed range.
  658. *
  659. * @private
  660. * @function Highcharts.RangeSelector#setSelected
  661. * @param {number} [selected]
  662. * @return {void}
  663. */
  664. RangeSelector.prototype.setSelected = function (selected) {
  665. this.selected = this.options.selected = selected;
  666. };
  667. /**
  668. * Initialize the range selector
  669. *
  670. * @private
  671. * @function Highcharts.RangeSelector#init
  672. * @param {Highcharts.Chart} chart
  673. * @return {void}
  674. */
  675. RangeSelector.prototype.init = function (chart) {
  676. var rangeSelector = this, options = chart.options.rangeSelector, buttonOptions = options.buttons || rangeSelector.defaultButtons.slice(), selectedOption = options.selected, blurInputs = function () {
  677. var minInput = rangeSelector.minInput, maxInput = rangeSelector.maxInput;
  678. // #3274 in some case blur is not defined
  679. if (minInput && minInput.blur) {
  680. fireEvent(minInput, 'blur');
  681. }
  682. if (maxInput && maxInput.blur) {
  683. fireEvent(maxInput, 'blur');
  684. }
  685. };
  686. rangeSelector.chart = chart;
  687. rangeSelector.options = options;
  688. rangeSelector.buttons = [];
  689. rangeSelector.buttonOptions = buttonOptions;
  690. this.unMouseDown = addEvent(chart.container, 'mousedown', blurInputs);
  691. this.unResize = addEvent(chart, 'resize', blurInputs);
  692. // Extend the buttonOptions with actual range
  693. buttonOptions.forEach(rangeSelector.computeButtonRange);
  694. // zoomed range based on a pre-selected button index
  695. if (typeof selectedOption !== 'undefined' &&
  696. buttonOptions[selectedOption]) {
  697. this.clickButton(selectedOption, false);
  698. }
  699. addEvent(chart, 'load', function () {
  700. // If a data grouping is applied to the current button, release it
  701. // when extremes change
  702. if (chart.xAxis && chart.xAxis[0]) {
  703. addEvent(chart.xAxis[0], 'setExtremes', function (e) {
  704. if (this.max - this.min !==
  705. chart.fixedRange &&
  706. e.trigger !== 'rangeSelectorButton' &&
  707. e.trigger !== 'updatedData' &&
  708. rangeSelector.forcedDataGrouping &&
  709. !rangeSelector.frozenStates) {
  710. this.setDataGrouping(false, false);
  711. }
  712. });
  713. }
  714. });
  715. };
  716. /**
  717. * Dynamically update the range selector buttons after a new range has been
  718. * set
  719. *
  720. * @private
  721. * @function Highcharts.RangeSelector#updateButtonStates
  722. * @return {void}
  723. */
  724. RangeSelector.prototype.updateButtonStates = function () {
  725. var rangeSelector = this, chart = this.chart, baseAxis = chart.xAxis[0], actualRange = Math.round(baseAxis.max - baseAxis.min), hasNoData = !baseAxis.hasVisibleSeries, day = 24 * 36e5, // A single day in milliseconds
  726. unionExtremes = (chart.scroller &&
  727. chart.scroller.getUnionExtremes()) || baseAxis, dataMin = unionExtremes.dataMin, dataMax = unionExtremes.dataMax, ytdExtremes = rangeSelector.getYTDExtremes(dataMax, dataMin, chart.time.useUTC), ytdMin = ytdExtremes.min, ytdMax = ytdExtremes.max, selected = rangeSelector.selected, selectedExists = isNumber(selected), allButtonsEnabled = rangeSelector.options.allButtonsEnabled, buttons = rangeSelector.buttons;
  728. rangeSelector.buttonOptions.forEach(function (rangeOptions, i) {
  729. var range = rangeOptions._range, type = rangeOptions.type, count = rangeOptions.count || 1, button = buttons[i], state = 0, disable, select, offsetRange = rangeOptions._offsetMax -
  730. rangeOptions._offsetMin, isSelected = i === selected,
  731. // Disable buttons where the range exceeds what is allowed in
  732. // the current view
  733. isTooGreatRange = range >
  734. dataMax - dataMin,
  735. // Disable buttons where the range is smaller than the minimum
  736. // range
  737. isTooSmallRange = range < baseAxis.minRange,
  738. // Do not select the YTD button if not explicitly told so
  739. isYTDButNotSelected = false,
  740. // Disable the All button if we're already showing all
  741. isAllButAlreadyShowingAll = false, isSameRange = range === actualRange;
  742. // Months and years have a variable range so we check the extremes
  743. if ((type === 'month' || type === 'year') &&
  744. (actualRange + 36e5 >=
  745. { month: 28, year: 365 }[type] * day * count - offsetRange) &&
  746. (actualRange - 36e5 <=
  747. { month: 31, year: 366 }[type] * day * count + offsetRange)) {
  748. isSameRange = true;
  749. }
  750. else if (type === 'ytd') {
  751. isSameRange = (ytdMax - ytdMin + offsetRange) === actualRange;
  752. isYTDButNotSelected = !isSelected;
  753. }
  754. else if (type === 'all') {
  755. isSameRange = (baseAxis.max - baseAxis.min >=
  756. dataMax - dataMin);
  757. isAllButAlreadyShowingAll = (!isSelected &&
  758. selectedExists &&
  759. isSameRange);
  760. }
  761. // The new zoom area happens to match the range for a button - mark
  762. // it selected. This happens when scrolling across an ordinal gap.
  763. // It can be seen in the intraday demos when selecting 1h and scroll
  764. // across the night gap.
  765. disable = (!allButtonsEnabled &&
  766. (isTooGreatRange ||
  767. isTooSmallRange ||
  768. isAllButAlreadyShowingAll ||
  769. hasNoData));
  770. select = ((isSelected && isSameRange) ||
  771. (isSameRange && !selectedExists && !isYTDButNotSelected) ||
  772. (isSelected && rangeSelector.frozenStates));
  773. if (disable) {
  774. state = 3;
  775. }
  776. else if (select) {
  777. selectedExists = true; // Only one button can be selected
  778. state = 2;
  779. }
  780. // If state has changed, update the button
  781. if (button.state !== state) {
  782. button.setState(state);
  783. // Reset (#9209)
  784. if (state === 0 && selected === i) {
  785. rangeSelector.setSelected(null);
  786. }
  787. }
  788. });
  789. };
  790. /**
  791. * Compute and cache the range for an individual button
  792. *
  793. * @private
  794. * @function Highcharts.RangeSelector#computeButtonRange
  795. * @param {Highcharts.RangeSelectorButtonsOptions} rangeOptions
  796. * @return {void}
  797. */
  798. RangeSelector.prototype.computeButtonRange = function (rangeOptions) {
  799. var type = rangeOptions.type, count = rangeOptions.count || 1,
  800. // these time intervals have a fixed number of milliseconds, as
  801. // opposed to month, ytd and year
  802. fixedTimes = {
  803. millisecond: 1,
  804. second: 1000,
  805. minute: 60 * 1000,
  806. hour: 3600 * 1000,
  807. day: 24 * 3600 * 1000,
  808. week: 7 * 24 * 3600 * 1000
  809. };
  810. // Store the range on the button object
  811. if (fixedTimes[type]) {
  812. rangeOptions._range = fixedTimes[type] * count;
  813. }
  814. else if (type === 'month' || type === 'year') {
  815. rangeOptions._range = {
  816. month: 30,
  817. year: 365
  818. }[type] * 24 * 36e5 * count;
  819. }
  820. rangeOptions._offsetMin = pick(rangeOptions.offsetMin, 0);
  821. rangeOptions._offsetMax = pick(rangeOptions.offsetMax, 0);
  822. rangeOptions._range +=
  823. rangeOptions._offsetMax - rangeOptions._offsetMin;
  824. };
  825. /**
  826. * Set the internal and displayed value of a HTML input for the dates
  827. *
  828. * @private
  829. * @function Highcharts.RangeSelector#setInputValue
  830. * @param {string} name
  831. * @param {number} [inputTime]
  832. * @return {void}
  833. */
  834. RangeSelector.prototype.setInputValue = function (name, inputTime) {
  835. var options = this.chart.options.rangeSelector, time = this.chart.time, input = this[name + 'Input'];
  836. if (defined(inputTime)) {
  837. input.previousValue = input.HCTime;
  838. input.HCTime = inputTime;
  839. }
  840. input.value = time.dateFormat(options.inputEditDateFormat || '%Y-%m-%d', input.HCTime);
  841. this[name + 'DateBox'].attr({
  842. text: time.dateFormat(options.inputDateFormat || '%b %e, %Y', input.HCTime)
  843. });
  844. };
  845. /**
  846. * @private
  847. * @function Highcharts.RangeSelector#showInput
  848. * @param {string} name
  849. * @return {void}
  850. */
  851. RangeSelector.prototype.showInput = function (name) {
  852. var inputGroup = this.inputGroup, dateBox = this[name + 'DateBox'];
  853. css(this[name + 'Input'], {
  854. left: (inputGroup.translateX + dateBox.x) + 'px',
  855. top: inputGroup.translateY + 'px',
  856. width: (dateBox.width - 2) + 'px',
  857. height: (dateBox.height - 2) + 'px',
  858. border: '2px solid silver'
  859. });
  860. };
  861. /**
  862. * @private
  863. * @function Highcharts.RangeSelector#hideInput
  864. * @param {string} name
  865. * @return {void}
  866. */
  867. RangeSelector.prototype.hideInput = function (name) {
  868. css(this[name + 'Input'], {
  869. border: 0,
  870. width: '1px',
  871. height: '1px'
  872. });
  873. this.setInputValue(name);
  874. };
  875. /**
  876. * Draw either the 'from' or the 'to' HTML input box of the range selector
  877. *
  878. * @private
  879. * @function Highcharts.RangeSelector#drawInput
  880. * @param {string} name
  881. * @return {void}
  882. */
  883. RangeSelector.prototype.drawInput = function (name) {
  884. var rangeSelector = this, chart = rangeSelector.chart, chartStyle = chart.renderer.style || {}, renderer = chart.renderer, options = chart.options.rangeSelector, lang = defaultOptions.lang, div = rangeSelector.div, isMin = name === 'min', input, label, dateBox, inputGroup = this.inputGroup;
  885. /**
  886. * @private
  887. */
  888. function updateExtremes() {
  889. var inputValue = input.value, value = (options.inputDateParser || Date.parse)(inputValue), chartAxis = chart.xAxis[0], dataAxis = chart.scroller && chart.scroller.xAxis ?
  890. chart.scroller.xAxis :
  891. chartAxis, dataMin = dataAxis.dataMin, dataMax = dataAxis.dataMax;
  892. if (value !== input.previousValue) {
  893. input.previousValue = value;
  894. // If the value isn't parsed directly to a value by the
  895. // browser's Date.parse method, like YYYY-MM-DD in IE, try
  896. // parsing it a different way
  897. if (!isNumber(value)) {
  898. value = inputValue.split('-');
  899. value = Date.UTC(pInt(value[0]), pInt(value[1]) - 1, pInt(value[2]));
  900. }
  901. if (isNumber(value)) {
  902. // Correct for timezone offset (#433)
  903. if (!chart.time.useUTC) {
  904. value =
  905. value + new Date().getTimezoneOffset() * 60 * 1000;
  906. }
  907. // Validate the extremes. If it goes beyound the data min or
  908. // max, use the actual data extreme (#2438).
  909. if (isMin) {
  910. if (value > rangeSelector.maxInput.HCTime) {
  911. value = void 0;
  912. }
  913. else if (value < dataMin) {
  914. value = dataMin;
  915. }
  916. }
  917. else {
  918. if (value < rangeSelector.minInput.HCTime) {
  919. value = void 0;
  920. }
  921. else if (value > dataMax) {
  922. value = dataMax;
  923. }
  924. }
  925. // Set the extremes
  926. if (typeof value !== 'undefined') { // @todo typof undefined
  927. chartAxis.setExtremes(isMin ? value : chartAxis.min, isMin ? chartAxis.max : value, void 0, void 0, { trigger: 'rangeSelectorInput' });
  928. }
  929. }
  930. }
  931. }
  932. // Create the text label
  933. this[name + 'Label'] = label = renderer
  934. .label(lang[isMin ? 'rangeSelectorFrom' : 'rangeSelectorTo'], this.inputGroup.offset)
  935. .addClass('highcharts-range-label')
  936. .attr({
  937. padding: 2
  938. })
  939. .add(inputGroup);
  940. inputGroup.offset += label.width + 5;
  941. // Create an SVG label that shows updated date ranges and and records
  942. // click events that bring in the HTML input.
  943. this[name + 'DateBox'] = dateBox = renderer
  944. .label('', inputGroup.offset)
  945. .addClass('highcharts-range-input')
  946. .attr({
  947. padding: 2,
  948. width: options.inputBoxWidth || 90,
  949. height: options.inputBoxHeight || 17,
  950. 'text-align': 'center'
  951. })
  952. .on('click', function () {
  953. // If it is already focused, the onfocus event doesn't fire
  954. // (#3713)
  955. rangeSelector.showInput(name);
  956. rangeSelector[name + 'Input'].focus();
  957. });
  958. if (!chart.styledMode) {
  959. dateBox.attr({
  960. stroke: options.inputBoxBorderColor || '#cccccc',
  961. 'stroke-width': 1
  962. });
  963. }
  964. dateBox.add(inputGroup);
  965. inputGroup.offset += dateBox.width + (isMin ? 10 : 0);
  966. // Create the HTML input element. This is rendered as 1x1 pixel then set
  967. // to the right size when focused.
  968. this[name + 'Input'] = input = createElement('input', {
  969. name: name,
  970. className: 'highcharts-range-selector',
  971. type: 'text'
  972. }, {
  973. top: chart.plotTop + 'px' // prevent jump on focus in Firefox
  974. }, div);
  975. if (!chart.styledMode) {
  976. // Styles
  977. label.css(merge(chartStyle, options.labelStyle));
  978. dateBox.css(merge({
  979. color: '#333333'
  980. }, chartStyle, options.inputStyle));
  981. css(input, extend({
  982. position: 'absolute',
  983. border: 0,
  984. width: '1px',
  985. height: '1px',
  986. padding: 0,
  987. textAlign: 'center',
  988. fontSize: chartStyle.fontSize,
  989. fontFamily: chartStyle.fontFamily,
  990. top: '-9999em' // #4798
  991. }, options.inputStyle));
  992. }
  993. // Blow up the input box
  994. input.onfocus = function () {
  995. rangeSelector.showInput(name);
  996. };
  997. // Hide away the input box
  998. input.onblur = function () {
  999. // update extermes only when inputs are active
  1000. if (input === H.doc.activeElement) { // Only when focused
  1001. // Update also when no `change` event is triggered, like when
  1002. // clicking inside the SVG (#4710)
  1003. updateExtremes();
  1004. }
  1005. // #10404 - move hide and blur outside focus
  1006. rangeSelector.hideInput(name);
  1007. input.blur(); // #4606
  1008. };
  1009. // handle changes in the input boxes
  1010. input.onchange = updateExtremes;
  1011. input.onkeypress = function (event) {
  1012. // IE does not fire onchange on enter
  1013. if (event.keyCode === 13) {
  1014. updateExtremes();
  1015. }
  1016. };
  1017. };
  1018. /**
  1019. * Get the position of the range selector buttons and inputs. This can be
  1020. * overridden from outside for custom positioning.
  1021. *
  1022. * @private
  1023. * @function Highcharts.RangeSelector#getPosition
  1024. *
  1025. * @return {Highcharts.Dictionary<number>}
  1026. */
  1027. RangeSelector.prototype.getPosition = function () {
  1028. var chart = this.chart, options = chart.options.rangeSelector, top = options.verticalAlign === 'top' ?
  1029. chart.plotTop - chart.axisOffset[0] :
  1030. 0; // set offset only for varticalAlign top
  1031. return {
  1032. buttonTop: top + options.buttonPosition.y,
  1033. inputTop: top + options.inputPosition.y - 10
  1034. };
  1035. };
  1036. /**
  1037. * Get the extremes of YTD. Will choose dataMax if its value is lower than
  1038. * the current timestamp. Will choose dataMin if its value is higher than
  1039. * the timestamp for the start of current year.
  1040. *
  1041. * @private
  1042. * @function Highcharts.RangeSelector#getYTDExtremes
  1043. *
  1044. * @param {number} dataMax
  1045. *
  1046. * @param {number} dataMin
  1047. *
  1048. * @return {*}
  1049. * Returns min and max for the YTD
  1050. */
  1051. RangeSelector.prototype.getYTDExtremes = function (dataMax, dataMin, useUTC) {
  1052. var time = this.chart.time, min, now = new time.Date(dataMax), year = time.get('FullYear', now), startOfYear = useUTC ?
  1053. time.Date.UTC(year, 0, 1) : // eslint-disable-line new-cap
  1054. +new time.Date(year, 0, 1);
  1055. min = Math.max(dataMin || 0, startOfYear);
  1056. now = now.getTime();
  1057. return {
  1058. max: Math.min(dataMax || now, now),
  1059. min: min
  1060. };
  1061. };
  1062. /**
  1063. * Render the range selector including the buttons and the inputs. The first
  1064. * time render is called, the elements are created and positioned. On
  1065. * subsequent calls, they are moved and updated.
  1066. *
  1067. * @private
  1068. * @function Highcharts.RangeSelector#render
  1069. * @param {number} [min]
  1070. * X axis minimum
  1071. * @param {number} [max]
  1072. * X axis maximum
  1073. * @return {void}
  1074. */
  1075. RangeSelector.prototype.render = function (min, max) {
  1076. var rangeSelector = this, chart = rangeSelector.chart, renderer = chart.renderer, container = chart.container, chartOptions = chart.options, navButtonOptions = (chartOptions.exporting &&
  1077. chartOptions.exporting.enabled !== false &&
  1078. chartOptions.navigation &&
  1079. chartOptions.navigation.buttonOptions), lang = defaultOptions.lang, div = rangeSelector.div, options = chartOptions.rangeSelector,
  1080. // Place inputs above the container
  1081. inputsZIndex = pick(chartOptions.chart.style &&
  1082. chartOptions.chart.style.zIndex, 0) + 1, floating = options.floating, buttons = rangeSelector.buttons, inputGroup = rangeSelector.inputGroup, buttonTheme = options.buttonTheme, buttonPosition = options.buttonPosition, inputPosition = options.inputPosition, inputEnabled = options.inputEnabled, states = buttonTheme && buttonTheme.states, plotLeft = chart.plotLeft, buttonLeft, buttonGroup = rangeSelector.buttonGroup, group, groupHeight, rendered = rangeSelector.rendered, verticalAlign = rangeSelector.options.verticalAlign, legend = chart.legend, legendOptions = legend && legend.options, buttonPositionY = buttonPosition.y, inputPositionY = inputPosition.y, animate = chart.hasLoaded, verb = animate ? 'animate' : 'attr', exportingX = 0, alignTranslateY, legendHeight, minPosition, translateY = 0, translateX;
  1083. if (options.enabled === false) {
  1084. return;
  1085. }
  1086. // create the elements
  1087. if (!rendered) {
  1088. rangeSelector.group = group = renderer.g('range-selector-group')
  1089. .attr({
  1090. zIndex: 7
  1091. })
  1092. .add();
  1093. rangeSelector.buttonGroup = buttonGroup =
  1094. renderer.g('range-selector-buttons').add(group);
  1095. rangeSelector.zoomText = renderer
  1096. .text(lang.rangeSelectorZoom, 0, 15)
  1097. .add(buttonGroup);
  1098. if (!chart.styledMode) {
  1099. rangeSelector.zoomText.css(options.labelStyle);
  1100. buttonTheme['stroke-width'] =
  1101. pick(buttonTheme['stroke-width'], 0);
  1102. }
  1103. rangeSelector.buttonOptions.forEach(function (rangeOptions, i) {
  1104. buttons[i] = renderer
  1105. .button(rangeOptions.text, 0, 0, function (e) {
  1106. // extract events from button object and call
  1107. var buttonEvents = (rangeOptions.events &&
  1108. rangeOptions.events.click), callDefaultEvent;
  1109. if (buttonEvents) {
  1110. callDefaultEvent =
  1111. buttonEvents.call(rangeOptions, e);
  1112. }
  1113. if (callDefaultEvent !== false) {
  1114. rangeSelector.clickButton(i);
  1115. }
  1116. rangeSelector.isActive = true;
  1117. }, buttonTheme, states && states.hover, states && states.select, states && states.disabled)
  1118. .attr({
  1119. 'text-align': 'center'
  1120. })
  1121. .add(buttonGroup);
  1122. });
  1123. // first create a wrapper outside the container in order to make
  1124. // the inputs work and make export correct
  1125. if (inputEnabled !== false) {
  1126. rangeSelector.div = div = createElement('div', null, {
  1127. position: 'relative',
  1128. height: 0,
  1129. zIndex: inputsZIndex
  1130. });
  1131. container.parentNode.insertBefore(div, container);
  1132. // Create the group to keep the inputs
  1133. rangeSelector.inputGroup = inputGroup =
  1134. renderer.g('input-group').add(group);
  1135. inputGroup.offset = 0;
  1136. rangeSelector.drawInput('min');
  1137. rangeSelector.drawInput('max');
  1138. }
  1139. }
  1140. // #8769, allow dynamically updating margins
  1141. rangeSelector.zoomText[verb]({
  1142. x: pick(plotLeft + buttonPosition.x, plotLeft)
  1143. });
  1144. // button start position
  1145. buttonLeft = pick(plotLeft + buttonPosition.x, plotLeft) +
  1146. rangeSelector.zoomText.getBBox().width + 5;
  1147. rangeSelector.buttonOptions.forEach(function (rangeOptions, i) {
  1148. buttons[i][verb]({ x: buttonLeft });
  1149. // increase button position for the next button
  1150. buttonLeft += buttons[i].width + pick(options.buttonSpacing, 5);
  1151. });
  1152. plotLeft = chart.plotLeft - chart.spacing[3];
  1153. rangeSelector.updateButtonStates();
  1154. // detect collisiton with exporting
  1155. if (navButtonOptions &&
  1156. this.titleCollision(chart) &&
  1157. verticalAlign === 'top' &&
  1158. buttonPosition.align === 'right' && ((buttonPosition.y +
  1159. buttonGroup.getBBox().height - 12) <
  1160. ((navButtonOptions.y || 0) +
  1161. navButtonOptions.height))) {
  1162. exportingX = -40;
  1163. }
  1164. translateX = buttonPosition.x - chart.spacing[3];
  1165. if (buttonPosition.align === 'right') {
  1166. translateX += exportingX - plotLeft; // (#13014)
  1167. }
  1168. else if (buttonPosition.align === 'center') {
  1169. translateX -= plotLeft / 2;
  1170. }
  1171. // align button group
  1172. buttonGroup.align({
  1173. y: buttonPosition.y,
  1174. width: buttonGroup.getBBox().width,
  1175. align: buttonPosition.align,
  1176. x: translateX
  1177. }, true, chart.spacingBox);
  1178. // skip animation
  1179. rangeSelector.group.placed = animate;
  1180. rangeSelector.buttonGroup.placed = animate;
  1181. if (inputEnabled !== false) {
  1182. var inputGroupX, inputGroupWidth, buttonGroupX, buttonGroupWidth;
  1183. // detect collision with exporting
  1184. if (navButtonOptions &&
  1185. this.titleCollision(chart) &&
  1186. verticalAlign === 'top' &&
  1187. inputPosition.align === 'right' && ((inputPosition.y -
  1188. inputGroup.getBBox().height - 12) <
  1189. ((navButtonOptions.y || 0) +
  1190. navButtonOptions.height +
  1191. chart.spacing[0]))) {
  1192. exportingX = -40;
  1193. }
  1194. else {
  1195. exportingX = 0;
  1196. }
  1197. if (inputPosition.align === 'left') {
  1198. translateX = plotLeft;
  1199. }
  1200. else if (inputPosition.align === 'right') {
  1201. translateX = -Math.max(chart.axisOffset[1], -exportingX);
  1202. }
  1203. // Update the alignment to the updated spacing box
  1204. inputGroup.align({
  1205. y: inputPosition.y,
  1206. width: inputGroup.getBBox().width,
  1207. align: inputPosition.align,
  1208. // fix wrong getBBox() value on right align
  1209. x: inputPosition.x + translateX - 2
  1210. }, true, chart.spacingBox);
  1211. // detect collision
  1212. inputGroupX = (inputGroup.alignAttr.translateX +
  1213. inputGroup.alignOptions.x -
  1214. exportingX +
  1215. // getBBox for detecing left margin
  1216. inputGroup.getBBox().x +
  1217. // 2px padding to not overlap input and label
  1218. 2);
  1219. inputGroupWidth = inputGroup.alignOptions.width;
  1220. buttonGroupX = buttonGroup.alignAttr.translateX +
  1221. buttonGroup.getBBox().x;
  1222. // 20 is minimal spacing between elements
  1223. buttonGroupWidth = buttonGroup.getBBox().width + 20;
  1224. if ((inputPosition.align ===
  1225. buttonPosition.align) || ((buttonGroupX + buttonGroupWidth > inputGroupX) &&
  1226. (inputGroupX + inputGroupWidth > buttonGroupX) &&
  1227. (buttonPositionY <
  1228. (inputPositionY +
  1229. inputGroup.getBBox().height)))) {
  1230. inputGroup.attr({
  1231. translateX: inputGroup.alignAttr.translateX +
  1232. (chart.axisOffset[1] >= -exportingX ? 0 : -exportingX),
  1233. translateY: inputGroup.alignAttr.translateY +
  1234. buttonGroup.getBBox().height + 10
  1235. });
  1236. }
  1237. // Set or reset the input values
  1238. rangeSelector.setInputValue('min', min);
  1239. rangeSelector.setInputValue('max', max);
  1240. // skip animation
  1241. rangeSelector.inputGroup.placed = animate;
  1242. }
  1243. // vertical align
  1244. rangeSelector.group.align({
  1245. verticalAlign: verticalAlign
  1246. }, true, chart.spacingBox);
  1247. // set position
  1248. groupHeight =
  1249. rangeSelector.group.getBBox().height + 20; // # 20 padding
  1250. alignTranslateY =
  1251. rangeSelector.group.alignAttr.translateY;
  1252. // calculate bottom position
  1253. if (verticalAlign === 'bottom') {
  1254. legendHeight = (legendOptions &&
  1255. legendOptions.verticalAlign === 'bottom' &&
  1256. legendOptions.enabled &&
  1257. !legendOptions.floating ?
  1258. legend.legendHeight + pick(legendOptions.margin, 10) :
  1259. 0);
  1260. groupHeight = groupHeight + legendHeight - 20;
  1261. translateY = (alignTranslateY -
  1262. groupHeight -
  1263. (floating ? 0 : options.y) -
  1264. (chart.titleOffset ? chart.titleOffset[2] : 0) -
  1265. 10 // 10 spacing
  1266. );
  1267. }
  1268. if (verticalAlign === 'top') {
  1269. if (floating) {
  1270. translateY = 0;
  1271. }
  1272. if (chart.titleOffset && chart.titleOffset[0]) {
  1273. translateY = chart.titleOffset[0];
  1274. }
  1275. translateY += ((chart.margin[0] - chart.spacing[0]) || 0);
  1276. }
  1277. else if (verticalAlign === 'middle') {
  1278. if (inputPositionY === buttonPositionY) {
  1279. if (inputPositionY < 0) {
  1280. translateY = alignTranslateY + minPosition;
  1281. }
  1282. else {
  1283. translateY = alignTranslateY;
  1284. }
  1285. }
  1286. else if (inputPositionY || buttonPositionY) {
  1287. if (inputPositionY < 0 ||
  1288. buttonPositionY < 0) {
  1289. translateY -= Math.min(inputPositionY, buttonPositionY);
  1290. }
  1291. else {
  1292. translateY =
  1293. alignTranslateY - groupHeight + minPosition;
  1294. }
  1295. }
  1296. }
  1297. rangeSelector.group.translate(options.x, options.y + Math.floor(translateY));
  1298. // translate HTML inputs
  1299. if (inputEnabled !== false) {
  1300. rangeSelector.minInput.style.marginTop =
  1301. rangeSelector.group.translateY + 'px';
  1302. rangeSelector.maxInput.style.marginTop =
  1303. rangeSelector.group.translateY + 'px';
  1304. }
  1305. rangeSelector.rendered = true;
  1306. };
  1307. /**
  1308. * Extracts height of range selector
  1309. *
  1310. * @private
  1311. * @function Highcharts.RangeSelector#getHeight
  1312. * @return {number}
  1313. * Returns rangeSelector height
  1314. */
  1315. RangeSelector.prototype.getHeight = function () {
  1316. var rangeSelector = this, options = rangeSelector.options, rangeSelectorGroup = rangeSelector.group, inputPosition = options.inputPosition, buttonPosition = options.buttonPosition, yPosition = options.y, buttonPositionY = buttonPosition.y, inputPositionY = inputPosition.y, rangeSelectorHeight = 0, minPosition;
  1317. if (options.height) {
  1318. return options.height;
  1319. }
  1320. rangeSelectorHeight = rangeSelectorGroup ?
  1321. // 13px to keep back compatibility
  1322. (rangeSelectorGroup.getBBox(true).height) + 13 +
  1323. yPosition :
  1324. 0;
  1325. minPosition = Math.min(inputPositionY, buttonPositionY);
  1326. if ((inputPositionY < 0 && buttonPositionY < 0) ||
  1327. (inputPositionY > 0 && buttonPositionY > 0)) {
  1328. rangeSelectorHeight += Math.abs(minPosition);
  1329. }
  1330. return rangeSelectorHeight;
  1331. };
  1332. /**
  1333. * Detect collision with title or subtitle
  1334. *
  1335. * @private
  1336. * @function Highcharts.RangeSelector#titleCollision
  1337. *
  1338. * @param {Highcharts.Chart} chart
  1339. *
  1340. * @return {boolean}
  1341. * Returns collision status
  1342. */
  1343. RangeSelector.prototype.titleCollision = function (chart) {
  1344. return !(chart.options.title.text ||
  1345. chart.options.subtitle.text);
  1346. };
  1347. /**
  1348. * Update the range selector with new options
  1349. *
  1350. * @private
  1351. * @function Highcharts.RangeSelector#update
  1352. * @param {Highcharts.RangeSelectorOptions} options
  1353. * @return {void}
  1354. */
  1355. RangeSelector.prototype.update = function (options) {
  1356. var chart = this.chart;
  1357. merge(true, chart.options.rangeSelector, options);
  1358. this.destroy();
  1359. this.init(chart);
  1360. chart.rangeSelector.render();
  1361. };
  1362. /**
  1363. * Destroys allocated elements.
  1364. *
  1365. * @private
  1366. * @function Highcharts.RangeSelector#destroy
  1367. */
  1368. RangeSelector.prototype.destroy = function () {
  1369. var rSelector = this, minInput = rSelector.minInput, maxInput = rSelector.maxInput;
  1370. rSelector.unMouseDown();
  1371. rSelector.unResize();
  1372. // Destroy elements in collections
  1373. destroyObjectProperties(rSelector.buttons);
  1374. // Clear input element events
  1375. if (minInput) {
  1376. minInput.onfocus = minInput.onblur = minInput.onchange = null;
  1377. }
  1378. if (maxInput) {
  1379. maxInput.onfocus = maxInput.onblur = maxInput.onchange = null;
  1380. }
  1381. // Destroy HTML and SVG elements
  1382. objectEach(rSelector, function (val, key) {
  1383. if (val && key !== 'chart') {
  1384. if (val instanceof SVGElement) {
  1385. // SVGElement
  1386. val.destroy();
  1387. }
  1388. else if (val instanceof window.HTMLElement) {
  1389. // HTML element
  1390. discardElement(val);
  1391. }
  1392. }
  1393. if (val !== RangeSelector.prototype[key]) {
  1394. rSelector[key] = null;
  1395. }
  1396. }, this);
  1397. };
  1398. return RangeSelector;
  1399. }());
  1400. /**
  1401. * The default buttons for pre-selecting time frames
  1402. */
  1403. RangeSelector.prototype.defaultButtons = [{
  1404. type: 'month',
  1405. count: 1,
  1406. text: '1m'
  1407. }, {
  1408. type: 'month',
  1409. count: 3,
  1410. text: '3m'
  1411. }, {
  1412. type: 'month',
  1413. count: 6,
  1414. text: '6m'
  1415. }, {
  1416. type: 'ytd',
  1417. text: 'YTD'
  1418. }, {
  1419. type: 'year',
  1420. count: 1,
  1421. text: '1y'
  1422. }, {
  1423. type: 'all',
  1424. text: 'All'
  1425. }];
  1426. /**
  1427. * Get the axis min value based on the range option and the current max. For
  1428. * stock charts this is extended via the {@link RangeSelector} so that if the
  1429. * selected range is a multiple of months or years, it is compensated for
  1430. * various month lengths.
  1431. *
  1432. * @private
  1433. * @function Highcharts.Axis#minFromRange
  1434. * @return {number|undefined}
  1435. * The new minimum value.
  1436. */
  1437. Axis.prototype.minFromRange = function () {
  1438. var rangeOptions = this.range, type = rangeOptions.type, min, max = this.max, dataMin, range, time = this.chart.time,
  1439. // Get the true range from a start date
  1440. getTrueRange = function (base, count) {
  1441. var timeName = type === 'year' ? 'FullYear' : 'Month';
  1442. var date = new time.Date(base);
  1443. var basePeriod = time.get(timeName, date);
  1444. time.set(timeName, date, basePeriod + count);
  1445. if (basePeriod === time.get(timeName, date)) {
  1446. time.set('Date', date, 0); // #6537
  1447. }
  1448. return date.getTime() - base;
  1449. };
  1450. if (isNumber(rangeOptions)) {
  1451. min = max - rangeOptions;
  1452. range = rangeOptions;
  1453. }
  1454. else {
  1455. min = max + getTrueRange(max, -rangeOptions.count);
  1456. // Let the fixedRange reflect initial settings (#5930)
  1457. if (this.chart) {
  1458. this.chart.fixedRange = max - min;
  1459. }
  1460. }
  1461. dataMin = pick(this.dataMin, Number.MIN_VALUE);
  1462. if (!isNumber(min)) {
  1463. min = dataMin;
  1464. }
  1465. if (min <= dataMin) {
  1466. min = dataMin;
  1467. if (typeof range === 'undefined') { // #4501
  1468. range = getTrueRange(min, rangeOptions.count);
  1469. }
  1470. this.newMax = Math.min(min + range, this.dataMax);
  1471. }
  1472. if (!isNumber(max)) {
  1473. min = void 0;
  1474. }
  1475. return min;
  1476. };
  1477. if (!H.RangeSelector) {
  1478. // Initialize rangeselector for stock charts
  1479. addEvent(Chart, 'afterGetContainer', function () {
  1480. if (this.options.rangeSelector.enabled) {
  1481. this.rangeSelector = new RangeSelector(this);
  1482. }
  1483. });
  1484. addEvent(Chart, 'beforeRender', function () {
  1485. var chart = this, axes = chart.axes, rangeSelector = chart.rangeSelector, verticalAlign;
  1486. if (rangeSelector) {
  1487. if (isNumber(rangeSelector.deferredYTDClick)) {
  1488. rangeSelector.clickButton(rangeSelector.deferredYTDClick);
  1489. delete rangeSelector.deferredYTDClick;
  1490. }
  1491. axes.forEach(function (axis) {
  1492. axis.updateNames();
  1493. axis.setScale();
  1494. });
  1495. chart.getAxisMargins();
  1496. rangeSelector.render();
  1497. verticalAlign = rangeSelector.options.verticalAlign;
  1498. if (!rangeSelector.options.floating) {
  1499. if (verticalAlign === 'bottom') {
  1500. this.extraBottomMargin = true;
  1501. }
  1502. else if (verticalAlign !== 'middle') {
  1503. this.extraTopMargin = true;
  1504. }
  1505. }
  1506. }
  1507. });
  1508. addEvent(Chart, 'update', function (e) {
  1509. var chart = this, options = e.options, optionsRangeSelector = options.rangeSelector, rangeSelector = chart.rangeSelector, verticalAlign, extraBottomMarginWas = this.extraBottomMargin, extraTopMarginWas = this.extraTopMargin;
  1510. if (optionsRangeSelector &&
  1511. optionsRangeSelector.enabled &&
  1512. !defined(rangeSelector)) {
  1513. this.options.rangeSelector.enabled = true;
  1514. this.rangeSelector = new RangeSelector(this);
  1515. }
  1516. this.extraBottomMargin = false;
  1517. this.extraTopMargin = false;
  1518. if (rangeSelector) {
  1519. rangeSelector.render();
  1520. verticalAlign = (optionsRangeSelector &&
  1521. optionsRangeSelector.verticalAlign) || (rangeSelector.options && rangeSelector.options.verticalAlign);
  1522. if (!rangeSelector.options.floating) {
  1523. if (verticalAlign === 'bottom') {
  1524. this.extraBottomMargin = true;
  1525. }
  1526. else if (verticalAlign !== 'middle') {
  1527. this.extraTopMargin = true;
  1528. }
  1529. }
  1530. if (this.extraBottomMargin !== extraBottomMarginWas ||
  1531. this.extraTopMargin !== extraTopMarginWas) {
  1532. this.isDirtyBox = true;
  1533. }
  1534. }
  1535. });
  1536. addEvent(Chart, 'render', function () {
  1537. var chart = this, rangeSelector = chart.rangeSelector, verticalAlign;
  1538. if (rangeSelector && !rangeSelector.options.floating) {
  1539. rangeSelector.render();
  1540. verticalAlign = rangeSelector.options.verticalAlign;
  1541. if (verticalAlign === 'bottom') {
  1542. this.extraBottomMargin = true;
  1543. }
  1544. else if (verticalAlign !== 'middle') {
  1545. this.extraTopMargin = true;
  1546. }
  1547. }
  1548. });
  1549. addEvent(Chart, 'getMargins', function () {
  1550. var rangeSelector = this.rangeSelector, rangeSelectorHeight;
  1551. if (rangeSelector) {
  1552. rangeSelectorHeight = rangeSelector.getHeight();
  1553. if (this.extraTopMargin) {
  1554. this.plotTop += rangeSelectorHeight;
  1555. }
  1556. if (this.extraBottomMargin) {
  1557. this.marginBottom += rangeSelectorHeight;
  1558. }
  1559. }
  1560. });
  1561. Chart.prototype.callbacks.push(function (chart) {
  1562. var extremes, rangeSelector = chart.rangeSelector, unbindRender, unbindSetExtremes, legend, alignTo, verticalAlign;
  1563. /**
  1564. * @private
  1565. */
  1566. function renderRangeSelector() {
  1567. extremes = chart.xAxis[0].getExtremes();
  1568. legend = chart.legend;
  1569. verticalAlign = rangeSelector === null || rangeSelector === void 0 ? void 0 : rangeSelector.options.verticalAlign;
  1570. if (isNumber(extremes.min)) {
  1571. rangeSelector.render(extremes.min, extremes.max);
  1572. }
  1573. // Re-align the legend so that it's below the rangeselector
  1574. if (rangeSelector && legend.display &&
  1575. verticalAlign === 'top' &&
  1576. verticalAlign === legend.options.verticalAlign) {
  1577. // Create a new alignment box for the legend.
  1578. alignTo = merge(chart.spacingBox);
  1579. if (legend.options.layout === 'vertical') {
  1580. alignTo.y = chart.plotTop;
  1581. }
  1582. else {
  1583. alignTo.y += rangeSelector.getHeight();
  1584. }
  1585. legend.group.placed = false; // Don't animate the alignment.
  1586. legend.align(alignTo);
  1587. }
  1588. }
  1589. if (rangeSelector) {
  1590. // redraw the scroller on setExtremes
  1591. unbindSetExtremes = addEvent(chart.xAxis[0], 'afterSetExtremes', function (e) {
  1592. rangeSelector.render(e.min, e.max);
  1593. });
  1594. // redraw the scroller chart resize
  1595. unbindRender = addEvent(chart, 'redraw', renderRangeSelector);
  1596. // do it now
  1597. renderRangeSelector();
  1598. }
  1599. // Remove resize/afterSetExtremes at chart destroy
  1600. addEvent(chart, 'destroy', function destroyEvents() {
  1601. if (rangeSelector) {
  1602. unbindRender();
  1603. unbindSetExtremes();
  1604. }
  1605. });
  1606. });
  1607. H.RangeSelector = RangeSelector;
  1608. }
  1609. export default H.RangeSelector;