converter.js 13 KB

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