Navigator.js 78 KB

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