echartsLayer.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. define([
  2. "dojo/_base/declare",
  3. "dojo/_base/lang",
  4. "esri/geometry/Point",
  5. "esri/geometry/SpatialReference"
  6. ], function (declare, lang, n, SpatialReference) {
  7. return declare("EchartsglLayer", null, {
  8. name: "EchartsglLayer",
  9. view: null,
  10. box: null,
  11. chart: null,
  12. chartOption: null,
  13. visible: true,
  14. constructor: function (view, option) {
  15. //如果在服务器上使用该代码 可以将echart对象传入到option中
  16. echarts.registerCoordinateSystem('arcgis', this.getE3CoordinateSystem(view));
  17. this.init(view, option);
  18. },
  19. init: function (view, option) {
  20. this.setBaseMap(view);
  21. this.createLayer();
  22. //this.setChartOption(option);
  23. },
  24. setBaseMap: function (view) {
  25. this.view = view;
  26. },
  27. setChartOption: function (option) {
  28. this.chartOption = option;
  29. this.setCharts();
  30. },
  31. setVisible: function (bool) {
  32. if (!this.box || this.visible === bool) return;
  33. this.box.hidden = !bool;
  34. this.visible = bool;
  35. bool === true && setCharts();
  36. },
  37. refreshBegin: function () {
  38. this.box.hidden = true;
  39. },
  40. refreshing: function () {
  41. setCharts();
  42. },
  43. refreshEnd: function () {
  44. this.box.hidden = false;
  45. },
  46. on: function (eventName, handler, context) {
  47. this.chart.on(eventName, handler, context);
  48. },
  49. off: function (eventName, handler, context) {
  50. this.chart.off(eventName, handler, context);
  51. },
  52. map_DragStart_Listener: null,
  53. map_DragEnd_Listener: null,
  54. map_ZoomStart_Listener: null,
  55. map_ZoomEnd_Listener: null,
  56. map_ExtentChange_Listener: null,
  57. map_click_Listener: null,
  58. setCharts: function () {
  59. if (!this.visible) return;
  60. if (this.chartOption == null || this.chartOption == 'undefined') return;
  61. let baseExtent = this.view.extent;
  62. //判断是否使用了mark类型标签,每次重绘要重新转换地理坐标到屏幕坐标
  63. //根据地图extent,重绘echarts
  64. this.chartOption.xAxis = { show: false, min: baseExtent.xmin, max: baseExtent.xmax };
  65. this.chartOption.yAxis = { show: false, min: baseExtent.ymin, max: baseExtent.ymax };
  66. this.chart.setOption(this.chartOption);
  67. this.chartOption.animation = false;
  68. },
  69. /*创建layer的容器,添加到map的layers下面*/
  70. createLayer: function () {
  71. let box = this.box = document.createElement("div");
  72. box.setAttribute("id", "echartsData")
  73. box.setAttribute("name", "echartsData")
  74. box.style.width = this.view.width + 'px';
  75. box.style.height = this.view.height + 'px';
  76. box.style.position = "absolute";
  77. box.style.top = 0;
  78. box.style.left = 0;
  79. let parent = document.getElementsByClassName("esri-view-surface")[0];
  80. parent.appendChild(box);
  81. this.chart = echarts.init(box);
  82. //this.setCharts();
  83. this.startMapEventListeners();
  84. },
  85. /*销毁实例*/
  86. removeLayer: function () {
  87. this.box.outerHTML = "";
  88. this.view = null;
  89. this.box = null;
  90. this.originLyr = null;
  91. this.features = null;
  92. this.screenData = [];
  93. this.chart = null;
  94. this.chartOption = null;
  95. if(this.map_DragStart_Listener)
  96. this.map_DragStart_Listener.remove();
  97. if(this.map_DragEnd_Listener)
  98. this.map_DragEnd_Listener.remove();
  99. if(this.map_ZoomStart_Listener)
  100. this.map_ZoomStart_Listener.remove();
  101. if(this.map_ZoomEnd_Listener)
  102. this.map_ZoomEnd_Listener.remove();
  103. if(this.map_ExtentChange_Listener)
  104. this.map_ExtentChange_Listener.remove();
  105. },
  106. /*监听地图事件,根据图层是否显示,判断是否重绘echarts*/
  107. startMapEventListeners: function () {
  108. let view = this.view;
  109. view.watch("extent", lang.hitch(this, function () {
  110. if (!this.visible) return;
  111. if(!this.chart) return;
  112. this.setCharts();
  113. this.chart.resize();
  114. this.box.hidden = false;
  115. }));
  116. view.watch("rotation", lang.hitch(this, function () {
  117. if (!this.visible) return;
  118. if(!this.chart) return;
  119. this.setCharts();
  120. this.chart.resize();
  121. this.box.hidden = false;
  122. }));
  123. },
  124. getE3CoordinateSystem: function (map) {
  125. var CoordSystem = function CoordSystem(map) {
  126. this.map = map;
  127. this._mapOffset = [0, 0];
  128. };
  129. CoordSystem.create = function (ecModel) {
  130. ecModel.eachSeries(function (seriesModel) {
  131. if (seriesModel.get('coordinateSystem') === 'arcgis') {
  132. seriesModel.coordinateSystem = new CoordSystem(map);
  133. }
  134. });
  135. };
  136. CoordSystem.getDimensionsInfo = function () {
  137. return ['x', 'y'];
  138. };
  139. CoordSystem.dimensions = ['x', 'y'];
  140. CoordSystem.prototype.dimensions = ['x', 'y'];
  141. CoordSystem.prototype.setMapOffset = function setMapOffset(mapOffset) {
  142. this._mapOffset = mapOffset;
  143. }
  144. CoordSystem.prototype.dataToPoint = function dataToPoint(data) {
  145. var point = {
  146. type: "point",
  147. x: data[0],
  148. y: data[1],
  149. spatialReference: new SpatialReference(3857)
  150. };
  151. var px = map.toScreen(point);
  152. var mapOffset = this._mapOffset;
  153. return [px.x - mapOffset[0], px.y - mapOffset[1]];
  154. }
  155. CoordSystem.prototype.pointToData = function pointToData(pt) {
  156. var mapOffset = this._mapOffset;
  157. var screentPoint = {
  158. x: pt[0] + mapOffset[0],
  159. y: pt[1] + mapOffset[1]
  160. };
  161. var data = map.toMap(screentPoint);
  162. return [data.x, data.y];
  163. };
  164. CoordSystem.prototype.getViewRect = function getViewRect() {
  165. return new graphic.BoundingRect(0, 0, this.map.width, this.map.height);
  166. };
  167. CoordSystem.prototype.getRoamTransform = function getRoamTransform() {
  168. return matrix.create();
  169. };
  170. return CoordSystem
  171. }
  172. });
  173. })