leaflet-echarts3.min.js 4.7 KB
!function(t,e){"function"==typeof define&&define.amd?define(["leaflet"],e):"object"==typeof module&&module.exports?module.exports=e(require("leaflet"),require("echarts")):"undefined"!=typeof t&&t.L&&t.echarts&&(LeafletEcharts=e(L,echarts))}(this,function(t,e){function n(t,e){this._lmap=t,this.dimensions=["lng","lat"],this._mapOffset=[0,0],this._api=e}function i(t,e,n){this._ec=t,this._dom=e,this._map=n}var a={},o="_lmap_",r=new Date-0;e.extendComponentModel({type:"lmap",getLMap:function(){return this.__lmap},defaultOption:{mapOptions:{center:[37.550339,104.114129],zoom:5}}}),e.extendComponentView({type:"lmap",render:function(t,e,n){}});return t.EchartLayer=t.Class.extend({includes:[t.Mixin.Events],_echartsContainer:null,_map:null,_api:null,_ecModel:null,_resizing:!1,initialize:function(t,e){this._api=t,this._echartsContainer=e?e:t.getDom()},getMap:function(){return this._map},getOverLayer:function(){return this.__overlayer},setModel:function(t){this._ecModel=t},onAdd:function(t){this._map=t;t.getSize();t.getPanes().overlayPane.appendChild(this._echartsContainer),t.on("moveend",this._moveend,this),t.on("resize",this._resize,this)},_resize:function(){var t=this._map._getMapPanePos();this._mapOffset=[-parseInt(t.x)||0,-parseInt(t.y)||0],this._echartsContainer.style.left=this._mapOffset[0]+"px",this._echartsContainer.style.top=this._mapOffset[1]+"px";var e=this._map.getSize();this._echartsContainer.style.height=e.y+"px",this._echartsContainer.style.width=e.x+"px",this._resizing=!0},_moveend:function(){var t=this._map._getMapPanePos();if(this._mapOffset=[-parseInt(t.x)||0,-parseInt(t.y)||0],this._echartsContainer.style.left=this._mapOffset[0]+"px",this._echartsContainer.style.top=this._mapOffset[1]+"px",1==this._resizing){this._resizing=!1;var n=e.getInstanceByDom(this._api.getDom());n.resize()}else this._api.dispatchAction({type:"mapMoveEnd"})},onRemove:function(t){t.getPanes().overlayPane.removeChild(this._root),t.off("viewreset",this._viewreset,this),t.off("resize",this._resize,this)}}),n.prototype.dimensions=["lng","lat"],n.prototype.setMapOffset=function(t){this._mapOffset=t},n.prototype.getLMap=function(){return this._lmap},n.prototype.dataToPoint=function(e){var n=new t.latLng(e[1],e[0]),i=this._lmap.latLngToContainerPoint(n),a=this._mapOffset;return[i.x-a[0],i.y-a[1]]},n.prototype.pointToData=function(t){var e=this._mapOffset,t=this._lmap.containerPointToLatLng({x:t[0]+e[0],y:t[1]+e[1]});return[t.lat,t.lng]},n.prototype.getViewRect=function(){var t=this._api;return new e.graphic.BoundingRect(0,0,t.getWidth(),t.getHeight())},n.prototype.getRoamTransform=function(){return e.matrix.create()},n.dimensions=n.prototype.dimensions,n.create=function(e,i){var s;e.eachComponent("lmap",function(e){if(s)throw new Error("Only one lmap component can exist");var p,h=i.getDom(),l=h.getAttribute(o);if(l)e.__lmap=a[l];else{if(p=i.getZr().painter.getViewportRoot(),"undefined"==typeof t)throw new Error("LMap api is not loaded");var f=h.querySelector(".ec-extension-lmap");f&&(p.style.left="0px",p.style.top="0px",h.removeChild(f)),f=document.createElement("div"),f.style.cssText="width:100%;height:100%",f.classList.add("ec-extension-lmap"),h.appendChild(f);var m=e.get("mapOptions");"function"==typeof m&&(m=m());var _=e.__lmap=new t.map(f,m),c="map_"+r++;a[c]=_,h.setAttribute&&h.setAttribute(o,c)}if(!e.__overlayer){var u=e.__overlayer=new t.EchartLayer(i,p);e.__lmap.addLayer(u),e.__overlayer.setModel(e)}s=new n(e.__lmap,i),s.setMapOffset(e.__mapOffset||[0,0]),e.coordinateSystem=s}),e.eachComponent("geo",function(e){var r=i.getDom(),p=r.getAttribute(o);if(!p)throw new Error("Must Init LeafletMap First!");var h=e.__lmap=a[p];if(s||(s=new n(h,i),s.setMapOffset(e.__mapOffset||[0,0])),!e.__overlayer){var l=e.__overlayer=new t.EchartLayer(i);h.addLayer(l),l.setModel(e)}}),e.eachSeries(function(t){var e=t.get("coordinateSystem");"geo"!==e&&"lmap"!==e||(t.coordinateSystem=s)})},e.registerCoordinateSystem("lmap",n),e.registerAction({type:"mapMoveEnd",event:"mapMoveEnd",update:"updateLayout"},function(t,e){e.eachComponent("lmap",function(t){})}),i.prototype.getMap=function(){if(this._map)return this._map;var t=this._dom.getAttribute(o);return a[t]},i.prototype.getEcharts=function(){return this._ec},i.prototype.getEchartLayer=function(){var t=this.getEcharts(),e=t.getModel().getComponent("lmap");return e||(e=t.getModel().getComponent("geo")),e.__overlayer},i.prototype.setOption=function(t,e,n){this._ec.setOption(t,e,n)},{initMap:function(n,s){var p=t.map(n,s),h="map_"+r++;a[h]=p;var l=document.createElement("div"),f=p.getSize();l.style.position="absolute",l.style.height=f.y+"px",l.style.width=f.x+"px",l.style.top=0,l.style.left=0;var m=e.init(l);return l.setAttribute&&l.setAttribute(o,h),new i(m,l,p)},initEcharts:function(t){var n=e.init(t);return new i(n,t)},version:"1.0.0"}});