RangeSelector.js 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  1. /* *
  2. *
  3. * (c) 2010-2021 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 '../Core/Axis/Axis.js';
  12. import Chart from '../Core/Chart/Chart.js';
  13. import H from '../Core/Globals.js';
  14. import O from '../Core/Options.js';
  15. var defaultOptions = O.defaultOptions;
  16. import palette from '../Core/Color/Palette.js';
  17. import SVGElement from '../Core/Renderer/SVG/SVGElement.js';
  18. import U from '../Core/Utilities.js';
  19. var addEvent = U.addEvent, createElement = U.createElement, css = U.css, defined = U.defined, destroyObjectProperties = U.destroyObjectProperties, discardElement = U.discardElement, extend = U.extend, find = U.find, fireEvent = U.fireEvent, isNumber = U.isNumber, merge = U.merge, objectEach = U.objectEach, pad = U.pad, pick = U.pick, pInt = U.pInt, splat = U.splat;
  20. /**
  21. * Define the time span for the button
  22. *
  23. * @typedef {"all"|"day"|"hour"|"millisecond"|"minute"|"month"|"second"|"week"|"year"|"ytd"} Highcharts.RangeSelectorButtonTypeValue
  24. */
  25. /**
  26. * Callback function to react on button clicks.
  27. *
  28. * @callback Highcharts.RangeSelectorClickCallbackFunction
  29. *
  30. * @param {global.Event} e
  31. * Event arguments.
  32. *
  33. * @param {boolean|undefined}
  34. * Return false to cancel the default button event.
  35. */
  36. /**
  37. * Callback function to parse values entered in the input boxes and return a
  38. * valid JavaScript time as milliseconds since 1970.
  39. *
  40. * @callback Highcharts.RangeSelectorParseCallbackFunction
  41. *
  42. * @param {string} value
  43. * Input value to parse.
  44. *
  45. * @return {number}
  46. * Parsed JavaScript time value.
  47. */
  48. /* ************************************************************************** *
  49. * Start Range Selector code *
  50. * ************************************************************************** */
  51. extend(defaultOptions, {
  52. /**
  53. * The range selector is a tool for selecting ranges to display within
  54. * the chart. It provides buttons to select preconfigured ranges in
  55. * the chart, like 1 day, 1 week, 1 month etc. It also provides input
  56. * boxes where min and max dates can be manually input.
  57. *
  58. * @product highstock gantt
  59. * @optionparent rangeSelector
  60. */
  61. rangeSelector: {
  62. /**
  63. * Whether to enable all buttons from the start. By default buttons are
  64. * only enabled if the corresponding time range exists on the X axis,
  65. * but enabling all buttons allows for dynamically loading different
  66. * time ranges.
  67. *
  68. * @sample {highstock} stock/rangeselector/allbuttonsenabled-true/
  69. * All buttons enabled
  70. *
  71. * @since 2.0.3
  72. */
  73. allButtonsEnabled: false,
  74. /**
  75. * An array of configuration objects for the buttons.
  76. *
  77. * Defaults to:
  78. * ```js
  79. * buttons: [{
  80. * type: 'month',
  81. * count: 1,
  82. * text: '1m',
  83. * title: 'View 1 month'
  84. * }, {
  85. * type: 'month',
  86. * count: 3,
  87. * text: '3m',
  88. * title: 'View 3 months'
  89. * }, {
  90. * type: 'month',
  91. * count: 6,
  92. * text: '6m',
  93. * title: 'View 6 months'
  94. * }, {
  95. * type: 'ytd',
  96. * text: 'YTD',
  97. * title: 'View year to date'
  98. * }, {
  99. * type: 'year',
  100. * count: 1,
  101. * text: '1y',
  102. * title: 'View 1 year'
  103. * }, {
  104. * type: 'all',
  105. * text: 'All',
  106. * title: 'View all'
  107. * }]
  108. * ```
  109. *
  110. * @sample {highstock} stock/rangeselector/datagrouping/
  111. * Data grouping by buttons
  112. *
  113. * @type {Array<*>}
  114. */
  115. buttons: void 0,
  116. /**
  117. * How many units of the defined type the button should span. If `type`
  118. * is "month" and `count` is 3, the button spans three months.
  119. *
  120. * @type {number}
  121. * @default 1
  122. * @apioption rangeSelector.buttons.count
  123. */
  124. /**
  125. * Fires when clicking on the rangeSelector button. One parameter,
  126. * event, is passed to the function, containing common event
  127. * information.
  128. *
  129. * ```js
  130. * click: function(e) {
  131. * console.log(this);
  132. * }
  133. * ```
  134. *
  135. * Return false to stop default button's click action.
  136. *
  137. * @sample {highstock} stock/rangeselector/button-click/
  138. * Click event on the button
  139. *
  140. * @type {Highcharts.RangeSelectorClickCallbackFunction}
  141. * @apioption rangeSelector.buttons.events.click
  142. */
  143. /**
  144. * Additional range (in milliseconds) added to the end of the calculated
  145. * time span.
  146. *
  147. * @sample {highstock} stock/rangeselector/min-max-offsets/
  148. * Button offsets
  149. *
  150. * @type {number}
  151. * @default 0
  152. * @since 6.0.0
  153. * @apioption rangeSelector.buttons.offsetMax
  154. */
  155. /**
  156. * Additional range (in milliseconds) added to the start of the
  157. * calculated time span.
  158. *
  159. * @sample {highstock} stock/rangeselector/min-max-offsets/
  160. * Button offsets
  161. *
  162. * @type {number}
  163. * @default 0
  164. * @since 6.0.0
  165. * @apioption rangeSelector.buttons.offsetMin
  166. */
  167. /**
  168. * When buttons apply dataGrouping on a series, by default zooming
  169. * in/out will deselect buttons and unset dataGrouping. Enable this
  170. * option to keep buttons selected when extremes change.
  171. *
  172. * @sample {highstock} stock/rangeselector/preserve-datagrouping/
  173. * Different preserveDataGrouping settings
  174. *
  175. * @type {boolean}
  176. * @default false
  177. * @since 6.1.2
  178. * @apioption rangeSelector.buttons.preserveDataGrouping
  179. */
  180. /**
  181. * A custom data grouping object for each button.
  182. *
  183. * @see [series.dataGrouping](#plotOptions.series.dataGrouping)
  184. *
  185. * @sample {highstock} stock/rangeselector/datagrouping/
  186. * Data grouping by range selector buttons
  187. *
  188. * @type {*}
  189. * @extends plotOptions.series.dataGrouping
  190. * @apioption rangeSelector.buttons.dataGrouping
  191. */
  192. /**
  193. * The text for the button itself.
  194. *
  195. * @type {string}
  196. * @apioption rangeSelector.buttons.text
  197. */
  198. /**
  199. * Explanation for the button, shown as a tooltip on hover, and used by
  200. * assistive technology.
  201. *
  202. * @type {string}
  203. * @apioption rangeSelector.buttons.title
  204. */
  205. /**
  206. * Defined the time span for the button. Can be one of `millisecond`,
  207. * `second`, `minute`, `hour`, `day`, `week`, `month`, `year`, `ytd`,
  208. * and `all`.
  209. *
  210. * @type {Highcharts.RangeSelectorButtonTypeValue}
  211. * @apioption rangeSelector.buttons.type
  212. */
  213. /**
  214. * The space in pixels between the buttons in the range selector.
  215. */
  216. buttonSpacing: 5,
  217. /**
  218. * Whether to collapse the range selector buttons into a dropdown when
  219. * there is not enough room to show everything in a single row, instead
  220. * of dividing the range selector into multiple rows.
  221. * Can be one of the following:
  222. * - `always`: Always collapse
  223. * - `responsive`: Only collapse when there is not enough room
  224. * - `never`: Never collapse
  225. *
  226. * @sample {highstock} stock/rangeselector/dropdown/
  227. * Dropdown option
  228. *
  229. * @validvalue ["always", "responsive", "never"]
  230. * @since 9.0.0
  231. */
  232. dropdown: 'responsive',
  233. /**
  234. * Enable or disable the range selector. Default to `true` for stock
  235. * charts, using the `stockChart` factory.
  236. *
  237. * @sample {highstock} stock/rangeselector/enabled/
  238. * Disable the range selector
  239. *
  240. * @type {boolean|undefined}
  241. * @default {highstock} true
  242. */
  243. enabled: void 0,
  244. /**
  245. * The vertical alignment of the rangeselector box. Allowed properties
  246. * are `top`, `middle`, `bottom`.
  247. *
  248. * @sample {highstock} stock/rangeselector/vertical-align-middle/
  249. * Middle
  250. * @sample {highstock} stock/rangeselector/vertical-align-bottom/
  251. * Bottom
  252. *
  253. * @type {Highcharts.VerticalAlignValue}
  254. * @since 6.0.0
  255. */
  256. verticalAlign: 'top',
  257. /**
  258. * A collection of attributes for the buttons. The object takes SVG
  259. * attributes like `fill`, `stroke`, `stroke-width`, as well as `style`,
  260. * a collection of CSS properties for the text.
  261. *
  262. * The object can also be extended with states, so you can set
  263. * presentational options for `hover`, `select` or `disabled` button
  264. * states.
  265. *
  266. * CSS styles for the text label.
  267. *
  268. * In styled mode, the buttons are styled by the
  269. * `.highcharts-range-selector-buttons .highcharts-button` rule with its
  270. * different states.
  271. *
  272. * @sample {highstock} stock/rangeselector/styling/
  273. * Styling the buttons and inputs
  274. *
  275. * @type {Highcharts.SVGAttributes}
  276. */
  277. buttonTheme: {
  278. /** @ignore */
  279. width: 28,
  280. /** @ignore */
  281. height: 18,
  282. /** @ignore */
  283. padding: 2,
  284. /** @ignore */
  285. zIndex: 7 // #484, #852
  286. },
  287. /**
  288. * When the rangeselector is floating, the plot area does not reserve
  289. * space for it. This opens for positioning anywhere on the chart.
  290. *
  291. * @sample {highstock} stock/rangeselector/floating/
  292. * Placing the range selector between the plot area and the
  293. * navigator
  294. *
  295. * @since 6.0.0
  296. */
  297. floating: false,
  298. /**
  299. * The x offset of the range selector relative to its horizontal
  300. * alignment within `chart.spacingLeft` and `chart.spacingRight`.
  301. *
  302. * @since 6.0.0
  303. */
  304. x: 0,
  305. /**
  306. * The y offset of the range selector relative to its horizontal
  307. * alignment within `chart.spacingLeft` and `chart.spacingRight`.
  308. *
  309. * @since 6.0.0
  310. */
  311. y: 0,
  312. /**
  313. * Deprecated. The height of the range selector. Currently it is
  314. * calculated dynamically.
  315. *
  316. * @deprecated
  317. * @type {number|undefined}
  318. * @since 2.1.9
  319. */
  320. height: void 0,
  321. /**
  322. * The border color of the date input boxes.
  323. *
  324. * @sample {highstock} stock/rangeselector/styling/
  325. * Styling the buttons and inputs
  326. *
  327. * @type {Highcharts.ColorString}
  328. * @since 1.3.7
  329. */
  330. inputBoxBorderColor: 'none',
  331. /**
  332. * The pixel height of the date input boxes.
  333. *
  334. * @sample {highstock} stock/rangeselector/styling/
  335. * Styling the buttons and inputs
  336. *
  337. * @since 1.3.7
  338. */
  339. inputBoxHeight: 17,
  340. /**
  341. * The pixel width of the date input boxes. When `undefined`, the width
  342. * is fitted to the rendered content.
  343. *
  344. * @sample {highstock} stock/rangeselector/styling/
  345. * Styling the buttons and inputs
  346. *
  347. * @type {number|undefined}
  348. * @since 1.3.7
  349. */
  350. inputBoxWidth: void 0,
  351. /**
  352. * The date format in the input boxes when not selected for editing.
  353. * Defaults to `%b %e, %Y`.
  354. *
  355. * This is used to determine which type of input to show,
  356. * `datetime-local`, `date` or `time` and falling back to `text` when
  357. * the browser does not support the input type or the format contains
  358. * milliseconds.
  359. *
  360. * @sample {highstock} stock/rangeselector/input-type/
  361. * Input types
  362. * @sample {highstock} stock/rangeselector/input-format/
  363. * Milliseconds in the range selector
  364. *
  365. */
  366. inputDateFormat: '%b %e, %Y',
  367. /**
  368. * A custom callback function to parse values entered in the input boxes
  369. * and return a valid JavaScript time as milliseconds since 1970.
  370. * The first argument passed is a value to parse,
  371. * second is a boolean indicating use of the UTC time.
  372. *
  373. * This will only get called for inputs of type `text`. Since v8.2.3,
  374. * the input type is dynamically determined based on the granularity
  375. * of the `inputDateFormat` and the browser support.
  376. *
  377. * @sample {highstock} stock/rangeselector/input-format/
  378. * Milliseconds in the range selector
  379. *
  380. * @type {Highcharts.RangeSelectorParseCallbackFunction}
  381. * @since 1.3.3
  382. */
  383. inputDateParser: void 0,
  384. /**
  385. * The date format in the input boxes when they are selected for
  386. * editing. This must be a format that is recognized by JavaScript
  387. * Date.parse.
  388. *
  389. * This will only be used for inputs of type `text`. Since v8.2.3,
  390. * the input type is dynamically determined based on the granularity
  391. * of the `inputDateFormat` and the browser support.
  392. *
  393. * @sample {highstock} stock/rangeselector/input-format/
  394. * Milliseconds in the range selector
  395. *
  396. */
  397. inputEditDateFormat: '%Y-%m-%d',
  398. /**
  399. * Enable or disable the date input boxes.
  400. */
  401. inputEnabled: true,
  402. /**
  403. * Positioning for the input boxes. Allowed properties are `align`,
  404. * `x` and `y`.
  405. *
  406. * @since 1.2.4
  407. */
  408. inputPosition: {
  409. /**
  410. * The alignment of the input box. Allowed properties are `left`,
  411. * `center`, `right`.
  412. *
  413. * @sample {highstock} stock/rangeselector/input-button-position/
  414. * Alignment
  415. *
  416. * @type {Highcharts.AlignValue}
  417. * @since 6.0.0
  418. */
  419. align: 'right',
  420. /**
  421. * X offset of the input row.
  422. */
  423. x: 0,
  424. /**
  425. * Y offset of the input row.
  426. */
  427. y: 0
  428. },
  429. /**
  430. * The space in pixels between the labels and the date input boxes in
  431. * the range selector.
  432. *
  433. * @since 9.0.0
  434. */
  435. inputSpacing: 5,
  436. /**
  437. * The index of the button to appear pre-selected.
  438. *
  439. * @type {number}
  440. */
  441. selected: void 0,
  442. /**
  443. * Positioning for the button row.
  444. *
  445. * @since 1.2.4
  446. */
  447. buttonPosition: {
  448. /**
  449. * The alignment of the input box. Allowed properties are `left`,
  450. * `center`, `right`.
  451. *
  452. * @sample {highstock} stock/rangeselector/input-button-position/
  453. * Alignment
  454. *
  455. * @type {Highcharts.AlignValue}
  456. * @since 6.0.0
  457. */
  458. align: 'left',
  459. /**
  460. * X offset of the button row.
  461. */
  462. x: 0,
  463. /**
  464. * Y offset of the button row.
  465. */
  466. y: 0
  467. },
  468. /**
  469. * CSS for the HTML inputs in the range selector.
  470. *
  471. * In styled mode, the inputs are styled by the
  472. * `.highcharts-range-input text` rule in SVG mode, and
  473. * `input.highcharts-range-selector` when active.
  474. *
  475. * @sample {highstock} stock/rangeselector/styling/
  476. * Styling the buttons and inputs
  477. *
  478. * @type {Highcharts.CSSObject}
  479. * @apioption rangeSelector.inputStyle
  480. */
  481. inputStyle: {
  482. /** @ignore */
  483. color: palette.highlightColor80,
  484. /** @ignore */
  485. cursor: 'pointer'
  486. },
  487. /**
  488. * CSS styles for the labels - the Zoom, From and To texts.
  489. *
  490. * In styled mode, the labels are styled by the
  491. * `.highcharts-range-label` class.
  492. *
  493. * @sample {highstock} stock/rangeselector/styling/
  494. * Styling the buttons and inputs
  495. *
  496. * @type {Highcharts.CSSObject}
  497. */
  498. labelStyle: {
  499. /** @ignore */
  500. color: palette.neutralColor60
  501. }
  502. }
  503. });
  504. extend(defaultOptions.lang,
  505. /**
  506. * Language object. The language object is global and it can't be set
  507. * on each chart initialization. Instead, use `Highcharts.setOptions` to
  508. * set it before any chart is initialized.
  509. *
  510. * ```js
  511. * Highcharts.setOptions({
  512. * lang: {
  513. * months: [
  514. * 'Janvier', 'Février', 'Mars', 'Avril',
  515. * 'Mai', 'Juin', 'Juillet', 'Août',
  516. * 'Septembre', 'Octobre', 'Novembre', 'Décembre'
  517. * ],
  518. * weekdays: [
  519. * 'Dimanche', 'Lundi', 'Mardi', 'Mercredi',
  520. * 'Jeudi', 'Vendredi', 'Samedi'
  521. * ]
  522. * }
  523. * });
  524. * ```
  525. *
  526. * @optionparent lang
  527. */
  528. {
  529. /**
  530. * The text for the label for the range selector buttons.
  531. *
  532. * @product highstock gantt
  533. */
  534. rangeSelectorZoom: 'Zoom',
  535. /**
  536. * The text for the label for the "from" input box in the range
  537. * selector. Since v9.0, this string is empty as the label is not
  538. * rendered by default.
  539. *
  540. * @product highstock gantt
  541. */
  542. rangeSelectorFrom: '',
  543. /**
  544. * The text for the label for the "to" input box in the range selector.
  545. *
  546. * @product highstock gantt
  547. */
  548. rangeSelectorTo: '→'
  549. });
  550. /* eslint-disable no-invalid-this, valid-jsdoc */
  551. /**
  552. * The range selector.
  553. *
  554. * @private
  555. * @class
  556. * @name Highcharts.RangeSelector
  557. * @param {Highcharts.Chart} chart
  558. */
  559. var RangeSelector = /** @class */ (function () {
  560. function RangeSelector(chart) {
  561. /* *
  562. *
  563. * Properties
  564. *
  565. * */
  566. this.buttons = void 0;
  567. this.buttonOptions = RangeSelector.prototype.defaultButtons;
  568. this.initialButtonGroupWidth = 0;
  569. this.options = void 0;
  570. this.chart = chart;
  571. // Run RangeSelector
  572. this.init(chart);
  573. }
  574. /**
  575. * The method to run when one of the buttons in the range selectors is
  576. * clicked
  577. *
  578. * @private
  579. * @function Highcharts.RangeSelector#clickButton
  580. * @param {number} i
  581. * The index of the button
  582. * @param {boolean} [redraw]
  583. * @return {void}
  584. */
  585. RangeSelector.prototype.clickButton = function (i, redraw) {
  586. 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
  587. type = rangeOptions.type, baseXAxisOptions, range = rangeOptions._range, rangeMin, minSetting, rangeSetting, ctx, ytdExtremes, dataGrouping = rangeOptions.dataGrouping;
  588. // chart has no data, base series is removed
  589. if (dataMin === null || dataMax === null) {
  590. return;
  591. }
  592. // Set the fixed range before range is altered
  593. chart.fixedRange = range;
  594. // Apply dataGrouping associated to button
  595. if (dataGrouping) {
  596. this.forcedDataGrouping = true;
  597. Axis.prototype.setDataGrouping.call(baseAxis || { chart: this.chart }, dataGrouping, false);
  598. this.frozenStates = rangeOptions.preserveDataGrouping;
  599. }
  600. // Apply range
  601. if (type === 'month' || type === 'year') {
  602. if (!baseAxis) {
  603. // This is set to the user options and picked up later when the
  604. // axis is instantiated so that we know the min and max.
  605. range = rangeOptions;
  606. }
  607. else {
  608. ctx = {
  609. range: rangeOptions,
  610. max: newMax,
  611. chart: chart,
  612. dataMin: dataMin,
  613. dataMax: dataMax
  614. };
  615. newMin = baseAxis.minFromRange.call(ctx);
  616. if (isNumber(ctx.newMax)) {
  617. newMax = ctx.newMax;
  618. }
  619. }
  620. // Fixed times like minutes, hours, days
  621. }
  622. else if (range) {
  623. newMin = Math.max(newMax - range, dataMin);
  624. newMax = Math.min(newMin + range, dataMax);
  625. }
  626. else if (type === 'ytd') {
  627. // On user clicks on the buttons, or a delayed action running from
  628. // the beforeRender event (below), the baseAxis is defined.
  629. if (baseAxis) {
  630. // When "ytd" is the pre-selected button for the initial view,
  631. // its calculation is delayed and rerun in the beforeRender
  632. // event (below). When the series are initialized, but before
  633. // the chart is rendered, we have access to the xData array
  634. // (#942).
  635. if (typeof dataMax === 'undefined') {
  636. dataMin = Number.MAX_VALUE;
  637. dataMax = Number.MIN_VALUE;
  638. chart.series.forEach(function (series) {
  639. // reassign it to the last item
  640. var xData = series.xData;
  641. dataMin = Math.min(xData[0], dataMin);
  642. dataMax = Math.max(xData[xData.length - 1], dataMax);
  643. });
  644. redraw = false;
  645. }
  646. ytdExtremes = rangeSelector.getYTDExtremes(dataMax, dataMin, chart.time.useUTC);
  647. newMin = rangeMin = ytdExtremes.min;
  648. newMax = ytdExtremes.max;
  649. // "ytd" is pre-selected. We don't yet have access to processed
  650. // point and extremes data (things like pointStart and pointInterval
  651. // are missing), so we delay the process (#942)
  652. }
  653. else {
  654. rangeSelector.deferredYTDClick = i;
  655. return;
  656. }
  657. }
  658. else if (type === 'all' && baseAxis) {
  659. newMin = dataMin;
  660. newMax = dataMax;
  661. }
  662. if (defined(newMin)) {
  663. newMin += rangeOptions._offsetMin;
  664. }
  665. if (defined(newMax)) {
  666. newMax += rangeOptions._offsetMax;
  667. }
  668. rangeSelector.setSelected(i);
  669. if (this.dropdown) {
  670. this.dropdown.selectedIndex = i + 1;
  671. }
  672. // Update the chart
  673. if (!baseAxis) {
  674. // Axis not yet instanciated. Temporarily set min and range
  675. // options and remove them on chart load (#4317).
  676. baseXAxisOptions = splat(chart.options.xAxis)[0];
  677. rangeSetting = baseXAxisOptions.range;
  678. baseXAxisOptions.range = range;
  679. minSetting = baseXAxisOptions.min;
  680. baseXAxisOptions.min = rangeMin;
  681. addEvent(chart, 'load', function resetMinAndRange() {
  682. baseXAxisOptions.range = rangeSetting;
  683. baseXAxisOptions.min = minSetting;
  684. });
  685. }
  686. else {
  687. // Existing axis object. Set extremes after render time.
  688. baseAxis.setExtremes(newMin, newMax, pick(redraw, true), void 0, // auto animation
  689. {
  690. trigger: 'rangeSelectorButton',
  691. rangeSelectorButton: rangeOptions
  692. });
  693. }
  694. fireEvent(this, 'afterBtnClick');
  695. };
  696. /**
  697. * Set the selected option. This method only sets the internal flag, it
  698. * doesn't update the buttons or the actual zoomed range.
  699. *
  700. * @private
  701. * @function Highcharts.RangeSelector#setSelected
  702. * @param {number} [selected]
  703. * @return {void}
  704. */
  705. RangeSelector.prototype.setSelected = function (selected) {
  706. this.selected = this.options.selected = selected;
  707. };
  708. /**
  709. * Initialize the range selector
  710. *
  711. * @private
  712. * @function Highcharts.RangeSelector#init
  713. * @param {Highcharts.Chart} chart
  714. * @return {void}
  715. */
  716. RangeSelector.prototype.init = function (chart) {
  717. var rangeSelector = this, options = chart.options.rangeSelector, buttonOptions = options.buttons || rangeSelector.defaultButtons.slice(), selectedOption = options.selected, blurInputs = function () {
  718. var minInput = rangeSelector.minInput, maxInput = rangeSelector.maxInput;
  719. // #3274 in some case blur is not defined
  720. if (minInput && minInput.blur) {
  721. fireEvent(minInput, 'blur');
  722. }
  723. if (maxInput && maxInput.blur) {
  724. fireEvent(maxInput, 'blur');
  725. }
  726. };
  727. rangeSelector.chart = chart;
  728. rangeSelector.options = options;
  729. rangeSelector.buttons = [];
  730. rangeSelector.buttonOptions = buttonOptions;
  731. this.eventsToUnbind = [];
  732. this.eventsToUnbind.push(addEvent(chart.container, 'mousedown', blurInputs));
  733. this.eventsToUnbind.push(addEvent(chart, 'resize', blurInputs));
  734. // Extend the buttonOptions with actual range
  735. buttonOptions.forEach(rangeSelector.computeButtonRange);
  736. // zoomed range based on a pre-selected button index
  737. if (typeof selectedOption !== 'undefined' &&
  738. buttonOptions[selectedOption]) {
  739. this.clickButton(selectedOption, false);
  740. }
  741. this.eventsToUnbind.push(addEvent(chart, 'load', function () {
  742. // If a data grouping is applied to the current button, release it
  743. // when extremes change
  744. if (chart.xAxis && chart.xAxis[0]) {
  745. addEvent(chart.xAxis[0], 'setExtremes', function (e) {
  746. if (this.max - this.min !==
  747. chart.fixedRange &&
  748. e.trigger !== 'rangeSelectorButton' &&
  749. e.trigger !== 'updatedData' &&
  750. rangeSelector.forcedDataGrouping &&
  751. !rangeSelector.frozenStates) {
  752. this.setDataGrouping(false, false);
  753. }
  754. });
  755. }
  756. }));
  757. };
  758. /**
  759. * Dynamically update the range selector buttons after a new range has been
  760. * set
  761. *
  762. * @private
  763. * @function Highcharts.RangeSelector#updateButtonStates
  764. * @return {void}
  765. */
  766. RangeSelector.prototype.updateButtonStates = function () {
  767. var rangeSelector = this, chart = this.chart, dropdown = this.dropdown, baseAxis = chart.xAxis[0], actualRange = Math.round(baseAxis.max - baseAxis.min), hasNoData = !baseAxis.hasVisibleSeries, day = 24 * 36e5, // A single day in milliseconds
  768. unionExtremes = (chart.scroller &&
  769. 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;
  770. rangeSelector.buttonOptions.forEach(function (rangeOptions, i) {
  771. var range = rangeOptions._range, type = rangeOptions.type, count = rangeOptions.count || 1, button = buttons[i], state = 0, disable, select, offsetRange = rangeOptions._offsetMax -
  772. rangeOptions._offsetMin, isSelected = i === selected,
  773. // Disable buttons where the range exceeds what is allowed in
  774. // the current view
  775. isTooGreatRange = range >
  776. dataMax - dataMin,
  777. // Disable buttons where the range is smaller than the minimum
  778. // range
  779. isTooSmallRange = range < baseAxis.minRange,
  780. // Do not select the YTD button if not explicitly told so
  781. isYTDButNotSelected = false,
  782. // Disable the All button if we're already showing all
  783. isAllButAlreadyShowingAll = false, isSameRange = range === actualRange;
  784. // Months and years have a variable range so we check the extremes
  785. if ((type === 'month' || type === 'year') &&
  786. (actualRange + 36e5 >=
  787. { month: 28, year: 365 }[type] * day * count - offsetRange) &&
  788. (actualRange - 36e5 <=
  789. { month: 31, year: 366 }[type] * day * count + offsetRange)) {
  790. isSameRange = true;
  791. }
  792. else if (type === 'ytd') {
  793. isSameRange = (ytdMax - ytdMin + offsetRange) === actualRange;
  794. isYTDButNotSelected = !isSelected;
  795. }
  796. else if (type === 'all') {
  797. isSameRange = (baseAxis.max - baseAxis.min >=
  798. dataMax - dataMin);
  799. isAllButAlreadyShowingAll = (!isSelected &&
  800. selectedExists &&
  801. isSameRange);
  802. }
  803. // The new zoom area happens to match the range for a button - mark
  804. // it selected. This happens when scrolling across an ordinal gap.
  805. // It can be seen in the intraday demos when selecting 1h and scroll
  806. // across the night gap.
  807. disable = (!allButtonsEnabled &&
  808. (isTooGreatRange ||
  809. isTooSmallRange ||
  810. isAllButAlreadyShowingAll ||
  811. hasNoData));
  812. select = ((isSelected && isSameRange) ||
  813. (isSameRange && !selectedExists && !isYTDButNotSelected) ||
  814. (isSelected && rangeSelector.frozenStates));
  815. if (disable) {
  816. state = 3;
  817. }
  818. else if (select) {
  819. selectedExists = true; // Only one button can be selected
  820. state = 2;
  821. }
  822. // If state has changed, update the button
  823. if (button.state !== state) {
  824. button.setState(state);
  825. if (dropdown) {
  826. dropdown.options[i + 1].disabled = disable;
  827. if (state === 2) {
  828. dropdown.selectedIndex = i + 1;
  829. }
  830. }
  831. // Reset (#9209)
  832. if (state === 0 && selected === i) {
  833. rangeSelector.setSelected();
  834. }
  835. }
  836. });
  837. };
  838. /**
  839. * Compute and cache the range for an individual button
  840. *
  841. * @private
  842. * @function Highcharts.RangeSelector#computeButtonRange
  843. * @param {Highcharts.RangeSelectorButtonsOptions} rangeOptions
  844. * @return {void}
  845. */
  846. RangeSelector.prototype.computeButtonRange = function (rangeOptions) {
  847. var type = rangeOptions.type, count = rangeOptions.count || 1,
  848. // these time intervals have a fixed number of milliseconds, as
  849. // opposed to month, ytd and year
  850. fixedTimes = {
  851. millisecond: 1,
  852. second: 1000,
  853. minute: 60 * 1000,
  854. hour: 3600 * 1000,
  855. day: 24 * 3600 * 1000,
  856. week: 7 * 24 * 3600 * 1000
  857. };
  858. // Store the range on the button object
  859. if (fixedTimes[type]) {
  860. rangeOptions._range = fixedTimes[type] * count;
  861. }
  862. else if (type === 'month' || type === 'year') {
  863. rangeOptions._range = {
  864. month: 30,
  865. year: 365
  866. }[type] * 24 * 36e5 * count;
  867. }
  868. rangeOptions._offsetMin = pick(rangeOptions.offsetMin, 0);
  869. rangeOptions._offsetMax = pick(rangeOptions.offsetMax, 0);
  870. rangeOptions._range +=
  871. rangeOptions._offsetMax - rangeOptions._offsetMin;
  872. };
  873. /**
  874. * Get the unix timestamp of a HTML input for the dates
  875. *
  876. * @private
  877. * @function Highcharts.RangeSelector#getInputValue
  878. * @param {string} name
  879. * @return {number}
  880. */
  881. RangeSelector.prototype.getInputValue = function (name) {
  882. var input = name === 'min' ? this.minInput : this.maxInput;
  883. var options = this.chart.options.rangeSelector;
  884. var time = this.chart.time;
  885. if (input) {
  886. return ((input.type === 'text' && options.inputDateParser) ||
  887. this.defaultInputDateParser)(input.value, time.useUTC, time);
  888. }
  889. return 0;
  890. };
  891. /**
  892. * Set the internal and displayed value of a HTML input for the dates
  893. *
  894. * @private
  895. * @function Highcharts.RangeSelector#setInputValue
  896. * @param {string} name
  897. * @param {number} [inputTime]
  898. * @return {void}
  899. */
  900. RangeSelector.prototype.setInputValue = function (name, inputTime) {
  901. var options = this.options, time = this.chart.time, input = name === 'min' ? this.minInput : this.maxInput, dateBox = name === 'min' ? this.minDateBox : this.maxDateBox;
  902. if (input) {
  903. var hcTimeAttr = input.getAttribute('data-hc-time');
  904. var updatedTime = defined(hcTimeAttr) ? Number(hcTimeAttr) : void 0;
  905. if (defined(inputTime)) {
  906. var previousTime = updatedTime;
  907. if (defined(previousTime)) {
  908. input.setAttribute('data-hc-time-previous', previousTime);
  909. }
  910. input.setAttribute('data-hc-time', inputTime);
  911. updatedTime = inputTime;
  912. }
  913. input.value = time.dateFormat(this.inputTypeFormats[input.type] || options.inputEditDateFormat, updatedTime);
  914. if (dateBox) {
  915. dateBox.attr({
  916. text: time.dateFormat(options.inputDateFormat, updatedTime)
  917. });
  918. }
  919. }
  920. };
  921. /**
  922. * Set the min and max value of a HTML input for the dates
  923. *
  924. * @private
  925. * @function Highcharts.RangeSelector#setInputExtremes
  926. * @param {string} name
  927. * @param {number} min
  928. * @param {number} max
  929. * @return {void}
  930. */
  931. RangeSelector.prototype.setInputExtremes = function (name, min, max) {
  932. var input = name === 'min' ? this.minInput : this.maxInput;
  933. if (input) {
  934. var format = this.inputTypeFormats[input.type];
  935. var time = this.chart.time;
  936. if (format) {
  937. var newMin = time.dateFormat(format, min);
  938. if (input.min !== newMin) {
  939. input.min = newMin;
  940. }
  941. var newMax = time.dateFormat(format, max);
  942. if (input.max !== newMax) {
  943. input.max = newMax;
  944. }
  945. }
  946. }
  947. };
  948. /**
  949. * @private
  950. * @function Highcharts.RangeSelector#showInput
  951. * @param {string} name
  952. * @return {void}
  953. */
  954. RangeSelector.prototype.showInput = function (name) {
  955. var dateBox = name === 'min' ? this.minDateBox : this.maxDateBox;
  956. var input = name === 'min' ? this.minInput : this.maxInput;
  957. if (input && dateBox && this.inputGroup) {
  958. var isTextInput = input.type === 'text';
  959. var _a = this.inputGroup, translateX = _a.translateX, translateY = _a.translateY;
  960. css(input, {
  961. width: isTextInput ? ((dateBox.width - 2) + 'px') : 'auto',
  962. height: isTextInput ? ((dateBox.height - 2) + 'px') : 'auto',
  963. border: '2px solid silver'
  964. });
  965. if (isTextInput) {
  966. css(input, {
  967. left: (translateX + dateBox.x) + 'px',
  968. top: translateY + 'px'
  969. });
  970. // Inputs of types date, time or datetime-local should be centered
  971. // on top of the dateBox
  972. }
  973. else {
  974. css(input, {
  975. left: Math.min(Math.round(dateBox.x +
  976. translateX -
  977. (input.offsetWidth - dateBox.width) / 2), this.chart.chartWidth - input.offsetWidth) + 'px',
  978. top: (translateY - (input.offsetHeight - dateBox.height) / 2) + 'px'
  979. });
  980. }
  981. }
  982. };
  983. /**
  984. * @private
  985. * @function Highcharts.RangeSelector#hideInput
  986. * @param {string} name
  987. * @return {void}
  988. */
  989. RangeSelector.prototype.hideInput = function (name) {
  990. var input = name === 'min' ? this.minInput : this.maxInput;
  991. if (input) {
  992. css(input, {
  993. top: '-9999em',
  994. border: 0,
  995. width: '1px',
  996. height: '1px'
  997. });
  998. }
  999. };
  1000. /**
  1001. * @private
  1002. * @function Highcharts.RangeSelector#defaultInputDateParser
  1003. */
  1004. RangeSelector.prototype.defaultInputDateParser = function (inputDate, useUTC, time) {
  1005. var hasTimezone = function (str) {
  1006. return str.length > 6 &&
  1007. (str.lastIndexOf('-') === str.length - 6 ||
  1008. str.lastIndexOf('+') === str.length - 6);
  1009. };
  1010. var input = inputDate.split('/').join('-').split(' ').join('T');
  1011. if (input.indexOf('T') === -1) {
  1012. input += 'T00:00';
  1013. }
  1014. if (useUTC) {
  1015. input += 'Z';
  1016. }
  1017. else if (H.isSafari && !hasTimezone(input)) {
  1018. var offset = new Date(input).getTimezoneOffset() / 60;
  1019. input += offset <= 0 ? "+" + pad(-offset) + ":00" : "-" + pad(offset) + ":00";
  1020. }
  1021. var date = Date.parse(input);
  1022. // If the value isn't parsed directly to a value by the
  1023. // browser's Date.parse method, like YYYY-MM-DD in IE8, try
  1024. // parsing it a different way
  1025. if (!isNumber(date)) {
  1026. var parts = inputDate.split('-');
  1027. date = Date.UTC(pInt(parts[0]), pInt(parts[1]) - 1, pInt(parts[2]));
  1028. }
  1029. if (time && useUTC) {
  1030. date += time.getTimezoneOffset(date);
  1031. }
  1032. return date;
  1033. };
  1034. /**
  1035. * Draw either the 'from' or the 'to' HTML input box of the range selector
  1036. *
  1037. * @private
  1038. * @function Highcharts.RangeSelector#drawInput
  1039. * @param {string} name
  1040. * @return {RangeSelectorInputElements}
  1041. */
  1042. RangeSelector.prototype.drawInput = function (name) {
  1043. var _a = this, chart = _a.chart, div = _a.div, inputGroup = _a.inputGroup;
  1044. var rangeSelector = this, chartStyle = chart.renderer.style || {}, renderer = chart.renderer, options = chart.options.rangeSelector, lang = defaultOptions.lang, isMin = name === 'min';
  1045. /**
  1046. * @private
  1047. */
  1048. function updateExtremes() {
  1049. var value = rangeSelector.getInputValue(name), chartAxis = chart.xAxis[0], dataAxis = chart.scroller && chart.scroller.xAxis ?
  1050. chart.scroller.xAxis :
  1051. chartAxis, dataMin = dataAxis.dataMin, dataMax = dataAxis.dataMax;
  1052. var maxInput = rangeSelector.maxInput, minInput = rangeSelector.minInput;
  1053. if (value !== Number(input.getAttribute('data-hc-time-previous')) &&
  1054. isNumber(value)) {
  1055. input.setAttribute('data-hc-time-previous', value);
  1056. // Validate the extremes. If it goes beyound the data min or
  1057. // max, use the actual data extreme (#2438).
  1058. if (isMin && maxInput && isNumber(dataMin)) {
  1059. if (value > Number(maxInput.getAttribute('data-hc-time'))) {
  1060. value = void 0;
  1061. }
  1062. else if (value < dataMin) {
  1063. value = dataMin;
  1064. }
  1065. }
  1066. else if (minInput && isNumber(dataMax)) {
  1067. if (value < Number(minInput.getAttribute('data-hc-time'))) {
  1068. value = void 0;
  1069. }
  1070. else if (value > dataMax) {
  1071. value = dataMax;
  1072. }
  1073. }
  1074. // Set the extremes
  1075. if (typeof value !== 'undefined') { // @todo typof undefined
  1076. chartAxis.setExtremes(isMin ? value : chartAxis.min, isMin ? chartAxis.max : value, void 0, void 0, { trigger: 'rangeSelectorInput' });
  1077. }
  1078. }
  1079. }
  1080. // Create the text label
  1081. var text = lang[isMin ? 'rangeSelectorFrom' : 'rangeSelectorTo'];
  1082. var label = renderer
  1083. .label(text, 0)
  1084. .addClass('highcharts-range-label')
  1085. .attr({
  1086. padding: text ? 2 : 0
  1087. })
  1088. .add(inputGroup);
  1089. // Create an SVG label that shows updated date ranges and and records
  1090. // click events that bring in the HTML input.
  1091. var dateBox = renderer
  1092. .label('', 0)
  1093. .addClass('highcharts-range-input')
  1094. .attr({
  1095. padding: 2,
  1096. width: options.inputBoxWidth,
  1097. height: options.inputBoxHeight,
  1098. 'text-align': 'center'
  1099. })
  1100. .on('click', function () {
  1101. // If it is already focused, the onfocus event doesn't fire
  1102. // (#3713)
  1103. rangeSelector.showInput(name);
  1104. rangeSelector[name + 'Input'].focus();
  1105. });
  1106. if (!chart.styledMode) {
  1107. dateBox.attr({
  1108. stroke: options.inputBoxBorderColor,
  1109. 'stroke-width': 1
  1110. });
  1111. }
  1112. dateBox.add(inputGroup);
  1113. // Create the HTML input element. This is rendered as 1x1 pixel then set
  1114. // to the right size when focused.
  1115. var input = createElement('input', {
  1116. name: name,
  1117. className: 'highcharts-range-selector'
  1118. }, void 0, div);
  1119. // #14788: Setting input.type to an unsupported type throws in IE, so
  1120. // we need to use setAttribute instead
  1121. input.setAttribute('type', preferredInputType(options.inputDateFormat || '%b %e, %Y'));
  1122. if (!chart.styledMode) {
  1123. // Styles
  1124. label.css(merge(chartStyle, options.labelStyle));
  1125. dateBox.css(merge({
  1126. color: palette.neutralColor80
  1127. }, chartStyle, options.inputStyle));
  1128. css(input, extend({
  1129. position: 'absolute',
  1130. border: 0,
  1131. boxShadow: '0 0 15px rgba(0,0,0,0.3)',
  1132. width: '1px',
  1133. height: '1px',
  1134. padding: 0,
  1135. textAlign: 'center',
  1136. fontSize: chartStyle.fontSize,
  1137. fontFamily: chartStyle.fontFamily,
  1138. top: '-9999em' // #4798
  1139. }, options.inputStyle));
  1140. }
  1141. // Blow up the input box
  1142. input.onfocus = function () {
  1143. rangeSelector.showInput(name);
  1144. };
  1145. // Hide away the input box
  1146. input.onblur = function () {
  1147. // update extermes only when inputs are active
  1148. if (input === H.doc.activeElement) { // Only when focused
  1149. // Update also when no `change` event is triggered, like when
  1150. // clicking inside the SVG (#4710)
  1151. updateExtremes();
  1152. }
  1153. // #10404 - move hide and blur outside focus
  1154. rangeSelector.hideInput(name);
  1155. rangeSelector.setInputValue(name);
  1156. input.blur(); // #4606
  1157. };
  1158. var keyDown = false;
  1159. // handle changes in the input boxes
  1160. input.onchange = function () {
  1161. updateExtremes();
  1162. // Blur input when clicking date input calendar
  1163. if (!keyDown) {
  1164. rangeSelector.hideInput(name);
  1165. input.blur();
  1166. }
  1167. };
  1168. input.onkeypress = function (event) {
  1169. // IE does not fire onchange on enter
  1170. if (event.keyCode === 13) {
  1171. updateExtremes();
  1172. }
  1173. };
  1174. input.onkeydown = function () {
  1175. keyDown = true;
  1176. };
  1177. input.onkeyup = function () {
  1178. keyDown = false;
  1179. };
  1180. return { dateBox: dateBox, input: input, label: label };
  1181. };
  1182. /**
  1183. * Get the position of the range selector buttons and inputs. This can be
  1184. * overridden from outside for custom positioning.
  1185. *
  1186. * @private
  1187. * @function Highcharts.RangeSelector#getPosition
  1188. *
  1189. * @return {Highcharts.Dictionary<number>}
  1190. */
  1191. RangeSelector.prototype.getPosition = function () {
  1192. var chart = this.chart, options = chart.options.rangeSelector, top = options.verticalAlign === 'top' ?
  1193. chart.plotTop - chart.axisOffset[0] :
  1194. 0; // set offset only for varticalAlign top
  1195. return {
  1196. buttonTop: top + options.buttonPosition.y,
  1197. inputTop: top + options.inputPosition.y - 10
  1198. };
  1199. };
  1200. /**
  1201. * Get the extremes of YTD. Will choose dataMax if its value is lower than
  1202. * the current timestamp. Will choose dataMin if its value is higher than
  1203. * the timestamp for the start of current year.
  1204. *
  1205. * @private
  1206. * @function Highcharts.RangeSelector#getYTDExtremes
  1207. *
  1208. * @param {number} dataMax
  1209. *
  1210. * @param {number} dataMin
  1211. *
  1212. * @return {*}
  1213. * Returns min and max for the YTD
  1214. */
  1215. RangeSelector.prototype.getYTDExtremes = function (dataMax, dataMin, useUTC) {
  1216. var time = this.chart.time, min, now = new time.Date(dataMax), year = time.get('FullYear', now), startOfYear = useUTC ?
  1217. time.Date.UTC(year, 0, 1) : // eslint-disable-line new-cap
  1218. +new time.Date(year, 0, 1);
  1219. min = Math.max(dataMin, startOfYear);
  1220. var ts = now.getTime();
  1221. return {
  1222. max: Math.min(dataMax || ts, ts),
  1223. min: min
  1224. };
  1225. };
  1226. /**
  1227. * Render the range selector including the buttons and the inputs. The first
  1228. * time render is called, the elements are created and positioned. On
  1229. * subsequent calls, they are moved and updated.
  1230. *
  1231. * @private
  1232. * @function Highcharts.RangeSelector#render
  1233. * @param {number} [min]
  1234. * X axis minimum
  1235. * @param {number} [max]
  1236. * X axis maximum
  1237. * @return {void}
  1238. */
  1239. RangeSelector.prototype.render = function (min, max) {
  1240. var chart = this.chart, renderer = chart.renderer, container = chart.container, chartOptions = chart.options, options = chartOptions.rangeSelector,
  1241. // Place inputs above the container
  1242. inputsZIndex = pick(chartOptions.chart.style &&
  1243. chartOptions.chart.style.zIndex, 0) + 1, inputEnabled = options.inputEnabled, rendered = this.rendered;
  1244. if (options.enabled === false) {
  1245. return;
  1246. }
  1247. // create the elements
  1248. if (!rendered) {
  1249. this.group = renderer.g('range-selector-group')
  1250. .attr({
  1251. zIndex: 7
  1252. })
  1253. .add();
  1254. this.div = createElement('div', void 0, {
  1255. position: 'relative',
  1256. height: 0,
  1257. zIndex: inputsZIndex
  1258. });
  1259. if (this.buttonOptions.length) {
  1260. this.renderButtons();
  1261. }
  1262. // First create a wrapper outside the container in order to make
  1263. // the inputs work and make export correct
  1264. if (container.parentNode) {
  1265. container.parentNode.insertBefore(this.div, container);
  1266. }
  1267. if (inputEnabled) {
  1268. // Create the group to keep the inputs
  1269. this.inputGroup = renderer.g('input-group').add(this.group);
  1270. var minElems = this.drawInput('min');
  1271. this.minDateBox = minElems.dateBox;
  1272. this.minLabel = minElems.label;
  1273. this.minInput = minElems.input;
  1274. var maxElems = this.drawInput('max');
  1275. this.maxDateBox = maxElems.dateBox;
  1276. this.maxLabel = maxElems.label;
  1277. this.maxInput = maxElems.input;
  1278. }
  1279. }
  1280. if (inputEnabled) {
  1281. // Set or reset the input values
  1282. this.setInputValue('min', min);
  1283. this.setInputValue('max', max);
  1284. var unionExtremes = (chart.scroller && chart.scroller.getUnionExtremes()) || chart.xAxis[0] || {};
  1285. if (defined(unionExtremes.dataMin) && defined(unionExtremes.dataMax)) {
  1286. var minRange = chart.xAxis[0].minRange || 0;
  1287. this.setInputExtremes('min', unionExtremes.dataMin, Math.min(unionExtremes.dataMax, this.getInputValue('max')) - minRange);
  1288. this.setInputExtremes('max', Math.max(unionExtremes.dataMin, this.getInputValue('min')) + minRange, unionExtremes.dataMax);
  1289. }
  1290. // Reflow
  1291. if (this.inputGroup) {
  1292. var x_1 = 0;
  1293. [
  1294. this.minLabel,
  1295. this.minDateBox,
  1296. this.maxLabel,
  1297. this.maxDateBox
  1298. ].forEach(function (label) {
  1299. if (label && label.width) {
  1300. label.attr({ x: x_1 });
  1301. x_1 += label.width + options.inputSpacing;
  1302. }
  1303. });
  1304. }
  1305. }
  1306. this.alignElements();
  1307. this.rendered = true;
  1308. };
  1309. /**
  1310. * Render the range buttons. This only runs the first time, later the
  1311. * positioning is laid out in alignElements.
  1312. *
  1313. * @private
  1314. * @function Highcharts.RangeSelector#renderButtons
  1315. * @return {void}
  1316. */
  1317. RangeSelector.prototype.renderButtons = function () {
  1318. var _this = this;
  1319. var _a = this, buttons = _a.buttons, chart = _a.chart, options = _a.options;
  1320. var lang = defaultOptions.lang;
  1321. var renderer = chart.renderer;
  1322. var buttonTheme = merge(options.buttonTheme);
  1323. var states = buttonTheme && buttonTheme.states;
  1324. // Prevent the button from resetting the width when the button state
  1325. // changes since we need more control over the width when collapsing
  1326. // the buttons
  1327. var width = buttonTheme.width || 28;
  1328. delete buttonTheme.width;
  1329. this.buttonGroup = renderer.g('range-selector-buttons').add(this.group);
  1330. var dropdown = this.dropdown = createElement('select', void 0, {
  1331. position: 'absolute',
  1332. width: '1px',
  1333. height: '1px',
  1334. padding: 0,
  1335. border: 0,
  1336. top: '-9999em',
  1337. cursor: 'pointer',
  1338. opacity: 0.0001
  1339. }, this.div);
  1340. // Prevent page zoom on iPhone
  1341. addEvent(dropdown, 'touchstart', function () {
  1342. dropdown.style.fontSize = '16px';
  1343. });
  1344. // Forward events from select to button
  1345. [
  1346. [H.isMS ? 'mouseover' : 'mouseenter'],
  1347. [H.isMS ? 'mouseout' : 'mouseleave'],
  1348. ['change', 'click']
  1349. ].forEach(function (_a) {
  1350. var from = _a[0], to = _a[1];
  1351. addEvent(dropdown, from, function () {
  1352. var button = buttons[_this.currentButtonIndex()];
  1353. if (button) {
  1354. fireEvent(button.element, to || from);
  1355. }
  1356. });
  1357. });
  1358. this.zoomText = renderer
  1359. .text(lang.rangeSelectorZoom, 0, 15)
  1360. .add(this.buttonGroup);
  1361. if (!this.chart.styledMode) {
  1362. this.zoomText.css(options.labelStyle);
  1363. buttonTheme['stroke-width'] = pick(buttonTheme['stroke-width'], 0);
  1364. }
  1365. createElement('option', {
  1366. textContent: this.zoomText.textStr,
  1367. disabled: true
  1368. }, void 0, dropdown);
  1369. this.buttonOptions.forEach(function (rangeOptions, i) {
  1370. createElement('option', {
  1371. textContent: rangeOptions.title || rangeOptions.text
  1372. }, void 0, dropdown);
  1373. buttons[i] = renderer
  1374. .button(rangeOptions.text, 0, 0, function (e) {
  1375. // extract events from button object and call
  1376. var buttonEvents = (rangeOptions.events &&
  1377. rangeOptions.events.click), callDefaultEvent;
  1378. if (buttonEvents) {
  1379. callDefaultEvent =
  1380. buttonEvents.call(rangeOptions, e);
  1381. }
  1382. if (callDefaultEvent !== false) {
  1383. _this.clickButton(i);
  1384. }
  1385. _this.isActive = true;
  1386. }, buttonTheme, states && states.hover, states && states.select, states && states.disabled)
  1387. .attr({
  1388. 'text-align': 'center',
  1389. width: width
  1390. })
  1391. .add(_this.buttonGroup);
  1392. if (rangeOptions.title) {
  1393. buttons[i].attr('title', rangeOptions.title);
  1394. }
  1395. });
  1396. };
  1397. /**
  1398. * Align the elements horizontally and vertically.
  1399. *
  1400. * @private
  1401. * @function Highcharts.RangeSelector#alignElements
  1402. * @return {void}
  1403. */
  1404. RangeSelector.prototype.alignElements = function () {
  1405. var _this = this;
  1406. var _a = this, buttonGroup = _a.buttonGroup, buttons = _a.buttons, chart = _a.chart, group = _a.group, inputGroup = _a.inputGroup, options = _a.options, zoomText = _a.zoomText;
  1407. var chartOptions = chart.options;
  1408. var navButtonOptions = (chartOptions.exporting &&
  1409. chartOptions.exporting.enabled !== false &&
  1410. chartOptions.navigation &&
  1411. chartOptions.navigation.buttonOptions);
  1412. var buttonPosition = options.buttonPosition, inputPosition = options.inputPosition, verticalAlign = options.verticalAlign;
  1413. // Get the X offset required to avoid overlapping with the exporting
  1414. // button. This is is used both by the buttonGroup and the inputGroup.
  1415. var getXOffsetForExportButton = function (group, position) {
  1416. if (navButtonOptions &&
  1417. _this.titleCollision(chart) &&
  1418. verticalAlign === 'top' &&
  1419. position.align === 'right' && ((position.y -
  1420. group.getBBox().height - 12) <
  1421. ((navButtonOptions.y || 0) +
  1422. (navButtonOptions.height || 0) +
  1423. chart.spacing[0]))) {
  1424. return -40;
  1425. }
  1426. return 0;
  1427. };
  1428. var plotLeft = chart.plotLeft;
  1429. if (group && buttonPosition && inputPosition) {
  1430. var translateX = buttonPosition.x - chart.spacing[3];
  1431. if (buttonGroup) {
  1432. this.positionButtons();
  1433. if (!this.initialButtonGroupWidth) {
  1434. var width_1 = 0;
  1435. if (zoomText) {
  1436. width_1 += zoomText.getBBox().width + 5;
  1437. }
  1438. buttons.forEach(function (button, i) {
  1439. width_1 += button.width;
  1440. if (i !== buttons.length - 1) {
  1441. width_1 += options.buttonSpacing;
  1442. }
  1443. });
  1444. this.initialButtonGroupWidth = width_1;
  1445. }
  1446. plotLeft -= chart.spacing[3];
  1447. this.updateButtonStates();
  1448. // Detect collision between button group and exporting
  1449. var xOffsetForExportButton_1 = getXOffsetForExportButton(buttonGroup, buttonPosition);
  1450. this.alignButtonGroup(xOffsetForExportButton_1);
  1451. // Skip animation
  1452. group.placed = buttonGroup.placed = chart.hasLoaded;
  1453. }
  1454. var xOffsetForExportButton = 0;
  1455. if (inputGroup) {
  1456. // Detect collision between the input group and exporting button
  1457. xOffsetForExportButton = getXOffsetForExportButton(inputGroup, inputPosition);
  1458. if (inputPosition.align === 'left') {
  1459. translateX = plotLeft;
  1460. }
  1461. else if (inputPosition.align === 'right') {
  1462. translateX = -Math.max(chart.axisOffset[1], -xOffsetForExportButton);
  1463. }
  1464. // Update the alignment to the updated spacing box
  1465. inputGroup.align({
  1466. y: inputPosition.y,
  1467. width: inputGroup.getBBox().width,
  1468. align: inputPosition.align,
  1469. // fix wrong getBBox() value on right align
  1470. x: inputPosition.x + translateX - 2
  1471. }, true, chart.spacingBox);
  1472. // Skip animation
  1473. inputGroup.placed = chart.hasLoaded;
  1474. }
  1475. this.handleCollision(xOffsetForExportButton);
  1476. // Vertical align
  1477. group.align({
  1478. verticalAlign: verticalAlign
  1479. }, true, chart.spacingBox);
  1480. var alignTranslateY = group.alignAttr.translateY;
  1481. // Set position
  1482. var groupHeight = group.getBBox().height + 20; // # 20 padding
  1483. var translateY = 0;
  1484. // Calculate bottom position
  1485. if (verticalAlign === 'bottom') {
  1486. var legendOptions = chart.legend && chart.legend.options;
  1487. var legendHeight = (legendOptions &&
  1488. legendOptions.verticalAlign === 'bottom' &&
  1489. legendOptions.enabled &&
  1490. !legendOptions.floating ?
  1491. (chart.legend.legendHeight +
  1492. pick(legendOptions.margin, 10)) :
  1493. 0);
  1494. groupHeight = groupHeight + legendHeight - 20;
  1495. translateY = (alignTranslateY -
  1496. groupHeight -
  1497. (options.floating ? 0 : options.y) -
  1498. (chart.titleOffset ? chart.titleOffset[2] : 0) -
  1499. 10 // 10 spacing
  1500. );
  1501. }
  1502. if (verticalAlign === 'top') {
  1503. if (options.floating) {
  1504. translateY = 0;
  1505. }
  1506. if (chart.titleOffset && chart.titleOffset[0]) {
  1507. translateY = chart.titleOffset[0];
  1508. }
  1509. translateY += ((chart.margin[0] - chart.spacing[0]) || 0);
  1510. }
  1511. else if (verticalAlign === 'middle') {
  1512. if (inputPosition.y === buttonPosition.y) {
  1513. translateY = alignTranslateY;
  1514. }
  1515. else if (inputPosition.y || buttonPosition.y) {
  1516. if (inputPosition.y < 0 ||
  1517. buttonPosition.y < 0) {
  1518. translateY -= Math.min(inputPosition.y, buttonPosition.y);
  1519. }
  1520. else {
  1521. translateY = alignTranslateY - groupHeight;
  1522. }
  1523. }
  1524. }
  1525. group.translate(options.x, options.y + Math.floor(translateY));
  1526. // Translate HTML inputs
  1527. var _b = this, minInput = _b.minInput, maxInput = _b.maxInput, dropdown = _b.dropdown;
  1528. if (options.inputEnabled && minInput && maxInput) {
  1529. minInput.style.marginTop = group.translateY + 'px';
  1530. maxInput.style.marginTop = group.translateY + 'px';
  1531. }
  1532. if (dropdown) {
  1533. dropdown.style.marginTop = group.translateY + 'px';
  1534. }
  1535. }
  1536. };
  1537. /**
  1538. * Align the button group horizontally and vertically.
  1539. *
  1540. * @private
  1541. * @function Highcharts.RangeSelector#alignButtonGroup
  1542. * @param {number} xOffsetForExportButton
  1543. * @param {number} [width]
  1544. * @return {void}
  1545. */
  1546. RangeSelector.prototype.alignButtonGroup = function (xOffsetForExportButton, width) {
  1547. var _a = this, chart = _a.chart, options = _a.options, buttonGroup = _a.buttonGroup, buttons = _a.buttons;
  1548. var buttonPosition = options.buttonPosition;
  1549. var plotLeft = chart.plotLeft - chart.spacing[3];
  1550. var translateX = buttonPosition.x - chart.spacing[3];
  1551. if (buttonPosition.align === 'right') {
  1552. translateX += xOffsetForExportButton - plotLeft; // #13014
  1553. }
  1554. else if (buttonPosition.align === 'center') {
  1555. translateX -= plotLeft / 2;
  1556. }
  1557. if (buttonGroup) {
  1558. // Align button group
  1559. buttonGroup.align({
  1560. y: buttonPosition.y,
  1561. width: pick(width, this.initialButtonGroupWidth),
  1562. align: buttonPosition.align,
  1563. x: translateX
  1564. }, true, chart.spacingBox);
  1565. }
  1566. };
  1567. /**
  1568. * @private
  1569. * @function Highcharts.RangeSelector#positionButtons
  1570. * @return {void}
  1571. */
  1572. RangeSelector.prototype.positionButtons = function () {
  1573. var _a = this, buttons = _a.buttons, chart = _a.chart, options = _a.options, zoomText = _a.zoomText;
  1574. var verb = chart.hasLoaded ? 'animate' : 'attr';
  1575. var buttonPosition = options.buttonPosition;
  1576. var plotLeft = chart.plotLeft;
  1577. var buttonLeft = plotLeft;
  1578. if (zoomText && zoomText.visibility !== 'hidden') {
  1579. // #8769, allow dynamically updating margins
  1580. zoomText[verb]({
  1581. x: pick(plotLeft + buttonPosition.x, plotLeft)
  1582. });
  1583. // Button start position
  1584. buttonLeft += buttonPosition.x +
  1585. zoomText.getBBox().width + 5;
  1586. }
  1587. this.buttonOptions.forEach(function (rangeOptions, i) {
  1588. if (buttons[i].visibility !== 'hidden') {
  1589. buttons[i][verb]({ x: buttonLeft });
  1590. // increase button position for the next button
  1591. buttonLeft += buttons[i].width + options.buttonSpacing;
  1592. }
  1593. else {
  1594. buttons[i][verb]({ x: plotLeft });
  1595. }
  1596. });
  1597. };
  1598. /**
  1599. * Handle collision between the button group and the input group
  1600. *
  1601. * @private
  1602. * @function Highcharts.RangeSelector#handleCollision
  1603. *
  1604. * @param {number} xOffsetForExportButton
  1605. * The X offset of the group required to make room for the
  1606. * exporting button
  1607. * @return {void}
  1608. */
  1609. RangeSelector.prototype.handleCollision = function (xOffsetForExportButton) {
  1610. var _this = this;
  1611. var _a = this, chart = _a.chart, buttonGroup = _a.buttonGroup, inputGroup = _a.inputGroup;
  1612. var _b = this.options, buttonPosition = _b.buttonPosition, dropdown = _b.dropdown, inputPosition = _b.inputPosition;
  1613. var maxButtonWidth = function () {
  1614. var buttonWidth = 0;
  1615. _this.buttons.forEach(function (button) {
  1616. var bBox = button.getBBox();
  1617. if (bBox.width > buttonWidth) {
  1618. buttonWidth = bBox.width;
  1619. }
  1620. });
  1621. return buttonWidth;
  1622. };
  1623. var groupsOverlap = function (buttonGroupWidth) {
  1624. if (inputGroup && buttonGroup) {
  1625. var inputGroupX = (inputGroup.alignAttr.translateX +
  1626. inputGroup.alignOptions.x -
  1627. xOffsetForExportButton +
  1628. // getBBox for detecing left margin
  1629. inputGroup.getBBox().x +
  1630. // 2px padding to not overlap input and label
  1631. 2);
  1632. var inputGroupWidth = inputGroup.alignOptions.width;
  1633. var buttonGroupX = buttonGroup.alignAttr.translateX +
  1634. buttonGroup.getBBox().x;
  1635. return (buttonGroupX + buttonGroupWidth > inputGroupX) &&
  1636. (inputGroupX + inputGroupWidth > buttonGroupX) &&
  1637. (buttonPosition.y <
  1638. (inputPosition.y +
  1639. inputGroup.getBBox().height));
  1640. }
  1641. return false;
  1642. };
  1643. var moveInputsDown = function () {
  1644. if (inputGroup && buttonGroup) {
  1645. inputGroup.attr({
  1646. translateX: inputGroup.alignAttr.translateX + (chart.axisOffset[1] >= -xOffsetForExportButton ?
  1647. 0 :
  1648. -xOffsetForExportButton),
  1649. translateY: inputGroup.alignAttr.translateY +
  1650. buttonGroup.getBBox().height + 10
  1651. });
  1652. }
  1653. };
  1654. if (buttonGroup) {
  1655. if (dropdown === 'always') {
  1656. this.collapseButtons(xOffsetForExportButton);
  1657. if (groupsOverlap(maxButtonWidth())) {
  1658. // Move the inputs down if there is still a collision
  1659. // after collapsing the buttons
  1660. moveInputsDown();
  1661. }
  1662. return;
  1663. }
  1664. if (dropdown === 'never') {
  1665. this.expandButtons();
  1666. }
  1667. }
  1668. // Detect collision
  1669. if (inputGroup && buttonGroup) {
  1670. if ((inputPosition.align === buttonPosition.align) ||
  1671. // 20 is minimal spacing between elements
  1672. groupsOverlap(this.initialButtonGroupWidth + 20)) {
  1673. if (dropdown === 'responsive') {
  1674. this.collapseButtons(xOffsetForExportButton);
  1675. if (groupsOverlap(maxButtonWidth())) {
  1676. moveInputsDown();
  1677. }
  1678. }
  1679. else {
  1680. moveInputsDown();
  1681. }
  1682. }
  1683. else if (dropdown === 'responsive') {
  1684. this.expandButtons();
  1685. }
  1686. }
  1687. else if (buttonGroup && dropdown === 'responsive') {
  1688. if (this.initialButtonGroupWidth > chart.plotWidth) {
  1689. this.collapseButtons(xOffsetForExportButton);
  1690. }
  1691. else {
  1692. this.expandButtons();
  1693. }
  1694. }
  1695. };
  1696. /**
  1697. * Collapse the buttons and put the select element on top.
  1698. *
  1699. * @private
  1700. * @function Highcharts.RangeSelector#collapseButtons
  1701. * @param {number} xOffsetForExportButton
  1702. * @return {void}
  1703. */
  1704. RangeSelector.prototype.collapseButtons = function (xOffsetForExportButton) {
  1705. var _a;
  1706. var _b = this, buttons = _b.buttons, buttonOptions = _b.buttonOptions, dropdown = _b.dropdown, options = _b.options, zoomText = _b.zoomText;
  1707. var getAttribs = function (text) { return ({
  1708. text: text ? text + " \u25BE" : '▾',
  1709. width: 'auto',
  1710. paddingLeft: 8,
  1711. paddingRight: 8
  1712. }); };
  1713. if (zoomText) {
  1714. zoomText.hide();
  1715. }
  1716. var hasActiveButton = false;
  1717. buttonOptions.forEach(function (rangeOptions, i) {
  1718. var button = buttons[i];
  1719. if (button.state !== 2) {
  1720. button.hide();
  1721. }
  1722. else {
  1723. button.show();
  1724. button.attr(getAttribs(rangeOptions.text));
  1725. hasActiveButton = true;
  1726. }
  1727. });
  1728. if (!hasActiveButton) {
  1729. if (dropdown) {
  1730. dropdown.selectedIndex = 0;
  1731. }
  1732. buttons[0].show();
  1733. buttons[0].attr(getAttribs((_a = this.zoomText) === null || _a === void 0 ? void 0 : _a.textStr));
  1734. }
  1735. var align = options.buttonPosition.align;
  1736. this.positionButtons();
  1737. if (align === 'right' || align === 'center') {
  1738. this.alignButtonGroup(xOffsetForExportButton, buttons[this.currentButtonIndex()].getBBox().width);
  1739. }
  1740. this.showDropdown();
  1741. };
  1742. /**
  1743. * Show all the buttons and hide the select element.
  1744. *
  1745. * @private
  1746. * @function Highcharts.RangeSelector#expandButtons
  1747. * @return {void}
  1748. */
  1749. RangeSelector.prototype.expandButtons = function () {
  1750. var _a = this, buttons = _a.buttons, buttonOptions = _a.buttonOptions, options = _a.options, zoomText = _a.zoomText;
  1751. this.hideDropdown();
  1752. if (zoomText) {
  1753. zoomText.show();
  1754. }
  1755. buttonOptions.forEach(function (rangeOptions, i) {
  1756. var button = buttons[i];
  1757. button.show();
  1758. button.attr({
  1759. text: rangeOptions.text,
  1760. width: options.buttonTheme.width || 28,
  1761. paddingLeft: 'unset',
  1762. paddingRight: 'unset'
  1763. });
  1764. if (button.state < 2) {
  1765. button.setState(0);
  1766. }
  1767. });
  1768. this.positionButtons();
  1769. };
  1770. /**
  1771. * Get the index of the visible button when the buttons are collapsed.
  1772. *
  1773. * @private
  1774. * @function Highcharts.RangeSelector#currentButtonIndex
  1775. * @return {number}
  1776. */
  1777. RangeSelector.prototype.currentButtonIndex = function () {
  1778. var dropdown = this.dropdown;
  1779. if (dropdown && dropdown.selectedIndex > 0) {
  1780. return dropdown.selectedIndex - 1;
  1781. }
  1782. return 0;
  1783. };
  1784. /**
  1785. * Position the select element on top of the button.
  1786. *
  1787. * @private
  1788. * @function Highcharts.RangeSelector#showDropdown
  1789. * @return {void}
  1790. */
  1791. RangeSelector.prototype.showDropdown = function () {
  1792. var _a = this, buttonGroup = _a.buttonGroup, buttons = _a.buttons, chart = _a.chart, dropdown = _a.dropdown;
  1793. if (buttonGroup && dropdown) {
  1794. var translateX = buttonGroup.translateX, translateY = buttonGroup.translateY;
  1795. var bBox = buttons[this.currentButtonIndex()].getBBox();
  1796. css(dropdown, {
  1797. left: (chart.plotLeft + translateX) + 'px',
  1798. top: (translateY + 0.5) + 'px',
  1799. width: bBox.width + 'px',
  1800. height: bBox.height + 'px'
  1801. });
  1802. this.hasVisibleDropdown = true;
  1803. }
  1804. };
  1805. /**
  1806. * @private
  1807. * @function Highcharts.RangeSelector#hideDropdown
  1808. * @return {void}
  1809. */
  1810. RangeSelector.prototype.hideDropdown = function () {
  1811. var dropdown = this.dropdown;
  1812. if (dropdown) {
  1813. css(dropdown, {
  1814. top: '-9999em',
  1815. width: '1px',
  1816. height: '1px'
  1817. });
  1818. this.hasVisibleDropdown = false;
  1819. }
  1820. };
  1821. /**
  1822. * Extracts height of range selector
  1823. *
  1824. * @private
  1825. * @function Highcharts.RangeSelector#getHeight
  1826. * @return {number}
  1827. * Returns rangeSelector height
  1828. */
  1829. RangeSelector.prototype.getHeight = function () {
  1830. 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;
  1831. if (options.height) {
  1832. return options.height;
  1833. }
  1834. // Align the elements before we read the height in case we're switching
  1835. // between wrapped and non-wrapped layout
  1836. this.alignElements();
  1837. rangeSelectorHeight = rangeSelectorGroup ?
  1838. // 13px to keep back compatibility
  1839. (rangeSelectorGroup.getBBox(true).height) + 13 +
  1840. yPosition :
  1841. 0;
  1842. minPosition = Math.min(inputPositionY, buttonPositionY);
  1843. if ((inputPositionY < 0 && buttonPositionY < 0) ||
  1844. (inputPositionY > 0 && buttonPositionY > 0)) {
  1845. rangeSelectorHeight += Math.abs(minPosition);
  1846. }
  1847. return rangeSelectorHeight;
  1848. };
  1849. /**
  1850. * Detect collision with title or subtitle
  1851. *
  1852. * @private
  1853. * @function Highcharts.RangeSelector#titleCollision
  1854. *
  1855. * @param {Highcharts.Chart} chart
  1856. *
  1857. * @return {boolean}
  1858. * Returns collision status
  1859. */
  1860. RangeSelector.prototype.titleCollision = function (chart) {
  1861. return !(chart.options.title.text ||
  1862. chart.options.subtitle.text);
  1863. };
  1864. /**
  1865. * Update the range selector with new options
  1866. *
  1867. * @private
  1868. * @function Highcharts.RangeSelector#update
  1869. * @param {Highcharts.RangeSelectorOptions} options
  1870. * @return {void}
  1871. */
  1872. RangeSelector.prototype.update = function (options) {
  1873. var chart = this.chart;
  1874. merge(true, chart.options.rangeSelector, options);
  1875. this.destroy();
  1876. this.init(chart);
  1877. this.render();
  1878. };
  1879. /**
  1880. * Destroys allocated elements.
  1881. *
  1882. * @private
  1883. * @function Highcharts.RangeSelector#destroy
  1884. */
  1885. RangeSelector.prototype.destroy = function () {
  1886. var rSelector = this, minInput = rSelector.minInput, maxInput = rSelector.maxInput;
  1887. if (rSelector.eventsToUnbind) {
  1888. rSelector.eventsToUnbind.forEach(function (unbind) { return unbind(); });
  1889. rSelector.eventsToUnbind = void 0;
  1890. }
  1891. // Destroy elements in collections
  1892. destroyObjectProperties(rSelector.buttons);
  1893. // Clear input element events
  1894. if (minInput) {
  1895. minInput.onfocus = minInput.onblur = minInput.onchange = null;
  1896. }
  1897. if (maxInput) {
  1898. maxInput.onfocus = maxInput.onblur = maxInput.onchange = null;
  1899. }
  1900. // Destroy HTML and SVG elements
  1901. objectEach(rSelector, function (val, key) {
  1902. if (val && key !== 'chart') {
  1903. if (val instanceof SVGElement) {
  1904. // SVGElement
  1905. val.destroy();
  1906. }
  1907. else if (val instanceof window.HTMLElement) {
  1908. // HTML element
  1909. discardElement(val);
  1910. }
  1911. }
  1912. if (val !== RangeSelector.prototype[key]) {
  1913. rSelector[key] = null;
  1914. }
  1915. }, this);
  1916. };
  1917. return RangeSelector;
  1918. }());
  1919. /**
  1920. * The default buttons for pre-selecting time frames
  1921. */
  1922. RangeSelector.prototype.defaultButtons = [{
  1923. type: 'month',
  1924. count: 1,
  1925. text: '1m',
  1926. title: 'View 1 month'
  1927. }, {
  1928. type: 'month',
  1929. count: 3,
  1930. text: '3m',
  1931. title: 'View 3 months'
  1932. }, {
  1933. type: 'month',
  1934. count: 6,
  1935. text: '6m',
  1936. title: 'View 6 months'
  1937. }, {
  1938. type: 'ytd',
  1939. text: 'YTD',
  1940. title: 'View year to date'
  1941. }, {
  1942. type: 'year',
  1943. count: 1,
  1944. text: '1y',
  1945. title: 'View 1 year'
  1946. }, {
  1947. type: 'all',
  1948. text: 'All',
  1949. title: 'View all'
  1950. }];
  1951. /**
  1952. * The date formats to use when setting min, max and value on date inputs
  1953. */
  1954. RangeSelector.prototype.inputTypeFormats = {
  1955. 'datetime-local': '%Y-%m-%dT%H:%M:%S',
  1956. 'date': '%Y-%m-%d',
  1957. 'time': '%H:%M:%S'
  1958. };
  1959. /**
  1960. * Get the preferred input type based on a date format string.
  1961. *
  1962. * @private
  1963. * @function preferredInputType
  1964. * @param {string} format
  1965. * @return {string}
  1966. */
  1967. function preferredInputType(format) {
  1968. var ms = format.indexOf('%L') !== -1;
  1969. if (ms) {
  1970. return 'text';
  1971. }
  1972. var date = ['a', 'A', 'd', 'e', 'w', 'b', 'B', 'm', 'o', 'y', 'Y'].some(function (char) {
  1973. return format.indexOf('%' + char) !== -1;
  1974. });
  1975. var time = ['H', 'k', 'I', 'l', 'M', 'S'].some(function (char) {
  1976. return format.indexOf('%' + char) !== -1;
  1977. });
  1978. if (date && time) {
  1979. return 'datetime-local';
  1980. }
  1981. if (date) {
  1982. return 'date';
  1983. }
  1984. if (time) {
  1985. return 'time';
  1986. }
  1987. return 'text';
  1988. }
  1989. /**
  1990. * Get the axis min value based on the range option and the current max. For
  1991. * stock charts this is extended via the {@link RangeSelector} so that if the
  1992. * selected range is a multiple of months or years, it is compensated for
  1993. * various month lengths.
  1994. *
  1995. * @private
  1996. * @function Highcharts.Axis#minFromRange
  1997. * @return {number|undefined}
  1998. * The new minimum value.
  1999. */
  2000. Axis.prototype.minFromRange = function () {
  2001. var rangeOptions = this.range, type = rangeOptions.type, min, max = this.max, dataMin, range, time = this.chart.time,
  2002. // Get the true range from a start date
  2003. getTrueRange = function (base, count) {
  2004. var timeName = type === 'year' ? 'FullYear' : 'Month';
  2005. var date = new time.Date(base);
  2006. var basePeriod = time.get(timeName, date);
  2007. time.set(timeName, date, basePeriod + count);
  2008. if (basePeriod === time.get(timeName, date)) {
  2009. time.set('Date', date, 0); // #6537
  2010. }
  2011. return date.getTime() - base;
  2012. };
  2013. if (isNumber(rangeOptions)) {
  2014. min = max - rangeOptions;
  2015. range = rangeOptions;
  2016. }
  2017. else {
  2018. min = max + getTrueRange(max, -rangeOptions.count);
  2019. // Let the fixedRange reflect initial settings (#5930)
  2020. if (this.chart) {
  2021. this.chart.fixedRange = max - min;
  2022. }
  2023. }
  2024. dataMin = pick(this.dataMin, Number.MIN_VALUE);
  2025. if (!isNumber(min)) {
  2026. min = dataMin;
  2027. }
  2028. if (min <= dataMin) {
  2029. min = dataMin;
  2030. if (typeof range === 'undefined') { // #4501
  2031. range = getTrueRange(min, rangeOptions.count);
  2032. }
  2033. this.newMax = Math.min(min + range, this.dataMax);
  2034. }
  2035. if (!isNumber(max)) {
  2036. min = void 0;
  2037. }
  2038. return min;
  2039. };
  2040. if (!H.RangeSelector) {
  2041. var chartDestroyEvents_1 = [];
  2042. var initRangeSelector_1 = function (chart) {
  2043. var extremes, rangeSelector = chart.rangeSelector, legend, alignTo, verticalAlign;
  2044. /**
  2045. * @private
  2046. */
  2047. function render() {
  2048. if (rangeSelector) {
  2049. extremes = chart.xAxis[0].getExtremes();
  2050. legend = chart.legend;
  2051. verticalAlign = rangeSelector === null || rangeSelector === void 0 ? void 0 : rangeSelector.options.verticalAlign;
  2052. if (isNumber(extremes.min)) {
  2053. rangeSelector.render(extremes.min, extremes.max);
  2054. }
  2055. // Re-align the legend so that it's below the rangeselector
  2056. if (legend.display &&
  2057. verticalAlign === 'top' &&
  2058. verticalAlign === legend.options.verticalAlign) {
  2059. // Create a new alignment box for the legend.
  2060. alignTo = merge(chart.spacingBox);
  2061. if (legend.options.layout === 'vertical') {
  2062. alignTo.y = chart.plotTop;
  2063. }
  2064. else {
  2065. alignTo.y += rangeSelector.getHeight();
  2066. }
  2067. legend.group.placed = false; // Don't animate the alignment.
  2068. legend.align(alignTo);
  2069. }
  2070. }
  2071. }
  2072. if (rangeSelector) {
  2073. var events = find(chartDestroyEvents_1, function (e) { return e[0] === chart; });
  2074. if (!events) {
  2075. chartDestroyEvents_1.push([chart, [
  2076. // redraw the scroller on setExtremes
  2077. addEvent(chart.xAxis[0], 'afterSetExtremes', function (e) {
  2078. if (rangeSelector) {
  2079. rangeSelector.render(e.min, e.max);
  2080. }
  2081. }),
  2082. // redraw the scroller chart resize
  2083. addEvent(chart, 'redraw', render)
  2084. ]]);
  2085. }
  2086. // do it now
  2087. render();
  2088. }
  2089. };
  2090. // Initialize rangeselector for stock charts
  2091. addEvent(Chart, 'afterGetContainer', function () {
  2092. var _a;
  2093. if ((_a = this.options.rangeSelector) === null || _a === void 0 ? void 0 : _a.enabled) {
  2094. this.rangeSelector = new RangeSelector(this);
  2095. }
  2096. });
  2097. addEvent(Chart, 'beforeRender', function () {
  2098. var chart = this, axes = chart.axes, rangeSelector = chart.rangeSelector, verticalAlign;
  2099. if (rangeSelector) {
  2100. if (isNumber(rangeSelector.deferredYTDClick)) {
  2101. rangeSelector.clickButton(rangeSelector.deferredYTDClick);
  2102. delete rangeSelector.deferredYTDClick;
  2103. }
  2104. axes.forEach(function (axis) {
  2105. axis.updateNames();
  2106. axis.setScale();
  2107. });
  2108. chart.getAxisMargins();
  2109. rangeSelector.render();
  2110. verticalAlign = rangeSelector.options.verticalAlign;
  2111. if (!rangeSelector.options.floating) {
  2112. if (verticalAlign === 'bottom') {
  2113. this.extraBottomMargin = true;
  2114. }
  2115. else if (verticalAlign !== 'middle') {
  2116. this.extraTopMargin = true;
  2117. }
  2118. }
  2119. }
  2120. });
  2121. addEvent(Chart, 'update', function (e) {
  2122. var chart = this, options = e.options, optionsRangeSelector = options.rangeSelector, rangeSelector = chart.rangeSelector, verticalAlign, extraBottomMarginWas = this.extraBottomMargin, extraTopMarginWas = this.extraTopMargin;
  2123. if (optionsRangeSelector &&
  2124. optionsRangeSelector.enabled &&
  2125. !defined(rangeSelector) &&
  2126. this.options.rangeSelector) {
  2127. this.options.rangeSelector.enabled = true;
  2128. this.rangeSelector = rangeSelector = new RangeSelector(this);
  2129. }
  2130. this.extraBottomMargin = false;
  2131. this.extraTopMargin = false;
  2132. if (rangeSelector) {
  2133. initRangeSelector_1(this);
  2134. verticalAlign = (optionsRangeSelector &&
  2135. optionsRangeSelector.verticalAlign) || (rangeSelector.options && rangeSelector.options.verticalAlign);
  2136. if (!rangeSelector.options.floating) {
  2137. if (verticalAlign === 'bottom') {
  2138. this.extraBottomMargin = true;
  2139. }
  2140. else if (verticalAlign !== 'middle') {
  2141. this.extraTopMargin = true;
  2142. }
  2143. }
  2144. if (this.extraBottomMargin !== extraBottomMarginWas ||
  2145. this.extraTopMargin !== extraTopMarginWas) {
  2146. this.isDirtyBox = true;
  2147. }
  2148. }
  2149. });
  2150. addEvent(Chart, 'render', function () {
  2151. var chart = this, rangeSelector = chart.rangeSelector, verticalAlign;
  2152. if (rangeSelector && !rangeSelector.options.floating) {
  2153. rangeSelector.render();
  2154. verticalAlign = rangeSelector.options.verticalAlign;
  2155. if (verticalAlign === 'bottom') {
  2156. this.extraBottomMargin = true;
  2157. }
  2158. else if (verticalAlign !== 'middle') {
  2159. this.extraTopMargin = true;
  2160. }
  2161. }
  2162. });
  2163. addEvent(Chart, 'getMargins', function () {
  2164. var rangeSelector = this.rangeSelector, rangeSelectorHeight;
  2165. if (rangeSelector) {
  2166. rangeSelectorHeight = rangeSelector.getHeight();
  2167. if (this.extraTopMargin) {
  2168. this.plotTop += rangeSelectorHeight;
  2169. }
  2170. if (this.extraBottomMargin) {
  2171. this.marginBottom += rangeSelectorHeight;
  2172. }
  2173. }
  2174. });
  2175. Chart.prototype.callbacks.push(initRangeSelector_1);
  2176. // Remove resize/afterSetExtremes at chart destroy
  2177. addEvent(Chart, 'destroy', function destroyEvents() {
  2178. for (var i = 0; i < chartDestroyEvents_1.length; i++) {
  2179. var events = chartDestroyEvents_1[i];
  2180. if (events[0] === this) {
  2181. events[1].forEach(function (unbind) { return unbind(); });
  2182. chartDestroyEvents_1.splice(i, 1);
  2183. return;
  2184. }
  2185. }
  2186. });
  2187. H.RangeSelector = RangeSelector;
  2188. }
  2189. export default H.RangeSelector;