ItemHighlight.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. topSuite("Ext.chart.interactions.ItemHighlight", ['Ext.chart.*', 'Ext.data.ArrayStore'],
  2. function() {
  3. var chart;
  4. afterEach(function() {
  5. chart = Ext.destroy(chart);
  6. });
  7. jasmine.supportsTouch ? xdescribe : describe("multiTooltips", function() {
  8. var layoutSpy, series1Spy, series2Spy, series3Spy, series4Spy,
  9. tooltips;
  10. function expectSpies(spy1, spy2, spy3, spy4) {
  11. if (spy1) {
  12. expect(series1Spy).toHaveBeenCalled();
  13. }
  14. else {
  15. expect(series1Spy).not.toHaveBeenCalled();
  16. }
  17. if (spy2) {
  18. expect(series2Spy).toHaveBeenCalled();
  19. }
  20. else {
  21. expect(series2Spy).not.toHaveBeenCalled();
  22. }
  23. if (spy3) {
  24. expect(series3Spy).toHaveBeenCalled();
  25. }
  26. else {
  27. expect(series3Spy).not.toHaveBeenCalled();
  28. }
  29. if (spy4) {
  30. expect(series4Spy).toHaveBeenCalled();
  31. }
  32. else {
  33. expect(series4Spy).not.toHaveBeenCalled();
  34. }
  35. }
  36. function resetSpies() {
  37. series1Spy.reset();
  38. series2Spy.reset();
  39. series3Spy.reset();
  40. series4Spy.reset();
  41. }
  42. beforeEach(function() {
  43. tooltips = [];
  44. layoutSpy = jasmine.createSpy('layout done');
  45. series1Spy = jasmine.createSpy('series 1 tooltip renderer');
  46. series2Spy = jasmine.createSpy('series 2 tooltip renderer');
  47. series3Spy = jasmine.createSpy('series 3 tooltip renderer');
  48. series4Spy = jasmine.createSpy('series 4 tooltip renderer');
  49. chart = Ext.create({
  50. xtype: 'cartesian',
  51. renderTo: document.body,
  52. width: 470,
  53. height: 300,
  54. store: {
  55. fields: ['name', 'data1', 'data2', 'data3', 'data4'],
  56. data: [{
  57. 'name': 'metric one',
  58. 'data1': 10,
  59. 'data2': 14,
  60. 'data3': 18,
  61. 'data4': 2
  62. }, {
  63. 'name': 'metric two',
  64. 'data1': 7,
  65. 'data2': 16,
  66. 'data3': 18,
  67. 'data4': 2
  68. }, {
  69. 'name': 'metric three',
  70. 'data1': 5,
  71. 'data2': 14,
  72. 'data3': 18,
  73. 'data4': 2
  74. }, {
  75. 'name': 'metric four',
  76. 'data1': 2,
  77. 'data2': 6,
  78. 'data3': 18,
  79. 'data4': 2
  80. }, {
  81. 'name': 'metric five',
  82. 'data1': 27,
  83. 'data2': 36,
  84. 'data3': 18,
  85. 'data4': 2
  86. }]
  87. },
  88. interactions: {
  89. type: 'itemhighlight',
  90. multiTooltips: true
  91. },
  92. series: [{
  93. type: 'line',
  94. title: 'Series 1',
  95. style: {
  96. lineWidth: 2,
  97. selectionTolerance: 40
  98. },
  99. xField: 'name',
  100. yField: 'data1',
  101. marker: {
  102. type: 'diamond',
  103. path: ['M', -4, 0, 0, 4, 4, 0, 0, -4, 'Z'],
  104. lineWidth: 2
  105. },
  106. tips: {
  107. trackMouse: true,
  108. renderer: series1Spy
  109. }
  110. }, {
  111. type: 'line',
  112. title: 'Series 2',
  113. style: {
  114. lineWidth: 2,
  115. selectionTolerance: 40
  116. },
  117. xField: 'name',
  118. yField: 'data2',
  119. marker: {
  120. type: 'diamond',
  121. path: ['M', -4, 0, 0, 4, 4, 0, 0, -4, 'Z'],
  122. lineWidth: 2
  123. },
  124. tips: {
  125. trackMouse: true,
  126. renderer: series2Spy
  127. }
  128. }, {
  129. type: 'line',
  130. title: 'Series 3',
  131. style: {
  132. lineWidth: 2,
  133. selectionTolerance: 40
  134. },
  135. xField: 'name',
  136. yField: 'data3',
  137. marker: {
  138. type: 'diamond',
  139. path: ['M', -4, 0, 0, 4, 4, 0, 0, -4, 'Z'],
  140. lineWidth: 2
  141. },
  142. tips: {
  143. trackMouse: true,
  144. renderer: series3Spy
  145. }
  146. }, {
  147. type: 'line',
  148. title: 'Series 4',
  149. style: {
  150. lineWidth: 2,
  151. selectionTolerance: 40
  152. },
  153. xField: 'name',
  154. yField: 'data4',
  155. marker: {
  156. type: 'diamond',
  157. path: ['M', -4, 0, 0, 4, 4, 0, 0, -4, 'Z'],
  158. lineWidth: 2
  159. },
  160. tips: {
  161. trackMouse: true,
  162. renderer: series4Spy
  163. }
  164. }],
  165. axes: [{
  166. type: 'numeric',
  167. position: 'left',
  168. fields: ['data1', 'data2', 'data3', 'data4'],
  169. title: {
  170. text: 'Sample Values',
  171. fontSize: 15
  172. },
  173. grid: true,
  174. minimum: 0
  175. }, {
  176. type: 'category',
  177. position: 'bottom',
  178. fields: ['name'],
  179. title: {
  180. text: 'Sample Values',
  181. fontSize: 15
  182. }
  183. }],
  184. listeners: {
  185. layout: layoutSpy
  186. }
  187. });
  188. series1Spy.andCallFake(function(tooltip) {
  189. tooltips.push(tooltip);
  190. });
  191. series2Spy.andCallFake(function(tooltip) {
  192. tooltips.push(tooltip);
  193. });
  194. series3Spy.andCallFake(function(tooltip) {
  195. tooltips.push(tooltip);
  196. });
  197. series4Spy.andCallFake(function(tooltip) {
  198. tooltips.push(tooltip);
  199. });
  200. waitForSpy(layoutSpy);
  201. });
  202. afterEach(function() {
  203. layoutSpy = series1Spy = series2Spy = series3Spy = series4Spy = null;
  204. tooltips = null;
  205. });
  206. describe("multiTooltips === false", function() {
  207. beforeEach(function() {
  208. chart.getInteractions()[0].setMultiTooltips(false);
  209. });
  210. it("should call showTooltip only for one series", function() {
  211. jasmine.fireMouseEvent(chart, 'mousemove', 257, 143);
  212. expectSpies(false, false, true, false);
  213. });
  214. it("should reuse tooltip instance when mouse moves within tolerance", function() {
  215. var tooltip;
  216. jasmine.fireMouseEvent(chart, 'mousemove', 257, 143);
  217. expectSpies(false, false, true, false);
  218. expect(tooltips.length).toBe(1);
  219. expect(tooltips[0].isVisible()).toBe(true);
  220. tooltip = tooltips[0];
  221. tooltips.length = 0;
  222. resetSpies();
  223. jasmine.fireMouseEvent(chart, 'mousemove', 260, 138);
  224. expectSpies(false, false, true, false);
  225. expect(tooltips.length).toBe(1);
  226. expect(tooltips[0]).toBe(tooltip);
  227. expect(tooltips[0].isVisible()).toBe(true);
  228. });
  229. it("should hide old tooltip when cursor moves outside of tolerance", function() {
  230. var tooltip;
  231. jasmine.fireMouseEvent(chart, 'mousemove', 254, 141);
  232. expectSpies(false, false, true, false);
  233. expect(tooltips.length).toBe(1);
  234. tooltip = tooltips[0];
  235. expect(tooltip.isVisible()).toBe(true);
  236. resetSpies();
  237. jasmine.fireMouseEvent(chart, 'mousemove', 0, 0);
  238. waitFor(function() {
  239. return !tooltip.isVisible();
  240. });
  241. runs(function() {
  242. expect(tooltip.isVisible()).toBe(false);
  243. expectSpies(false, false, false, false);
  244. });
  245. });
  246. it("should hide old tooltip and show new one", function() {
  247. var tooltip;
  248. jasmine.fireMouseEvent(chart, 'mousemove', 140, 240);
  249. expectSpies(false, false, false, true);
  250. expect(tooltips.length).toBe(1);
  251. tooltip = tooltips[0];
  252. expect(tooltip.isVisible()).toBe(true);
  253. tooltips.length = 0;
  254. resetSpies();
  255. jasmine.fireMouseEvent(chart, 'mousemove', 356, 120);
  256. waitFor(function() {
  257. return !tooltip.isVisible();
  258. });
  259. runs(function() {
  260. expectSpies(false, false, true, false);
  261. expect(tooltip.isVisible()).toBe(false);
  262. expect(tooltips.length).toBe(1);
  263. expect(tooltips[0]).not.toBe(tooltip);
  264. expect(tooltips[0].isVisible()).toBe(true);
  265. });
  266. });
  267. });
  268. describe("multiTooltips === true", function() {
  269. it("should call showTooltip for multiple series", function() {
  270. jasmine.fireMouseEvent(chart, 'mousemove', 154, 120);
  271. expectSpies(false, true, true, false);
  272. expect(tooltips.length).toBe(2);
  273. });
  274. it("should add tooltips when more than one series is within radius", function() {
  275. jasmine.fireMouseEvent(chart, 'mousemove', 370, 125);
  276. expectSpies(false, false, true, false);
  277. expect(tooltips.length).toBe(1);
  278. tooltips.length = 0;
  279. resetSpies();
  280. jasmine.fireMouseEvent(chart, 'mousemove', 168, 125);
  281. expectSpies(false, true, true, false);
  282. expect(tooltips.length).toBe(2);
  283. });
  284. it("should reuse tooltip instances when mouse moves within tolerance", function() {
  285. var oldTooltips;
  286. jasmine.fireMouseEvent(chart, 'mousemove', 168, 125);
  287. expectSpies(false, true, true, false);
  288. expect(tooltips.length).toBe(2);
  289. expect(tooltips[0].isVisible()).toBe(true);
  290. expect(tooltips[1].isVisible()).toBe(true);
  291. oldTooltips = tooltips;
  292. tooltips.length = 0;
  293. resetSpies();
  294. jasmine.fireMouseEvent(chart, 'mousemove', 264, 146);
  295. expectSpies(false, true, true, false);
  296. expect(tooltips.length).toBe(2);
  297. expect(tooltips[0]).toBe(oldTooltips[0]);
  298. expect(tooltips[1]).toBe(oldTooltips[1]);
  299. });
  300. it("should hide old tooltips and show new ones", function() {
  301. var oldTooltips = [];
  302. jasmine.fireMouseEvent(chart, 'mousemove', 264, 146);
  303. expectSpies(false, true, true, false);
  304. expect(tooltips.length).toBe(2);
  305. expect(tooltips[0].isVisible()).toBe(true);
  306. expect(tooltips[1].isVisible()).toBe(true);
  307. oldTooltips[0] = tooltips[0];
  308. oldTooltips[1] = tooltips[1];
  309. tooltips.length = 0;
  310. resetSpies();
  311. jasmine.fireMouseEvent(chart, 'mousemove', 147, 222);
  312. waitFor(function() {
  313. return !oldTooltips[0].isVisible() && !oldTooltips[1].isVisible();
  314. }, 'tooltips to hide', 1000);
  315. runs(function() {
  316. expectSpies(true, false, false, true);
  317. expect(tooltips.length).toBe(2);
  318. expect(tooltips[0].isVisible()).toBe(true);
  319. expect(tooltips[1].isVisible()).toBe(true);
  320. expect(Ext.Array.contains(oldTooltips, tooltips[0])).toBe(false);
  321. expect(Ext.Array.contains(oldTooltips, tooltips[1])).toBe(false);
  322. });
  323. });
  324. });
  325. });
  326. });