Pointer.js 59 KB

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