containPixel.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* jshint maxlen:200 */
  2. describe('api/containPixel', function() {
  3. var utHelper = window.utHelper;
  4. var testCase = utHelper.prepare([
  5. 'echarts/src/chart/pie',
  6. 'echarts/src/chart/map',
  7. 'echarts/src/chart/scatter',
  8. 'echarts/src/chart/graph',
  9. 'echarts/src/component/geo',
  10. 'echarts/src/component/grid',
  11. 'echarts/src/component/polar'
  12. ]);
  13. var testGeoJson1 = {
  14. 'type': 'FeatureCollection',
  15. 'features': [
  16. {
  17. 'geometry': {
  18. 'type': 'Polygon',
  19. 'coordinates': [
  20. [
  21. [
  22. 2000,
  23. 3000
  24. ],
  25. [
  26. 5000,
  27. 3000
  28. ],
  29. [
  30. 5000,
  31. 8000
  32. ],
  33. [
  34. 2000,
  35. 8000
  36. ]
  37. ]
  38. ]
  39. },
  40. 'properties': {
  41. 'name': 'Afghanistan',
  42. 'childNum': 1
  43. }
  44. }
  45. ]
  46. };
  47. var testGeoJson2 = {
  48. 'type': 'FeatureCollection',
  49. 'features': [
  50. {
  51. 'geometry': {
  52. 'type': 'Polygon',
  53. 'coordinates': [
  54. [
  55. [
  56. 200,
  57. 300
  58. ],
  59. [
  60. 500,
  61. 300
  62. ],
  63. [
  64. 500,
  65. 800
  66. ],
  67. [
  68. 200,
  69. 800
  70. ]
  71. ]
  72. ]
  73. },
  74. 'properties': {
  75. 'name': 'Afghanistan',
  76. 'childNum': 1
  77. }
  78. }
  79. ]
  80. };
  81. testCase.createChart(1, 200, 150)('geo', function () {
  82. this.echarts.registerMap('test1', testGeoJson1);
  83. this.echarts.registerMap('test2', testGeoJson2);
  84. var chart = this.chart;
  85. chart.setOption({
  86. geo: [
  87. {
  88. id: 'aa',
  89. left: 10,
  90. right: '50%',
  91. top: 30,
  92. bottom: 40,
  93. map: 'test1'
  94. },
  95. {
  96. id: 'bb',
  97. layoutCenter: ['50%', 50],
  98. layoutSize: 20,
  99. aspectScale: 1,
  100. map: 'test2'
  101. },
  102. {
  103. id: 'cc',
  104. aspectScale: 1,
  105. left: 0,
  106. width: '50%',
  107. top: 0,
  108. height: '50%',
  109. zoom: 0.5, // test roam
  110. map: 'test1'
  111. }
  112. ],
  113. series: [
  114. {id: 'k1', type: 'scatter', coordinateSystem: 'geo', geoIndex: 1},
  115. {id: 'k2', type: 'scatter', coordinateSystem: 'geo'}
  116. ]
  117. });
  118. var width = chart.getWidth();
  119. var height = chart.getWidth();
  120. expect(chart.containPixel('geo', [15, 30])).toEqual(true);
  121. expect(chart.containPixel('geo', [9.5, 30])).toEqual(false);
  122. expect(chart.containPixel({geoIndex: 1}, [width / 2, 50])).toEqual(true);
  123. expect(chart.containPixel({geoIndex: 1}, [10, 20])).toEqual(false);
  124. expect(chart.containPixel({geoId: 'cc'}, [0, 0])).toEqual(false);
  125. });
  126. testCase.createChart(1, 200, 150)('map', function () {
  127. this.echarts.registerMap('test1', testGeoJson1);
  128. this.echarts.registerMap('test2', testGeoJson2);
  129. var chart = this.chart;
  130. chart.setOption({
  131. series: [
  132. {
  133. id: 'k1',
  134. type: 'map',
  135. map: 'test1',
  136. left: 10,
  137. right: '50%',
  138. top: 30,
  139. bottom: 40
  140. },
  141. {
  142. id: 'k2',
  143. type: 'map',
  144. map: 'test2',
  145. layoutCenter: ['50%', 50],
  146. layoutSize: 20,
  147. aspectScale: 1
  148. }
  149. ]
  150. });
  151. var width = chart.getWidth();
  152. expect(chart.containPixel('series', [15, 30])).toEqual(true);
  153. expect(chart.containPixel('series', [9.5, 30])).toEqual(false);
  154. expect(chart.containPixel({seriesId: 'k2'}, [width / 2, 50])).toEqual(true);
  155. expect(chart.containPixel({seriesId: 1}, [10, 20])).toEqual(false);
  156. });
  157. testCase.createChart()('cartesian', function () {
  158. this.echarts.registerMap('test1', testGeoJson1);
  159. var chart = this.chart;
  160. chart.setOption({
  161. geo: [ // Should not affect grid converter.
  162. {
  163. map: 'test1'
  164. }
  165. ],
  166. grid: [
  167. {
  168. id: 'g0',
  169. left: 10,
  170. right: '50%',
  171. top: 30,
  172. bottom: 40
  173. },
  174. {
  175. id: 'g1',
  176. left: '50%',
  177. right: 20,
  178. top: 30,
  179. bottom: 40
  180. }
  181. ],
  182. xAxis: [
  183. {
  184. id: 'x0',
  185. type: 'value',
  186. min: -500,
  187. max: 3000,
  188. gridId: 'g0'
  189. },
  190. {
  191. id: 'x1',
  192. type: 'value',
  193. min: -50,
  194. max: 300,
  195. gridId: 'g0'
  196. },
  197. {
  198. id: 'x2',
  199. type: 'value',
  200. min: -50,
  201. max: 300,
  202. gridId: 'g1'
  203. }
  204. ],
  205. yAxis: [
  206. {
  207. id: 'y0',
  208. type: 'value',
  209. min: 6000,
  210. max: 9000,
  211. gridId: 'g0'
  212. },
  213. {
  214. id: 'y1',
  215. type: 'value',
  216. inverse: true, // test inverse
  217. min: 600,
  218. max: 900,
  219. gridId: 'g1'
  220. }
  221. ],
  222. series: [
  223. {
  224. id: 'k1',
  225. type: 'scatter',
  226. left: 0,
  227. right: 0,
  228. top: 0,
  229. bottom: 0,
  230. data: [[1000, 700]]
  231. },
  232. {
  233. id: 'k2',
  234. type: 'scatter',
  235. left: 0,
  236. right: 0,
  237. top: 0,
  238. bottom: 0,
  239. data: [[100, 800]]
  240. },
  241. {
  242. id: 'j1',
  243. type: 'scatter',
  244. left: 0,
  245. right: 0,
  246. top: 0,
  247. bottom: 0,
  248. data: [[100, 800]],
  249. xAxisIndex: 1
  250. },
  251. {
  252. id: 'i1',
  253. type: 'scatter',
  254. left: 0,
  255. right: 0,
  256. top: 0,
  257. bottom: 0,
  258. data: [],
  259. xAxisId: 'x2',
  260. yAxisId: 'y1'
  261. }
  262. ]
  263. });
  264. var width = chart.getWidth();
  265. expect(chart.containPixel('grid', [15, 30])).toEqual(true);
  266. expect(chart.containPixel('grid', [9.5, 30])).toEqual(false);
  267. expect(chart.containPixel({gridIndex: 1}, [width / 2, 50])).toEqual(true);
  268. expect(chart.containPixel({gridIndex: 1}, [10, 20])).toEqual(false);
  269. });
  270. testCase.createChart()('pie', function () {
  271. var chart = this.chart;
  272. chart.setOption({
  273. series: [
  274. {
  275. id: 'k1',
  276. type: 'pie',
  277. center: [40, '50%'],
  278. radius: [10, '50%'],
  279. data: [
  280. {x: 1000, y: 2000},
  281. {x: 1000, y: 5000},
  282. {x: 3000, y: 5000},
  283. {x: 3000, y: 2000}
  284. ],
  285. links: []
  286. }
  287. ]
  288. });
  289. var height = chart.getHeight();
  290. expect(chart.containPixel('series', [40, height / 2])).toEqual(false);
  291. expect(chart.containPixel('series', [40, height / 2 + 10])).toEqual(true);
  292. expect(chart.containPixel('series', [9.5, 1])).toEqual(false);
  293. });
  294. testCase.createChart()('pieAndGeo', function () {
  295. this.echarts.registerMap('test1', testGeoJson1);
  296. this.echarts.registerMap('test2', testGeoJson2);
  297. var chart = this.chart;
  298. chart.setOption({
  299. geo: [
  300. {
  301. id: 'aa',
  302. left: 10,
  303. top: 10,
  304. width: 10,
  305. height: 10,
  306. map: 'test1'
  307. },
  308. {
  309. id: 'bb',
  310. left: 100,
  311. top: 10,
  312. width: 10,
  313. height: 10,
  314. map: 'test2'
  315. }
  316. ],
  317. series: [
  318. {id: 'k1', type: 'scatter', coordinateSystem: 'geo', geoIndex: 1},
  319. {id: 'k2', type: 'scatter', coordinateSystem: 'geo'},
  320. {
  321. id: 'k3',
  322. type: 'pie',
  323. center: [40, 100],
  324. radius: [3, 10],
  325. data: [
  326. {x: 1000, y: 2000},
  327. {x: 1000, y: 5000}
  328. ],
  329. links: []
  330. }
  331. ]
  332. });
  333. expect(chart.containPixel({geoIndex: [0, 1], seriesId: 'k3'}, [15, 15])).toEqual(true);
  334. expect(chart.containPixel({geoIndex: [0, 1], seriesId: 'k3'}, [15, 25])).toEqual(false);
  335. expect(chart.containPixel({geoIndex: [0, 1], seriesId: 'k3'}, [105, 15])).toEqual(true);
  336. expect(chart.containPixel({geoIndex: [0, 1], seriesId: 'k3'}, [105, 25])).toEqual(false);
  337. expect(chart.containPixel({geoIndex: [0, 1], seriesId: 'k3'}, [45, 100])).toEqual(true);
  338. expect(chart.containPixel({geoIndex: [0, 1], seriesId: 'k3'}, [55, 100])).toEqual(false);
  339. });
  340. testCase.createChart()('graph', function () {
  341. this.echarts.registerMap('test1', testGeoJson1);
  342. var chart = this.chart;
  343. chart.setOption({
  344. geo: [ // Should not affect graph converter.
  345. {
  346. map: 'test1'
  347. }
  348. ],
  349. series: [
  350. {
  351. id: 'k1',
  352. type: 'graph',
  353. left: 10,
  354. right: '50%',
  355. top: 30,
  356. bottom: 40,
  357. data: [
  358. {x: 1000, y: 2000},
  359. {x: 1000, y: 5000},
  360. {x: 3000, y: 5000},
  361. {x: 3000, y: 2000}
  362. ],
  363. links: []
  364. }
  365. ]
  366. });
  367. expect(chart.containPixel('series', [15, 35])).toEqual(true);
  368. expect(chart.containPixel('series', [3, 4])).toEqual(false);
  369. });
  370. });