jquery.flot.touchNavigate.Test.js 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /* eslint-disable */
  2. /* global $, describe, it, xit, xdescribe, after, afterEach, expect*/
  3. describe("flot touch navigate plugin", function () {
  4. var placeholder, plot, options;
  5. var minFrameDuration = 1 / 60 * 1000;
  6. beforeEach(function () {
  7. placeholder = setFixtures('<div id="test-container" style="width: 600px;height: 400px">')
  8. .find('#test-container');
  9. options = {
  10. xaxes: [{ autoScale: 'exact' }],
  11. yaxes: [{ autoScale: 'exact' }],
  12. zoom: { interactive: true, active: true, amount: 10, enableTouch: true },
  13. pan: { interactive: true, active: true, frameRate: -1, enableTouch: true }
  14. };
  15. jasmine.clock().install();
  16. });
  17. afterEach(function() {
  18. jasmine.clock().uninstall();
  19. });
  20. it('shows that the eventHolder is cleared through shutdown when the plot is replaced', function() {
  21. plot = $.plot(placeholder, [[]], options);
  22. var eventPlaceholder = plot.getEventHolder(),
  23. spy = spyOn(eventPlaceholder, 'removeEventListener').and.callThrough();
  24. plot = $.plot(placeholder, [[]], options);
  25. expect(spy).toHaveBeenCalledWith('panstart', jasmine.any(Function))
  26. expect(spy).toHaveBeenCalledWith('pandrag', jasmine.any(Function));
  27. expect(spy).toHaveBeenCalledWith('panend', jasmine.any(Function));
  28. });
  29. var simulateTouchEvent = function (coords, eventHolder, eventName) {
  30. simulate.sendTouchEvents(coords, eventHolder, eventName);
  31. jasmine.clock().tick(minFrameDuration);
  32. }
  33. describe('touchZoom', function() {
  34. it('should zoom the plot',function() {
  35. plot = $.plot(placeholder, [
  36. [
  37. [-1, 2],
  38. [11, 12]
  39. ]
  40. ], options);
  41. var eventHolder = plot.getEventHolder(),
  42. xaxis = plot.getXAxes()[0],
  43. yaxis = plot.getYAxes()[0],
  44. initialXmin = xaxis.min,
  45. initialXmax = xaxis.max,
  46. initialYmin = yaxis.min,
  47. initialYmax = yaxis.max,
  48. initialCoords = [
  49. getPairOfCoords(xaxis, yaxis, 3, 5),
  50. getPairOfCoords(xaxis, yaxis, 7, 9)
  51. ],
  52. finalCoords = [
  53. getPairOfCoords(xaxis, yaxis, 2, 4),
  54. getPairOfCoords(xaxis, yaxis, 8, 10)
  55. ],
  56. midPointCoords = {
  57. x: (xaxis.c2p(finalCoords[0].x - plot.offset().left) + xaxis.c2p(finalCoords[1].x - plot.offset().left)) / 2,
  58. y: (yaxis.c2p(finalCoords[0].y - plot.offset().top) + yaxis.c2p(finalCoords[1].y - plot.offset().top)) / 2
  59. },
  60. amount = getDistance(finalCoords) / getDistance(initialCoords);
  61. simulateTouchEvent(initialCoords, eventHolder, 'touchstart');
  62. simulateTouchEvent(finalCoords, eventHolder, 'touchmove');
  63. simulateTouchEvent(finalCoords, eventHolder, 'touchend');
  64. expect(xaxis.min).toBeCloseTo((midPointCoords.x - initialXmin) * (1 - 1/amount) + initialXmin, 6);
  65. expect(xaxis.max).toBeCloseTo(initialXmax - (initialXmax - midPointCoords.x) * (1 - 1/amount), 6);
  66. expect(yaxis.min).toBeCloseTo((midPointCoords.y - initialYmin) * (1 - 1/amount) + initialYmin, 6);
  67. expect(yaxis.max).toBeCloseTo(initialYmax - (initialYmax - midPointCoords.y) * (1 - 1/amount), 6);
  68. });
  69. it('should pan the plot for two fingers with constant distance',function() {
  70. plot = $.plot(placeholder, [
  71. [
  72. [-1, 2],
  73. [11, 12]
  74. ]
  75. ], options);
  76. var eventHolder = plot.getEventHolder(),
  77. xaxis = plot.getXAxes()[0],
  78. yaxis = plot.getYAxes()[0],
  79. initialXmin = xaxis.min,
  80. initialXmax = xaxis.max,
  81. initialYmin = yaxis.min,
  82. initialYmax = yaxis.max,
  83. canvasCoords = [ { x : 3, y : 5 }, { x : 7, y : 9 }, { x : 2, y : 4 }, { x : 6, y : 8 }],
  84. initialCoords = [
  85. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  86. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  87. ],
  88. finalCoords = [
  89. getPairOfCoords(xaxis, yaxis, canvasCoords[2].x, canvasCoords[2].y),
  90. getPairOfCoords(xaxis, yaxis, canvasCoords[3].x, canvasCoords[3].y)
  91. ];
  92. simulateTouchEvent(initialCoords, eventHolder, 'touchstart');
  93. simulateTouchEvent(finalCoords, eventHolder, 'touchmove');
  94. simulateTouchEvent(finalCoords, eventHolder, 'touchend');
  95. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[2].x), 6);
  96. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[2].x), 6);
  97. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[0].y - canvasCoords[2].y), 6);
  98. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[0].y - canvasCoords[2].y), 6);
  99. });
  100. it('should not zoom the plot for two fingers with small distance variation',function() {
  101. plot = $.plot(placeholder, [
  102. [
  103. [-1, 2],
  104. [11, 12]
  105. ]
  106. ], options);
  107. var eventHolder = plot.getEventHolder(),
  108. xaxis = plot.getXAxes()[0],
  109. yaxis = plot.getYAxes()[0],
  110. initialXmin = xaxis.min,
  111. initialXmax = xaxis.max,
  112. initialYmin = yaxis.min,
  113. initialYmax = yaxis.max,
  114. canvasCoords = [ { x : 3, y : 5 }, { x : 7, y : 8.999 }, { x : 2, y : 4 }, { x : 6.001, y : 8 }],
  115. initialCoords = [
  116. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  117. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  118. ],
  119. finalCoords = [
  120. getPairOfCoords(xaxis, yaxis, canvasCoords[2].x, canvasCoords[2].y),
  121. getPairOfCoords(xaxis, yaxis, canvasCoords[3].x, canvasCoords[3].y)
  122. ];
  123. var spy = spyOn(plot, 'zoom').and.callThrough();
  124. simulateTouchEvent(initialCoords, eventHolder, 'touchstart');
  125. simulateTouchEvent(finalCoords, eventHolder, 'touchmove');
  126. simulateTouchEvent(finalCoords, eventHolder, 'touchend');
  127. expect(spy).not.toHaveBeenCalled();
  128. });
  129. it('should zoom the plot correctly using pageXY when the canvas is placed in the bottom scrollable area of the page', function () {
  130. var largeDiv = $('<div style="height: 800px"> </div>');
  131. $(largeDiv).insertBefore(placeholder);
  132. plot = $.plot(placeholder, [
  133. [
  134. [-1, 2],
  135. [11, 12]
  136. ]
  137. ], options);
  138. var eventHolder = plot.getEventHolder(),
  139. xaxis = plot.getXAxes()[0],
  140. yaxis = plot.getYAxes()[0],
  141. initialXmin = xaxis.min,
  142. initialXmax = xaxis.max,
  143. initialYmin = yaxis.min,
  144. initialYmax = yaxis.max,
  145. initialCoords = [
  146. getPairOfCoords(xaxis, yaxis, 3, 5),
  147. getPairOfCoords(xaxis, yaxis, 7, 9)
  148. ],
  149. finalCoords = [
  150. getPairOfCoords(xaxis, yaxis, 2, 4),
  151. getPairOfCoords(xaxis, yaxis, 8, 10)
  152. ],
  153. midPointCoords = {
  154. x: (xaxis.c2p(finalCoords[0].x - plot.offset().left) + xaxis.c2p(finalCoords[1].x - plot.offset().left)) / 2,
  155. y: (yaxis.c2p(finalCoords[0].y - plot.offset().top) + yaxis.c2p(finalCoords[1].y - plot.offset().top)) / 2
  156. },
  157. amount = getDistance(finalCoords) / getDistance(initialCoords);
  158. simulateTouchEvent(initialCoords, plot.getEventHolder(), 'touchstart');
  159. simulateTouchEvent(finalCoords, plot.getEventHolder(), 'touchmove');
  160. simulateTouchEvent(finalCoords, plot.getEventHolder(), 'touchend');
  161. expect(xaxis.min).toBeCloseTo((midPointCoords.x - initialXmin) * (1 - 1/amount) + initialXmin, 6);
  162. expect(xaxis.max).toBeCloseTo(initialXmax - (initialXmax - midPointCoords.x) * (1 - 1/amount), 6);
  163. expect(yaxis.min).toBeCloseTo((midPointCoords.y - initialYmin) * (1 - 1/amount) + initialYmin, 6);
  164. expect(yaxis.max).toBeCloseTo(initialYmax - (initialYmax - midPointCoords.y) * (1 - 1/amount), 6);
  165. });
  166. it('should zoom the entire plot for touches not on the same axis',function() {
  167. plot = $.plot(placeholder, [
  168. [
  169. [-1, 2],
  170. [11, 12]
  171. ]
  172. ], options);
  173. var eventHolder = plot.getEventHolder(),
  174. xaxis = plot.getXAxes()[0],
  175. yaxis = plot.getYAxes()[0],
  176. initialXmin = xaxis.min,
  177. initialXmax = xaxis.max,
  178. initialYmin = yaxis.min,
  179. initialYmax = yaxis.max,
  180. initialCoords = [
  181. { x: xaxis.p2c(4), y: xaxis.box.top + plot.offset().top + 10 },
  182. { x: xaxis.p2c(5), y: xaxis.box.top + plot.offset().top - 10 }
  183. ],
  184. finalCoords = [
  185. getPairOfCoords(xaxis, yaxis, 2, 4),
  186. getPairOfCoords(xaxis, yaxis, 6, 6)
  187. ],
  188. midPointCoords = {
  189. x: (xaxis.c2p(finalCoords[0].x - plot.offset().left) + xaxis.c2p(finalCoords[1].x - plot.offset().left)) / 2,
  190. y: (yaxis.c2p(finalCoords[0].y - plot.offset().top) + yaxis.c2p(finalCoords[1].y - plot.offset().top)) / 2
  191. },
  192. amount = getDistance(finalCoords) / getDistance(initialCoords);
  193. simulateTouchEvent(initialCoords, eventHolder, 'touchstart');
  194. simulateTouchEvent(finalCoords, eventHolder, 'touchmove');
  195. simulateTouchEvent(finalCoords, eventHolder, 'touchend');
  196. expect(xaxis.min).not.toBeCloseTo(initialYmin, 6);
  197. expect(xaxis.max).not.toBeCloseTo(initialYmax, 6);
  198. expect(yaxis.min).not.toBeCloseTo(initialYmin, 6);
  199. expect(yaxis.max).not.toBeCloseTo(initialYmax, 6);
  200. });
  201. it('should zoom the plot on axis x',function() {
  202. plot = $.plot(placeholder, [
  203. [
  204. [-1, 2],
  205. [11, 12]
  206. ]
  207. ], options);
  208. var eventHolder = plot.getEventHolder(),
  209. xaxis = plot.getXAxes()[0],
  210. yaxis = plot.getYAxes()[0],
  211. initialXmin = xaxis.min,
  212. initialXmax = xaxis.max,
  213. initialYmin = yaxis.min,
  214. initialYmax = yaxis.max,
  215. initialCoords = [
  216. { x: xaxis.p2c(4), y: xaxis.box.top + plot.offset().top + 10 },
  217. { x: xaxis.p2c(5), y: xaxis.box.top + plot.offset().top + 10 }
  218. ],
  219. finalCoords = [
  220. getPairOfCoords(xaxis, yaxis, 2, 4),
  221. getPairOfCoords(xaxis, yaxis, 6, 6)
  222. ],
  223. midPointCoords = {
  224. x: (xaxis.c2p(finalCoords[0].x - plot.offset().left) + xaxis.c2p(finalCoords[1].x - plot.offset().left)) / 2,
  225. y: (yaxis.c2p(finalCoords[0].y - plot.offset().top) + yaxis.c2p(finalCoords[1].y - plot.offset().top)) / 2
  226. },
  227. amount = getDistance(finalCoords) / getDistance(initialCoords);
  228. simulateTouchEvent(initialCoords, eventHolder, 'touchstart');
  229. simulateTouchEvent(finalCoords, eventHolder, 'touchmove');
  230. simulateTouchEvent(finalCoords, eventHolder, 'touchend');
  231. expect(Math.abs(xaxis.min - ((midPointCoords.x - initialXmin) * (1 - 1/amount) + initialXmin))).toBeLessThan(1);
  232. expect(Math.abs(xaxis.max - (initialXmax - (initialXmax - midPointCoords.x) * (1 - 1/amount)))).toBeLessThan(1);
  233. expect(yaxis.min).toBeCloseTo(initialYmin, 6);
  234. expect(yaxis.max).toBeCloseTo(initialYmax, 6);
  235. });
  236. it('should zoom the plot on axis y',function() {
  237. plot = $.plot(placeholder, [
  238. [
  239. [-1, 2],
  240. [11, 12]
  241. ]
  242. ], options);
  243. var eventHolder = plot.getEventHolder(),
  244. xaxis = plot.getXAxes()[0],
  245. yaxis = plot.getYAxes()[0],
  246. initialXmin = xaxis.min,
  247. initialXmax = xaxis.max,
  248. initialYmin = yaxis.min,
  249. initialYmax = yaxis.max,
  250. initialCoords = [
  251. { x: xaxis.box.left - 10, y: yaxis.p2c(4) + plot.offset().top},
  252. { x: xaxis.box.left - 11, y: yaxis.p2c(5) + plot.offset().top}
  253. ],
  254. finalCoords = [
  255. getPairOfCoords(xaxis, yaxis, 2, 4),
  256. getPairOfCoords(xaxis, yaxis, 6, 6)
  257. ],
  258. midPointCoords = {
  259. x: (xaxis.c2p(finalCoords[0].x - plot.offset().left) + xaxis.c2p(finalCoords[1].x - plot.offset().left)) / 2,
  260. y: (yaxis.c2p(finalCoords[0].y - plot.offset().top) + yaxis.c2p(finalCoords[1].y - plot.offset().top)) / 2
  261. },
  262. amount = getDistance(finalCoords) / getDistance(initialCoords);
  263. simulateTouchEvent(initialCoords, eventHolder, 'touchstart');
  264. simulateTouchEvent(finalCoords, eventHolder, 'touchmove');
  265. simulateTouchEvent(finalCoords, eventHolder, 'touchend');
  266. expect(xaxis.min).toBeCloseTo(initialXmin, 6);
  267. expect(xaxis.max).toBeCloseTo(initialXmax, 6);
  268. expect(Math.abs(yaxis.min - ((midPointCoords.y - initialYmin) * (1 - 1/amount) + initialYmin))).toBeLessThan(1);
  269. expect(Math.abs(yaxis.max - (initialYmax - (initialYmax - midPointCoords.x) * (1 - 1/amount)))).toBeLessThan(1);
  270. });
  271. it('should not zoom the plot on axis for axis zoom not enabled',function() {
  272. plot = $.plot(placeholder, [
  273. [
  274. [0, 0],
  275. [10, 10]
  276. ]
  277. ], {
  278. xaxes: [{ autoScale: 'exact', axisZoom: false, axisPan: false }],
  279. yaxes: [{ autoScale: 'exact' }],
  280. zoom: { interactive: true, active: true, amount: 10 },
  281. pan: { interactive: true, active: true, frameRate: -1, enableTouch: true }
  282. });
  283. var eventHolder = plot.getEventHolder(),
  284. xaxis = plot.getXAxes()[0],
  285. yaxis = plot.getYAxes()[0],
  286. initialCoords = [
  287. { x: xaxis.p2c(4), y: xaxis.box.top + plot.offset().top + 10 },
  288. { x: xaxis.p2c(5), y: xaxis.box.top + plot.offset().top + 10 }
  289. ],
  290. finalCoords = [
  291. getPairOfCoords(xaxis, yaxis, 2, 4),
  292. getPairOfCoords(xaxis, yaxis, 6, 6)
  293. ],
  294. amount = getDistance(finalCoords) / getDistance(initialCoords);
  295. simulateTouchEvent(initialCoords, eventHolder, 'touchstart');
  296. simulateTouchEvent(finalCoords, eventHolder, 'touchmove');
  297. simulateTouchEvent(finalCoords, eventHolder, 'touchend');
  298. expect(xaxis.min).toBe(0);
  299. expect(xaxis.max).toBe(10);
  300. expect(yaxis.min).toBe(0);
  301. expect(yaxis.max).toBe(10);
  302. });
  303. it('should not zoom the axis for plot zoom not enabled',function() {
  304. plot = $.plot(placeholder, [
  305. [
  306. [0, 0],
  307. [10, 10]
  308. ]
  309. ], {
  310. xaxes: [{ autoScale: 'exact', plotZoom: false, plotPan: false }],
  311. yaxes: [{ autoScale: 'exact' }],
  312. zoom: { interactive: true, active: true, amount: 10, enableTouch: true },
  313. pan: { interactive: true, active: true, frameRate: -1, enableTouch: true }
  314. });
  315. var eventHolder = plot.getEventHolder(),
  316. xaxis = plot.getXAxes()[0],
  317. yaxis = plot.getYAxes()[0],
  318. initialCoords = [
  319. { x: 0, y: 10 },
  320. { x: 1, y: 50 }
  321. ],
  322. finalCoords = [
  323. getPairOfCoords(xaxis, yaxis, 2, 4),
  324. getPairOfCoords(xaxis, yaxis, 6, 6)
  325. ],
  326. amount = getDistance(finalCoords) / getDistance(initialCoords);
  327. simulateTouchEvent(initialCoords, eventHolder, 'touchstart');
  328. simulateTouchEvent(finalCoords, eventHolder, 'touchmove');
  329. simulateTouchEvent(finalCoords, eventHolder, 'touchend');
  330. expect(xaxis.min).toBe(0);
  331. expect(xaxis.max).toBe(10);
  332. expect(yaxis.min).not.toBe(0);
  333. expect(yaxis.max).not.toBe(10);
  334. });
  335. function getDistance(coords) {
  336. return Math.sqrt((coords[0].x - coords[1].x) * (coords[0].x - coords[1].x) + ((coords[0].y - coords[1].y) * (coords[0].y - coords[1].y)));
  337. }
  338. });
  339. describe('touchDrag', function() {
  340. it('should drag the plot',function() {
  341. plot = $.plot(placeholder, [
  342. [
  343. [-10, -10],
  344. [120, 120]
  345. ]
  346. ], options);
  347. var eventHolder = plot.getEventHolder(),
  348. xaxis = plot.getXAxes()[0],
  349. yaxis = plot.getYAxes()[0],
  350. initialXmin = xaxis.min,
  351. initialXmax = xaxis.max,
  352. initialYmin = yaxis.min,
  353. initialYmax = yaxis.max,
  354. canvasCoords = [ { x : 1, y : 1 }, { x : 100, y : 100 }],
  355. pointCoords = [
  356. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  357. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  358. ];
  359. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  360. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  361. simulateTouchEvent([], eventHolder, 'touchend'); // it can correctly trigger panend event.
  362. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[1].x), 6);
  363. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[1].x), 6);
  364. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[0].y - canvasCoords[1].y), 6);
  365. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[0].y - canvasCoords[1].y), 6);
  366. });
  367. it('should snap horizontal drag to x direction in smart pan mode', function() {
  368. var oldTouchMode = options.pan.touchMode;
  369. options.pan.touchMode = 'smart';
  370. plot = $.plot(placeholder, [
  371. [
  372. [-10, -10],
  373. [120, 120]
  374. ]
  375. ], options);
  376. var eventHolder = plot.getEventHolder(),
  377. xaxis = plot.getXAxes()[0],
  378. yaxis = plot.getYAxes()[0];
  379. var initialXmin = xaxis.min,
  380. initialXmax = xaxis.max,
  381. initialYmin = yaxis.min,
  382. initialYmax = yaxis.max,
  383. canvasCoords = [ { x : 1, y : 1 }, { x : 100, y : 2 }],
  384. pointCoords = [
  385. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  386. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  387. ];
  388. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  389. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  390. simulateTouchEvent([], eventHolder, 'touchend'); // it can correctly trigger panend event.
  391. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[1].x), 6);
  392. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[1].x), 6);
  393. expect(yaxis.min).toBe(initialYmin);
  394. expect(yaxis.max).toBe(initialYmax);
  395. options.pan.touchMode = oldTouchMode;
  396. });
  397. it('should snap vertical drag to x direction in smart pan mode', function() {
  398. var oldTouchMode = options.pan.touchMode;
  399. options.pan.touchMode = 'smart';
  400. plot = $.plot(placeholder, [
  401. [
  402. [-10, -10],
  403. [120, 120]
  404. ]
  405. ], options);
  406. var eventHolder = plot.getEventHolder(),
  407. xaxis = plot.getXAxes()[0],
  408. yaxis = plot.getYAxes()[0];
  409. var initialXmin = xaxis.min,
  410. initialXmax = xaxis.max,
  411. initialYmin = yaxis.min,
  412. initialYmax = yaxis.max,
  413. canvasCoords = [ { x : 1, y : 1 }, { x : 2, y : 100 }],
  414. pointCoords = [
  415. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  416. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  417. ];
  418. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  419. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  420. simulateTouchEvent([], eventHolder, 'touchend'); // it can correctly trigger panend event.
  421. expect(xaxis.min).toBe(initialXmin);
  422. expect(xaxis.max).toBe(initialXmax);
  423. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[0].y - canvasCoords[1].y), 6);
  424. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[0].y - canvasCoords[1].y), 6);
  425. options.pan.touchMode = oldTouchMode;
  426. });
  427. it('should no snap diagonal drag in smart pan mode', function() {
  428. var oldTouchMode = options.pan.touchMode;
  429. options.pan.touchMode = 'smart';
  430. plot = $.plot(placeholder, [
  431. [
  432. [-10, -10],
  433. [120, 120]
  434. ]
  435. ], options);
  436. var eventHolder = plot.getEventHolder(),
  437. xaxis = plot.getXAxes()[0],
  438. yaxis = plot.getYAxes()[0];
  439. var initialXmin = xaxis.min,
  440. initialXmax = xaxis.max,
  441. initialYmin = yaxis.min,
  442. initialYmax = yaxis.max,
  443. canvasCoords = [ { x : 1, y : 1 }, { x : 100, y : 100 }],
  444. pointCoords = [
  445. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  446. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  447. ];
  448. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  449. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  450. simulateTouchEvent([], eventHolder, 'touchend'); // it can correctly trigger panend event.
  451. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[1].x), 6);
  452. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[1].x), 6);
  453. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[0].y - canvasCoords[1].y), 6);
  454. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[0].y - canvasCoords[1].y), 6);
  455. options.pan.touchMode = oldTouchMode;
  456. });
  457. it('should snap horizontally-start-drag to x direction in smartLock pan mode', function() {
  458. var oldTouchMode = options.pan.touchMode;
  459. options.pan.touchMode = 'smartLock';
  460. plot = $.plot(placeholder, [
  461. [
  462. [-10, -10],
  463. [120, 120]
  464. ]
  465. ], options);
  466. var eventHolder = plot.getEventHolder(),
  467. xaxis = plot.getXAxes()[0],
  468. yaxis = plot.getYAxes()[0];
  469. var initialXmin = xaxis.min,
  470. initialXmax = xaxis.max,
  471. initialYmin = yaxis.min,
  472. initialYmax = yaxis.max,
  473. canvasCoords = [ { x : 1, y : 1 }, { x : 100, y : 2 }, { x: 100, y: 100 }],
  474. pointCoords = [
  475. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  476. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y),
  477. getPairOfCoords(xaxis, yaxis, canvasCoords[2].x, canvasCoords[2].y)
  478. ];
  479. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  480. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  481. simulate.touchmove(eventHolder, pointCoords[2].x, pointCoords[2].y);
  482. simulateTouchEvent([], eventHolder, 'touchend'); // it can correctly trigger panend event.
  483. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[1].x), 6);
  484. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[1].x), 6);
  485. expect(yaxis.min).toBe(initialYmin);
  486. expect(yaxis.max).toBe(initialYmax);
  487. options.pan.touchMode = oldTouchMode;
  488. });
  489. it('should snap vertically-start-drag to y direction in smartLock pan mode', function() {
  490. var oldTouchMode = options.pan.touchMode;
  491. options.pan.touchMode = 'smartLock';
  492. plot = $.plot(placeholder, [
  493. [
  494. [-10, -10],
  495. [120, 120]
  496. ]
  497. ], options);
  498. var eventHolder = plot.getEventHolder(),
  499. xaxis = plot.getXAxes()[0],
  500. yaxis = plot.getYAxes()[0];
  501. var initialXmin = xaxis.min,
  502. initialXmax = xaxis.max,
  503. initialYmin = yaxis.min,
  504. initialYmax = yaxis.max,
  505. canvasCoords = [ { x : 1, y : 1 }, { x : 2, y : 100 }, { x: 100, y: 100 }],
  506. pointCoords = [
  507. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  508. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y),
  509. getPairOfCoords(xaxis, yaxis, canvasCoords[2].x, canvasCoords[2].y)
  510. ];
  511. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  512. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  513. simulate.touchmove(eventHolder, pointCoords[2].x, pointCoords[2].y);
  514. simulateTouchEvent([], eventHolder, 'touchend'); // it can correctly trigger panend event.
  515. expect(xaxis.min).toBe(initialXmin);
  516. expect(xaxis.max).toBe(initialXmax);
  517. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[0].y - canvasCoords[1].y), 6);
  518. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[0].y - canvasCoords[1].y), 6);
  519. options.pan.touchMode = oldTouchMode;
  520. });
  521. it('should not drag if pan mode is invalid', function () {
  522. var oldTouchMode = options.pan.touchMode;
  523. options.pan.touchMode = '';
  524. plot = $.plot(placeholder, [
  525. [
  526. [-10, -10],
  527. [120, 120]
  528. ]
  529. ], options);
  530. var eventHolder = plot.getEventHolder(),
  531. xaxis = plot.getXAxes()[0],
  532. yaxis = plot.getYAxes()[0],
  533. initialXmin = xaxis.min,
  534. initialXmax = xaxis.max,
  535. initialYmin = yaxis.min,
  536. initialYmax = yaxis.max,
  537. canvasCoords = [ { x : 1, y : 1 }, { x : 100, y : 100 }],
  538. pointCoords = [
  539. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  540. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  541. ];
  542. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  543. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  544. simulateTouchEvent([], eventHolder, 'touchend'); // it can correctly trigger panend event.
  545. expect(xaxis.min).toBe(initialXmin);
  546. expect(xaxis.max).toBe(initialXmax);
  547. expect(yaxis.min).toBe(initialYmin);
  548. expect(yaxis.max).toBe(initialYmax);
  549. options.pan.touchMode = oldTouchMode;
  550. });
  551. it('should drag the logarithmic plot', function() {
  552. var d1 = [];
  553. for (var i = 0; i < 14; i += 0.2) {
  554. d1.push([i, 1.01 + Math.sin(i)]);
  555. }
  556. var plot = $.plot(placeholder, [d1], {
  557. series: {
  558. lines: { show: true },
  559. points: { show: true }
  560. },
  561. xaxis: { autoScale: 'exact' },
  562. yaxis: { mode: 'log', showTickLabels: "all", autoScale: 'exact' },
  563. zoom: { interactive: true, active: true },
  564. pan: { interactive: true, active: true, enableTouch: true }
  565. });
  566. var eventHolder = plot.getEventHolder(),
  567. xaxis = plot.getXAxes()[0],
  568. yaxis = plot.getYAxes()[0],
  569. initialXmin = xaxis.min,
  570. initialXmax = xaxis.max,
  571. initialYmin = yaxis.min,
  572. initialYmax = yaxis.max,
  573. canvasCoords = [ { x : 4, y : 0.7 }, { x : 2, y : 10 } ],
  574. pointCoords = [
  575. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  576. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  577. ];
  578. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  579. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  580. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  581. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[1].x), 6);
  582. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[1].x), 6);
  583. expect(yaxis.min).toBeCloseTo((yaxis.c2p(yaxis.p2c(initialYmin) + (pointCoords[0].y - pointCoords[1].y))), 6);
  584. expect(yaxis.max).toBeCloseTo((yaxis.c2p(yaxis.p2c(initialYmax) + (pointCoords[0].y - pointCoords[1].y))), 6);
  585. });
  586. it('should drag the point in the same way for many sequential moves as for one long move',function() {
  587. //deactivate ticks for precision
  588. options.yaxes[0].showTickLabels = 'none';
  589. options.xaxes[0].showTickLabels = 'all';
  590. plot = $.plot(placeholder, [
  591. [
  592. [-10, -10],
  593. [120, 120]
  594. ]
  595. ], options);
  596. var eventHolder = plot.getEventHolder(),
  597. xaxis = plot.getXAxes()[0],
  598. yaxis = plot.getYAxes()[0],
  599. initialXmin = xaxis.min,
  600. initialXmax = xaxis.max,
  601. initialYmin = yaxis.min,
  602. initialYmax = yaxis.max,
  603. limit = 5,
  604. canvasCoords = [],
  605. pointCoords = [];
  606. for (var i = 1; i <= limit; i++) {
  607. canvasCoords[i] = { x: i, y: i };
  608. pointCoords[i] = getPairOfCoords(xaxis, yaxis, canvasCoords[i].x, canvasCoords[i].y);
  609. }
  610. //simulate drag from (1, 1) to (100, 100) sequentially
  611. simulate.touchstart(eventHolder, pointCoords[1].x, pointCoords[1].y);
  612. for (var i = 2; i <= limit; i++) {
  613. simulate.touchmove(eventHolder, pointCoords[i].x, pointCoords[i].y);
  614. }
  615. simulate.touchend(eventHolder, pointCoords[limit].x, pointCoords[limit].y);
  616. // compare axes after sequential drag with axes after direct drag
  617. expect(Math.abs(xaxis.min - (initialXmin + (canvasCoords[1].x - canvasCoords[limit].x)))).toBeLessThan(1);
  618. expect(Math.abs(xaxis.max - (initialXmax + (canvasCoords[1].x - canvasCoords[limit].x)))).toBeLessThan(1);
  619. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[1].y - canvasCoords[limit].y), 0);
  620. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[1].y - canvasCoords[limit].y), 0);
  621. });
  622. it('should not pan the plot for a finger outside canvas',function() {
  623. plot = $.plot(placeholder, [
  624. [
  625. [-10, 120],
  626. [-10, 120]
  627. ]
  628. ], options);
  629. var eventHolder = plot.getEventHolder(),
  630. xaxis = plot.getXAxes()[0],
  631. yaxis = plot.getYAxes()[0],
  632. initialXmin = xaxis.min,
  633. initialXmax = xaxis.max,
  634. initialYmin = yaxis.min,
  635. initialYmax = yaxis.max,
  636. canvasCoords = [ { x : 20, y : 20 }, { x : 120, y : 120 }],
  637. plotRight = plot.width() + plot.offset().left + plot.offset().right,
  638. plotBottom = plot.height() + plot.offset().top + plot.offset().bottom,
  639. pointCoords = [
  640. {x: plotRight + canvasCoords[0].x, y: plotBottom + canvasCoords[0].y},
  641. {x: plotRight + canvasCoords[1].x, y: plotBottom + canvasCoords[1].y}
  642. ];
  643. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  644. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  645. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  646. expect(xaxis.min).toBeCloseTo(initialXmin, 6);
  647. expect(xaxis.max).toBeCloseTo(initialXmax, 6);
  648. expect(yaxis.min).toBeCloseTo(initialYmin, 6);
  649. expect(yaxis.max).toBeCloseTo(initialYmax, 6);
  650. });
  651. it('should not pan the plot for a finger which comes from outside canvas',function() {
  652. plot = $.plot(placeholder, [
  653. [
  654. [-10, 120],
  655. [-10, 120]
  656. ]
  657. ], options);
  658. var eventHolder = plot.getEventHolder(),
  659. xaxis = plot.getXAxes()[0],
  660. yaxis = plot.getYAxes()[0],
  661. initialXmin = xaxis.min,
  662. initialXmax = xaxis.max,
  663. initialYmin = yaxis.min,
  664. initialYmax = yaxis.max,
  665. canvasCoords = [ { x : 20, y : 20 }, { x : 120, y : 120 }],
  666. plotRight = plot.width() + plot.offset().left + plot.offset().right,
  667. plotBottom = plot.height() + plot.offset().top + plot.offset().bottom,
  668. pointCoords = [
  669. {x: plotRight + canvasCoords[0].x, y: plotBottom + canvasCoords[0].y},
  670. {x: canvasCoords[1].x, y: canvasCoords[1].y}
  671. ];
  672. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  673. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  674. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  675. expect(xaxis.min).toBeCloseTo(initialXmin, 6);
  676. expect(xaxis.max).toBeCloseTo(initialXmax, 6);
  677. expect(yaxis.min).toBeCloseTo(initialYmin, 6);
  678. expect(yaxis.max).toBeCloseTo(initialYmax, 6);
  679. });
  680. it('should drag the plot on the yaxis only', function() {
  681. plot = $.plot(placeholder, [
  682. [
  683. [0, 0],
  684. [10, 10]
  685. ]
  686. ], options);
  687. var eventHolder = plot.getEventHolder(),
  688. xaxis = plot.getXAxes()[0],
  689. yaxis = plot.getYAxes()[0],
  690. initialXmin = xaxis.min,
  691. initialXmax = xaxis.max,
  692. initialYmin = yaxis.min,
  693. initialYmax = yaxis.max,
  694. pointCoords = [
  695. { x: xaxis.box.left - 10, y: yaxis.p2c(4) },
  696. { x: xaxis.box.left - 20, y: yaxis.p2c(5) }
  697. ];
  698. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  699. simulate.touchmove(eventHolder, pointCoords[0].x, pointCoords[0].y);
  700. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  701. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  702. expect(xaxis.min).toBe(0);
  703. expect(xaxis.max).toBe(10);
  704. expect(yaxis.min).toBeCloseTo(yaxis.c2p(yaxis.p2c(initialYmin) + (pointCoords[0].y - pointCoords[1].y)), 6);
  705. expect(yaxis.max).toBeCloseTo(yaxis.c2p(yaxis.p2c(initialYmax) + (pointCoords[0].y - pointCoords[1].y)), 6);
  706. });
  707. it('should drag the plot on the xaxis only', function() {
  708. plot = $.plot(placeholder, [
  709. [
  710. [0, 0],
  711. [10, 10]
  712. ]
  713. ], options);
  714. var eventHolder = plot.getEventHolder(),
  715. xaxis = plot.getXAxes()[0],
  716. yaxis = plot.getYAxes()[0],
  717. initialXmin = xaxis.min,
  718. initialXmax = xaxis.max,
  719. pointCoords = [
  720. { x: xaxis.p2c(4), y: xaxis.box.top + plot.offset().top + 10 },
  721. { x: xaxis.p2c(5), y: xaxis.box.top + plot.offset().top + 15 }
  722. ];
  723. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  724. simulate.touchmove(eventHolder, pointCoords[0].x, pointCoords[0].y);
  725. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  726. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  727. expect(xaxis.min).toBeCloseTo(xaxis.c2p(xaxis.p2c(initialXmin) + (pointCoords[0].x - pointCoords[1].x)), 6);
  728. expect(xaxis.max).toBeCloseTo(xaxis.c2p(xaxis.p2c(initialXmax) + (pointCoords[0].x - pointCoords[1].x)), 6);
  729. expect(yaxis.min).toBe(0);
  730. expect(yaxis.max).toBe(10);
  731. });
  732. it('should not drag the plot on axis for axis pan not enabled', function() {
  733. plot = $.plot(placeholder, [
  734. [
  735. [0, 0],
  736. [10, 10]
  737. ]
  738. ], {
  739. xaxes: [{ autoScale: 'exact', axisPan: false }],
  740. yaxes: [{ autoScale: 'exact' }],
  741. zoom: { interactive: true, active: true, amount: 10 },
  742. pan: { interactive: true, active: true, frameRate: -1, enableTouch: true }
  743. });
  744. var eventHolder = plot.getEventHolder(),
  745. xaxis = plot.getXAxes()[0],
  746. yaxis = plot.getYAxes()[0],
  747. initialXmin = xaxis.min,
  748. initialXmax = xaxis.max,
  749. pointCoords = [
  750. { x: xaxis.p2c(4), y: xaxis.box.top + plot.offset().top + 10 },
  751. { x: xaxis.p2c(5), y: xaxis.box.top + plot.offset().top + 15 }
  752. ];
  753. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  754. simulate.touchmove(eventHolder, pointCoords[0].x, pointCoords[0].y);
  755. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  756. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  757. expect(xaxis.min).toBe(0);
  758. expect(yaxis.max).toBe(10);
  759. expect(yaxis.min).toBe(0);
  760. expect(yaxis.max).toBe(10);
  761. });
  762. it('should not drag the axis for plot pan not enabled', function() {
  763. plot = $.plot(placeholder, [
  764. [
  765. [0, 0],
  766. [10, 10]
  767. ]
  768. ], {
  769. xaxes: [{ autoScale: 'exact', plotPan: false }],
  770. yaxes: [{ autoScale: 'exact', plotPan: false }],
  771. zoom: { interactive: true, active: true, amount: 10 },
  772. pan: { interactive: true, active: true, frameRate: -1, enableTouch: true }
  773. });
  774. var eventHolder = plot.getEventHolder(),
  775. xaxis = plot.getXAxes()[0],
  776. yaxis = plot.getYAxes()[0],
  777. initialXmin = xaxis.min,
  778. initialXmax = xaxis.max,
  779. pointCoords = [
  780. { x: 0, y: 50 },
  781. { x: 50, y: 100 }
  782. ];
  783. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  784. simulate.touchmove(eventHolder, pointCoords[0].x, pointCoords[0].y);
  785. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  786. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  787. expect(xaxis.min).toBe(0);
  788. expect(xaxis.max).toBe(10);
  789. expect(yaxis.min).toBe(0);
  790. expect(yaxis.max).toBe(10);
  791. });
  792. it('should recompute the axis to drag after a pinch event ended',function() {
  793. plot = $.plot(placeholder, [
  794. [
  795. [-1, 2],
  796. [11, 12]
  797. ]
  798. ], options);
  799. var eventHolder = plot.getEventHolder(),
  800. xaxis = plot.getXAxes()[0],
  801. yaxis = plot.getYAxes()[0],
  802. initialXmin = xaxis.min,
  803. initialXmax = xaxis.max,
  804. initialYmin = yaxis.min,
  805. initialYmax = yaxis.max,
  806. previousXmin, previousXmax, previousYmin, previousYmax,
  807. initialCoords = [
  808. getPairOfCoords(xaxis, yaxis, 1, 3),
  809. getPairOfCoords(xaxis, yaxis, 2, 4),
  810. ],
  811. midPointCoords = [
  812. getPairOfCoords(xaxis, yaxis, 3, 7),
  813. { x: xaxis.p2c(4), y: xaxis.box.top + plot.offset().top + 10 }
  814. ],
  815. finalCoordsPinch = [
  816. { x: xaxis.p2c(4), y: xaxis.box.top + plot.offset().top + 10 }
  817. ],
  818. finalCoordsPan = [
  819. { x: xaxis.p2c(5), y: xaxis.box.top + plot.offset().top + 15 }
  820. ];
  821. //simulate pinch
  822. simulateTouchEvent(initialCoords, eventHolder, 'touchstart');
  823. simulateTouchEvent(midPointCoords, eventHolder, 'touchmove');
  824. simulateTouchEvent(finalCoordsPinch, eventHolder, 'touchend');
  825. previousXmin = xaxis.min;
  826. previousXmax = xaxis.max;
  827. previousYmin = yaxis.min;
  828. previousYmax = yaxis.max;
  829. expect(previousXmin).not.toBeCloseTo(initialXmin, 6);
  830. expect(previousXmax).not.toBeCloseTo(initialXmax, 6);
  831. expect(previousYmin).not.toBeCloseTo(initialYmin, 6);
  832. expect(previousYmax).not.toBeCloseTo(initialYmax, 6);
  833. //simulate pan after pinch event
  834. simulateTouchEvent(finalCoordsPan, eventHolder, 'touchmove');
  835. simulateTouchEvent(finalCoordsPan, eventHolder, 'touchend');
  836. expect(xaxis.min).toBeCloseTo(xaxis.c2p(xaxis.p2c(previousXmin) + (finalCoordsPinch[0].x - finalCoordsPan[0].x)), 6);
  837. expect(xaxis.max).toBeCloseTo(xaxis.c2p(xaxis.p2c(previousXmax) + (finalCoordsPinch[0].x - finalCoordsPan[0].x)), 6);
  838. expect(yaxis.min).toBe(previousYmin);
  839. expect(yaxis.max).toBe(previousYmax);
  840. });
  841. it('can disable pinch', function() {
  842. var oldTouchZoom = options.zoom.enableTouch;
  843. options.zoom.enableTouch = false;
  844. plot = $.plot(placeholder, [
  845. [
  846. [-10, -10],
  847. [120, 120]
  848. ]
  849. ], options);
  850. var eventHolder = plot.getEventHolder(),
  851. xaxis = plot.getXAxes()[0],
  852. yaxis = plot.getYAxes()[0];
  853. var initialXmin = xaxis.min,
  854. initialXmax = xaxis.max,
  855. initialYmin = yaxis.min,
  856. initialYmax = yaxis.max,
  857. canvasCoords = [ { x : 1, y : 1 }, { x : 100, y : 5 }],
  858. initialCoords = [
  859. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  860. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x + 1, canvasCoords[0].y + 1),
  861. ],
  862. finalCoordsPinch = [
  863. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y),
  864. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x + 1, canvasCoords[1].y + 1),
  865. ];
  866. //simulate pinch
  867. simulateTouchEvent(initialCoords, eventHolder, 'touchstart');
  868. simulateTouchEvent(finalCoordsPinch, eventHolder, 'touchmove');
  869. simulateTouchEvent(finalCoordsPinch, eventHolder, 'touchend');
  870. expect(xaxis.min).toBe(initialXmin);
  871. expect(xaxis.max).toBe(initialXmax);
  872. expect(yaxis.min).toBe(initialYmin);
  873. expect(yaxis.max).toBe(initialYmax);
  874. options.zoom.enableTouch = oldTouchZoom;
  875. })
  876. });
  877. describe('doubleTap', function() {
  878. it('should recenter the plot',function() {
  879. plot = $.plot(placeholder, [
  880. [
  881. [-10, 120],
  882. [-10, 120]
  883. ]
  884. ], options);
  885. var eventHolder = plot.getEventHolder(),
  886. xaxis = plot.getXAxes()[0],
  887. yaxis = plot.getYAxes()[0],
  888. initialXmin = xaxis.min,
  889. initialXmax = xaxis.max,
  890. initialYmin = yaxis.min,
  891. initialYmax = yaxis.max,
  892. canvasCoords = [ { x : 1, y : 2 }, { x : 3, y : 5 }],
  893. pointCoords = [
  894. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  895. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  896. ];
  897. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  898. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  899. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  900. //check if the drag modified the plot correctly
  901. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[1].x), 6);
  902. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[1].x), 6);
  903. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[0].y - canvasCoords[1].y), 6);
  904. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[0].y - canvasCoords[1].y), 6);
  905. //simulate double tap
  906. simulate.touchstart(eventHolder, pointCoords[1].x, pointCoords[1].y);
  907. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  908. simulate.touchstart(eventHolder, pointCoords[1].x, pointCoords[1].y);
  909. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  910. //check if axis values returned to initial coordinates
  911. expect(xaxis.min).toBeCloseTo(initialXmin, 6);
  912. expect(xaxis.max).toBeCloseTo(initialXmax, 6);
  913. expect(yaxis.min).toBeCloseTo(initialYmin, 6);
  914. expect(yaxis.max).toBeCloseTo(initialYmax, 6);
  915. });
  916. it('should not recenter for doubletap event triggered by touchmove', function() {
  917. //when touchmove to somewhere close to last doubletap point, another doubletap will be triggered
  918. //this doubletap event should not do recenter
  919. plot = $.plot(placeholder, [
  920. [
  921. [-10, 120],
  922. [-10, 120]
  923. ]
  924. ], options);
  925. var eventHolder = plot.getEventHolder(),
  926. xaxis = plot.getXAxes()[0],
  927. yaxis = plot.getYAxes()[0],
  928. initialXmin = xaxis.min,
  929. initialXmax = xaxis.max,
  930. initialYmin = yaxis.min,
  931. initialYmax = yaxis.max,
  932. canvasCoords = [ { x : 1, y : 2 }, { x : 3, y : 5 }],
  933. pointCoords = [
  934. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  935. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  936. ];
  937. //simulate double tap
  938. simulate.touchstart(eventHolder, pointCoords[1].x, pointCoords[1].y);
  939. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  940. simulate.touchstart(eventHolder, pointCoords[1].x, pointCoords[1].y);
  941. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  942. //drag the plot
  943. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  944. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  945. //check if the drag modified the plot correctly
  946. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[1].x), 6);
  947. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[1].x), 6);
  948. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[0].y - canvasCoords[1].y), 6);
  949. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[0].y - canvasCoords[1].y), 6);
  950. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  951. });
  952. it('should recenter the plot on axis x',function() {
  953. plot = $.plot(placeholder, [
  954. [
  955. [-10, -10],
  956. [120, 120]
  957. ]
  958. ], options);
  959. var eventHolder = plot.getEventHolder(),
  960. xaxis = plot.getXAxes()[0],
  961. yaxis = plot.getYAxes()[0],
  962. initialXmin = xaxis.min,
  963. initialXmax = xaxis.max,
  964. initialYmin = yaxis.min,
  965. initialYmax = yaxis.max,
  966. canvasCoords = [ { x : 1, y : 2 }, { x : 3, y : 5 }],
  967. pointCoords = [
  968. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  969. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  970. ];
  971. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  972. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  973. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  974. //check if the drag modified the plot correctly
  975. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[1].x), 6);
  976. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[1].x), 6);
  977. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[0].y - canvasCoords[1].y), 6);
  978. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[0].y - canvasCoords[1].y), 6);
  979. //simulate double tap
  980. simulate.touchstart(eventHolder, xaxis.p2c(5), xaxis.box.top + plot.offset().top + 15);
  981. simulate.touchend(eventHolder, xaxis.p2c(5), xaxis.box.top + plot.offset().top + 15);
  982. simulate.touchstart(eventHolder, xaxis.p2c(5), xaxis.box.top + plot.offset().top + 15);
  983. simulate.touchend(eventHolder, xaxis.p2c(5), xaxis.box.top + plot.offset().top + 15);
  984. //check if axis values returned to initial coordinates
  985. expect(xaxis.min).toBeCloseTo(initialXmin, 6);
  986. expect(xaxis.max).toBeCloseTo(initialXmax, 6);
  987. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[0].y - canvasCoords[1].y), 6);
  988. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[0].y - canvasCoords[1].y), 6);
  989. });
  990. it('should recenter the plot on axis y',function() {
  991. plot = $.plot(placeholder, [
  992. [
  993. [-10, -10],
  994. [120, 120]
  995. ]
  996. ], options);
  997. var eventHolder = plot.getEventHolder(),
  998. xaxis = plot.getXAxes()[0],
  999. yaxis = plot.getYAxes()[0],
  1000. initialXmin = xaxis.min,
  1001. initialXmax = xaxis.max,
  1002. initialYmin = yaxis.min,
  1003. initialYmax = yaxis.max,
  1004. canvasCoords = [ { x : 1, y : 2 }, { x : 3, y : 5 }],
  1005. pointCoords = [
  1006. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  1007. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  1008. ];
  1009. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  1010. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  1011. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  1012. //check if the drag modified the plot correctly
  1013. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[1].x), 6);
  1014. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[1].x), 6);
  1015. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[0].y - canvasCoords[1].y), 6);
  1016. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[0].y - canvasCoords[1].y), 6);
  1017. //simulate double tap
  1018. simulate.touchstart(eventHolder, xaxis.box.left - 20, yaxis.p2c(5));
  1019. simulate.touchend(eventHolder, xaxis.box.left - 20, yaxis.p2c(5));
  1020. simulate.touchstart(eventHolder, xaxis.box.left - 20, yaxis.p2c(5));
  1021. simulate.touchend(eventHolder, xaxis.box.left - 20, yaxis.p2c(5));
  1022. //check if axis values returned to initial coordinates
  1023. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[1].x), 6);
  1024. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[1].x), 6);
  1025. expect(yaxis.min).toBeCloseTo(initialYmin, 6);
  1026. expect(yaxis.max).toBeCloseTo(initialYmax, 6);
  1027. });
  1028. it('should not recenter the plot for one touch inside axis box and one inside plot area', function(){
  1029. plot = $.plot(placeholder, [
  1030. [
  1031. [-10, -10],
  1032. [120, 120]
  1033. ]
  1034. ], options);
  1035. var eventHolder = plot.getEventHolder(),
  1036. xaxis = plot.getXAxes()[0],
  1037. yaxis = plot.getYAxes()[0],
  1038. initialXmin = xaxis.min,
  1039. initialXmax = xaxis.max,
  1040. initialYmin = yaxis.min,
  1041. initialYmax = yaxis.max,
  1042. canvasCoords = [ { x : 1, y : 2 }, { x : 3, y : 5 }],
  1043. pointCoords = [
  1044. getPairOfCoords(xaxis, yaxis, canvasCoords[0].x, canvasCoords[0].y),
  1045. getPairOfCoords(xaxis, yaxis, canvasCoords[1].x, canvasCoords[1].y)
  1046. ];
  1047. simulate.touchstart(eventHolder, pointCoords[0].x, pointCoords[0].y);
  1048. simulate.touchmove(eventHolder, pointCoords[1].x, pointCoords[1].y);
  1049. simulate.touchend(eventHolder, pointCoords[1].x, pointCoords[1].y);
  1050. //check if the drag modified the plot correctly
  1051. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[1].x), 6);
  1052. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[1].x), 6);
  1053. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[0].y - canvasCoords[1].y), 6);
  1054. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[0].y - canvasCoords[1].y), 6);
  1055. //simulate double tap
  1056. simulate.touchstart(eventHolder, xaxis.box.left - 20, yaxis.p2c(5));
  1057. simulate.touchend(eventHolder, xaxis.box.left - 20, yaxis.p2c(5));
  1058. simulate.touchstart(eventHolder, 10, 20);
  1059. simulate.touchend(eventHolder, 10, 20);
  1060. //check if axis values remained the same
  1061. expect(xaxis.min).toBeCloseTo(initialXmin + (canvasCoords[0].x - canvasCoords[1].x), 6);
  1062. expect(xaxis.max).toBeCloseTo(initialXmax + (canvasCoords[0].x - canvasCoords[1].x), 6);
  1063. expect(yaxis.min).toBeCloseTo(initialYmin + (canvasCoords[0].y - canvasCoords[1].y), 6);
  1064. expect(yaxis.max).toBeCloseTo(initialYmax + (canvasCoords[0].y - canvasCoords[1].y), 6);
  1065. });
  1066. });
  1067. function getPairOfCoords(xaxis, yaxis, x, y) {
  1068. return {
  1069. x : xaxis.p2c(x) + plot.offset().left,
  1070. y : yaxis.p2c(y) + plot.offset().top
  1071. }
  1072. }
  1073. });