Pointer.js 57 KB

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