Navigator.js 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482
  1. /**
  2. * (c) 2010-2019 Torstein Honsi
  3. *
  4. * License: www.highcharts.com/license
  5. */
  6. 'use strict';
  7. import H from './Globals.js';
  8. import './Utilities.js';
  9. import './Color.js';
  10. import './Axis.js';
  11. import './Chart.js';
  12. import './Series.js';
  13. import './Options.js';
  14. import './Scrollbar.js';
  15. var addEvent = H.addEvent,
  16. Axis = H.Axis,
  17. Chart = H.Chart,
  18. color = H.color,
  19. defaultDataGroupingUnits = H.defaultDataGroupingUnits,
  20. defaultOptions = H.defaultOptions,
  21. defined = H.defined,
  22. destroyObjectProperties = H.destroyObjectProperties,
  23. erase = H.erase,
  24. extend = H.extend,
  25. hasTouch = H.hasTouch,
  26. isArray = H.isArray,
  27. isNumber = H.isNumber,
  28. isTouchDevice = H.isTouchDevice,
  29. merge = H.merge,
  30. pick = H.pick,
  31. removeEvent = H.removeEvent,
  32. Scrollbar = H.Scrollbar,
  33. Series = H.Series,
  34. seriesTypes = H.seriesTypes,
  35. units = [].concat(defaultDataGroupingUnits), // copy
  36. defaultSeriesType,
  37. // Finding the min or max of a set of variables where we don't know if they
  38. // are defined, is a pattern that is repeated several places in Highcharts.
  39. // Consider making this a global utility method.
  40. numExt = function (extreme) {
  41. var numbers = [].filter.call(arguments, isNumber);
  42. if (numbers.length) {
  43. return Math[extreme].apply(0, numbers);
  44. }
  45. };
  46. // add more resolution to units
  47. units[4] = ['day', [1, 2, 3, 4]]; // allow more days
  48. units[5] = ['week', [1, 2, 3]]; // allow more weeks
  49. defaultSeriesType = seriesTypes.areaspline === undefined ?
  50. 'line' :
  51. 'areaspline';
  52. extend(defaultOptions, {
  53. /**
  54. * Maximum range which can be set using the navigator's handles.
  55. * Opposite of [xAxis.minRange](#xAxis.minRange).
  56. *
  57. * @sample {highstock} stock/navigator/maxrange/
  58. * Defined max and min range
  59. *
  60. * @type {number}
  61. * @since 6.0.0
  62. * @product highstock
  63. * @apioption xAxis.maxRange
  64. */
  65. /**
  66. * The navigator is a small series below the main series, displaying
  67. * a view of the entire data set. It provides tools to zoom in and
  68. * out on parts of the data as well as panning across the dataset.
  69. *
  70. * @product highstock
  71. * @optionparent navigator
  72. */
  73. navigator: {
  74. /**
  75. * Whether the navigator and scrollbar should adapt to updated data
  76. * in the base X axis. When loading data async, as in the demo below,
  77. * this should be `false`. Otherwise new data will trigger navigator
  78. * redraw, which will cause unwanted looping. In the demo below, the
  79. * data in the navigator is set only once. On navigating, only the main
  80. * chart content is updated.
  81. *
  82. * @sample {highstock} stock/demo/lazy-loading/
  83. * Set to false with async data loading
  84. *
  85. * @type {boolean}
  86. * @default true
  87. * @product highstock
  88. * @apioption navigator.adaptToUpdatedData
  89. */
  90. /**
  91. * An integer identifying the index to use for the base series, or a
  92. * string representing the id of the series.
  93. *
  94. * **Note**: As of Highcharts 5.0, this is now a deprecated option.
  95. * Prefer [series.showInNavigator](#plotOptions.series.showInNavigator).
  96. *
  97. * @see [series.showInNavigator](#plotOptions.series.showInNavigator)
  98. *
  99. * @deprecated
  100. * @type {*}
  101. * @default 0
  102. * @product highstock
  103. * @apioption navigator.baseSeries
  104. */
  105. /**
  106. * Enable or disable the navigator.
  107. *
  108. * @sample {highstock} stock/navigator/enabled/
  109. * Disable the navigator
  110. *
  111. * @type {boolean}
  112. * @default true
  113. * @product highstock
  114. * @apioption navigator.enabled
  115. */
  116. /**
  117. * When the chart is inverted, whether to draw the navigator on the
  118. * opposite side.
  119. *
  120. * @type {boolean}
  121. * @default false
  122. * @since 5.0.8
  123. * @product highstock
  124. * @apioption navigator.opposite
  125. */
  126. /**
  127. * The height of the navigator.
  128. *
  129. * @sample {highstock} stock/navigator/height/
  130. * A higher navigator
  131. *
  132. * @product highstock
  133. */
  134. height: 40,
  135. /**
  136. * The distance from the nearest element, the X axis or X axis labels.
  137. *
  138. * @sample {highstock} stock/navigator/margin/
  139. * A margin of 2 draws the navigator closer to the X axis labels
  140. *
  141. * @product highstock
  142. */
  143. margin: 25,
  144. /**
  145. * Whether the mask should be inside the range marking the zoomed
  146. * range, or outside. In Highstock 1.x it was always `false`.
  147. *
  148. * @sample {highstock} stock/navigator/maskinside-false/
  149. * False, mask outside
  150. *
  151. * @since 2.0
  152. * @product highstock
  153. */
  154. maskInside: true,
  155. /**
  156. * Options for the handles for dragging the zoomed area.
  157. *
  158. * @sample {highstock} stock/navigator/handles/
  159. * Colored handles
  160. *
  161. * @product highstock
  162. */
  163. handles: {
  164. /**
  165. * Width for handles.
  166. *
  167. * @sample {highstock} stock/navigator/styled-handles/
  168. * Styled handles
  169. *
  170. * @since 6.0.0
  171. * @product highstock
  172. */
  173. width: 7,
  174. /**
  175. * Height for handles.
  176. *
  177. * @sample {highstock} stock/navigator/styled-handles/
  178. * Styled handles
  179. *
  180. * @since 6.0.0
  181. * @product highstock
  182. */
  183. height: 15,
  184. /**
  185. * Array to define shapes of handles. 0-index for left, 1-index for
  186. * right.
  187. *
  188. * Additionally, the URL to a graphic can be given on this form:
  189. * `url(graphic.png)`. Note that for the image to be applied to
  190. * exported charts, its URL needs to be accessible by the export
  191. * server.
  192. *
  193. * Custom callbacks for symbol path generation can also be added to
  194. * `Highcharts.SVGRenderer.prototype.symbols`. The callback is then
  195. * used by its method name, as shown in the demo.
  196. *
  197. * @sample {highstock} stock/navigator/styled-handles/
  198. * Styled handles
  199. *
  200. * @type {Array<string>}
  201. * @default ["navigator-handle", "navigator-handle"]
  202. * @since 6.0.0
  203. * @product highstock
  204. */
  205. symbols: ['navigator-handle', 'navigator-handle'],
  206. /**
  207. * Allows to enable/disable handles.
  208. *
  209. * @since 6.0.0
  210. * @product highstock
  211. */
  212. enabled: true,
  213. /**
  214. * The width for the handle border and the stripes inside.
  215. *
  216. * @sample {highstock} stock/navigator/styled-handles/
  217. * Styled handles
  218. *
  219. * @since 6.0.0
  220. * @product highstock
  221. * @apioption navigator.handles.lineWidth
  222. */
  223. lineWidth: 1,
  224. /**
  225. * The fill for the handle.
  226. *
  227. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  228. * @product highstock
  229. */
  230. backgroundColor: '#f2f2f2',
  231. /**
  232. * The stroke for the handle border and the stripes inside.
  233. *
  234. * @type {Highcharts.ColorString}
  235. * @product highstock
  236. */
  237. borderColor: '#999999'
  238. },
  239. /**
  240. * The color of the mask covering the areas of the navigator series
  241. * that are currently not visible in the main series. The default
  242. * color is bluish with an opacity of 0.3 to see the series below.
  243. *
  244. * @see In styled mode, the mask is styled with the
  245. * `.highcharts-navigator-mask` and
  246. * `.highcharts-navigator-mask-inside` classes.
  247. *
  248. * @sample {highstock} stock/navigator/maskfill/
  249. * Blue, semi transparent mask
  250. *
  251. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  252. * @default rgba(102,133,194,0.3)
  253. * @product highstock
  254. */
  255. maskFill: color('#6685c2').setOpacity(0.3).get(),
  256. /**
  257. * The color of the line marking the currently zoomed area in the
  258. * navigator.
  259. *
  260. * @sample {highstock} stock/navigator/outline/
  261. * 2px blue outline
  262. *
  263. * @type {Highcharts.ColorString}
  264. * @default #cccccc
  265. * @product highstock
  266. */
  267. outlineColor: '#cccccc',
  268. /**
  269. * The width of the line marking the currently zoomed area in the
  270. * navigator.
  271. *
  272. * @see In styled mode, the outline stroke width is set with the
  273. * `.highcharts-navigator-outline` class.
  274. *
  275. * @sample {highstock} stock/navigator/outline/
  276. * 2px blue outline
  277. *
  278. * @type {number}
  279. * @product highstock
  280. */
  281. outlineWidth: 1,
  282. /**
  283. * Options for the navigator series. Available options are the same
  284. * as any series, documented at [plotOptions](#plotOptions.series)
  285. * and [series](#series).
  286. *
  287. * Unless data is explicitly defined on navigator.series, the data
  288. * is borrowed from the first series in the chart.
  289. *
  290. * Default series options for the navigator series are:
  291. *
  292. * <pre>series: {
  293. * type: 'areaspline',
  294. * fillOpacity: 0.05,
  295. * dataGrouping: {
  296. * smoothed: true
  297. * },
  298. * lineWidth: 1,
  299. * marker: {
  300. * enabled: false
  301. * }
  302. * }</pre>
  303. *
  304. * @see In styled mode, the navigator series is styled with the
  305. * `.highcharts-navigator-series` class.
  306. *
  307. * @sample {highstock} stock/navigator/series-data/
  308. * Using a separate data set for the navigator
  309. * @sample {highstock} stock/navigator/series/
  310. * A green navigator series
  311. *
  312. * @product highstock
  313. */
  314. series: {
  315. /**
  316. * The type of the navigator series. Defaults to `areaspline` if
  317. * defined, otherwise `line`.
  318. *
  319. * @type {string}
  320. * @default areaspline
  321. */
  322. type: defaultSeriesType,
  323. /**
  324. * The fill opacity of the navigator series.
  325. */
  326. fillOpacity: 0.05,
  327. /**
  328. * The pixel line width of the navigator series.
  329. */
  330. lineWidth: 1,
  331. /**
  332. * @ignore-option
  333. */
  334. compare: null,
  335. /**
  336. * Data grouping options for the navigator series.
  337. *
  338. * @extends plotOptions.series.dataGrouping
  339. */
  340. dataGrouping: {
  341. approximation: 'average',
  342. enabled: true,
  343. groupPixelWidth: 2,
  344. smoothed: true,
  345. units: units
  346. },
  347. /**
  348. * Data label options for the navigator series. Data labels are
  349. * disabled by default on the navigator series.
  350. *
  351. * @extends plotOptions.series.dataLabels
  352. */
  353. dataLabels: {
  354. enabled: false,
  355. zIndex: 2 // #1839
  356. },
  357. id: 'highcharts-navigator-series',
  358. className: 'highcharts-navigator-series',
  359. /**
  360. * Line color for the navigator series. Allows setting the color
  361. * while disallowing the default candlestick setting.
  362. *
  363. * @type {Highcharts.ColorString|null}
  364. */
  365. lineColor: null, // #4602
  366. marker: {
  367. enabled: false
  368. },
  369. pointRange: 0,
  370. /**
  371. * The threshold option. Setting it to 0 will make the default
  372. * navigator area series draw its area from the 0 value and up.
  373. *
  374. * @type {number|null}
  375. */
  376. threshold: null
  377. },
  378. /**
  379. * Options for the navigator X axis. Default series options
  380. * for the navigator xAxis are:
  381. *
  382. * <pre>xAxis: {
  383. * tickWidth: 0,
  384. * lineWidth: 0,
  385. * gridLineWidth: 1,
  386. * tickPixelInterval: 200,
  387. * labels: {
  388. * align: 'left',
  389. * style: {
  390. * color: '#888'
  391. * },
  392. * x: 3,
  393. * y: -4
  394. * }
  395. * }</pre>
  396. *
  397. * @extends xAxis
  398. * @excluding linkedTo, maxZoom, minRange, opposite, range, scrollbar,
  399. * showEmpty, maxRange
  400. * @product highstock
  401. */
  402. xAxis: {
  403. /**
  404. * Additional range on the right side of the xAxis. Works similar to
  405. * xAxis.maxPadding, but value is set in milliseconds.
  406. * Can be set for both, main xAxis and navigator's xAxis.
  407. *
  408. * @since 6.0.0
  409. * @product highstock
  410. */
  411. overscroll: 0,
  412. className: 'highcharts-navigator-xaxis',
  413. tickLength: 0,
  414. lineWidth: 0,
  415. gridLineColor: '#e6e6e6',
  416. gridLineWidth: 1,
  417. tickPixelInterval: 200,
  418. labels: {
  419. align: 'left',
  420. /**
  421. * @type {Highcharts.CSSObject}
  422. */
  423. style: {
  424. /** @ignore */
  425. color: '#999999'
  426. },
  427. x: 3,
  428. y: -4
  429. },
  430. crosshair: false
  431. },
  432. /**
  433. * Options for the navigator Y axis. Default series options
  434. * for the navigator yAxis are:
  435. *
  436. * <pre>yAxis: {
  437. * gridLineWidth: 0,
  438. * startOnTick: false,
  439. * endOnTick: false,
  440. * minPadding: 0.1,
  441. * maxPadding: 0.1,
  442. * labels: {
  443. * enabled: false
  444. * },
  445. * title: {
  446. * text: null
  447. * },
  448. * tickWidth: 0
  449. * }</pre>
  450. *
  451. * @extends yAxis
  452. * @excluding height, linkedTo, maxZoom, minRange, ordinal, range,
  453. * showEmpty, scrollbar, top, units, maxRange, minLength,
  454. * maxLength, resize
  455. * @product highstock
  456. */
  457. yAxis: {
  458. className: 'highcharts-navigator-yaxis',
  459. gridLineWidth: 0,
  460. startOnTick: false,
  461. endOnTick: false,
  462. minPadding: 0.1,
  463. maxPadding: 0.1,
  464. labels: {
  465. enabled: false
  466. },
  467. crosshair: false,
  468. title: {
  469. text: null
  470. },
  471. tickLength: 0,
  472. tickWidth: 0
  473. }
  474. }
  475. });
  476. /**
  477. * Draw one of the handles on the side of the zoomed range in the navigator
  478. *
  479. * @function Highcharts.Renderer#symbols.navigator-handle
  480. *
  481. * @param {boolean} inverted
  482. * flag for chart.inverted
  483. *
  484. * @return {Highcharts.SVGPathArray}
  485. * Path to be used in a handle
  486. */
  487. H.Renderer.prototype.symbols['navigator-handle'] = function (
  488. x,
  489. y,
  490. w,
  491. h,
  492. options
  493. ) {
  494. var halfWidth = options.width / 2,
  495. markerPosition = Math.round(halfWidth / 3) + 0.5,
  496. height = options.height;
  497. return [
  498. 'M',
  499. -halfWidth - 1, 0.5,
  500. 'L',
  501. halfWidth, 0.5,
  502. 'L',
  503. halfWidth, height + 0.5,
  504. 'L',
  505. -halfWidth - 1, height + 0.5,
  506. 'L',
  507. -halfWidth - 1, 0.5,
  508. 'M',
  509. -markerPosition, 4,
  510. 'L',
  511. -markerPosition, height - 3,
  512. 'M',
  513. markerPosition - 1, 4,
  514. 'L',
  515. markerPosition - 1, height - 3
  516. ];
  517. };
  518. /**
  519. * The Navigator class
  520. *
  521. * @private
  522. * @class
  523. * @name Highcharts.Navigator
  524. *
  525. * @param {Highcharts.Chart} chart
  526. * Chart object
  527. */
  528. function Navigator(chart) {
  529. this.init(chart);
  530. }
  531. Navigator.prototype = {
  532. /**
  533. * Draw one of the handles on the side of the zoomed range in the navigator
  534. *
  535. * @private
  536. * @function Highcharts.Navigator#drawHandle
  537. *
  538. * @param {number} x
  539. * The x center for the handle
  540. *
  541. * @param {number} index
  542. * 0 for left and 1 for right
  543. *
  544. * @param {boolean} inverted
  545. * flag for chart.inverted
  546. *
  547. * @param {string} verb
  548. * use 'animate' or 'attr'
  549. */
  550. drawHandle: function (x, index, inverted, verb) {
  551. var navigator = this,
  552. height = navigator.navigatorOptions.handles.height;
  553. // Place it
  554. navigator.handles[index][verb](inverted ? {
  555. translateX: Math.round(navigator.left + navigator.height / 2),
  556. translateY: Math.round(
  557. navigator.top + parseInt(x, 10) + 0.5 - height
  558. )
  559. } : {
  560. translateX: Math.round(navigator.left + parseInt(x, 10)),
  561. translateY: Math.round(
  562. navigator.top + navigator.height / 2 - height / 2 - 1
  563. )
  564. });
  565. },
  566. /**
  567. * Render outline around the zoomed range
  568. *
  569. * @private
  570. * @function Highcharts.Navigator#drawOutline
  571. *
  572. * @param {number} zoomedMin
  573. * in pixels position where zoomed range starts
  574. *
  575. * @param {number} zoomedMax
  576. * in pixels position where zoomed range ends
  577. *
  578. * @param {boolean} inverted
  579. * flag if chart is inverted
  580. *
  581. * @param {string} verb
  582. * use 'animate' or 'attr'
  583. */
  584. drawOutline: function (zoomedMin, zoomedMax, inverted, verb) {
  585. var navigator = this,
  586. maskInside = navigator.navigatorOptions.maskInside,
  587. outlineWidth = navigator.outline.strokeWidth(),
  588. halfOutline = outlineWidth / 2,
  589. outlineCorrection = (outlineWidth % 2) / 2, // #5800
  590. outlineHeight = navigator.outlineHeight,
  591. scrollbarHeight = navigator.scrollbarHeight,
  592. navigatorSize = navigator.size,
  593. left = navigator.left - scrollbarHeight,
  594. navigatorTop = navigator.top,
  595. verticalMin,
  596. path;
  597. if (inverted) {
  598. left -= halfOutline;
  599. verticalMin = navigatorTop + zoomedMax + outlineCorrection;
  600. zoomedMax = navigatorTop + zoomedMin + outlineCorrection;
  601. path = [
  602. 'M',
  603. left + outlineHeight,
  604. navigatorTop - scrollbarHeight - outlineCorrection, // top edge
  605. 'L',
  606. left + outlineHeight,
  607. verticalMin, // top right of zoomed range
  608. 'L',
  609. left,
  610. verticalMin, // top left of z.r.
  611. 'L',
  612. left,
  613. zoomedMax, // bottom left of z.r.
  614. 'L',
  615. left + outlineHeight,
  616. zoomedMax, // bottom right of z.r.
  617. 'L',
  618. left + outlineHeight,
  619. navigatorTop + navigatorSize + scrollbarHeight // bottom edge
  620. ].concat(maskInside ? [
  621. 'M',
  622. left + outlineHeight,
  623. verticalMin - halfOutline, // upper left of zoomed range
  624. 'L',
  625. left + outlineHeight,
  626. zoomedMax + halfOutline // upper right of z.r.
  627. ] : []);
  628. } else {
  629. zoomedMin += left + scrollbarHeight - outlineCorrection;
  630. zoomedMax += left + scrollbarHeight - outlineCorrection;
  631. navigatorTop += halfOutline;
  632. path = [
  633. 'M',
  634. left,
  635. navigatorTop, // left
  636. 'L',
  637. zoomedMin,
  638. navigatorTop, // upper left of zoomed range
  639. 'L',
  640. zoomedMin,
  641. navigatorTop + outlineHeight, // lower left of z.r.
  642. 'L',
  643. zoomedMax,
  644. navigatorTop + outlineHeight, // lower right of z.r.
  645. 'L',
  646. zoomedMax,
  647. navigatorTop, // upper right of z.r.
  648. 'L',
  649. left + navigatorSize + scrollbarHeight * 2,
  650. navigatorTop // right
  651. ].concat(maskInside ? [
  652. 'M',
  653. zoomedMin - halfOutline,
  654. navigatorTop, // upper left of zoomed range
  655. 'L',
  656. zoomedMax + halfOutline,
  657. navigatorTop // upper right of z.r.
  658. ] : []);
  659. }
  660. navigator.outline[verb]({
  661. d: path
  662. });
  663. },
  664. /**
  665. * Render outline around the zoomed range
  666. *
  667. * @private
  668. * @function Highcharts.Navigator#drawMasks
  669. *
  670. * @param {number} zoomedMin
  671. * in pixels position where zoomed range starts
  672. *
  673. * @param {number} zoomedMax
  674. * in pixels position where zoomed range ends
  675. *
  676. * @param {boolean} inverted
  677. * flag if chart is inverted
  678. *
  679. * @param {string} verb
  680. * use 'animate' or 'attr'
  681. */
  682. drawMasks: function (zoomedMin, zoomedMax, inverted, verb) {
  683. var navigator = this,
  684. left = navigator.left,
  685. top = navigator.top,
  686. navigatorHeight = navigator.height,
  687. height,
  688. width,
  689. x,
  690. y;
  691. // Determine rectangle position & size
  692. // According to (non)inverted position:
  693. if (inverted) {
  694. x = [left, left, left];
  695. y = [top, top + zoomedMin, top + zoomedMax];
  696. width = [navigatorHeight, navigatorHeight, navigatorHeight];
  697. height = [
  698. zoomedMin,
  699. zoomedMax - zoomedMin,
  700. navigator.size - zoomedMax
  701. ];
  702. } else {
  703. x = [left, left + zoomedMin, left + zoomedMax];
  704. y = [top, top, top];
  705. width = [
  706. zoomedMin,
  707. zoomedMax - zoomedMin,
  708. navigator.size - zoomedMax
  709. ];
  710. height = [navigatorHeight, navigatorHeight, navigatorHeight];
  711. }
  712. navigator.shades.forEach(function (shade, i) {
  713. shade[verb]({
  714. x: x[i],
  715. y: y[i],
  716. width: width[i],
  717. height: height[i]
  718. });
  719. });
  720. },
  721. /**
  722. * Generate DOM elements for a navigator:
  723. *
  724. * - main navigator group
  725. *
  726. * - all shades
  727. *
  728. * - outline
  729. *
  730. * - handles
  731. *
  732. * @private
  733. * @function Highcharts.Navigator#renderElements
  734. */
  735. renderElements: function () {
  736. var navigator = this,
  737. navigatorOptions = navigator.navigatorOptions,
  738. maskInside = navigatorOptions.maskInside,
  739. chart = navigator.chart,
  740. inverted = chart.inverted,
  741. renderer = chart.renderer,
  742. navigatorGroup,
  743. mouseCursor = {
  744. cursor: inverted ? 'ns-resize' : 'ew-resize'
  745. };
  746. // Create the main navigator group
  747. navigator.navigatorGroup = navigatorGroup = renderer.g('navigator')
  748. .attr({
  749. zIndex: 8,
  750. visibility: 'hidden'
  751. })
  752. .add();
  753. // Create masks, each mask will get events and fill:
  754. [
  755. !maskInside,
  756. maskInside,
  757. !maskInside
  758. ].forEach(function (hasMask, index) {
  759. navigator.shades[index] = renderer.rect()
  760. .addClass('highcharts-navigator-mask' +
  761. (index === 1 ? '-inside' : '-outside'))
  762. .add(navigatorGroup);
  763. if (!chart.styledMode) {
  764. navigator.shades[index]
  765. .attr({
  766. fill: hasMask ?
  767. navigatorOptions.maskFill :
  768. 'rgba(0,0,0,0)'
  769. })
  770. .css(index === 1 && mouseCursor);
  771. }
  772. });
  773. // Create the outline:
  774. navigator.outline = renderer.path()
  775. .addClass('highcharts-navigator-outline')
  776. .add(navigatorGroup);
  777. if (!chart.styledMode) {
  778. navigator.outline.attr({
  779. 'stroke-width': navigatorOptions.outlineWidth,
  780. stroke: navigatorOptions.outlineColor
  781. });
  782. }
  783. // Create the handlers:
  784. if (navigatorOptions.handles.enabled) {
  785. [0, 1].forEach(function (index) {
  786. navigatorOptions.handles.inverted = chart.inverted;
  787. navigator.handles[index] = renderer.symbol(
  788. navigatorOptions.handles.symbols[index],
  789. -navigatorOptions.handles.width / 2 - 1,
  790. 0,
  791. navigatorOptions.handles.width,
  792. navigatorOptions.handles.height,
  793. navigatorOptions.handles
  794. );
  795. // zIndex = 6 for right handle, 7 for left.
  796. // Can't be 10, because of the tooltip in inverted chart #2908
  797. navigator.handles[index].attr({ zIndex: 7 - index })
  798. .addClass(
  799. 'highcharts-navigator-handle ' +
  800. 'highcharts-navigator-handle-' +
  801. ['left', 'right'][index]
  802. ).add(navigatorGroup);
  803. if (!chart.styledMode) {
  804. var handlesOptions = navigatorOptions.handles;
  805. navigator.handles[index]
  806. .attr({
  807. fill: handlesOptions.backgroundColor,
  808. stroke: handlesOptions.borderColor,
  809. 'stroke-width': handlesOptions.lineWidth
  810. })
  811. .css(mouseCursor);
  812. }
  813. });
  814. }
  815. },
  816. /**
  817. * Update navigator
  818. *
  819. * @private
  820. * @function Highcharts.Navigator#update
  821. *
  822. * @param {Highcharts.NavigatorOptions} options
  823. * Options to merge in when updating navigator
  824. */
  825. update: function (options) {
  826. // Remove references to old navigator series in base series
  827. (this.series || []).forEach(function (series) {
  828. if (series.baseSeries) {
  829. delete series.baseSeries.navigatorSeries;
  830. }
  831. });
  832. // Destroy and rebuild navigator
  833. this.destroy();
  834. var chartOptions = this.chart.options;
  835. merge(true, chartOptions.navigator, this.options, options);
  836. this.init(this.chart);
  837. },
  838. /**
  839. * Render the navigator
  840. *
  841. * @private
  842. * @function Highcharts.Navigator#render
  843. *
  844. * @param {number} min
  845. * X axis value minimum
  846. *
  847. * @param {number} max
  848. * X axis value maximum
  849. *
  850. * @param {number} pxMin
  851. * Pixel value minimum
  852. *
  853. * @param {number} pxMax
  854. * Pixel value maximum
  855. */
  856. render: function (min, max, pxMin, pxMax) {
  857. var navigator = this,
  858. chart = navigator.chart,
  859. navigatorWidth,
  860. scrollbarLeft,
  861. scrollbarTop,
  862. scrollbarHeight = navigator.scrollbarHeight,
  863. navigatorSize,
  864. xAxis = navigator.xAxis,
  865. scrollbarXAxis = xAxis.fake ? chart.xAxis[0] : xAxis,
  866. navigatorEnabled = navigator.navigatorEnabled,
  867. zoomedMin,
  868. zoomedMax,
  869. rendered = navigator.rendered,
  870. inverted = chart.inverted,
  871. verb,
  872. newMin,
  873. newMax,
  874. currentRange,
  875. minRange = chart.xAxis[0].minRange,
  876. maxRange = chart.xAxis[0].options.maxRange;
  877. // Don't redraw while moving the handles (#4703).
  878. if (this.hasDragged && !defined(pxMin)) {
  879. return;
  880. }
  881. // Don't render the navigator until we have data (#486, #4202, #5172).
  882. if (!isNumber(min) || !isNumber(max)) {
  883. // However, if navigator was already rendered, we may need to resize
  884. // it. For example hidden series, but visible navigator (#6022).
  885. if (rendered) {
  886. pxMin = 0;
  887. pxMax = pick(xAxis.width, scrollbarXAxis.width);
  888. } else {
  889. return;
  890. }
  891. }
  892. navigator.left = pick(
  893. xAxis.left,
  894. // in case of scrollbar only, without navigator
  895. chart.plotLeft + scrollbarHeight + (inverted ? chart.plotWidth : 0)
  896. );
  897. navigator.size = zoomedMax = navigatorSize = pick(
  898. xAxis.len,
  899. (inverted ? chart.plotHeight : chart.plotWidth) -
  900. 2 * scrollbarHeight
  901. );
  902. if (inverted) {
  903. navigatorWidth = scrollbarHeight;
  904. } else {
  905. navigatorWidth = navigatorSize + 2 * scrollbarHeight;
  906. }
  907. // Get the pixel position of the handles
  908. pxMin = pick(pxMin, xAxis.toPixels(min, true));
  909. pxMax = pick(pxMax, xAxis.toPixels(max, true));
  910. // Verify (#1851, #2238)
  911. if (!isNumber(pxMin) || Math.abs(pxMin) === Infinity) {
  912. pxMin = 0;
  913. pxMax = navigatorWidth;
  914. }
  915. // Are we below the minRange? (#2618, #6191)
  916. newMin = xAxis.toValue(pxMin, true);
  917. newMax = xAxis.toValue(pxMax, true);
  918. currentRange = Math.abs(H.correctFloat(newMax - newMin));
  919. if (currentRange < minRange) {
  920. if (this.grabbedLeft) {
  921. pxMin = xAxis.toPixels(newMax - minRange, true);
  922. } else if (this.grabbedRight) {
  923. pxMax = xAxis.toPixels(newMin + minRange, true);
  924. }
  925. } else if (defined(maxRange) && currentRange > maxRange) {
  926. if (this.grabbedLeft) {
  927. pxMin = xAxis.toPixels(newMax - maxRange, true);
  928. } else if (this.grabbedRight) {
  929. pxMax = xAxis.toPixels(newMin + maxRange, true);
  930. }
  931. }
  932. // Handles are allowed to cross, but never exceed the plot area
  933. navigator.zoomedMax = Math.min(Math.max(pxMin, pxMax, 0), zoomedMax);
  934. navigator.zoomedMin = Math.min(
  935. Math.max(
  936. navigator.fixedWidth ?
  937. navigator.zoomedMax - navigator.fixedWidth :
  938. Math.min(pxMin, pxMax),
  939. 0
  940. ),
  941. zoomedMax
  942. );
  943. navigator.range = navigator.zoomedMax - navigator.zoomedMin;
  944. zoomedMax = Math.round(navigator.zoomedMax);
  945. zoomedMin = Math.round(navigator.zoomedMin);
  946. if (navigatorEnabled) {
  947. navigator.navigatorGroup.attr({
  948. visibility: 'visible'
  949. });
  950. // Place elements
  951. verb = rendered && !navigator.hasDragged ? 'animate' : 'attr';
  952. navigator.drawMasks(zoomedMin, zoomedMax, inverted, verb);
  953. navigator.drawOutline(zoomedMin, zoomedMax, inverted, verb);
  954. if (navigator.navigatorOptions.handles.enabled) {
  955. navigator.drawHandle(zoomedMin, 0, inverted, verb);
  956. navigator.drawHandle(zoomedMax, 1, inverted, verb);
  957. }
  958. }
  959. if (navigator.scrollbar) {
  960. if (inverted) {
  961. scrollbarTop = navigator.top - scrollbarHeight;
  962. scrollbarLeft = navigator.left - scrollbarHeight +
  963. (navigatorEnabled || !scrollbarXAxis.opposite ? 0 :
  964. // Multiple axes has offsets:
  965. (scrollbarXAxis.titleOffset || 0) +
  966. // Self margin from the axis.title
  967. scrollbarXAxis.axisTitleMargin
  968. );
  969. scrollbarHeight = navigatorSize + 2 * scrollbarHeight;
  970. } else {
  971. scrollbarTop = navigator.top +
  972. (navigatorEnabled ? navigator.height : -scrollbarHeight);
  973. scrollbarLeft = navigator.left - scrollbarHeight;
  974. }
  975. // Reposition scrollbar
  976. navigator.scrollbar.position(
  977. scrollbarLeft,
  978. scrollbarTop,
  979. navigatorWidth,
  980. scrollbarHeight
  981. );
  982. // Keep scale 0-1
  983. navigator.scrollbar.setRange(
  984. // Use real value, not rounded because range can be very small
  985. // (#1716)
  986. navigator.zoomedMin / (navigatorSize || 1),
  987. navigator.zoomedMax / (navigatorSize || 1)
  988. );
  989. }
  990. navigator.rendered = true;
  991. },
  992. /**
  993. * Set up the mouse and touch events for the navigator
  994. *
  995. * @private
  996. * @function Highcharts.Navigator#addMouseEvents
  997. */
  998. addMouseEvents: function () {
  999. var navigator = this,
  1000. chart = navigator.chart,
  1001. container = chart.container,
  1002. eventsToUnbind = [],
  1003. mouseMoveHandler,
  1004. mouseUpHandler;
  1005. /**
  1006. * Create mouse events' handlers.
  1007. * Make them as separate functions to enable wrapping them:
  1008. */
  1009. navigator.mouseMoveHandler = mouseMoveHandler = function (e) {
  1010. navigator.onMouseMove(e);
  1011. };
  1012. navigator.mouseUpHandler = mouseUpHandler = function (e) {
  1013. navigator.onMouseUp(e);
  1014. };
  1015. // Add shades and handles mousedown events
  1016. eventsToUnbind = navigator.getPartsEvents('mousedown');
  1017. // Add mouse move and mouseup events. These are bind to doc/container,
  1018. // because Navigator.grabbedSomething flags are stored in mousedown
  1019. // events
  1020. eventsToUnbind.push(
  1021. addEvent(container, 'mousemove', mouseMoveHandler),
  1022. addEvent(container.ownerDocument, 'mouseup', mouseUpHandler)
  1023. );
  1024. // Touch events
  1025. if (hasTouch) {
  1026. eventsToUnbind.push(
  1027. addEvent(container, 'touchmove', mouseMoveHandler),
  1028. addEvent(container.ownerDocument, 'touchend', mouseUpHandler)
  1029. );
  1030. eventsToUnbind.concat(navigator.getPartsEvents('touchstart'));
  1031. }
  1032. navigator.eventsToUnbind = eventsToUnbind;
  1033. // Data events
  1034. if (navigator.series && navigator.series[0]) {
  1035. eventsToUnbind.push(
  1036. addEvent(
  1037. navigator.series[0].xAxis,
  1038. 'foundExtremes',
  1039. function () {
  1040. chart.navigator.modifyNavigatorAxisExtremes();
  1041. }
  1042. )
  1043. );
  1044. }
  1045. },
  1046. /**
  1047. * Generate events for handles and masks
  1048. *
  1049. * @private
  1050. * @function Highcharts.Navigator#getPartsEvents
  1051. *
  1052. * @param {string} eventName
  1053. * Event name handler, 'mousedown' or 'touchstart'
  1054. *
  1055. * @return {Array<Function>}
  1056. * An array of functions to remove navigator functions from the
  1057. * events again.
  1058. */
  1059. getPartsEvents: function (eventName) {
  1060. var navigator = this,
  1061. events = [];
  1062. ['shades', 'handles'].forEach(function (name) {
  1063. navigator[name].forEach(function (navigatorItem, index) {
  1064. events.push(
  1065. addEvent(
  1066. navigatorItem.element,
  1067. eventName,
  1068. function (e) {
  1069. navigator[name + 'Mousedown'](e, index);
  1070. }
  1071. )
  1072. );
  1073. });
  1074. });
  1075. return events;
  1076. },
  1077. /**
  1078. * Mousedown on a shaded mask, either:
  1079. *
  1080. * - will be stored for future drag&drop
  1081. *
  1082. * - will directly shift to a new range
  1083. *
  1084. * @private
  1085. * @function Highcharts.Navigator#shadesMousedown
  1086. *
  1087. * @param {global.PointerEventObject} e
  1088. * Mouse event
  1089. *
  1090. * @param {number} index
  1091. * Index of a mask in Navigator.shades array
  1092. */
  1093. shadesMousedown: function (e, index) {
  1094. e = this.chart.pointer.normalize(e);
  1095. var navigator = this,
  1096. chart = navigator.chart,
  1097. xAxis = navigator.xAxis,
  1098. zoomedMin = navigator.zoomedMin,
  1099. navigatorPosition = navigator.left,
  1100. navigatorSize = navigator.size,
  1101. range = navigator.range,
  1102. chartX = e.chartX,
  1103. fixedMax,
  1104. fixedMin,
  1105. ext,
  1106. left;
  1107. // For inverted chart, swap some options:
  1108. if (chart.inverted) {
  1109. chartX = e.chartY;
  1110. navigatorPosition = navigator.top;
  1111. }
  1112. if (index === 1) {
  1113. // Store information for drag&drop
  1114. navigator.grabbedCenter = chartX;
  1115. navigator.fixedWidth = range;
  1116. navigator.dragOffset = chartX - zoomedMin;
  1117. } else {
  1118. // Shift the range by clicking on shaded areas
  1119. left = chartX - navigatorPosition - range / 2;
  1120. if (index === 0) {
  1121. left = Math.max(0, left);
  1122. } else if (index === 2 && left + range >= navigatorSize) {
  1123. left = navigatorSize - range;
  1124. if (navigator.reversedExtremes) {
  1125. // #7713
  1126. left -= range;
  1127. fixedMin = navigator.getUnionExtremes().dataMin;
  1128. } else {
  1129. // #2293, #3543
  1130. fixedMax = navigator.getUnionExtremes().dataMax;
  1131. }
  1132. }
  1133. if (left !== zoomedMin) { // it has actually moved
  1134. navigator.fixedWidth = range; // #1370
  1135. ext = xAxis.toFixedRange(
  1136. left,
  1137. left + range,
  1138. fixedMin,
  1139. fixedMax
  1140. );
  1141. if (defined(ext.min)) { // #7411
  1142. chart.xAxis[0].setExtremes(
  1143. Math.min(ext.min, ext.max),
  1144. Math.max(ext.min, ext.max),
  1145. true,
  1146. null, // auto animation
  1147. { trigger: 'navigator' }
  1148. );
  1149. }
  1150. }
  1151. }
  1152. },
  1153. /**
  1154. * Mousedown on a handle mask.
  1155. * Will store necessary information for drag&drop.
  1156. *
  1157. * @private
  1158. * @function Highcharts.Navigator#handlesMousedown
  1159. *
  1160. * @param {Highcharts.PointerEventObject} e
  1161. * Mouse event
  1162. *
  1163. * @param {number} index
  1164. * Index of a handle in Navigator.handles array
  1165. */
  1166. handlesMousedown: function (e, index) {
  1167. e = this.chart.pointer.normalize(e);
  1168. var navigator = this,
  1169. chart = navigator.chart,
  1170. baseXAxis = chart.xAxis[0],
  1171. // For reversed axes, min and max are changed,
  1172. // so the other extreme should be stored
  1173. reverse = navigator.reversedExtremes;
  1174. if (index === 0) {
  1175. // Grab the left handle
  1176. navigator.grabbedLeft = true;
  1177. navigator.otherHandlePos = navigator.zoomedMax;
  1178. navigator.fixedExtreme = reverse ? baseXAxis.min : baseXAxis.max;
  1179. } else {
  1180. // Grab the right handle
  1181. navigator.grabbedRight = true;
  1182. navigator.otherHandlePos = navigator.zoomedMin;
  1183. navigator.fixedExtreme = reverse ? baseXAxis.max : baseXAxis.min;
  1184. }
  1185. chart.fixedRange = null;
  1186. },
  1187. /**
  1188. * Mouse move event based on x/y mouse position.
  1189. *
  1190. * @private
  1191. * @function Highcharts.Navigator#onMouseMove
  1192. *
  1193. * @param {Highcharts.PointerEventObject} e
  1194. * Mouse event
  1195. */
  1196. onMouseMove: function (e) {
  1197. var navigator = this,
  1198. chart = navigator.chart,
  1199. left = navigator.left,
  1200. navigatorSize = navigator.navigatorSize,
  1201. range = navigator.range,
  1202. dragOffset = navigator.dragOffset,
  1203. inverted = chart.inverted,
  1204. chartX;
  1205. // In iOS, a mousemove event with e.pageX === 0 is fired when holding
  1206. // the finger down in the center of the scrollbar. This should be
  1207. // ignored.
  1208. if (!e.touches || e.touches[0].pageX !== 0) { // #4696
  1209. e = chart.pointer.normalize(e);
  1210. chartX = e.chartX;
  1211. // Swap some options for inverted chart
  1212. if (inverted) {
  1213. left = navigator.top;
  1214. chartX = e.chartY;
  1215. }
  1216. // Drag left handle or top handle
  1217. if (navigator.grabbedLeft) {
  1218. navigator.hasDragged = true;
  1219. navigator.render(
  1220. 0,
  1221. 0,
  1222. chartX - left,
  1223. navigator.otherHandlePos
  1224. );
  1225. // Drag right handle or bottom handle
  1226. } else if (navigator.grabbedRight) {
  1227. navigator.hasDragged = true;
  1228. navigator.render(
  1229. 0,
  1230. 0,
  1231. navigator.otherHandlePos,
  1232. chartX - left
  1233. );
  1234. // Drag scrollbar or open area in navigator
  1235. } else if (navigator.grabbedCenter) {
  1236. navigator.hasDragged = true;
  1237. if (chartX < dragOffset) { // outside left
  1238. chartX = dragOffset;
  1239. // outside right
  1240. } else if (chartX > navigatorSize + dragOffset - range) {
  1241. chartX = navigatorSize + dragOffset - range;
  1242. }
  1243. navigator.render(
  1244. 0,
  1245. 0,
  1246. chartX - dragOffset,
  1247. chartX - dragOffset + range
  1248. );
  1249. }
  1250. if (
  1251. navigator.hasDragged &&
  1252. navigator.scrollbar &&
  1253. pick(
  1254. navigator.scrollbar.options.liveRedraw,
  1255. // By default, don't run live redraw on VML, on touch
  1256. // devices or if the chart is in boost.
  1257. H.svg && !isTouchDevice && !this.chart.isBoosting
  1258. )
  1259. ) {
  1260. e.DOMType = e.type; // DOMType is for IE8
  1261. setTimeout(function () {
  1262. navigator.onMouseUp(e);
  1263. }, 0);
  1264. }
  1265. }
  1266. },
  1267. /**
  1268. * Mouse up event based on x/y mouse position.
  1269. *
  1270. * @private
  1271. * @function Highcharts.Navigator#onMouseUp
  1272. *
  1273. * @param {Highcharts.PointerEventObject} e
  1274. * Mouse event
  1275. */
  1276. onMouseUp: function (e) {
  1277. var navigator = this,
  1278. chart = navigator.chart,
  1279. xAxis = navigator.xAxis,
  1280. scrollbar = navigator.scrollbar,
  1281. unionExtremes,
  1282. fixedMin,
  1283. fixedMax,
  1284. ext,
  1285. DOMEvent = e.DOMEvent || e;
  1286. if (
  1287. // MouseUp is called for both, navigator and scrollbar (that order),
  1288. // which causes calling afterSetExtremes twice. Prevent first call
  1289. // by checking if scrollbar is going to set new extremes (#6334)
  1290. (navigator.hasDragged && (!scrollbar || !scrollbar.hasDragged)) ||
  1291. e.trigger === 'scrollbar'
  1292. ) {
  1293. unionExtremes = navigator.getUnionExtremes();
  1294. // When dragging one handle, make sure the other one doesn't change
  1295. if (navigator.zoomedMin === navigator.otherHandlePos) {
  1296. fixedMin = navigator.fixedExtreme;
  1297. } else if (navigator.zoomedMax === navigator.otherHandlePos) {
  1298. fixedMax = navigator.fixedExtreme;
  1299. }
  1300. // Snap to right edge (#4076)
  1301. if (navigator.zoomedMax === navigator.size) {
  1302. fixedMax = navigator.reversedExtremes ?
  1303. unionExtremes.dataMin : unionExtremes.dataMax;
  1304. }
  1305. // Snap to left edge (#7576)
  1306. if (navigator.zoomedMin === 0) {
  1307. fixedMin = navigator.reversedExtremes ?
  1308. unionExtremes.dataMax : unionExtremes.dataMin;
  1309. }
  1310. ext = xAxis.toFixedRange(
  1311. navigator.zoomedMin,
  1312. navigator.zoomedMax,
  1313. fixedMin,
  1314. fixedMax
  1315. );
  1316. if (defined(ext.min)) {
  1317. chart.xAxis[0].setExtremes(
  1318. Math.min(ext.min, ext.max),
  1319. Math.max(ext.min, ext.max),
  1320. true,
  1321. // Run animation when clicking buttons, scrollbar track etc,
  1322. // but not when dragging handles or scrollbar
  1323. navigator.hasDragged ? false : null,
  1324. {
  1325. trigger: 'navigator',
  1326. triggerOp: 'navigator-drag',
  1327. DOMEvent: DOMEvent // #1838
  1328. }
  1329. );
  1330. }
  1331. }
  1332. if (e.DOMType !== 'mousemove') {
  1333. navigator.grabbedLeft = navigator.grabbedRight =
  1334. navigator.grabbedCenter = navigator.fixedWidth =
  1335. navigator.fixedExtreme = navigator.otherHandlePos =
  1336. navigator.hasDragged = navigator.dragOffset = null;
  1337. }
  1338. },
  1339. /**
  1340. * Removes the event handlers attached previously with addEvents.
  1341. *
  1342. * @private
  1343. * @function Highcharts.Navigator#removeEvents
  1344. */
  1345. removeEvents: function () {
  1346. if (this.eventsToUnbind) {
  1347. this.eventsToUnbind.forEach(function (unbind) {
  1348. unbind();
  1349. });
  1350. this.eventsToUnbind = undefined;
  1351. }
  1352. this.removeBaseSeriesEvents();
  1353. },
  1354. /**
  1355. * Remove data events.
  1356. *
  1357. * @private
  1358. * @function Highcharts.Navigator#removeBaseSeriesEvents
  1359. */
  1360. removeBaseSeriesEvents: function () {
  1361. var baseSeries = this.baseSeries || [];
  1362. if (this.navigatorEnabled && baseSeries[0]) {
  1363. if (this.navigatorOptions.adaptToUpdatedData !== false) {
  1364. baseSeries.forEach(function (series) {
  1365. removeEvent(series, 'updatedData', this.updatedDataHandler);
  1366. }, this);
  1367. }
  1368. // We only listen for extremes-events on the first baseSeries
  1369. if (baseSeries[0].xAxis) {
  1370. removeEvent(
  1371. baseSeries[0].xAxis,
  1372. 'foundExtremes',
  1373. this.modifyBaseAxisExtremes
  1374. );
  1375. }
  1376. }
  1377. },
  1378. /**
  1379. * Initiate the Navigator object
  1380. *
  1381. * @private
  1382. * @function Highcharts.Navigator#init
  1383. *
  1384. * @param {Highcharts.Chart} chart
  1385. */
  1386. init: function (chart) {
  1387. var chartOptions = chart.options,
  1388. navigatorOptions = chartOptions.navigator,
  1389. navigatorEnabled = navigatorOptions.enabled,
  1390. scrollbarOptions = chartOptions.scrollbar,
  1391. scrollbarEnabled = scrollbarOptions.enabled,
  1392. height = navigatorEnabled ? navigatorOptions.height : 0,
  1393. scrollbarHeight = scrollbarEnabled ? scrollbarOptions.height : 0;
  1394. this.handles = [];
  1395. this.shades = [];
  1396. this.chart = chart;
  1397. this.setBaseSeries();
  1398. this.height = height;
  1399. this.scrollbarHeight = scrollbarHeight;
  1400. this.scrollbarEnabled = scrollbarEnabled;
  1401. this.navigatorEnabled = navigatorEnabled;
  1402. this.navigatorOptions = navigatorOptions;
  1403. this.scrollbarOptions = scrollbarOptions;
  1404. this.outlineHeight = height + scrollbarHeight;
  1405. this.opposite = pick(
  1406. navigatorOptions.opposite,
  1407. !navigatorEnabled && chart.inverted
  1408. ); // #6262
  1409. var navigator = this,
  1410. baseSeries = navigator.baseSeries,
  1411. xAxisIndex = chart.xAxis.length,
  1412. yAxisIndex = chart.yAxis.length,
  1413. baseXaxis = baseSeries && baseSeries[0] && baseSeries[0].xAxis ||
  1414. chart.xAxis[0] || { options: {} };
  1415. chart.isDirtyBox = true;
  1416. if (navigator.navigatorEnabled) {
  1417. // an x axis is required for scrollbar also
  1418. navigator.xAxis = new Axis(chart, merge({
  1419. // inherit base xAxis' break and ordinal options
  1420. breaks: baseXaxis.options.breaks,
  1421. ordinal: baseXaxis.options.ordinal
  1422. }, navigatorOptions.xAxis, {
  1423. id: 'navigator-x-axis',
  1424. yAxis: 'navigator-y-axis',
  1425. isX: true,
  1426. type: 'datetime',
  1427. index: xAxisIndex,
  1428. isInternal: true,
  1429. offset: 0,
  1430. keepOrdinalPadding: true, // #2436
  1431. startOnTick: false,
  1432. endOnTick: false,
  1433. minPadding: 0,
  1434. maxPadding: 0,
  1435. zoomEnabled: false
  1436. }, chart.inverted ? {
  1437. offsets: [scrollbarHeight, 0, -scrollbarHeight, 0],
  1438. width: height
  1439. } : {
  1440. offsets: [0, -scrollbarHeight, 0, scrollbarHeight],
  1441. height: height
  1442. }));
  1443. navigator.yAxis = new Axis(chart, merge(navigatorOptions.yAxis, {
  1444. id: 'navigator-y-axis',
  1445. alignTicks: false,
  1446. offset: 0,
  1447. index: yAxisIndex,
  1448. isInternal: true,
  1449. zoomEnabled: false
  1450. }, chart.inverted ? {
  1451. width: height
  1452. } : {
  1453. height: height
  1454. }));
  1455. // If we have a base series, initialize the navigator series
  1456. if (baseSeries || navigatorOptions.series.data) {
  1457. navigator.updateNavigatorSeries(false);
  1458. // If not, set up an event to listen for added series
  1459. } else if (chart.series.length === 0) {
  1460. navigator.unbindRedraw = addEvent(
  1461. chart,
  1462. 'beforeRedraw',
  1463. function () {
  1464. // We've got one, now add it as base
  1465. if (chart.series.length > 0 && !navigator.series) {
  1466. navigator.setBaseSeries();
  1467. navigator.unbindRedraw(); // reset
  1468. }
  1469. }
  1470. );
  1471. }
  1472. navigator.reversedExtremes = (
  1473. chart.inverted && !navigator.xAxis.reversed
  1474. ) || (
  1475. !chart.inverted && navigator.xAxis.reversed
  1476. );
  1477. // Render items, so we can bind events to them:
  1478. navigator.renderElements();
  1479. // Add mouse events
  1480. navigator.addMouseEvents();
  1481. // in case of scrollbar only, fake an x axis to get translation
  1482. } else {
  1483. navigator.xAxis = {
  1484. translate: function (value, reverse) {
  1485. var axis = chart.xAxis[0],
  1486. ext = axis.getExtremes(),
  1487. scrollTrackWidth = axis.len - 2 * scrollbarHeight,
  1488. min = numExt('min', axis.options.min, ext.dataMin),
  1489. valueRange = numExt(
  1490. 'max',
  1491. axis.options.max,
  1492. ext.dataMax
  1493. ) - min;
  1494. return reverse ?
  1495. // from pixel to value
  1496. (value * valueRange / scrollTrackWidth) + min :
  1497. // from value to pixel
  1498. scrollTrackWidth * (value - min) / valueRange;
  1499. },
  1500. toPixels: function (value) {
  1501. return this.translate(value);
  1502. },
  1503. toValue: function (value) {
  1504. return this.translate(value, true);
  1505. },
  1506. toFixedRange: Axis.prototype.toFixedRange,
  1507. fake: true
  1508. };
  1509. }
  1510. // Initialize the scrollbar
  1511. if (chart.options.scrollbar.enabled) {
  1512. chart.scrollbar = navigator.scrollbar = new Scrollbar(
  1513. chart.renderer,
  1514. merge(chart.options.scrollbar, {
  1515. margin: navigator.navigatorEnabled ? 0 : 10,
  1516. vertical: chart.inverted
  1517. }),
  1518. chart
  1519. );
  1520. addEvent(navigator.scrollbar, 'changed', function (e) {
  1521. var range = navigator.size,
  1522. to = range * this.to,
  1523. from = range * this.from;
  1524. navigator.hasDragged = navigator.scrollbar.hasDragged;
  1525. navigator.render(0, 0, from, to);
  1526. if (
  1527. chart.options.scrollbar.liveRedraw ||
  1528. (
  1529. e.DOMType !== 'mousemove' &&
  1530. e.DOMType !== 'touchmove'
  1531. )
  1532. ) {
  1533. setTimeout(function () {
  1534. navigator.onMouseUp(e);
  1535. });
  1536. }
  1537. });
  1538. }
  1539. // Add data events
  1540. navigator.addBaseSeriesEvents();
  1541. // Add redraw events
  1542. navigator.addChartEvents();
  1543. },
  1544. /**
  1545. * Get the union data extremes of the chart - the outer data extremes of the
  1546. * base X axis and the navigator axis.
  1547. *
  1548. * @private
  1549. * @function Highcharts.Navigator#getUnionExtremes
  1550. *
  1551. * @param {boolean} returnFalseOnNoBaseSeries
  1552. * as the param says.
  1553. *
  1554. * @return {*}
  1555. */
  1556. getUnionExtremes: function (returnFalseOnNoBaseSeries) {
  1557. var baseAxis = this.chart.xAxis[0],
  1558. navAxis = this.xAxis,
  1559. navAxisOptions = navAxis.options,
  1560. baseAxisOptions = baseAxis.options,
  1561. ret;
  1562. if (!returnFalseOnNoBaseSeries || baseAxis.dataMin !== null) {
  1563. ret = {
  1564. dataMin: pick( // #4053
  1565. navAxisOptions && navAxisOptions.min,
  1566. numExt(
  1567. 'min',
  1568. baseAxisOptions.min,
  1569. baseAxis.dataMin,
  1570. navAxis.dataMin,
  1571. navAxis.min
  1572. )
  1573. ),
  1574. dataMax: pick(
  1575. navAxisOptions && navAxisOptions.max,
  1576. numExt(
  1577. 'max',
  1578. baseAxisOptions.max,
  1579. baseAxis.dataMax,
  1580. navAxis.dataMax,
  1581. navAxis.max
  1582. )
  1583. )
  1584. };
  1585. }
  1586. return ret;
  1587. },
  1588. /**
  1589. * Set the base series and update the navigator series from this. With a bit
  1590. * of modification we should be able to make this an API method to be called
  1591. * from the outside
  1592. *
  1593. * @private
  1594. * @function Highcharts.Navigator#setBaseSeries
  1595. *
  1596. * @param {*} baseSeriesOptions
  1597. * Additional series options for a navigator
  1598. *
  1599. * @param {boolean} [redraw]
  1600. * Whether to redraw after update.
  1601. */
  1602. setBaseSeries: function (baseSeriesOptions, redraw) {
  1603. var chart = this.chart,
  1604. baseSeries = this.baseSeries = [];
  1605. baseSeriesOptions = (
  1606. baseSeriesOptions ||
  1607. chart.options && chart.options.navigator.baseSeries ||
  1608. 0
  1609. );
  1610. // Iterate through series and add the ones that should be shown in
  1611. // navigator.
  1612. (chart.series || []).forEach(function (series, i) {
  1613. if (
  1614. // Don't include existing nav series
  1615. !series.options.isInternal &&
  1616. (
  1617. series.options.showInNavigator ||
  1618. (
  1619. i === baseSeriesOptions ||
  1620. series.options.id === baseSeriesOptions
  1621. ) &&
  1622. series.options.showInNavigator !== false
  1623. )
  1624. ) {
  1625. baseSeries.push(series);
  1626. }
  1627. });
  1628. // When run after render, this.xAxis already exists
  1629. if (this.xAxis && !this.xAxis.fake) {
  1630. this.updateNavigatorSeries(true, redraw);
  1631. }
  1632. },
  1633. /**
  1634. * Update series in the navigator from baseSeries, adding new if does not
  1635. * exist.
  1636. *
  1637. * @private
  1638. * @function Highcharts.Navigator.updateNavigatorSeries
  1639. *
  1640. * @param {boolean} addEvents
  1641. *
  1642. * @param {boolean} redraw
  1643. */
  1644. updateNavigatorSeries: function (addEvents, redraw) {
  1645. var navigator = this,
  1646. chart = navigator.chart,
  1647. baseSeries = navigator.baseSeries,
  1648. baseOptions,
  1649. mergedNavSeriesOptions,
  1650. chartNavigatorSeriesOptions = navigator.navigatorOptions.series,
  1651. baseNavigatorOptions,
  1652. navSeriesMixin = {
  1653. enableMouseTracking: false,
  1654. index: null, // #6162
  1655. linkedTo: null, // #6734
  1656. group: 'nav', // for columns
  1657. padXAxis: false,
  1658. xAxis: 'navigator-x-axis',
  1659. yAxis: 'navigator-y-axis',
  1660. showInLegend: false,
  1661. stacking: false, // #4823
  1662. isInternal: true
  1663. },
  1664. // Remove navigator series that are no longer in the baseSeries
  1665. navigatorSeries = navigator.series =
  1666. (navigator.series || []).filter(function (navSeries) {
  1667. var base = navSeries.baseSeries;
  1668. if (baseSeries.indexOf(base) < 0) { // Not in array
  1669. // If there is still a base series connected to this
  1670. // series, remove event handler and reference.
  1671. if (base) {
  1672. removeEvent(
  1673. base,
  1674. 'updatedData',
  1675. navigator.updatedDataHandler
  1676. );
  1677. delete base.navigatorSeries;
  1678. }
  1679. // Kill the nav series. It may already have been
  1680. // destroyed (#8715).
  1681. if (navSeries.chart) {
  1682. navSeries.destroy();
  1683. }
  1684. return false;
  1685. }
  1686. return true;
  1687. });
  1688. // Go through each base series and merge the options to create new
  1689. // series
  1690. if (baseSeries && baseSeries.length) {
  1691. baseSeries.forEach(function eachBaseSeries(base) {
  1692. var linkedNavSeries = base.navigatorSeries,
  1693. userNavOptions = extend(
  1694. // Grab color and visibility from base as default
  1695. {
  1696. color: base.color,
  1697. visible: base.visible
  1698. },
  1699. !isArray(chartNavigatorSeriesOptions) ?
  1700. chartNavigatorSeriesOptions :
  1701. defaultOptions.navigator.series
  1702. );
  1703. // Don't update if the series exists in nav and we have disabled
  1704. // adaptToUpdatedData.
  1705. if (
  1706. linkedNavSeries &&
  1707. navigator.navigatorOptions.adaptToUpdatedData === false
  1708. ) {
  1709. return;
  1710. }
  1711. navSeriesMixin.name = 'Navigator ' + baseSeries.length;
  1712. baseOptions = base.options || {};
  1713. baseNavigatorOptions = baseOptions.navigatorOptions || {};
  1714. mergedNavSeriesOptions = merge(
  1715. baseOptions,
  1716. navSeriesMixin,
  1717. userNavOptions,
  1718. baseNavigatorOptions
  1719. );
  1720. // Merge data separately. Do a slice to avoid mutating the
  1721. // navigator options from base series (#4923).
  1722. var navigatorSeriesData =
  1723. baseNavigatorOptions.data || userNavOptions.data;
  1724. navigator.hasNavigatorData =
  1725. navigator.hasNavigatorData || !!navigatorSeriesData;
  1726. mergedNavSeriesOptions.data =
  1727. navigatorSeriesData ||
  1728. baseOptions.data && baseOptions.data.slice(0);
  1729. // Update or add the series
  1730. if (linkedNavSeries && linkedNavSeries.options) {
  1731. linkedNavSeries.update(mergedNavSeriesOptions, redraw);
  1732. } else {
  1733. base.navigatorSeries = chart.initSeries(
  1734. mergedNavSeriesOptions
  1735. );
  1736. base.navigatorSeries.baseSeries = base; // Store ref
  1737. navigatorSeries.push(base.navigatorSeries);
  1738. }
  1739. });
  1740. }
  1741. // If user has defined data (and no base series) or explicitly defined
  1742. // navigator.series as an array, we create these series on top of any
  1743. // base series.
  1744. if (
  1745. chartNavigatorSeriesOptions.data &&
  1746. !(baseSeries && baseSeries.length) ||
  1747. isArray(chartNavigatorSeriesOptions)
  1748. ) {
  1749. navigator.hasNavigatorData = false;
  1750. // Allow navigator.series to be an array
  1751. chartNavigatorSeriesOptions = H.splat(chartNavigatorSeriesOptions);
  1752. chartNavigatorSeriesOptions
  1753. .forEach(function (userSeriesOptions, i) {
  1754. navSeriesMixin.name =
  1755. 'Navigator ' + (navigatorSeries.length + 1);
  1756. mergedNavSeriesOptions = merge(
  1757. defaultOptions.navigator.series,
  1758. {
  1759. // Since we don't have a base series to pull color from,
  1760. // try to fake it by using color from series with same
  1761. // index. Otherwise pull from the colors array. We need
  1762. // an explicit color as otherwise updates will increment
  1763. // color counter and we'll get a new color for each
  1764. // update of the nav series.
  1765. color: chart.series[i] &&
  1766. !chart.series[i].options.isInternal &&
  1767. chart.series[i].color ||
  1768. chart.options.colors[i] ||
  1769. chart.options.colors[0]
  1770. },
  1771. navSeriesMixin,
  1772. userSeriesOptions
  1773. );
  1774. mergedNavSeriesOptions.data = userSeriesOptions.data;
  1775. if (mergedNavSeriesOptions.data) {
  1776. navigator.hasNavigatorData = true;
  1777. navigatorSeries.push(
  1778. chart.initSeries(mergedNavSeriesOptions)
  1779. );
  1780. }
  1781. });
  1782. }
  1783. if (addEvents) {
  1784. this.addBaseSeriesEvents();
  1785. }
  1786. },
  1787. /**
  1788. * Add data events.
  1789. * For example when main series is updated we need to recalculate extremes
  1790. *
  1791. * @private
  1792. * @function Highcharts.Navigator#addBaseSeriesEvent
  1793. */
  1794. addBaseSeriesEvents: function () {
  1795. var navigator = this,
  1796. baseSeries = navigator.baseSeries || [];
  1797. // Bind modified extremes event to first base's xAxis only.
  1798. // In event of > 1 base-xAxes, the navigator will ignore those.
  1799. // Adding this multiple times to the same axis is no problem, as
  1800. // duplicates should be discarded by the browser.
  1801. if (baseSeries[0] && baseSeries[0].xAxis) {
  1802. addEvent(
  1803. baseSeries[0].xAxis,
  1804. 'foundExtremes',
  1805. this.modifyBaseAxisExtremes
  1806. );
  1807. }
  1808. baseSeries.forEach(function (base) {
  1809. // Link base series show/hide to navigator series visibility
  1810. addEvent(base, 'show', function () {
  1811. if (this.navigatorSeries) {
  1812. this.navigatorSeries.setVisible(true, false);
  1813. }
  1814. });
  1815. addEvent(base, 'hide', function () {
  1816. if (this.navigatorSeries) {
  1817. this.navigatorSeries.setVisible(false, false);
  1818. }
  1819. });
  1820. // Respond to updated data in the base series, unless explicitily
  1821. // not adapting to data changes.
  1822. if (this.navigatorOptions.adaptToUpdatedData !== false) {
  1823. if (base.xAxis) {
  1824. addEvent(base, 'updatedData', this.updatedDataHandler);
  1825. }
  1826. }
  1827. // Handle series removal
  1828. addEvent(base, 'remove', function () {
  1829. if (this.navigatorSeries) {
  1830. erase(navigator.series, this.navigatorSeries);
  1831. if (defined(this.navigatorSeries.options)) {
  1832. this.navigatorSeries.remove(false);
  1833. }
  1834. delete this.navigatorSeries;
  1835. }
  1836. });
  1837. }, this);
  1838. },
  1839. /**
  1840. * Get minimum from all base series connected to the navigator
  1841. *
  1842. * @param {number} currentSeriesMin
  1843. * Minium from the current series
  1844. *
  1845. * @return {number} Minimum from all series
  1846. */
  1847. getBaseSeriesMin: function (currentSeriesMin) {
  1848. return this.baseSeries.reduce(
  1849. function (min, series) {
  1850. return Math.min(min, series.xData[0]);
  1851. },
  1852. currentSeriesMin
  1853. );
  1854. },
  1855. /**
  1856. * Set the navigator x axis extremes to reflect the total. The navigator
  1857. * extremes should always be the extremes of the union of all series in the
  1858. * chart as well as the navigator series.
  1859. *
  1860. * @private
  1861. * @function Highcharts.Navigator#modifyNavigatorAxisExtremes
  1862. */
  1863. modifyNavigatorAxisExtremes: function () {
  1864. var xAxis = this.xAxis,
  1865. unionExtremes;
  1866. if (xAxis.getExtremes) {
  1867. unionExtremes = this.getUnionExtremes(true);
  1868. if (
  1869. unionExtremes &&
  1870. (
  1871. unionExtremes.dataMin !== xAxis.min ||
  1872. unionExtremes.dataMax !== xAxis.max
  1873. )
  1874. ) {
  1875. xAxis.min = unionExtremes.dataMin;
  1876. xAxis.max = unionExtremes.dataMax;
  1877. }
  1878. }
  1879. },
  1880. /**
  1881. * Hook to modify the base axis extremes with information from the Navigator
  1882. *
  1883. * @private
  1884. * @function Highcharts.Navigator#modifyBaseAxisExtremes
  1885. */
  1886. modifyBaseAxisExtremes: function () {
  1887. var baseXAxis = this,
  1888. navigator = baseXAxis.chart.navigator,
  1889. baseExtremes = baseXAxis.getExtremes(),
  1890. baseMin = baseExtremes.min,
  1891. baseMax = baseExtremes.max,
  1892. baseDataMin = baseExtremes.dataMin,
  1893. baseDataMax = baseExtremes.dataMax,
  1894. range = baseMax - baseMin,
  1895. stickToMin = navigator.stickToMin,
  1896. stickToMax = navigator.stickToMax,
  1897. overscroll = pick(baseXAxis.options.overscroll, 0),
  1898. newMax,
  1899. newMin,
  1900. navigatorSeries = navigator.series && navigator.series[0],
  1901. hasSetExtremes = !!baseXAxis.setExtremes,
  1902. // When the extremes have been set by range selector button, don't
  1903. // stick to min or max. The range selector buttons will handle the
  1904. // extremes. (#5489)
  1905. unmutable = baseXAxis.eventArgs &&
  1906. baseXAxis.eventArgs.trigger === 'rangeSelectorButton';
  1907. if (!unmutable) {
  1908. // If the zoomed range is already at the min, move it to the right
  1909. // as new data comes in
  1910. if (stickToMin) {
  1911. newMin = baseDataMin;
  1912. newMax = newMin + range;
  1913. }
  1914. // If the zoomed range is already at the max, move it to the right
  1915. // as new data comes in
  1916. if (stickToMax) {
  1917. newMax = baseDataMax + overscroll;
  1918. // if stickToMin is true, the new min value is set above
  1919. if (!stickToMin) {
  1920. newMin = Math.max(
  1921. newMax - range,
  1922. navigator.getBaseSeriesMin(
  1923. navigatorSeries && navigatorSeries.xData ?
  1924. navigatorSeries.xData[0] :
  1925. -Number.MAX_VALUE
  1926. )
  1927. );
  1928. }
  1929. }
  1930. // Update the extremes
  1931. if (hasSetExtremes && (stickToMin || stickToMax)) {
  1932. if (isNumber(newMin)) {
  1933. baseXAxis.min = baseXAxis.userMin = newMin;
  1934. baseXAxis.max = baseXAxis.userMax = newMax;
  1935. }
  1936. }
  1937. }
  1938. // Reset
  1939. navigator.stickToMin = navigator.stickToMax = null;
  1940. },
  1941. /**
  1942. * Handler for updated data on the base series. When data is modified, the
  1943. * navigator series must reflect it. This is called from the Chart.redraw
  1944. * function before axis and series extremes are computed.
  1945. *
  1946. * @private
  1947. * @function Highcharts.Navigator#updateDataHandler
  1948. */
  1949. updatedDataHandler: function () {
  1950. var navigator = this.chart.navigator,
  1951. baseSeries = this,
  1952. navigatorSeries = this.navigatorSeries,
  1953. xDataMin = navigator.getBaseSeriesMin(baseSeries.xData[0]);
  1954. // If the scrollbar is scrolled all the way to the right, keep right as
  1955. // new data comes in.
  1956. navigator.stickToMax = navigator.reversedExtremes ?
  1957. Math.round(navigator.zoomedMin) === 0 :
  1958. Math.round(navigator.zoomedMax) >= Math.round(navigator.size);
  1959. // Detect whether the zoomed area should stick to the minimum or
  1960. // maximum. If the current axis minimum falls outside the new updated
  1961. // dataset, we must adjust.
  1962. navigator.stickToMin = isNumber(baseSeries.xAxis.min) &&
  1963. (baseSeries.xAxis.min <= xDataMin) &&
  1964. (!this.chart.fixedRange || !navigator.stickToMax);
  1965. // Set the navigator series data to the new data of the base series
  1966. if (navigatorSeries && !navigator.hasNavigatorData) {
  1967. navigatorSeries.options.pointStart = baseSeries.xData[0];
  1968. navigatorSeries.setData(
  1969. baseSeries.options.data,
  1970. false,
  1971. null,
  1972. false
  1973. ); // #5414
  1974. }
  1975. },
  1976. /**
  1977. * Add chart events, like redrawing navigator, when chart requires that.
  1978. *
  1979. * @private
  1980. * @function Highcharts.Navigator#addChartEvents
  1981. */
  1982. addChartEvents: function () {
  1983. if (!this.eventsToUnbind) {
  1984. this.eventsToUnbind = [];
  1985. }
  1986. this.eventsToUnbind.push(
  1987. // Move the scrollbar after redraw, like after data updata even if
  1988. // axes don't redraw
  1989. addEvent(
  1990. this.chart,
  1991. 'redraw',
  1992. function () {
  1993. var navigator = this.navigator,
  1994. xAxis = navigator && (
  1995. navigator.baseSeries &&
  1996. navigator.baseSeries[0] &&
  1997. navigator.baseSeries[0].xAxis ||
  1998. navigator.scrollbar && this.xAxis[0]
  1999. ); // #5709
  2000. if (xAxis) {
  2001. navigator.render(xAxis.min, xAxis.max);
  2002. }
  2003. }
  2004. ),
  2005. // Make room for the navigator, can be placed around the chart:
  2006. addEvent(
  2007. this.chart,
  2008. 'getMargins',
  2009. function () {
  2010. var chart = this,
  2011. navigator = chart.navigator,
  2012. marginName = navigator.opposite ?
  2013. 'plotTop' : 'marginBottom';
  2014. if (chart.inverted) {
  2015. marginName = navigator.opposite ?
  2016. 'marginRight' : 'plotLeft';
  2017. }
  2018. chart[marginName] = (chart[marginName] || 0) + (
  2019. navigator.navigatorEnabled || !chart.inverted ?
  2020. navigator.outlineHeight :
  2021. 0
  2022. ) + navigator.navigatorOptions.margin;
  2023. }
  2024. )
  2025. );
  2026. },
  2027. /**
  2028. * Destroys allocated elements.
  2029. *
  2030. * @private
  2031. * @function Highcharts.Navigator#destroy
  2032. */
  2033. destroy: function () {
  2034. // Disconnect events added in addEvents
  2035. this.removeEvents();
  2036. if (this.xAxis) {
  2037. erase(this.chart.xAxis, this.xAxis);
  2038. erase(this.chart.axes, this.xAxis);
  2039. }
  2040. if (this.yAxis) {
  2041. erase(this.chart.yAxis, this.yAxis);
  2042. erase(this.chart.axes, this.yAxis);
  2043. }
  2044. // Destroy series
  2045. (this.series || []).forEach(function (s) {
  2046. if (s.destroy) {
  2047. s.destroy();
  2048. }
  2049. });
  2050. // Destroy properties
  2051. [
  2052. 'series', 'xAxis', 'yAxis', 'shades', 'outline', 'scrollbarTrack',
  2053. 'scrollbarRifles', 'scrollbarGroup', 'scrollbar', 'navigatorGroup',
  2054. 'rendered'
  2055. ].forEach(function (prop) {
  2056. if (this[prop] && this[prop].destroy) {
  2057. this[prop].destroy();
  2058. }
  2059. this[prop] = null;
  2060. }, this);
  2061. // Destroy elements in collection
  2062. [this.handles].forEach(function (coll) {
  2063. destroyObjectProperties(coll);
  2064. }, this);
  2065. }
  2066. };
  2067. H.Navigator = Navigator;
  2068. /*
  2069. * For Stock charts, override selection zooming with some special features
  2070. * because X axis zooming is already allowed by the Navigator and Range
  2071. * selector.
  2072. */
  2073. addEvent(Axis, 'zoom', function (e) {
  2074. var chart = this.chart,
  2075. chartOptions = chart.options,
  2076. zoomType = chartOptions.chart.zoomType,
  2077. pinchType = chartOptions.chart.pinchType,
  2078. previousZoom,
  2079. navigator = chartOptions.navigator,
  2080. rangeSelector = chartOptions.rangeSelector;
  2081. if (this.isXAxis && ((navigator && navigator.enabled) ||
  2082. (rangeSelector && rangeSelector.enabled))) {
  2083. // For y only zooming, ignore the X axis completely
  2084. if (zoomType === 'y') {
  2085. e.zoomed = false;
  2086. // For xy zooming, record the state of the zoom before zoom selection,
  2087. // then when the reset button is pressed, revert to this state. This
  2088. // should apply only if the chart is initialized with a range (#6612),
  2089. // otherwise zoom all the way out.
  2090. } else if (
  2091. (
  2092. (!isTouchDevice && zoomType === 'xy') ||
  2093. (isTouchDevice && pinchType === 'xy')
  2094. ) &&
  2095. this.options.range
  2096. ) {
  2097. previousZoom = this.previousZoom;
  2098. if (defined(e.newMin)) {
  2099. this.previousZoom = [this.min, this.max];
  2100. } else if (previousZoom) {
  2101. e.newMin = previousZoom[0];
  2102. e.newMax = previousZoom[1];
  2103. delete this.previousZoom;
  2104. }
  2105. }
  2106. }
  2107. if (e.zoomed !== undefined) {
  2108. e.preventDefault();
  2109. }
  2110. });
  2111. /**
  2112. * For Stock charts. For x only zooming, do not to create the zoom button
  2113. * because X axis zooming is already allowed by the Navigator and Range
  2114. * selector. (#9285)
  2115. */
  2116. addEvent(Chart, 'beforeShowResetZoom', function () {
  2117. var chartOptions = this.options,
  2118. navigator = chartOptions.navigator,
  2119. rangeSelector = chartOptions.rangeSelector;
  2120. if (
  2121. (
  2122. (navigator && navigator.enabled) ||
  2123. (rangeSelector && rangeSelector.enabled)
  2124. ) && (
  2125. (!isTouchDevice && chartOptions.chart.zoomType === 'x') ||
  2126. (isTouchDevice && chartOptions.chart.pinchType === 'x')
  2127. )
  2128. ) {
  2129. return false;
  2130. }
  2131. });
  2132. // Initialize navigator for stock charts
  2133. addEvent(Chart, 'beforeRender', function () {
  2134. var options = this.options;
  2135. if (options.navigator.enabled || options.scrollbar.enabled) {
  2136. this.scroller = this.navigator = new Navigator(this);
  2137. }
  2138. });
  2139. /*
  2140. * For stock charts, extend the Chart.setChartSize method so that we can set the
  2141. * final top position of the navigator once the height of the chart, including
  2142. * the legend, is determined. #367. We can't use Chart.getMargins, because
  2143. * labels offsets are not calculated yet.
  2144. */
  2145. addEvent(Chart, 'afterSetChartSize', function () {
  2146. var legend = this.legend,
  2147. navigator = this.navigator,
  2148. scrollbarHeight,
  2149. legendOptions,
  2150. xAxis,
  2151. yAxis;
  2152. if (navigator) {
  2153. legendOptions = legend && legend.options;
  2154. xAxis = navigator.xAxis;
  2155. yAxis = navigator.yAxis;
  2156. scrollbarHeight = navigator.scrollbarHeight;
  2157. // Compute the top position
  2158. if (this.inverted) {
  2159. navigator.left = navigator.opposite ?
  2160. this.chartWidth - scrollbarHeight - navigator.height :
  2161. this.spacing[3] + scrollbarHeight;
  2162. navigator.top = this.plotTop + scrollbarHeight;
  2163. } else {
  2164. navigator.left = this.plotLeft + scrollbarHeight;
  2165. navigator.top = navigator.navigatorOptions.top ||
  2166. this.chartHeight -
  2167. navigator.height -
  2168. scrollbarHeight -
  2169. this.spacing[2] -
  2170. (
  2171. this.rangeSelector && this.extraBottomMargin ?
  2172. this.rangeSelector.getHeight() :
  2173. 0
  2174. ) -
  2175. (
  2176. (
  2177. legendOptions &&
  2178. legendOptions.verticalAlign === 'bottom' &&
  2179. legendOptions.enabled &&
  2180. !legendOptions.floating
  2181. ) ?
  2182. legend.legendHeight + pick(legendOptions.margin, 10) :
  2183. 0
  2184. );
  2185. }
  2186. if (xAxis && yAxis) { // false if navigator is disabled (#904)
  2187. if (this.inverted) {
  2188. xAxis.options.left = yAxis.options.left = navigator.left;
  2189. } else {
  2190. xAxis.options.top = yAxis.options.top = navigator.top;
  2191. }
  2192. xAxis.setAxisSize();
  2193. yAxis.setAxisSize();
  2194. }
  2195. }
  2196. });
  2197. // Merge options, if no scrolling exists yet
  2198. addEvent(Chart, 'update', function (e) {
  2199. var navigatorOptions = (e.options.navigator || {}),
  2200. scrollbarOptions = (e.options.scrollbar || {});
  2201. if (!this.navigator && !this.scroller &&
  2202. (navigatorOptions.enabled || scrollbarOptions.enabled)
  2203. ) {
  2204. merge(true, this.options.navigator, navigatorOptions);
  2205. merge(true, this.options.scrollbar, scrollbarOptions);
  2206. delete e.options.navigator;
  2207. delete e.options.scrollbar;
  2208. }
  2209. });
  2210. // Initiate navigator, if no scrolling exists yet
  2211. addEvent(Chart, 'afterUpdate', function () {
  2212. if (!this.navigator && !this.scroller &&
  2213. (this.options.navigator.enabled || this.options.scrollbar.enabled)
  2214. ) {
  2215. this.scroller = this.navigator = new Navigator(this);
  2216. }
  2217. });
  2218. // Handle adding new series
  2219. addEvent(Chart, 'afterAddSeries', function () {
  2220. if (this.navigator) {
  2221. // Recompute which series should be shown in navigator, and add them
  2222. this.navigator.setBaseSeries(null, false);
  2223. }
  2224. });
  2225. // Handle updating series
  2226. addEvent(Series, 'afterUpdate', function () {
  2227. if (this.chart.navigator && !this.options.isInternal) {
  2228. this.chart.navigator.setBaseSeries(null, false);
  2229. }
  2230. });
  2231. Chart.prototype.callbacks.push(function (chart) {
  2232. var extremes,
  2233. navigator = chart.navigator;
  2234. // Initiate the navigator
  2235. if (navigator && chart.xAxis[0]) {
  2236. extremes = chart.xAxis[0].getExtremes();
  2237. navigator.render(extremes.min, extremes.max);
  2238. }
  2239. });