Navigator.js 77 KB

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