Navigator.js 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  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/Axis.js';
  12. import Chart from './Chart/Chart.js';
  13. import Color from './Color.js';
  14. var color = Color.parse;
  15. import H from './Globals.js';
  16. import NavigatorAxis from './Axis/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/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.
  285. *
  286. * Heads up:
  287. * In column-type navigator, zooming is limited to at least one
  288. * point with its `pointRange`.
  289. *
  290. * @sample {highstock} stock/navigator/column/
  291. * Column type navigator
  292. *
  293. * @type {string}
  294. * @default {highstock} `areaspline` if defined, otherwise `line`
  295. * @default {gantt} gantt
  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, 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. isNumber(navigator.zoomedMin) &&
  1183. isNumber(navigator.zoomedMax)) {
  1184. zoomedMin = Math.round(navigator.zoomedMin);
  1185. zoomedMax = Math.round(navigator.zoomedMax);
  1186. if (navigator.shades) {
  1187. navigator.drawMasks(zoomedMin, zoomedMax, inverted, verb);
  1188. }
  1189. if (navigator.outline) {
  1190. navigator.drawOutline(zoomedMin, zoomedMax, inverted, verb);
  1191. }
  1192. if (navigator.navigatorOptions.handles.enabled &&
  1193. Object.keys(navigator.handles).length ===
  1194. navigator.handles.length) {
  1195. navigator.drawHandle(zoomedMin, 0, inverted, verb);
  1196. navigator.drawHandle(zoomedMax, 1, inverted, verb);
  1197. }
  1198. }
  1199. };
  1200. /**
  1201. * Removes the event handlers attached previously with addEvents.
  1202. *
  1203. * @private
  1204. * @function Highcharts.Navigator#removeEvents
  1205. * @return {void}
  1206. */
  1207. Navigator.prototype.removeEvents = function () {
  1208. if (this.eventsToUnbind) {
  1209. this.eventsToUnbind.forEach(function (unbind) {
  1210. unbind();
  1211. });
  1212. this.eventsToUnbind = void 0;
  1213. }
  1214. this.removeBaseSeriesEvents();
  1215. };
  1216. /**
  1217. * Remove data events.
  1218. *
  1219. * @private
  1220. * @function Highcharts.Navigator#removeBaseSeriesEvents
  1221. * @return {void}
  1222. */
  1223. Navigator.prototype.removeBaseSeriesEvents = function () {
  1224. var baseSeries = this.baseSeries || [];
  1225. if (this.navigatorEnabled && baseSeries[0]) {
  1226. if (this.navigatorOptions.adaptToUpdatedData !== false) {
  1227. baseSeries.forEach(function (series) {
  1228. removeEvent(series, 'updatedData', this.updatedDataHandler);
  1229. }, this);
  1230. }
  1231. // We only listen for extremes-events on the first baseSeries
  1232. if (baseSeries[0].xAxis) {
  1233. removeEvent(baseSeries[0].xAxis, 'foundExtremes', this.modifyBaseAxisExtremes);
  1234. }
  1235. }
  1236. };
  1237. /**
  1238. * Initialize the Navigator object
  1239. *
  1240. * @private
  1241. * @function Highcharts.Navigator#init
  1242. *
  1243. * @param {Highcharts.Chart} chart
  1244. */
  1245. Navigator.prototype.init = function (chart) {
  1246. var chartOptions = chart.options, navigatorOptions = chartOptions.navigator, navigatorEnabled = navigatorOptions.enabled, scrollbarOptions = chartOptions.scrollbar, scrollbarEnabled = scrollbarOptions.enabled, height = navigatorEnabled ? navigatorOptions.height : 0, scrollbarHeight = scrollbarEnabled ?
  1247. scrollbarOptions.height :
  1248. 0;
  1249. this.handles = [];
  1250. this.shades = [];
  1251. this.chart = chart;
  1252. this.setBaseSeries();
  1253. this.height = height;
  1254. this.scrollbarHeight = scrollbarHeight;
  1255. this.scrollbarEnabled = scrollbarEnabled;
  1256. this.navigatorEnabled = navigatorEnabled;
  1257. this.navigatorOptions = navigatorOptions;
  1258. this.scrollbarOptions = scrollbarOptions;
  1259. this.outlineHeight = height + scrollbarHeight;
  1260. this.opposite = pick(navigatorOptions.opposite, Boolean(!navigatorEnabled && chart.inverted)); // #6262
  1261. var navigator = this, baseSeries = navigator.baseSeries, xAxisIndex = chart.xAxis.length, yAxisIndex = chart.yAxis.length, baseXaxis = baseSeries && baseSeries[0] && baseSeries[0].xAxis ||
  1262. chart.xAxis[0] || { options: {} };
  1263. chart.isDirtyBox = true;
  1264. if (navigator.navigatorEnabled) {
  1265. // an x axis is required for scrollbar also
  1266. navigator.xAxis = new Axis(chart, merge({
  1267. // inherit base xAxis' break and ordinal options
  1268. breaks: baseXaxis.options.breaks,
  1269. ordinal: baseXaxis.options.ordinal
  1270. }, navigatorOptions.xAxis, {
  1271. id: 'navigator-x-axis',
  1272. yAxis: 'navigator-y-axis',
  1273. isX: true,
  1274. type: 'datetime',
  1275. index: xAxisIndex,
  1276. isInternal: true,
  1277. offset: 0,
  1278. keepOrdinalPadding: true,
  1279. startOnTick: false,
  1280. endOnTick: false,
  1281. minPadding: 0,
  1282. maxPadding: 0,
  1283. zoomEnabled: false
  1284. }, chart.inverted ? {
  1285. offsets: [scrollbarHeight, 0, -scrollbarHeight, 0],
  1286. width: height
  1287. } : {
  1288. offsets: [0, -scrollbarHeight, 0, scrollbarHeight],
  1289. height: height
  1290. }));
  1291. navigator.yAxis = new Axis(chart, merge(navigatorOptions.yAxis, {
  1292. id: 'navigator-y-axis',
  1293. alignTicks: false,
  1294. offset: 0,
  1295. index: yAxisIndex,
  1296. isInternal: true,
  1297. zoomEnabled: false
  1298. }, chart.inverted ? {
  1299. width: height
  1300. } : {
  1301. height: height
  1302. }));
  1303. // If we have a base series, initialize the navigator series
  1304. if (baseSeries || navigatorOptions.series.data) {
  1305. navigator.updateNavigatorSeries(false);
  1306. // If not, set up an event to listen for added series
  1307. }
  1308. else if (chart.series.length === 0) {
  1309. navigator.unbindRedraw = addEvent(chart, 'beforeRedraw', function () {
  1310. // We've got one, now add it as base
  1311. if (chart.series.length > 0 && !navigator.series) {
  1312. navigator.setBaseSeries();
  1313. navigator.unbindRedraw(); // reset
  1314. }
  1315. });
  1316. }
  1317. navigator.reversedExtremes = (chart.inverted && !navigator.xAxis.reversed) || (!chart.inverted && navigator.xAxis.reversed);
  1318. // Render items, so we can bind events to them:
  1319. navigator.renderElements();
  1320. // Add mouse events
  1321. navigator.addMouseEvents();
  1322. // in case of scrollbar only, fake an x axis to get translation
  1323. }
  1324. else {
  1325. navigator.xAxis = {
  1326. chart: chart,
  1327. navigatorAxis: {
  1328. fake: true
  1329. },
  1330. translate: function (value, reverse) {
  1331. 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;
  1332. return reverse ?
  1333. // from pixel to value
  1334. (value * valueRange / scrollTrackWidth) + min :
  1335. // from value to pixel
  1336. scrollTrackWidth * (value - min) / valueRange;
  1337. },
  1338. toPixels: function (value) {
  1339. return this.translate(value);
  1340. },
  1341. toValue: function (value) {
  1342. return this.translate(value, true);
  1343. }
  1344. };
  1345. navigator.xAxis.navigatorAxis.axis = navigator.xAxis;
  1346. navigator.xAxis.navigatorAxis.toFixedRange = (NavigatorAxis.AdditionsClass.prototype.toFixedRange.bind(navigator.xAxis.navigatorAxis));
  1347. }
  1348. // Initialize the scrollbar
  1349. if (chart.options.scrollbar.enabled) {
  1350. chart.scrollbar = navigator.scrollbar = new Scrollbar(chart.renderer, merge(chart.options.scrollbar, {
  1351. margin: navigator.navigatorEnabled ? 0 : 10,
  1352. vertical: chart.inverted
  1353. }), chart);
  1354. addEvent(navigator.scrollbar, 'changed', function (e) {
  1355. var range = navigator.size, to = range * this.to, from = range * this.from;
  1356. navigator.hasDragged = navigator.scrollbar.hasDragged;
  1357. navigator.render(0, 0, from, to);
  1358. if (chart.options.scrollbar.liveRedraw ||
  1359. (e.DOMType !== 'mousemove' &&
  1360. e.DOMType !== 'touchmove')) {
  1361. setTimeout(function () {
  1362. navigator.onMouseUp(e);
  1363. });
  1364. }
  1365. });
  1366. }
  1367. // Add data events
  1368. navigator.addBaseSeriesEvents();
  1369. // Add redraw events
  1370. navigator.addChartEvents();
  1371. };
  1372. /**
  1373. * Get the union data extremes of the chart - the outer data extremes of the
  1374. * base X axis and the navigator axis.
  1375. *
  1376. * @private
  1377. * @function Highcharts.Navigator#getUnionExtremes
  1378. * @param {boolean} [returnFalseOnNoBaseSeries]
  1379. * as the param says.
  1380. * @return {Highcharts.Dictionary<(number|undefined)>|undefined}
  1381. */
  1382. Navigator.prototype.getUnionExtremes = function (returnFalseOnNoBaseSeries) {
  1383. var baseAxis = this.chart.xAxis[0], navAxis = this.xAxis, navAxisOptions = navAxis.options, baseAxisOptions = baseAxis.options, ret;
  1384. if (!returnFalseOnNoBaseSeries || baseAxis.dataMin !== null) {
  1385. ret = {
  1386. dataMin: pick(// #4053
  1387. navAxisOptions && navAxisOptions.min, numExt('min', baseAxisOptions.min, baseAxis.dataMin, navAxis.dataMin, navAxis.min)),
  1388. dataMax: pick(navAxisOptions && navAxisOptions.max, numExt('max', baseAxisOptions.max, baseAxis.dataMax, navAxis.dataMax, navAxis.max))
  1389. };
  1390. }
  1391. return ret;
  1392. };
  1393. /**
  1394. * Set the base series and update the navigator series from this. With a bit
  1395. * of modification we should be able to make this an API method to be called
  1396. * from the outside
  1397. *
  1398. * @private
  1399. * @function Highcharts.Navigator#setBaseSeries
  1400. * @param {Highcharts.SeriesOptionsType} [baseSeriesOptions]
  1401. * Additional series options for a navigator
  1402. * @param {boolean} [redraw]
  1403. * Whether to redraw after update.
  1404. * @return {void}
  1405. */
  1406. Navigator.prototype.setBaseSeries = function (baseSeriesOptions, redraw) {
  1407. var chart = this.chart, baseSeries = this.baseSeries = [];
  1408. baseSeriesOptions = (baseSeriesOptions ||
  1409. chart.options && chart.options.navigator.baseSeries ||
  1410. (chart.series.length ?
  1411. // Find the first non-navigator series (#8430)
  1412. find(chart.series, function (s) {
  1413. return !s.options.isInternal;
  1414. }).index :
  1415. 0));
  1416. // Iterate through series and add the ones that should be shown in
  1417. // navigator.
  1418. (chart.series || []).forEach(function (series, i) {
  1419. if (
  1420. // Don't include existing nav series
  1421. !series.options.isInternal &&
  1422. (series.options.showInNavigator ||
  1423. (i === baseSeriesOptions ||
  1424. series.options.id === baseSeriesOptions) &&
  1425. series.options.showInNavigator !== false)) {
  1426. baseSeries.push(series);
  1427. }
  1428. });
  1429. // When run after render, this.xAxis already exists
  1430. if (this.xAxis && !this.xAxis.navigatorAxis.fake) {
  1431. this.updateNavigatorSeries(true, redraw);
  1432. }
  1433. };
  1434. /**
  1435. * Update series in the navigator from baseSeries, adding new if does not
  1436. * exist.
  1437. *
  1438. * @private
  1439. * @function Highcharts.Navigator.updateNavigatorSeries
  1440. * @param {boolean} addEvents
  1441. * @param {boolean} [redraw]
  1442. * @return {void}
  1443. */
  1444. Navigator.prototype.updateNavigatorSeries = function (addEvents, redraw) {
  1445. var navigator = this, chart = navigator.chart, baseSeries = navigator.baseSeries, baseOptions, mergedNavSeriesOptions, chartNavigatorSeriesOptions = navigator.navigatorOptions.series, baseNavigatorOptions, navSeriesMixin = {
  1446. enableMouseTracking: false,
  1447. index: null,
  1448. linkedTo: null,
  1449. group: 'nav',
  1450. padXAxis: false,
  1451. xAxis: 'navigator-x-axis',
  1452. yAxis: 'navigator-y-axis',
  1453. showInLegend: false,
  1454. stacking: void 0,
  1455. isInternal: true,
  1456. states: {
  1457. inactive: {
  1458. opacity: 1
  1459. }
  1460. }
  1461. },
  1462. // Remove navigator series that are no longer in the baseSeries
  1463. navigatorSeries = navigator.series =
  1464. (navigator.series || []).filter(function (navSeries) {
  1465. var base = navSeries.baseSeries;
  1466. if (baseSeries.indexOf(base) < 0) { // Not in array
  1467. // If there is still a base series connected to this
  1468. // series, remove event handler and reference.
  1469. if (base) {
  1470. removeEvent(base, 'updatedData', navigator.updatedDataHandler);
  1471. delete base.navigatorSeries;
  1472. }
  1473. // Kill the nav series. It may already have been
  1474. // destroyed (#8715).
  1475. if (navSeries.chart) {
  1476. navSeries.destroy();
  1477. }
  1478. return false;
  1479. }
  1480. return true;
  1481. });
  1482. // Go through each base series and merge the options to create new
  1483. // series
  1484. if (baseSeries && baseSeries.length) {
  1485. baseSeries.forEach(function eachBaseSeries(base) {
  1486. var linkedNavSeries = base.navigatorSeries, userNavOptions = extend(
  1487. // Grab color and visibility from base as default
  1488. {
  1489. color: base.color,
  1490. visible: base.visible
  1491. }, !isArray(chartNavigatorSeriesOptions) ?
  1492. chartNavigatorSeriesOptions :
  1493. defaultOptions.navigator.series);
  1494. // Don't update if the series exists in nav and we have disabled
  1495. // adaptToUpdatedData.
  1496. if (linkedNavSeries &&
  1497. navigator.navigatorOptions.adaptToUpdatedData === false) {
  1498. return;
  1499. }
  1500. navSeriesMixin.name = 'Navigator ' + baseSeries.length;
  1501. baseOptions = base.options || {};
  1502. baseNavigatorOptions = baseOptions.navigatorOptions || {};
  1503. mergedNavSeriesOptions = merge(baseOptions, navSeriesMixin, userNavOptions, baseNavigatorOptions);
  1504. // Once nav series type is resolved, pick correct pointRange
  1505. mergedNavSeriesOptions.pointRange = pick(
  1506. // Stricte set pointRange in options
  1507. userNavOptions.pointRange, baseNavigatorOptions.pointRange,
  1508. // Fallback to default values, e.g. `null` for column
  1509. defaultOptions.plotOptions[mergedNavSeriesOptions.type || 'line'].pointRange);
  1510. // Merge data separately. Do a slice to avoid mutating the
  1511. // navigator options from base series (#4923).
  1512. var navigatorSeriesData = baseNavigatorOptions.data || userNavOptions.data;
  1513. navigator.hasNavigatorData =
  1514. navigator.hasNavigatorData || !!navigatorSeriesData;
  1515. mergedNavSeriesOptions.data =
  1516. navigatorSeriesData ||
  1517. baseOptions.data && baseOptions.data.slice(0);
  1518. // Update or add the series
  1519. if (linkedNavSeries && linkedNavSeries.options) {
  1520. linkedNavSeries.update(mergedNavSeriesOptions, redraw);
  1521. }
  1522. else {
  1523. base.navigatorSeries = chart.initSeries(mergedNavSeriesOptions);
  1524. base.navigatorSeries.baseSeries = base; // Store ref
  1525. navigatorSeries.push(base.navigatorSeries);
  1526. }
  1527. });
  1528. }
  1529. // If user has defined data (and no base series) or explicitly defined
  1530. // navigator.series as an array, we create these series on top of any
  1531. // base series.
  1532. if (chartNavigatorSeriesOptions.data &&
  1533. !(baseSeries && baseSeries.length) ||
  1534. isArray(chartNavigatorSeriesOptions)) {
  1535. navigator.hasNavigatorData = false;
  1536. // Allow navigator.series to be an array
  1537. chartNavigatorSeriesOptions =
  1538. splat(chartNavigatorSeriesOptions);
  1539. chartNavigatorSeriesOptions.forEach(function (userSeriesOptions, i) {
  1540. navSeriesMixin.name =
  1541. 'Navigator ' + (navigatorSeries.length + 1);
  1542. mergedNavSeriesOptions = merge(defaultOptions.navigator.series, {
  1543. // Since we don't have a base series to pull color from,
  1544. // try to fake it by using color from series with same
  1545. // index. Otherwise pull from the colors array. We need
  1546. // an explicit color as otherwise updates will increment
  1547. // color counter and we'll get a new color for each
  1548. // update of the nav series.
  1549. color: chart.series[i] &&
  1550. !chart.series[i].options.isInternal &&
  1551. chart.series[i].color ||
  1552. chart.options.colors[i] ||
  1553. chart.options.colors[0]
  1554. }, navSeriesMixin, userSeriesOptions);
  1555. mergedNavSeriesOptions.data = userSeriesOptions.data;
  1556. if (mergedNavSeriesOptions.data) {
  1557. navigator.hasNavigatorData = true;
  1558. navigatorSeries.push(chart.initSeries(mergedNavSeriesOptions));
  1559. }
  1560. });
  1561. }
  1562. if (addEvents) {
  1563. this.addBaseSeriesEvents();
  1564. }
  1565. };
  1566. /**
  1567. * Add data events.
  1568. * For example when main series is updated we need to recalculate extremes
  1569. *
  1570. * @private
  1571. * @function Highcharts.Navigator#addBaseSeriesEvent
  1572. * @return {void}
  1573. */
  1574. Navigator.prototype.addBaseSeriesEvents = function () {
  1575. var navigator = this, baseSeries = navigator.baseSeries || [];
  1576. // Bind modified extremes event to first base's xAxis only.
  1577. // In event of > 1 base-xAxes, the navigator will ignore those.
  1578. // Adding this multiple times to the same axis is no problem, as
  1579. // duplicates should be discarded by the browser.
  1580. if (baseSeries[0] && baseSeries[0].xAxis) {
  1581. addEvent(baseSeries[0].xAxis, 'foundExtremes', this.modifyBaseAxisExtremes);
  1582. }
  1583. baseSeries.forEach(function (base) {
  1584. // Link base series show/hide to navigator series visibility
  1585. addEvent(base, 'show', function () {
  1586. if (this.navigatorSeries) {
  1587. this.navigatorSeries.setVisible(true, false);
  1588. }
  1589. });
  1590. addEvent(base, 'hide', function () {
  1591. if (this.navigatorSeries) {
  1592. this.navigatorSeries.setVisible(false, false);
  1593. }
  1594. });
  1595. // Respond to updated data in the base series, unless explicitily
  1596. // not adapting to data changes.
  1597. if (this.navigatorOptions.adaptToUpdatedData !== false) {
  1598. if (base.xAxis) {
  1599. addEvent(base, 'updatedData', this.updatedDataHandler);
  1600. }
  1601. }
  1602. // Handle series removal
  1603. addEvent(base, 'remove', function () {
  1604. if (this.navigatorSeries) {
  1605. erase(navigator.series, this.navigatorSeries);
  1606. if (defined(this.navigatorSeries.options)) {
  1607. this.navigatorSeries.remove(false);
  1608. }
  1609. delete this.navigatorSeries;
  1610. }
  1611. });
  1612. }, this);
  1613. };
  1614. /**
  1615. * Get minimum from all base series connected to the navigator
  1616. * @private
  1617. * @param {number} currentSeriesMin
  1618. * Minium from the current series
  1619. * @return {number} Minimum from all series
  1620. */
  1621. Navigator.prototype.getBaseSeriesMin = function (currentSeriesMin) {
  1622. return this.baseSeries.reduce(function (min, series) {
  1623. // (#10193)
  1624. return Math.min(min, series.xData ? series.xData[0] : min);
  1625. }, currentSeriesMin);
  1626. };
  1627. /**
  1628. * Set the navigator x axis extremes to reflect the total. The navigator
  1629. * extremes should always be the extremes of the union of all series in the
  1630. * chart as well as the navigator series.
  1631. *
  1632. * @private
  1633. * @function Highcharts.Navigator#modifyNavigatorAxisExtremes
  1634. */
  1635. Navigator.prototype.modifyNavigatorAxisExtremes = function () {
  1636. var xAxis = this.xAxis, unionExtremes;
  1637. if (typeof xAxis.getExtremes !== 'undefined') {
  1638. unionExtremes = this.getUnionExtremes(true);
  1639. if (unionExtremes &&
  1640. (unionExtremes.dataMin !== xAxis.min ||
  1641. unionExtremes.dataMax !== xAxis.max)) {
  1642. xAxis.min = unionExtremes.dataMin;
  1643. xAxis.max = unionExtremes.dataMax;
  1644. }
  1645. }
  1646. };
  1647. /**
  1648. * Hook to modify the base axis extremes with information from the Navigator
  1649. *
  1650. * @private
  1651. * @function Highcharts.Navigator#modifyBaseAxisExtremes
  1652. */
  1653. Navigator.prototype.modifyBaseAxisExtremes = function () {
  1654. 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,
  1655. // When the extremes have been set by range selector button, don't
  1656. // stick to min or max. The range selector buttons will handle the
  1657. // extremes. (#5489)
  1658. unmutable = baseXAxis.eventArgs &&
  1659. baseXAxis.eventArgs.trigger === 'rangeSelectorButton';
  1660. if (!unmutable) {
  1661. // If the zoomed range is already at the min, move it to the right
  1662. // as new data comes in
  1663. if (stickToMin) {
  1664. newMin = baseDataMin;
  1665. newMax = newMin + range;
  1666. }
  1667. // If the zoomed range is already at the max, move it to the right
  1668. // as new data comes in
  1669. if (stickToMax) {
  1670. newMax = baseDataMax + overscroll;
  1671. // If stickToMin is true, the new min value is set above
  1672. if (!stickToMin) {
  1673. newMin = Math.max(baseDataMin, // don't go below data extremes (#13184)
  1674. newMax - range, navigator.getBaseSeriesMin(navigatorSeries && navigatorSeries.xData ?
  1675. navigatorSeries.xData[0] :
  1676. -Number.MAX_VALUE));
  1677. }
  1678. }
  1679. // Update the extremes
  1680. if (hasSetExtremes && (stickToMin || stickToMax)) {
  1681. if (isNumber(newMin)) {
  1682. baseXAxis.min = baseXAxis.userMin = newMin;
  1683. baseXAxis.max = baseXAxis.userMax = newMax;
  1684. }
  1685. }
  1686. }
  1687. // Reset
  1688. navigator.stickToMin =
  1689. navigator.stickToMax = null;
  1690. };
  1691. /**
  1692. * Handler for updated data on the base series. When data is modified, the
  1693. * navigator series must reflect it. This is called from the Chart.redraw
  1694. * function before axis and series extremes are computed.
  1695. *
  1696. * @private
  1697. * @function Highcharts.Navigator#updateDataHandler
  1698. */
  1699. Navigator.prototype.updatedDataHandler = function () {
  1700. var navigator = this.chart.navigator, baseSeries = this, navigatorSeries = this.navigatorSeries, xDataMin = navigator.getBaseSeriesMin(baseSeries.xData[0]);
  1701. // If the scrollbar is scrolled all the way to the right, keep right as
  1702. // new data comes in.
  1703. navigator.stickToMax = navigator.reversedExtremes ?
  1704. Math.round(navigator.zoomedMin) === 0 :
  1705. Math.round(navigator.zoomedMax) >= Math.round(navigator.size);
  1706. // Detect whether the zoomed area should stick to the minimum or
  1707. // maximum. If the current axis minimum falls outside the new updated
  1708. // dataset, we must adjust.
  1709. navigator.stickToMin = isNumber(baseSeries.xAxis.min) &&
  1710. (baseSeries.xAxis.min <= xDataMin) &&
  1711. (!this.chart.fixedRange || !navigator.stickToMax);
  1712. // Set the navigator series data to the new data of the base series
  1713. if (navigatorSeries && !navigator.hasNavigatorData) {
  1714. navigatorSeries.options.pointStart = baseSeries.xData[0];
  1715. navigatorSeries.setData(baseSeries.options.data, false, null, false); // #5414
  1716. }
  1717. };
  1718. /**
  1719. * Add chart events, like redrawing navigator, when chart requires that.
  1720. *
  1721. * @private
  1722. * @function Highcharts.Navigator#addChartEvents
  1723. * @return {void}
  1724. */
  1725. Navigator.prototype.addChartEvents = function () {
  1726. if (!this.eventsToUnbind) {
  1727. this.eventsToUnbind = [];
  1728. }
  1729. this.eventsToUnbind.push(
  1730. // Move the scrollbar after redraw, like after data updata even if
  1731. // axes don't redraw
  1732. addEvent(this.chart, 'redraw', function () {
  1733. var navigator = this.navigator, xAxis = navigator && (navigator.baseSeries &&
  1734. navigator.baseSeries[0] &&
  1735. navigator.baseSeries[0].xAxis ||
  1736. this.xAxis[0]); // #5709, #13114
  1737. if (xAxis) {
  1738. navigator.render(xAxis.min, xAxis.max);
  1739. }
  1740. }),
  1741. // Make room for the navigator, can be placed around the chart:
  1742. addEvent(this.chart, 'getMargins', function () {
  1743. var chart = this, navigator = chart.navigator, marginName = navigator.opposite ?
  1744. 'plotTop' : 'marginBottom';
  1745. if (chart.inverted) {
  1746. marginName = navigator.opposite ?
  1747. 'marginRight' : 'plotLeft';
  1748. }
  1749. chart[marginName] =
  1750. (chart[marginName] || 0) + (navigator.navigatorEnabled || !chart.inverted ?
  1751. navigator.outlineHeight :
  1752. 0) + navigator.navigatorOptions.margin;
  1753. }));
  1754. };
  1755. /**
  1756. * Destroys allocated elements.
  1757. *
  1758. * @private
  1759. * @function Highcharts.Navigator#destroy
  1760. */
  1761. Navigator.prototype.destroy = function () {
  1762. // Disconnect events added in addEvents
  1763. this.removeEvents();
  1764. if (this.xAxis) {
  1765. erase(this.chart.xAxis, this.xAxis);
  1766. erase(this.chart.axes, this.xAxis);
  1767. }
  1768. if (this.yAxis) {
  1769. erase(this.chart.yAxis, this.yAxis);
  1770. erase(this.chart.axes, this.yAxis);
  1771. }
  1772. // Destroy series
  1773. (this.series || []).forEach(function (s) {
  1774. if (s.destroy) {
  1775. s.destroy();
  1776. }
  1777. });
  1778. // Destroy properties
  1779. [
  1780. 'series', 'xAxis', 'yAxis', 'shades', 'outline', 'scrollbarTrack',
  1781. 'scrollbarRifles', 'scrollbarGroup', 'scrollbar', 'navigatorGroup',
  1782. 'rendered'
  1783. ].forEach(function (prop) {
  1784. if (this[prop] && this[prop].destroy) {
  1785. this[prop].destroy();
  1786. }
  1787. this[prop] = null;
  1788. }, this);
  1789. // Destroy elements in collection
  1790. [this.handles].forEach(function (coll) {
  1791. destroyObjectProperties(coll);
  1792. }, this);
  1793. };
  1794. return Navigator;
  1795. }());
  1796. // End of prototype
  1797. if (!H.Navigator) {
  1798. H.Navigator = Navigator;
  1799. NavigatorAxis.compose(Axis);
  1800. // For Stock charts. For x only zooming, do not to create the zoom button
  1801. // because X axis zooming is already allowed by the Navigator and Range
  1802. // selector. (#9285)
  1803. addEvent(Chart, 'beforeShowResetZoom', function () {
  1804. var chartOptions = this.options, navigator = chartOptions.navigator, rangeSelector = chartOptions.rangeSelector;
  1805. if (((navigator && navigator.enabled) ||
  1806. (rangeSelector && rangeSelector.enabled)) &&
  1807. ((!isTouchDevice && chartOptions.chart.zoomType === 'x') ||
  1808. (isTouchDevice && chartOptions.chart.pinchType === 'x'))) {
  1809. return false;
  1810. }
  1811. });
  1812. // Initialize navigator for stock charts
  1813. addEvent(Chart, 'beforeRender', function () {
  1814. var options = this.options;
  1815. if (options.navigator.enabled ||
  1816. options.scrollbar.enabled) {
  1817. this.scroller = this.navigator = new Navigator(this);
  1818. }
  1819. });
  1820. // For stock charts, extend the Chart.setChartSize method so that we can set
  1821. // the final top position of the navigator once the height of the chart,
  1822. // including the legend, is determined. #367. We can't use Chart.getMargins,
  1823. // because labels offsets are not calculated yet.
  1824. addEvent(Chart, 'afterSetChartSize', function () {
  1825. var legend = this.legend, navigator = this.navigator, scrollbarHeight, legendOptions, xAxis, yAxis;
  1826. if (navigator) {
  1827. legendOptions = legend && legend.options;
  1828. xAxis = navigator.xAxis;
  1829. yAxis = navigator.yAxis;
  1830. scrollbarHeight = navigator.scrollbarHeight;
  1831. // Compute the top position
  1832. if (this.inverted) {
  1833. navigator.left = navigator.opposite ?
  1834. this.chartWidth - scrollbarHeight -
  1835. navigator.height :
  1836. this.spacing[3] + scrollbarHeight;
  1837. navigator.top = this.plotTop + scrollbarHeight;
  1838. }
  1839. else {
  1840. navigator.left = this.plotLeft + scrollbarHeight;
  1841. navigator.top = navigator.navigatorOptions.top ||
  1842. this.chartHeight -
  1843. navigator.height -
  1844. scrollbarHeight -
  1845. this.spacing[2] -
  1846. (this.rangeSelector && this.extraBottomMargin ?
  1847. this.rangeSelector.getHeight() :
  1848. 0) -
  1849. ((legendOptions &&
  1850. legendOptions.verticalAlign === 'bottom' &&
  1851. legendOptions.layout !== 'proximate' && // #13392
  1852. legendOptions.enabled &&
  1853. !legendOptions.floating) ?
  1854. legend.legendHeight +
  1855. pick(legendOptions.margin, 10) :
  1856. 0) -
  1857. (this.titleOffset ? this.titleOffset[2] : 0);
  1858. }
  1859. if (xAxis && yAxis) { // false if navigator is disabled (#904)
  1860. if (this.inverted) {
  1861. xAxis.options.left = yAxis.options.left = navigator.left;
  1862. }
  1863. else {
  1864. xAxis.options.top = yAxis.options.top = navigator.top;
  1865. }
  1866. xAxis.setAxisSize();
  1867. yAxis.setAxisSize();
  1868. }
  1869. }
  1870. });
  1871. // Merge options, if no scrolling exists yet
  1872. addEvent(Chart, 'update', function (e) {
  1873. var navigatorOptions = (e.options.navigator || {}), scrollbarOptions = (e.options.scrollbar || {});
  1874. if (!this.navigator && !this.scroller &&
  1875. (navigatorOptions.enabled || scrollbarOptions.enabled)) {
  1876. merge(true, this.options.navigator, navigatorOptions);
  1877. merge(true, this.options.scrollbar, scrollbarOptions);
  1878. delete e.options.navigator;
  1879. delete e.options.scrollbar;
  1880. }
  1881. });
  1882. // Initialize navigator, if no scrolling exists yet
  1883. addEvent(Chart, 'afterUpdate', function (event) {
  1884. if (!this.navigator && !this.scroller &&
  1885. (this.options.navigator.enabled ||
  1886. this.options.scrollbar.enabled)) {
  1887. this.scroller = this.navigator = new Navigator(this);
  1888. if (pick(event.redraw, true)) {
  1889. this.redraw(event.animation); // #7067
  1890. }
  1891. }
  1892. });
  1893. // Handle adding new series
  1894. addEvent(Chart, 'afterAddSeries', function () {
  1895. if (this.navigator) {
  1896. // Recompute which series should be shown in navigator, and add them
  1897. this.navigator.setBaseSeries(null, false);
  1898. }
  1899. });
  1900. // Handle updating series
  1901. addEvent(Series, 'afterUpdate', function () {
  1902. if (this.chart.navigator && !this.options.isInternal) {
  1903. this.chart.navigator.setBaseSeries(null, false);
  1904. }
  1905. });
  1906. Chart.prototype.callbacks.push(function (chart) {
  1907. var extremes, navigator = chart.navigator;
  1908. // Initialize the navigator
  1909. if (navigator && chart.xAxis[0]) {
  1910. extremes = chart.xAxis[0].getExtremes();
  1911. navigator.render(extremes.min, extremes.max);
  1912. }
  1913. });
  1914. }
  1915. H.Navigator = Navigator;
  1916. export default H.Navigator;