UNPKG

@naivemap/mapbox-gl-echarts-layer

Version:

A Mapbox GL JS layer to integrate Apache ECharts' Lines and Scatter charts.

2 lines (1 loc) 2.96 kB
(function(n,s){typeof exports=="object"&&typeof module<"u"?module.exports=s(require("echarts")):typeof define=="function"&&define.amd?define(["echarts"],s):(n=typeof globalThis<"u"?globalThis:n||self,n.EChartsLayer=s(n.echarts))})(this,function(n){"use strict";const s="gl-layer-echarts";class a{id;dimensions=["x","y"];_map;_mapOffset=[0,0];constructor(e,t){this.id=e,this._map=t}create(e){e.eachSeries(t=>{t.get("coordinateSystem")===this.id&&(t.coordinateSystem=new a(this.id,this._map))})}dataToPoint(e){const t=this._map.project(e),i=this._mapOffset;return[t.x-i[0],t.y-i[1]]}pointToData(e){const t=this._mapOffset,i=this._map.unproject([e[0]+t[0],e[1]+t[1]]);return[i.lng,i.lat]}}class h{_container;_map;_ec;_coordSystemName;_ecOption;constructor(e){this._coordSystemName=s+"-"+Math.random().toString(16).substring(2),this._ecOption=e}onAdd(e){if(this._map=e,this._createLayerContainer(),!n.getCoordinateSystemDimensions(this._coordSystemName)){const t=new a(this._coordSystemName,this._map);n.registerCoordinateSystem(this._coordSystemName,t)}}onRemove(){this._ec?.dispose(),this._removeLayerContainer()}setOption(e,t){this._ecOption=e,this._ec?.setOption(e,t)}render(){if(this._container||this._createLayerContainer(),!this._ec){this._ec=n.init(this._container),this._prepareECharts(),this._ec.setOption(this._ecOption);return}if(this._map.isMoving()){this._ec.clear();return}this._ec.resize({width:this._map.getCanvas().width,height:this._map.getCanvas().height}),this._prepareECharts(),this._ec.setOption(this._ecOption)}_prepareECharts(){const e=this._ecOption.series;if(e)for(let t=e.length-1;t>=0;t--)e[t].coordinateSystem=this._coordSystemName}_createLayerContainer(){const e=this._map.getCanvasContainer();this._container=document.createElement("div"),this._container.style.width=this._map.getCanvas().style.width,this._container.style.height=this._map.getCanvas().style.height,e.appendChild(this._container)}_removeLayerContainer(){this._container&&this._container.parentNode?.removeChild(this._container)}}function d(r){if(Array.isArray(r))return r;const e=r?.defaultProjectionData?.mainMatrix;if(e)return e;throw new Error("Unable to resolve projection matrix from render arguments.")}function c(r,e){let t;return{id:e.id,type:"custom",renderingMode:e.renderingMode??"2d",onAdd(i,o){t=i,r.onAdd(i,o)},onRemove(i,o){r.onRemove(i,o),t=void 0},render(i,o){if(!t)throw new Error("Layer adaptor render invoked before onAdd.");r.render({map:t,gl:i,matrix:(e.resolveMatrix??d)(o),rawArgs:o,prepareStencilMask:e.prepareStencilMask?()=>e.prepareStencilMask(t):void 0})}}}const p=c;class _{id;type;renderingMode;_core;_adaptor;constructor(e,t){this.id=e,this.type="custom",this.renderingMode="2d",this._core=new h(t),this._adaptor=p(this._core,{id:e,renderingMode:this.renderingMode})}onAdd(e,t){this._adaptor.onAdd(e,t)}onRemove(e,t){this._adaptor.onRemove(e,t)}setOption(e,t){this._core.setOption(e,t)}render(e,t){this._adaptor.render(e,t)}}return _});