Pointer.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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 Color from './Color.js';
  12. var color = Color.parse;
  13. import H from './Globals.js';
  14. var charts = H.charts, noop = H.noop;
  15. import Tooltip from './Tooltip.js';
  16. import U from './Utilities.js';
  17. var addEvent = U.addEvent, attr = U.attr, css = U.css, defined = U.defined, extend = U.extend, find = U.find, fireEvent = U.fireEvent, isNumber = U.isNumber, isObject = U.isObject, objectEach = U.objectEach, offset = U.offset, pick = U.pick, splat = U.splat;
  18. /**
  19. * One position in relation to an axis.
  20. *
  21. * @interface Highcharts.PointerAxisCoordinateObject
  22. */ /**
  23. * Related axis.
  24. *
  25. * @name Highcharts.PointerAxisCoordinateObject#axis
  26. * @type {Highcharts.Axis}
  27. */ /**
  28. * Axis value.
  29. *
  30. * @name Highcharts.PointerAxisCoordinateObject#value
  31. * @type {number}
  32. */
  33. /**
  34. * Positions in terms of axis values.
  35. *
  36. * @interface Highcharts.PointerAxisCoordinatesObject
  37. */ /**
  38. * Positions on the x-axis.
  39. * @name Highcharts.PointerAxisCoordinatesObject#xAxis
  40. * @type {Array<Highcharts.PointerAxisCoordinateObject>}
  41. */ /**
  42. * Positions on the y-axis.
  43. * @name Highcharts.PointerAxisCoordinatesObject#yAxis
  44. * @type {Array<Highcharts.PointerAxisCoordinateObject>}
  45. */
  46. /**
  47. * Pointer coordinates.
  48. *
  49. * @interface Highcharts.PointerCoordinatesObject
  50. */ /**
  51. * @name Highcharts.PointerCoordinatesObject#chartX
  52. * @type {number}
  53. */ /**
  54. * @name Highcharts.PointerCoordinatesObject#chartY
  55. * @type {number}
  56. */
  57. /**
  58. * A native browser mouse or touch event, extended with position information
  59. * relative to the {@link Chart.container}.
  60. *
  61. * @interface Highcharts.PointerEventObject
  62. * @extends global.PointerEvent
  63. */ /**
  64. * The X coordinate of the pointer interaction relative to the chart.
  65. *
  66. * @name Highcharts.PointerEventObject#chartX
  67. * @type {number}
  68. */ /**
  69. * The Y coordinate of the pointer interaction relative to the chart.
  70. *
  71. * @name Highcharts.PointerEventObject#chartY
  72. * @type {number}
  73. */
  74. /**
  75. * Axis-specific data of a selection.
  76. *
  77. * @interface Highcharts.SelectDataObject
  78. */ /**
  79. * @name Highcharts.SelectDataObject#axis
  80. * @type {Highcharts.Axis}
  81. */ /**
  82. * @name Highcharts.SelectDataObject#max
  83. * @type {number}
  84. */ /**
  85. * @name Highcharts.SelectDataObject#min
  86. * @type {number}
  87. */
  88. /**
  89. * Object for select events.
  90. *
  91. * @interface Highcharts.SelectEventObject
  92. */ /**
  93. * @name Highcharts.SelectEventObject#originalEvent
  94. * @type {global.Event}
  95. */ /**
  96. * @name Highcharts.SelectEventObject#xAxis
  97. * @type {Array<Highcharts.SelectDataObject>}
  98. */ /**
  99. * @name Highcharts.SelectEventObject#yAxis
  100. * @type {Array<Highcharts.SelectDataObject>}
  101. */
  102. ''; // detach doclets above
  103. /* eslint-disable no-invalid-this, valid-jsdoc */
  104. /**
  105. * The mouse and touch tracker object. Each {@link Chart} item has one
  106. * assosiated Pointer item that can be accessed from the {@link Chart.pointer}
  107. * property.
  108. *
  109. * @class
  110. * @name Highcharts.Pointer
  111. *
  112. * @param {Highcharts.Chart} chart
  113. * The chart instance.
  114. *
  115. * @param {Highcharts.Options} options
  116. * The root options object. The pointer uses options from the chart and
  117. * tooltip structures.
  118. */
  119. var Pointer = /** @class */ (function () {
  120. /* *
  121. *
  122. * Constructors
  123. *
  124. * */
  125. function Pointer(chart, options) {
  126. this.lastValidTouch = {};
  127. this.pinchDown = [];
  128. this.runChartClick = false;
  129. this.chart = chart;
  130. this.hasDragged = false;
  131. this.options = options;
  132. this.unbindContainerMouseLeave = function () { };
  133. this.init(chart, options);
  134. }
  135. /* *
  136. *
  137. * Functions
  138. *
  139. * */
  140. /**
  141. * Set inactive state to all series that are not currently hovered,
  142. * or, if `inactiveOtherPoints` is set to true, set inactive state to
  143. * all points within that series.
  144. *
  145. * @private
  146. * @function Highcharts.Pointer#applyInactiveState
  147. * @param {Array<Highcharts.Point>} points
  148. * Currently hovered points
  149. */
  150. Pointer.prototype.applyInactiveState = function (points) {
  151. var activeSeries = [], series;
  152. // Get all active series from the hovered points
  153. (points || []).forEach(function (item) {
  154. series = item.series;
  155. // Include itself
  156. activeSeries.push(series);
  157. // Include parent series
  158. if (series.linkedParent) {
  159. activeSeries.push(series.linkedParent);
  160. }
  161. // Include all child series
  162. if (series.linkedSeries) {
  163. activeSeries = activeSeries.concat(series.linkedSeries);
  164. }
  165. // Include navigator series
  166. if (series.navigatorSeries) {
  167. activeSeries.push(series.navigatorSeries);
  168. }
  169. });
  170. // Now loop over all series, filtering out active series
  171. this.chart.series.forEach(function (inactiveSeries) {
  172. if (activeSeries.indexOf(inactiveSeries) === -1) {
  173. // Inactive series
  174. inactiveSeries.setState('inactive', true);
  175. }
  176. else if (inactiveSeries.options.inactiveOtherPoints) {
  177. // Active series, but other points should be inactivated
  178. inactiveSeries.setAllPointsToState('inactive');
  179. }
  180. });
  181. };
  182. /**
  183. * Destroys the Pointer object and disconnects DOM events.
  184. *
  185. * @function Highcharts.Pointer#destroy
  186. */
  187. Pointer.prototype.destroy = function () {
  188. var pointer = this;
  189. if (typeof pointer.unDocMouseMove !== 'undefined') {
  190. pointer.unDocMouseMove();
  191. }
  192. this.unbindContainerMouseLeave();
  193. if (!H.chartCount) {
  194. if (H.unbindDocumentMouseUp) {
  195. H.unbindDocumentMouseUp = H.unbindDocumentMouseUp();
  196. }
  197. if (H.unbindDocumentTouchEnd) {
  198. H.unbindDocumentTouchEnd = H.unbindDocumentTouchEnd();
  199. }
  200. }
  201. // memory and CPU leak
  202. clearInterval(pointer.tooltipTimeout);
  203. objectEach(pointer, function (_val, prop) {
  204. pointer[prop] = void 0;
  205. });
  206. };
  207. /**
  208. * Perform a drag operation in response to a mousemove event while the mouse
  209. * is down.
  210. *
  211. * @private
  212. * @function Highcharts.Pointer#drag
  213. *
  214. * @param {Highcharts.PointerEventObject} e
  215. *
  216. * @return {void}
  217. */
  218. Pointer.prototype.drag = function (e) {
  219. var chart = this.chart, chartOptions = chart.options.chart, chartX = e.chartX, chartY = e.chartY, zoomHor = this.zoomHor, zoomVert = this.zoomVert, plotLeft = chart.plotLeft, plotTop = chart.plotTop, plotWidth = chart.plotWidth, plotHeight = chart.plotHeight, clickedInside, size, selectionMarker = this.selectionMarker, mouseDownX = (this.mouseDownX || 0), mouseDownY = (this.mouseDownY || 0), panningEnabled = isObject(chartOptions.panning) ?
  220. chartOptions.panning && chartOptions.panning.enabled :
  221. chartOptions.panning, panKey = (chartOptions.panKey && e[chartOptions.panKey + 'Key']);
  222. // If the device supports both touch and mouse (like IE11), and we are
  223. // touch-dragging inside the plot area, don't handle the mouse event.
  224. // #4339.
  225. if (selectionMarker && selectionMarker.touch) {
  226. return;
  227. }
  228. // If the mouse is outside the plot area, adjust to cooordinates
  229. // inside to prevent the selection marker from going outside
  230. if (chartX < plotLeft) {
  231. chartX = plotLeft;
  232. }
  233. else if (chartX > plotLeft + plotWidth) {
  234. chartX = plotLeft + plotWidth;
  235. }
  236. if (chartY < plotTop) {
  237. chartY = plotTop;
  238. }
  239. else if (chartY > plotTop + plotHeight) {
  240. chartY = plotTop + plotHeight;
  241. }
  242. // determine if the mouse has moved more than 10px
  243. this.hasDragged = Math.sqrt(Math.pow(mouseDownX - chartX, 2) +
  244. Math.pow(mouseDownY - chartY, 2));
  245. if (this.hasDragged > 10) {
  246. clickedInside = chart.isInsidePlot(mouseDownX - plotLeft, mouseDownY - plotTop);
  247. // make a selection
  248. if (chart.hasCartesianSeries &&
  249. (this.zoomX || this.zoomY) &&
  250. clickedInside &&
  251. !panKey) {
  252. if (!selectionMarker) {
  253. this.selectionMarker = selectionMarker =
  254. chart.renderer.rect(plotLeft, plotTop, zoomHor ? 1 : plotWidth, zoomVert ? 1 : plotHeight, 0)
  255. .attr({
  256. 'class': 'highcharts-selection-marker',
  257. zIndex: 7
  258. })
  259. .add();
  260. if (!chart.styledMode) {
  261. selectionMarker.attr({
  262. fill: (chartOptions.selectionMarkerFill ||
  263. color('#335cad')
  264. .setOpacity(0.25).get())
  265. });
  266. }
  267. }
  268. }
  269. // adjust the width of the selection marker
  270. if (selectionMarker && zoomHor) {
  271. size = chartX - mouseDownX;
  272. selectionMarker.attr({
  273. width: Math.abs(size),
  274. x: (size > 0 ? 0 : size) + mouseDownX
  275. });
  276. }
  277. // adjust the height of the selection marker
  278. if (selectionMarker && zoomVert) {
  279. size = chartY - mouseDownY;
  280. selectionMarker.attr({
  281. height: Math.abs(size),
  282. y: (size > 0 ? 0 : size) + mouseDownY
  283. });
  284. }
  285. // panning
  286. if (clickedInside &&
  287. !selectionMarker &&
  288. panningEnabled) {
  289. chart.pan(e, chartOptions.panning);
  290. }
  291. }
  292. };
  293. /**
  294. * Start a drag operation.
  295. *
  296. * @private
  297. * @function Highcharts.Pointer#dragStart
  298. *
  299. * @param {Highcharts.PointerEventObject} e
  300. *
  301. * @return {void}
  302. */
  303. Pointer.prototype.dragStart = function (e) {
  304. var chart = this.chart;
  305. // Record the start position
  306. chart.mouseIsDown = e.type;
  307. chart.cancelClick = false;
  308. chart.mouseDownX = this.mouseDownX = e.chartX;
  309. chart.mouseDownY = this.mouseDownY = e.chartY;
  310. };
  311. /**
  312. * On mouse up or touch end across the entire document, drop the selection.
  313. *
  314. * @private
  315. * @function Highcharts.Pointer#drop
  316. *
  317. * @param {global.Event} e
  318. */
  319. Pointer.prototype.drop = function (e) {
  320. var pointer = this, chart = this.chart, hasPinched = this.hasPinched;
  321. if (this.selectionMarker) {
  322. var selectionData = {
  323. originalEvent: e,
  324. xAxis: [],
  325. yAxis: []
  326. }, selectionBox = this.selectionMarker, selectionLeft = selectionBox.attr ?
  327. selectionBox.attr('x') :
  328. selectionBox.x, selectionTop = selectionBox.attr ?
  329. selectionBox.attr('y') :
  330. selectionBox.y, selectionWidth = selectionBox.attr ?
  331. selectionBox.attr('width') :
  332. selectionBox.width, selectionHeight = selectionBox.attr ?
  333. selectionBox.attr('height') :
  334. selectionBox.height, runZoom;
  335. // a selection has been made
  336. if (this.hasDragged || hasPinched) {
  337. // record each axis' min and max
  338. chart.axes.forEach(function (axis) {
  339. if (axis.zoomEnabled &&
  340. defined(axis.min) &&
  341. (hasPinched ||
  342. pointer[{
  343. xAxis: 'zoomX',
  344. yAxis: 'zoomY'
  345. }[axis.coll]]) &&
  346. isNumber(selectionLeft) &&
  347. isNumber(selectionTop)) { // #859, #3569
  348. var horiz = axis.horiz, minPixelPadding = e.type === 'touchend' ?
  349. axis.minPixelPadding :
  350. 0, // #1207, #3075
  351. selectionMin = axis.toValue((horiz ? selectionLeft : selectionTop) +
  352. minPixelPadding), selectionMax = axis.toValue((horiz ?
  353. selectionLeft + selectionWidth :
  354. selectionTop + selectionHeight) - minPixelPadding);
  355. selectionData[axis.coll].push({
  356. axis: axis,
  357. // Min/max for reversed axes
  358. min: Math.min(selectionMin, selectionMax),
  359. max: Math.max(selectionMin, selectionMax)
  360. });
  361. runZoom = true;
  362. }
  363. });
  364. if (runZoom) {
  365. fireEvent(chart, 'selection', selectionData, function (args) {
  366. chart.zoom(extend(args, hasPinched ?
  367. { animation: false } :
  368. null));
  369. });
  370. }
  371. }
  372. if (isNumber(chart.index)) {
  373. this.selectionMarker = this.selectionMarker.destroy();
  374. }
  375. // Reset scaling preview
  376. if (hasPinched) {
  377. this.scaleGroups();
  378. }
  379. }
  380. // Reset all. Check isNumber because it may be destroyed on mouse up
  381. // (#877)
  382. if (chart && isNumber(chart.index)) {
  383. css(chart.container, { cursor: chart._cursor });
  384. chart.cancelClick = this.hasDragged > 10; // #370
  385. chart.mouseIsDown = this.hasDragged = this.hasPinched = false;
  386. this.pinchDown = [];
  387. }
  388. };
  389. /**
  390. * Finds the closest point to a set of coordinates, using the k-d-tree
  391. * algorithm.
  392. *
  393. * @function Highcharts.Pointer#findNearestKDPoint
  394. *
  395. * @param {Array<Highcharts.Series>} series
  396. * All the series to search in.
  397. *
  398. * @param {boolean|undefined} shared
  399. * Whether it is a shared tooltip or not.
  400. *
  401. * @param {Highcharts.PointerEventObject} e
  402. * The pointer event object, containing chart coordinates of the
  403. * pointer.
  404. *
  405. * @return {Highcharts.Point|undefined}
  406. * The point closest to given coordinates.
  407. */
  408. Pointer.prototype.findNearestKDPoint = function (series, shared, e) {
  409. var chart = this.chart;
  410. var hoverPoint = chart.hoverPoint;
  411. var tooltip = chart.tooltip;
  412. if (hoverPoint &&
  413. tooltip &&
  414. tooltip.isStickyOnContact()) {
  415. return hoverPoint;
  416. }
  417. var closest;
  418. /** @private */
  419. function sort(p1, p2) {
  420. var isCloserX = p1.distX - p2.distX, isCloser = p1.dist - p2.dist, isAbove = (p2.series.group && p2.series.group.zIndex) -
  421. (p1.series.group && p1.series.group.zIndex), result;
  422. // We have two points which are not in the same place on xAxis
  423. // and shared tooltip:
  424. if (isCloserX !== 0 && shared) { // #5721
  425. result = isCloserX;
  426. // Points are not exactly in the same place on x/yAxis:
  427. }
  428. else if (isCloser !== 0) {
  429. result = isCloser;
  430. // The same xAxis and yAxis position, sort by z-index:
  431. }
  432. else if (isAbove !== 0) {
  433. result = isAbove;
  434. // The same zIndex, sort by array index:
  435. }
  436. else {
  437. result =
  438. p1.series.index > p2.series.index ?
  439. -1 :
  440. 1;
  441. }
  442. return result;
  443. }
  444. series.forEach(function (s) {
  445. var noSharedTooltip = s.noSharedTooltip && shared, compareX = (!noSharedTooltip &&
  446. s.options.findNearestPointBy.indexOf('y') < 0), point = s.searchPoint(e, compareX);
  447. if ( // Check that we actually found a point on the series.
  448. isObject(point, true) &&
  449. // Use the new point if it is closer.
  450. (!isObject(closest, true) ||
  451. (sort(closest, point) > 0))) {
  452. closest = point;
  453. }
  454. });
  455. return closest;
  456. };
  457. /**
  458. * @private
  459. * @function Highcharts.Pointer#getChartCoordinatesFromPoint
  460. * @param {Highcharts.Point} point
  461. * @param {boolean} [inverted]
  462. * @return {Highcharts.PointerCoordinatesObject|undefined}
  463. */
  464. Pointer.prototype.getChartCoordinatesFromPoint = function (point, inverted) {
  465. var series = point.series, xAxis = series.xAxis, yAxis = series.yAxis, plotX = pick(point.clientX, point.plotX), shapeArgs = point.shapeArgs;
  466. if (xAxis && yAxis) {
  467. return inverted ? {
  468. chartX: xAxis.len + xAxis.pos - plotX,
  469. chartY: yAxis.len + yAxis.pos - point.plotY
  470. } : {
  471. chartX: plotX + xAxis.pos,
  472. chartY: point.plotY + yAxis.pos
  473. };
  474. }
  475. if (shapeArgs && shapeArgs.x && shapeArgs.y) {
  476. // E.g. pies do not have axes
  477. return {
  478. chartX: shapeArgs.x,
  479. chartY: shapeArgs.y
  480. };
  481. }
  482. };
  483. /**
  484. * Return the cached chartPosition if it is available on the Pointer,
  485. * otherwise find it. Running offset is quite expensive, so it should be
  486. * avoided when we know the chart hasn't moved.
  487. *
  488. * @function Highcharts.Pointer#getChartPosition
  489. *
  490. * @return {Highcharts.OffsetObject}
  491. * The offset of the chart container within the page
  492. */
  493. Pointer.prototype.getChartPosition = function () {
  494. return (this.chartPosition ||
  495. (this.chartPosition = offset(this.chart.container)));
  496. };
  497. /**
  498. * Get the click position in terms of axis values.
  499. *
  500. * @function Highcharts.Pointer#getCoordinates
  501. *
  502. * @param {Highcharts.PointerEventObject} e
  503. * Pointer event, extended with `chartX` and `chartY` properties.
  504. *
  505. * @return {Highcharts.PointerAxisCoordinatesObject}
  506. */
  507. Pointer.prototype.getCoordinates = function (e) {
  508. var coordinates = {
  509. xAxis: [],
  510. yAxis: []
  511. };
  512. this.chart.axes.forEach(function (axis) {
  513. coordinates[axis.isXAxis ? 'xAxis' : 'yAxis'].push({
  514. axis: axis,
  515. value: axis.toValue(e[axis.horiz ? 'chartX' : 'chartY'])
  516. });
  517. });
  518. return coordinates;
  519. };
  520. /**
  521. * Calculates what is the current hovered point/points and series.
  522. *
  523. * @private
  524. * @function Highcharts.Pointer#getHoverData
  525. *
  526. * @param {Highcharts.Point|undefined} existingHoverPoint
  527. * The point currrently beeing hovered.
  528. *
  529. * @param {Highcharts.Series|undefined} existingHoverSeries
  530. * The series currently beeing hovered.
  531. *
  532. * @param {Array<Highcharts.Series>} series
  533. * All the series in the chart.
  534. *
  535. * @param {boolean} isDirectTouch
  536. * Is the pointer directly hovering the point.
  537. *
  538. * @param {boolean|undefined} shared
  539. * Whether it is a shared tooltip or not.
  540. *
  541. * @param {Highcharts.PointerEventObject} [e]
  542. * The triggering event, containing chart coordinates of the pointer.
  543. *
  544. * @return {object}
  545. * Object containing resulting hover data: hoverPoint, hoverSeries,
  546. * and hoverPoints.
  547. */
  548. Pointer.prototype.getHoverData = function (existingHoverPoint, existingHoverSeries, series, isDirectTouch, shared, e) {
  549. var hoverPoint, hoverPoints = [], hoverSeries = existingHoverSeries, useExisting = !!(isDirectTouch && existingHoverPoint), notSticky = hoverSeries && !hoverSeries.stickyTracking,
  550. // Which series to look in for the hover point
  551. searchSeries,
  552. // Parameters needed for beforeGetHoverData event.
  553. eventArgs = {
  554. chartX: e ? e.chartX : void 0,
  555. chartY: e ? e.chartY : void 0,
  556. shared: shared
  557. }, filter = function (s) {
  558. return (s.visible &&
  559. !(!shared && s.directTouch) && // #3821
  560. pick(s.options.enableMouseTracking, true));
  561. };
  562. // Find chart.hoverPane and update filter method in polar.
  563. fireEvent(this, 'beforeGetHoverData', eventArgs);
  564. searchSeries = notSticky ?
  565. // Only search on hovered series if it has stickyTracking false
  566. [hoverSeries] :
  567. // Filter what series to look in.
  568. series.filter(function (s) {
  569. return eventArgs.filter ? eventArgs.filter(s) : filter(s) &&
  570. s.stickyTracking;
  571. });
  572. // Use existing hovered point or find the one closest to coordinates.
  573. hoverPoint = useExisting || !e ?
  574. existingHoverPoint :
  575. this.findNearestKDPoint(searchSeries, shared, e);
  576. // Assign hover series
  577. hoverSeries = hoverPoint && hoverPoint.series;
  578. // If we have a hoverPoint, assign hoverPoints.
  579. if (hoverPoint) {
  580. // When tooltip is shared, it displays more than one point
  581. if (shared && !hoverSeries.noSharedTooltip) {
  582. searchSeries = series.filter(function (s) {
  583. return eventArgs.filter ?
  584. eventArgs.filter(s) : filter(s) && !s.noSharedTooltip;
  585. });
  586. // Get all points with the same x value as the hoverPoint
  587. searchSeries.forEach(function (s) {
  588. var point = find(s.points, function (p) {
  589. return p.x === hoverPoint.x && !p.isNull;
  590. });
  591. if (isObject(point)) {
  592. /*
  593. * Boost returns a minimal point. Convert it to a usable
  594. * point for tooltip and states.
  595. */
  596. if (s.chart.isBoosting) {
  597. point = s.getPoint(point);
  598. }
  599. hoverPoints.push(point);
  600. }
  601. });
  602. }
  603. else {
  604. hoverPoints.push(hoverPoint);
  605. }
  606. }
  607. // Check whether the hoverPoint is inside pane we are hovering over.
  608. eventArgs = { hoverPoint: hoverPoint };
  609. fireEvent(this, 'afterGetHoverData', eventArgs);
  610. return {
  611. hoverPoint: eventArgs.hoverPoint,
  612. hoverSeries: hoverSeries,
  613. hoverPoints: hoverPoints
  614. };
  615. };
  616. /**
  617. * @private
  618. * @function Highcharts.Pointer#getPointFromEvent
  619. *
  620. * @param {global.Event} e
  621. *
  622. * @return {Highcharts.Point|undefined}
  623. */
  624. Pointer.prototype.getPointFromEvent = function (e) {
  625. var target = e.target, point;
  626. while (target && !point) {
  627. point = target.point;
  628. target = target.parentNode;
  629. }
  630. return point;
  631. };
  632. /**
  633. * @private
  634. * @function Highcharts.Pointer#onTrackerMouseOut
  635. *
  636. * @param {Highcharts.PointerEventObject} e
  637. *
  638. * @return {void}
  639. */
  640. Pointer.prototype.onTrackerMouseOut = function (e) {
  641. var chart = this.chart;
  642. var relatedTarget = e.relatedTarget || e.toElement;
  643. var series = chart.hoverSeries;
  644. this.isDirectTouch = false;
  645. if (series &&
  646. relatedTarget &&
  647. !series.stickyTracking &&
  648. !this.inClass(relatedTarget, 'highcharts-tooltip') &&
  649. (!this.inClass(relatedTarget, 'highcharts-series-' + series.index) || // #2499, #4465, #5553
  650. !this.inClass(relatedTarget, 'highcharts-tracker'))) {
  651. series.onMouseOut();
  652. }
  653. };
  654. /**
  655. * Utility to detect whether an element has, or has a parent with, a
  656. * specificclass name. Used on detection of tracker objects and on deciding
  657. * whether hovering the tooltip should cause the active series to mouse out.
  658. *
  659. * @function Highcharts.Pointer#inClass
  660. *
  661. * @param {Highcharts.SVGDOMElement|Highcharts.HTMLDOMElement} element
  662. * The element to investigate.
  663. *
  664. * @param {string} className
  665. * The class name to look for.
  666. *
  667. * @return {boolean|undefined}
  668. * True if either the element or one of its parents has the given
  669. * class name.
  670. */
  671. Pointer.prototype.inClass = function (element, className) {
  672. var elemClassName;
  673. while (element) {
  674. elemClassName = attr(element, 'class');
  675. if (elemClassName) {
  676. if (elemClassName.indexOf(className) !== -1) {
  677. return true;
  678. }
  679. if (elemClassName.indexOf('highcharts-container') !== -1) {
  680. return false;
  681. }
  682. }
  683. element = element.parentNode;
  684. }
  685. };
  686. /**
  687. * Initialize the Pointer.
  688. *
  689. * @private
  690. * @function Highcharts.Pointer#init
  691. *
  692. * @param {Highcharts.Chart} chart
  693. * The Chart instance.
  694. *
  695. * @param {Highcharts.Options} options
  696. * The root options object. The pointer uses options from the chart
  697. * and tooltip structures.
  698. *
  699. * @return {void}
  700. */
  701. Pointer.prototype.init = function (chart, options) {
  702. // Store references
  703. this.options = options;
  704. this.chart = chart;
  705. // Do we need to handle click on a touch device?
  706. this.runChartClick =
  707. options.chart.events &&
  708. !!options.chart.events.click;
  709. this.pinchDown = [];
  710. this.lastValidTouch = {};
  711. if (Tooltip) {
  712. /**
  713. * Tooltip object for points of series.
  714. *
  715. * @name Highcharts.Chart#tooltip
  716. * @type {Highcharts.Tooltip}
  717. */
  718. chart.tooltip = new Tooltip(chart, options.tooltip);
  719. this.followTouchMove = pick(options.tooltip.followTouchMove, true);
  720. }
  721. this.setDOMEvents();
  722. };
  723. /**
  724. * Takes a browser event object and extends it with custom Highcharts
  725. * properties `chartX` and `chartY` in order to work on the internal
  726. * coordinate system.
  727. *
  728. * @function Highcharts.Pointer#normalize
  729. *
  730. * @param {global.MouseEvent|global.PointerEvent|global.TouchEvent} e
  731. * Event object in standard browsers.
  732. *
  733. * @param {Highcharts.OffsetObject} [chartPosition]
  734. * Additional chart offset.
  735. *
  736. * @return {Highcharts.PointerEventObject}
  737. * A browser event with extended properties `chartX` and `chartY`.
  738. */
  739. Pointer.prototype.normalize = function (e, chartPosition) {
  740. var touches = e.touches;
  741. // iOS (#2757)
  742. var ePos = (touches ?
  743. touches.length ?
  744. touches.item(0) :
  745. (pick(// #13534
  746. touches.changedTouches, e.changedTouches))[0] :
  747. e);
  748. // Get mouse position
  749. if (!chartPosition) {
  750. chartPosition = this.getChartPosition();
  751. }
  752. var chartX = ePos.pageX - chartPosition.left, chartY = ePos.pageY - chartPosition.top;
  753. // #11329 - when there is scaling on a parent element, we need to take
  754. // this into account
  755. var containerScaling = this.chart.containerScaling;
  756. if (containerScaling) {
  757. chartX /= containerScaling.scaleX;
  758. chartY /= containerScaling.scaleY;
  759. }
  760. return extend(e, {
  761. chartX: Math.round(chartX),
  762. chartY: Math.round(chartY)
  763. });
  764. };
  765. /**
  766. * @private
  767. * @function Highcharts.Pointer#onContainerClick
  768. */
  769. Pointer.prototype.onContainerClick = function (e) {
  770. var chart = this.chart;
  771. var hoverPoint = chart.hoverPoint;
  772. var pEvt = this.normalize(e);
  773. var plotLeft = chart.plotLeft;
  774. var plotTop = chart.plotTop;
  775. if (!chart.cancelClick) {
  776. // On tracker click, fire the series and point events. #783, #1583
  777. if (hoverPoint &&
  778. this.inClass(pEvt.target, 'highcharts-tracker')) {
  779. // the series click event
  780. fireEvent(hoverPoint.series, 'click', extend(pEvt, {
  781. point: hoverPoint
  782. }));
  783. // the point click event
  784. if (chart.hoverPoint) { // it may be destroyed (#1844)
  785. hoverPoint.firePointEvent('click', pEvt);
  786. }
  787. // When clicking outside a tracker, fire a chart event
  788. }
  789. else {
  790. extend(pEvt, this.getCoordinates(pEvt));
  791. // fire a click event in the chart
  792. if (chart.isInsidePlot((pEvt.chartX - plotLeft), (pEvt.chartY - plotTop))) {
  793. fireEvent(chart, 'click', pEvt);
  794. }
  795. }
  796. }
  797. };
  798. /**
  799. * @private
  800. * @function Highcharts.Pointer#onContainerMouseDown
  801. *
  802. * @param {global.MouseEvent} e
  803. */
  804. Pointer.prototype.onContainerMouseDown = function (e) {
  805. // Normalize before the 'if' for the legacy IE (#7850)
  806. e = this.normalize(e);
  807. // #11635, Firefox does not reliable fire move event after click scroll
  808. if (H.isFirefox &&
  809. e.button !== 0) {
  810. this.onContainerMouseMove(e);
  811. }
  812. // #11635, limiting to primary button (incl. IE 8 support)
  813. if (typeof e.button === 'undefined' ||
  814. ((e.buttons || e.button) & 1) === 1) {
  815. this.zoomOption(e);
  816. this.dragStart(e);
  817. }
  818. };
  819. /**
  820. * When mouse leaves the container, hide the tooltip.
  821. *
  822. * @private
  823. * @function Highcharts.Pointer#onContainerMouseLeave
  824. *
  825. * @param {global.MouseEvent} e
  826. *
  827. * @return {void}
  828. */
  829. Pointer.prototype.onContainerMouseLeave = function (e) {
  830. var chart = charts[pick(H.hoverChartIndex, -1)];
  831. var tooltip = this.chart.tooltip;
  832. e = this.normalize(e);
  833. // #4886, MS Touch end fires mouseleave but with no related target
  834. if (chart &&
  835. (e.relatedTarget || e.toElement)) {
  836. chart.pointer.reset();
  837. // Also reset the chart position, used in #149 fix
  838. chart.pointer.chartPosition = void 0;
  839. }
  840. if ( // #11635, Firefox wheel scroll does not fire out events consistently
  841. tooltip &&
  842. !tooltip.isHidden) {
  843. this.reset();
  844. }
  845. };
  846. /**
  847. * The mousemove, touchmove and touchstart event handler
  848. *
  849. * @private
  850. * @function Highcharts.Pointer#onContainerMouseMove
  851. *
  852. * @param {global.MouseEvent} e
  853. *
  854. * @return {void}
  855. */
  856. Pointer.prototype.onContainerMouseMove = function (e) {
  857. var chart = this.chart;
  858. var pEvt = this.normalize(e);
  859. this.setHoverChartIndex();
  860. // In IE8 we apparently need this returnValue set to false in order to
  861. // avoid text being selected. But in Chrome, e.returnValue is prevented,
  862. // plus we don't need to run e.preventDefault to prevent selected text
  863. // in modern browsers. So we set it conditionally. Remove it when IE8 is
  864. // no longer needed. #2251, #3224.
  865. if (!pEvt.preventDefault) {
  866. pEvt.returnValue = false;
  867. }
  868. if (chart.mouseIsDown === 'mousedown') {
  869. this.drag(pEvt);
  870. }
  871. // Show the tooltip and run mouse over events (#977)
  872. if (!chart.openMenu &&
  873. (this.inClass(pEvt.target, 'highcharts-tracker') ||
  874. chart.isInsidePlot((pEvt.chartX - chart.plotLeft), (pEvt.chartY - chart.plotTop)))) {
  875. this.runPointActions(pEvt);
  876. }
  877. };
  878. /**
  879. * @private
  880. * @function Highcharts.Pointer#onDocumentTouchEnd
  881. *
  882. * @param {Highcharts.PointerEventObject} e
  883. *
  884. * @return {void}
  885. */
  886. Pointer.prototype.onDocumentTouchEnd = function (e) {
  887. if (charts[H.hoverChartIndex]) {
  888. charts[H.hoverChartIndex].pointer.drop(e);
  889. }
  890. };
  891. /**
  892. * @private
  893. * @function Highcharts.Pointer#onContainerTouchMove
  894. *
  895. * @param {Highcharts.PointerEventObject} e
  896. *
  897. * @return {void}
  898. */
  899. Pointer.prototype.onContainerTouchMove = function (e) {
  900. this.touch(e);
  901. };
  902. /**
  903. * @private
  904. * @function Highcharts.Pointer#onContainerTouchStart
  905. *
  906. * @param {Highcharts.PointerEventObject} e
  907. *
  908. * @return {void}
  909. */
  910. Pointer.prototype.onContainerTouchStart = function (e) {
  911. this.zoomOption(e);
  912. this.touch(e, true);
  913. };
  914. /**
  915. * Special handler for mouse move that will hide the tooltip when the mouse
  916. * leaves the plotarea. Issue #149 workaround. The mouseleave event does not
  917. * always fire.
  918. *
  919. * @private
  920. * @function Highcharts.Pointer#onDocumentMouseMove
  921. *
  922. * @param {global.MouseEvent} e
  923. *
  924. * @return {void}
  925. */
  926. Pointer.prototype.onDocumentMouseMove = function (e) {
  927. var chart = this.chart;
  928. var chartPosition = this.chartPosition;
  929. var pEvt = this.normalize(e, chartPosition);
  930. var tooltip = chart.tooltip;
  931. // If we're outside, hide the tooltip
  932. if (chartPosition &&
  933. (!tooltip ||
  934. !tooltip.isStickyOnContact()) &&
  935. !chart.isInsidePlot(pEvt.chartX - chart.plotLeft, pEvt.chartY - chart.plotTop) &&
  936. !this.inClass(pEvt.target, 'highcharts-tracker')) {
  937. this.reset();
  938. }
  939. };
  940. /**
  941. * @private
  942. * @function Highcharts.Pointer#onDocumentMouseUp
  943. *
  944. * @param {global.MouseEvent} e
  945. *
  946. * @return {void}
  947. */
  948. Pointer.prototype.onDocumentMouseUp = function (e) {
  949. var chart = charts[pick(H.hoverChartIndex, -1)];
  950. if (chart) {
  951. chart.pointer.drop(e);
  952. }
  953. };
  954. /**
  955. * Handle touch events with two touches
  956. *
  957. * @private
  958. * @function Highcharts.Pointer#pinch
  959. *
  960. * @param {Highcharts.PointerEventObject} e
  961. *
  962. * @return {void}
  963. */
  964. Pointer.prototype.pinch = function (e) {
  965. var self = this, chart = self.chart, pinchDown = self.pinchDown, touches = (e.touches || []), touchesLength = touches.length, lastValidTouch = self.lastValidTouch, hasZoom = self.hasZoom, selectionMarker = self.selectionMarker, transform = {}, fireClickEvent = touchesLength === 1 && ((self.inClass(e.target, 'highcharts-tracker') &&
  966. chart.runTrackerClick) ||
  967. self.runChartClick), clip = {};
  968. // Don't initiate panning until the user has pinched. This prevents us
  969. // from blocking page scrolling as users scroll down a long page
  970. // (#4210).
  971. if (touchesLength > 1) {
  972. self.initiated = true;
  973. }
  974. // On touch devices, only proceed to trigger click if a handler is
  975. // defined
  976. if (hasZoom && self.initiated && !fireClickEvent) {
  977. e.preventDefault();
  978. }
  979. // Normalize each touch
  980. [].map.call(touches, function (e) {
  981. return self.normalize(e);
  982. });
  983. // Register the touch start position
  984. if (e.type === 'touchstart') {
  985. [].forEach.call(touches, function (e, i) {
  986. pinchDown[i] = { chartX: e.chartX, chartY: e.chartY };
  987. });
  988. lastValidTouch.x = [pinchDown[0].chartX, pinchDown[1] &&
  989. pinchDown[1].chartX];
  990. lastValidTouch.y = [pinchDown[0].chartY, pinchDown[1] &&
  991. pinchDown[1].chartY];
  992. // Identify the data bounds in pixels
  993. chart.axes.forEach(function (axis) {
  994. if (axis.zoomEnabled) {
  995. var bounds = chart.bounds[axis.horiz ? 'h' : 'v'], minPixelPadding = axis.minPixelPadding, min = axis.toPixels(Math.min(pick(axis.options.min, axis.dataMin), axis.dataMin)), max = axis.toPixels(Math.max(pick(axis.options.max, axis.dataMax), axis.dataMax)), absMin = Math.min(min, max), absMax = Math.max(min, max);
  996. // Store the bounds for use in the touchmove handler
  997. bounds.min = Math.min(axis.pos, absMin - minPixelPadding);
  998. bounds.max = Math.max(axis.pos + axis.len, absMax + minPixelPadding);
  999. }
  1000. });
  1001. self.res = true; // reset on next move
  1002. // Optionally move the tooltip on touchmove
  1003. }
  1004. else if (self.followTouchMove && touchesLength === 1) {
  1005. this.runPointActions(self.normalize(e));
  1006. // Event type is touchmove, handle panning and pinching
  1007. }
  1008. else if (pinchDown.length) { // can be 0 when releasing, if touchend
  1009. // fires first
  1010. // Set the marker
  1011. if (!selectionMarker) {
  1012. self.selectionMarker = selectionMarker = extend({
  1013. destroy: noop,
  1014. touch: true
  1015. }, chart.plotBox);
  1016. }
  1017. self.pinchTranslate(pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  1018. self.hasPinched = hasZoom;
  1019. // Scale and translate the groups to provide visual feedback during
  1020. // pinching
  1021. self.scaleGroups(transform, clip);
  1022. if (self.res) {
  1023. self.res = false;
  1024. this.reset(false, 0);
  1025. }
  1026. }
  1027. };
  1028. /**
  1029. * Run translation operations
  1030. *
  1031. * @private
  1032. * @function Highcharts.Pointer#pinchTranslate
  1033. *
  1034. * @param {Array<*>} pinchDown
  1035. *
  1036. * @param {Array<Highcharts.PointerEventObject>} touches
  1037. *
  1038. * @param {*} transform
  1039. *
  1040. * @param {*} selectionMarker
  1041. *
  1042. * @param {*} clip
  1043. *
  1044. * @param {*} lastValidTouch
  1045. *
  1046. * @return {void}
  1047. */
  1048. Pointer.prototype.pinchTranslate = function (pinchDown, touches, transform, selectionMarker, clip, lastValidTouch) {
  1049. if (this.zoomHor) {
  1050. this.pinchTranslateDirection(true, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  1051. }
  1052. if (this.zoomVert) {
  1053. this.pinchTranslateDirection(false, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  1054. }
  1055. };
  1056. /**
  1057. * Run translation operations for each direction (horizontal and vertical)
  1058. * independently.
  1059. *
  1060. * @private
  1061. * @function Highcharts.Pointer#pinchTranslateDirection
  1062. *
  1063. * @param {boolean} horiz
  1064. *
  1065. * @param {Array<*>} pinchDown
  1066. *
  1067. * @param {Array<Highcharts.PointerEventObject>} touches
  1068. *
  1069. * @param {*} transform
  1070. *
  1071. * @param {*} selectionMarker
  1072. *
  1073. * @param {*} clip
  1074. *
  1075. * @param {*} lastValidTouch
  1076. *
  1077. * @param {number|undefined} [forcedScale=1]
  1078. *
  1079. * @return {void}
  1080. */
  1081. Pointer.prototype.pinchTranslateDirection = function (horiz, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch, forcedScale) {
  1082. var chart = this.chart, xy = horiz ? 'x' : 'y', XY = horiz ? 'X' : 'Y', sChartXY = ('chart' + XY), wh = horiz ? 'width' : 'height', plotLeftTop = chart['plot' + (horiz ? 'Left' : 'Top')], selectionWH, selectionXY, clipXY, scale = forcedScale || 1, inverted = chart.inverted, bounds = chart.bounds[horiz ? 'h' : 'v'], singleTouch = pinchDown.length === 1, touch0Start = pinchDown[0][sChartXY], touch0Now = touches[0][sChartXY], touch1Start = !singleTouch && pinchDown[1][sChartXY], touch1Now = !singleTouch && touches[1][sChartXY], outOfBounds, transformScale, scaleKey, setScale = function () {
  1083. // Don't zoom if fingers are too close on this axis
  1084. if (typeof touch1Now === 'number' &&
  1085. Math.abs(touch0Start - touch1Start) > 20) {
  1086. scale = forcedScale ||
  1087. Math.abs(touch0Now - touch1Now) /
  1088. Math.abs(touch0Start - touch1Start);
  1089. }
  1090. clipXY = ((plotLeftTop - touch0Now) / scale) + touch0Start;
  1091. selectionWH = chart['plot' + (horiz ? 'Width' : 'Height')] / scale;
  1092. };
  1093. // Set the scale, first pass
  1094. setScale();
  1095. // The clip position (x or y) is altered if out of bounds, the selection
  1096. // position is not
  1097. selectionXY = clipXY;
  1098. // Out of bounds
  1099. if (selectionXY < bounds.min) {
  1100. selectionXY = bounds.min;
  1101. outOfBounds = true;
  1102. }
  1103. else if (selectionXY + selectionWH > bounds.max) {
  1104. selectionXY = bounds.max - selectionWH;
  1105. outOfBounds = true;
  1106. }
  1107. // Is the chart dragged off its bounds, determined by dataMin and
  1108. // dataMax?
  1109. if (outOfBounds) {
  1110. // Modify the touchNow position in order to create an elastic drag
  1111. // movement. This indicates to the user that the chart is responsive
  1112. // but can't be dragged further.
  1113. touch0Now -= 0.8 * (touch0Now - lastValidTouch[xy][0]);
  1114. if (typeof touch1Now === 'number') {
  1115. touch1Now -= 0.8 * (touch1Now - lastValidTouch[xy][1]);
  1116. }
  1117. // Set the scale, second pass to adapt to the modified touchNow
  1118. // positions
  1119. setScale();
  1120. }
  1121. else {
  1122. lastValidTouch[xy] = [touch0Now, touch1Now];
  1123. }
  1124. // Set geometry for clipping, selection and transformation
  1125. if (!inverted) {
  1126. clip[xy] = clipXY - plotLeftTop;
  1127. clip[wh] = selectionWH;
  1128. }
  1129. scaleKey = inverted ? (horiz ? 'scaleY' : 'scaleX') : 'scale' + XY;
  1130. transformScale = inverted ? 1 / scale : scale;
  1131. selectionMarker[wh] = selectionWH;
  1132. selectionMarker[xy] = selectionXY;
  1133. transform[scaleKey] = scale;
  1134. transform['translate' + XY] = (transformScale * plotLeftTop) +
  1135. (touch0Now - (transformScale * touch0Start));
  1136. };
  1137. /**
  1138. * Reset the tracking by hiding the tooltip, the hover series state and the
  1139. * hover point
  1140. *
  1141. * @function Highcharts.Pointer#reset
  1142. *
  1143. * @param {boolean} [allowMove]
  1144. * Instead of destroying the tooltip altogether, allow moving it if
  1145. * possible.
  1146. *
  1147. * @param {number} [delay]
  1148. *
  1149. * @return {void}
  1150. */
  1151. Pointer.prototype.reset = function (allowMove, delay) {
  1152. var pointer = this, chart = pointer.chart, hoverSeries = chart.hoverSeries, hoverPoint = chart.hoverPoint, hoverPoints = chart.hoverPoints, tooltip = chart.tooltip, tooltipPoints = tooltip && tooltip.shared ?
  1153. hoverPoints :
  1154. hoverPoint;
  1155. // Check if the points have moved outside the plot area (#1003, #4736,
  1156. // #5101)
  1157. if (allowMove && tooltipPoints) {
  1158. splat(tooltipPoints).forEach(function (point) {
  1159. if (point.series.isCartesian &&
  1160. typeof point.plotX === 'undefined') {
  1161. allowMove = false;
  1162. }
  1163. });
  1164. }
  1165. // Just move the tooltip, #349
  1166. if (allowMove) {
  1167. if (tooltip && tooltipPoints && splat(tooltipPoints).length) {
  1168. tooltip.refresh(tooltipPoints);
  1169. if (tooltip.shared && hoverPoints) { // #8284
  1170. hoverPoints.forEach(function (point) {
  1171. point.setState(point.state, true);
  1172. if (point.series.isCartesian) {
  1173. if (point.series.xAxis.crosshair) {
  1174. point.series.xAxis
  1175. .drawCrosshair(null, point);
  1176. }
  1177. if (point.series.yAxis.crosshair) {
  1178. point.series.yAxis
  1179. .drawCrosshair(null, point);
  1180. }
  1181. }
  1182. });
  1183. }
  1184. else if (hoverPoint) { // #2500
  1185. hoverPoint.setState(hoverPoint.state, true);
  1186. chart.axes.forEach(function (axis) {
  1187. if (axis.crosshair &&
  1188. hoverPoint.series[axis.coll] === axis) {
  1189. axis.drawCrosshair(null, hoverPoint);
  1190. }
  1191. });
  1192. }
  1193. }
  1194. // Full reset
  1195. }
  1196. else {
  1197. if (hoverPoint) {
  1198. hoverPoint.onMouseOut();
  1199. }
  1200. if (hoverPoints) {
  1201. hoverPoints.forEach(function (point) {
  1202. point.setState();
  1203. });
  1204. }
  1205. if (hoverSeries) {
  1206. hoverSeries.onMouseOut();
  1207. }
  1208. if (tooltip) {
  1209. tooltip.hide(delay);
  1210. }
  1211. if (pointer.unDocMouseMove) {
  1212. pointer.unDocMouseMove = pointer.unDocMouseMove();
  1213. }
  1214. // Remove crosshairs
  1215. chart.axes.forEach(function (axis) {
  1216. axis.hideCrosshair();
  1217. });
  1218. pointer.hoverX = chart.hoverPoints = chart.hoverPoint = null;
  1219. }
  1220. };
  1221. /**
  1222. * With line type charts with a single tracker, get the point closest to the
  1223. * mouse. Run Point.onMouseOver and display tooltip for the point or points.
  1224. *
  1225. * @private
  1226. * @function Highcharts.Pointer#runPointActions
  1227. *
  1228. * @param {global.Event} e
  1229. *
  1230. * @param {Highcharts.PointerEventObject} [p]
  1231. *
  1232. * @return {void}
  1233. *
  1234. * @fires Highcharts.Point#event:mouseOut
  1235. * @fires Highcharts.Point#event:mouseOver
  1236. */
  1237. Pointer.prototype.runPointActions = function (e, p) {
  1238. var pointer = this, chart = pointer.chart, series = chart.series, tooltip = (chart.tooltip && chart.tooltip.options.enabled ?
  1239. chart.tooltip :
  1240. void 0), shared = (tooltip ?
  1241. tooltip.shared :
  1242. false), hoverPoint = p || chart.hoverPoint, hoverSeries = hoverPoint && hoverPoint.series || chart.hoverSeries,
  1243. // onMouseOver or already hovering a series with directTouch
  1244. isDirectTouch = (!e || e.type !== 'touchmove') && (!!p || ((hoverSeries && hoverSeries.directTouch) &&
  1245. pointer.isDirectTouch)), hoverData = this.getHoverData(hoverPoint, hoverSeries, series, isDirectTouch, shared, e), useSharedTooltip, followPointer, anchor, points;
  1246. // Update variables from hoverData.
  1247. hoverPoint = hoverData.hoverPoint;
  1248. points = hoverData.hoverPoints;
  1249. hoverSeries = hoverData.hoverSeries;
  1250. followPointer = hoverSeries && hoverSeries.tooltipOptions.followPointer;
  1251. useSharedTooltip = (shared &&
  1252. hoverSeries &&
  1253. !hoverSeries.noSharedTooltip);
  1254. // Refresh tooltip for kdpoint if new hover point or tooltip was hidden
  1255. // #3926, #4200
  1256. if (hoverPoint &&
  1257. // !(hoverSeries && hoverSeries.directTouch) &&
  1258. (hoverPoint !== chart.hoverPoint || (tooltip && tooltip.isHidden))) {
  1259. (chart.hoverPoints || []).forEach(function (p) {
  1260. if (points.indexOf(p) === -1) {
  1261. p.setState();
  1262. }
  1263. });
  1264. // Set normal state to previous series
  1265. if (chart.hoverSeries !== hoverSeries) {
  1266. hoverSeries.onMouseOver();
  1267. }
  1268. pointer.applyInactiveState(points);
  1269. // Do mouseover on all points (#3919, #3985, #4410, #5622)
  1270. (points || []).forEach(function (p) {
  1271. p.setState('hover');
  1272. });
  1273. // If tracking is on series in stead of on each point,
  1274. // fire mouseOver on hover point. // #4448
  1275. if (chart.hoverPoint) {
  1276. chart.hoverPoint.firePointEvent('mouseOut');
  1277. }
  1278. // Hover point may have been destroyed in the event handlers (#7127)
  1279. if (!hoverPoint.series) {
  1280. return;
  1281. }
  1282. /**
  1283. * Contains all hovered points.
  1284. *
  1285. * @name Highcharts.Chart#hoverPoints
  1286. * @type {Array<Highcharts.Point>|null}
  1287. */
  1288. chart.hoverPoints = points;
  1289. /**
  1290. * Contains the original hovered point.
  1291. *
  1292. * @name Highcharts.Chart#hoverPoint
  1293. * @type {Highcharts.Point|null}
  1294. */
  1295. chart.hoverPoint = hoverPoint;
  1296. /**
  1297. * Hover state should not be lost when axis is updated (#12569)
  1298. * Axis.update runs pointer.reset which uses chart.hoverPoint.state
  1299. * to apply state which does not exist in hoverPoint yet.
  1300. * The mouseOver event should be triggered when hoverPoint
  1301. * is correct.
  1302. */
  1303. hoverPoint.firePointEvent('mouseOver');
  1304. // Draw tooltip if necessary
  1305. if (tooltip) {
  1306. tooltip.refresh(useSharedTooltip ? points : hoverPoint, e);
  1307. }
  1308. // Update positions (regardless of kdpoint or hoverPoint)
  1309. }
  1310. else if (followPointer && tooltip && !tooltip.isHidden) {
  1311. anchor = tooltip.getAnchor([{}], e);
  1312. tooltip.updatePosition({ plotX: anchor[0], plotY: anchor[1] });
  1313. }
  1314. // Start the event listener to pick up the tooltip and crosshairs
  1315. if (!pointer.unDocMouseMove) {
  1316. pointer.unDocMouseMove = addEvent(chart.container.ownerDocument, 'mousemove', function (e) {
  1317. var chart = charts[H.hoverChartIndex];
  1318. if (chart) {
  1319. chart.pointer.onDocumentMouseMove(e);
  1320. }
  1321. });
  1322. }
  1323. // Issues related to crosshair #4927, #5269 #5066, #5658
  1324. chart.axes.forEach(function drawAxisCrosshair(axis) {
  1325. var snap = pick((axis.crosshair || {}).snap, true);
  1326. var point;
  1327. if (snap) {
  1328. point = chart.hoverPoint; // #13002
  1329. if (!point || point.series[axis.coll] !== axis) {
  1330. point = find(points, function (p) {
  1331. return p.series[axis.coll] === axis;
  1332. });
  1333. }
  1334. }
  1335. // Axis has snapping crosshairs, and one of the hover points belongs
  1336. // to axis. Always call drawCrosshair when it is not snap.
  1337. if (point || !snap) {
  1338. axis.drawCrosshair(e, point);
  1339. // Axis has snapping crosshairs, but no hover point belongs to axis
  1340. }
  1341. else {
  1342. axis.hideCrosshair();
  1343. }
  1344. });
  1345. };
  1346. /**
  1347. * Scale series groups to a certain scale and translation.
  1348. *
  1349. * @private
  1350. * @function Highcharts.Pointer#scaleGroups
  1351. *
  1352. * @param {Highcharts.SeriesPlotBoxObject} [attribs]
  1353. *
  1354. * @param {boolean} [clip]
  1355. *
  1356. * @return {void}
  1357. */
  1358. Pointer.prototype.scaleGroups = function (attribs, clip) {
  1359. var chart = this.chart, seriesAttribs;
  1360. // Scale each series
  1361. chart.series.forEach(function (series) {
  1362. seriesAttribs = attribs || series.getPlotBox(); // #1701
  1363. if (series.xAxis && series.xAxis.zoomEnabled && series.group) {
  1364. series.group.attr(seriesAttribs);
  1365. if (series.markerGroup) {
  1366. series.markerGroup.attr(seriesAttribs);
  1367. series.markerGroup.clip(clip ? chart.clipRect : null);
  1368. }
  1369. if (series.dataLabelsGroup) {
  1370. series.dataLabelsGroup.attr(seriesAttribs);
  1371. }
  1372. }
  1373. });
  1374. // Clip
  1375. chart.clipRect.attr(clip || chart.clipBox);
  1376. };
  1377. /**
  1378. * Set the JS DOM events on the container and document. This method should
  1379. * contain a one-to-one assignment between methods and their handlers. Any
  1380. * advanced logic should be moved to the handler reflecting the event's
  1381. * name.
  1382. *
  1383. * @private
  1384. * @function Highcharts.Pointer#setDOMEvents
  1385. *
  1386. * @return {void}
  1387. */
  1388. Pointer.prototype.setDOMEvents = function () {
  1389. var container = this.chart.container, ownerDoc = container.ownerDocument;
  1390. container.onmousedown = this.onContainerMouseDown.bind(this);
  1391. container.onmousemove = this.onContainerMouseMove.bind(this);
  1392. container.onclick = this.onContainerClick.bind(this);
  1393. this.unbindContainerMouseLeave = addEvent(container, 'mouseleave', this.onContainerMouseLeave.bind(this));
  1394. if (!H.unbindDocumentMouseUp) {
  1395. H.unbindDocumentMouseUp = addEvent(ownerDoc, 'mouseup', this.onDocumentMouseUp.bind(this));
  1396. }
  1397. if (H.hasTouch) {
  1398. addEvent(container, 'touchstart', this.onContainerTouchStart.bind(this));
  1399. addEvent(container, 'touchmove', this.onContainerTouchMove.bind(this));
  1400. if (!H.unbindDocumentTouchEnd) {
  1401. H.unbindDocumentTouchEnd = addEvent(ownerDoc, 'touchend', this.onDocumentTouchEnd.bind(this));
  1402. }
  1403. }
  1404. };
  1405. /**
  1406. * Sets the index of the hovered chart and leaves the previous hovered
  1407. * chart, to reset states like tooltip.
  1408. *
  1409. * @private
  1410. * @function Highcharts.Pointer#setHoverChartIndex
  1411. */
  1412. Pointer.prototype.setHoverChartIndex = function () {
  1413. var chart = this.chart;
  1414. var hoverChart = H.charts[pick(H.hoverChartIndex, -1)];
  1415. if (hoverChart &&
  1416. hoverChart !== chart) {
  1417. hoverChart.pointer.onContainerMouseLeave({ relatedTarget: true });
  1418. }
  1419. if (!hoverChart ||
  1420. !hoverChart.mouseIsDown) {
  1421. H.hoverChartIndex = chart.index;
  1422. }
  1423. };
  1424. /**
  1425. * General touch handler shared by touchstart and touchmove.
  1426. *
  1427. * @private
  1428. * @function Highcharts.Pointer#touch
  1429. *
  1430. * @param {Highcharts.PointerEventObject} e
  1431. *
  1432. * @param {boolean} [start]
  1433. *
  1434. * @return {void}
  1435. */
  1436. Pointer.prototype.touch = function (e, start) {
  1437. var chart = this.chart, hasMoved, pinchDown, isInside;
  1438. this.setHoverChartIndex();
  1439. if (e.touches.length === 1) {
  1440. e = this.normalize(e);
  1441. isInside = chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop);
  1442. if (isInside && !chart.openMenu) {
  1443. // Run mouse events and display tooltip etc
  1444. if (start) {
  1445. this.runPointActions(e);
  1446. }
  1447. // Android fires touchmove events after the touchstart even if
  1448. // the finger hasn't moved, or moved only a pixel or two. In iOS
  1449. // however, the touchmove doesn't fire unless the finger moves
  1450. // more than ~4px. So we emulate this behaviour in Android by
  1451. // checking how much it moved, and cancelling on small
  1452. // distances. #3450.
  1453. if (e.type === 'touchmove') {
  1454. pinchDown = this.pinchDown;
  1455. hasMoved = pinchDown[0] ? Math.sqrt(// #5266
  1456. Math.pow(pinchDown[0].chartX - e.chartX, 2) +
  1457. Math.pow(pinchDown[0].chartY - e.chartY, 2)) >= 4 : false;
  1458. }
  1459. if (pick(hasMoved, true)) {
  1460. this.pinch(e);
  1461. }
  1462. }
  1463. else if (start) {
  1464. // Hide the tooltip on touching outside the plot area (#1203)
  1465. this.reset();
  1466. }
  1467. }
  1468. else if (e.touches.length === 2) {
  1469. this.pinch(e);
  1470. }
  1471. };
  1472. /**
  1473. * Resolve the zoomType option, this is reset on all touch start and mouse
  1474. * down events.
  1475. *
  1476. * @private
  1477. * @function Highcharts.Pointer#zoomOption
  1478. *
  1479. * @param {global.Event} e
  1480. * Event object.
  1481. *
  1482. * @param {void}
  1483. */
  1484. Pointer.prototype.zoomOption = function (e) {
  1485. var chart = this.chart, options = chart.options.chart, zoomType = options.zoomType || '', inverted = chart.inverted, zoomX, zoomY;
  1486. // Look for the pinchType option
  1487. if (/touch/.test(e.type)) {
  1488. zoomType = pick(options.pinchType, zoomType);
  1489. }
  1490. this.zoomX = zoomX = /x/.test(zoomType);
  1491. this.zoomY = zoomY = /y/.test(zoomType);
  1492. this.zoomHor = (zoomX && !inverted) || (zoomY && inverted);
  1493. this.zoomVert = (zoomY && !inverted) || (zoomX && inverted);
  1494. this.hasZoom = zoomX || zoomY;
  1495. };
  1496. return Pointer;
  1497. }());
  1498. H.Pointer = Pointer;
  1499. export default Pointer;