Pointer.js 58 KB

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