forked from happyport/Cesium-Echarts4
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEchartsLayer.js
165 lines (144 loc) · 5.29 KB
/
EchartsLayer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import echarts from 'echarts'
import Cesium from 'cesium/Cesium'
var GLMapCoordSys = function (GLMap, api) {
this._GLMap = GLMap,
this.dimensions = ['lng', 'lat'],
this._mapOffset = [0, 0],
this._api = api;
}
GLMapCoordSys.prototype.dimensions = ['lng', 'lat']
GLMapCoordSys.prototype.setMapOffset = function (mapOffset) {
this._mapOffset = mapOffset
}
GLMapCoordSys.prototype.getBMap = function () {
return this._GLMap
}
GLMapCoordSys.prototype.dataToPoint = function (data) {
var e = [99999, 99999],
i = Cesium.Cartesian3.fromDegrees(data[0], data[1]);
if (!i) return e;
var n = this._GLMap.cartesianToCanvasCoordinates(i);
if (!n) return e;
return !(Cesium.Cartesian3.angleBetween(this._GLMap.camera.position, i) > Cesium.Math.toRadians(75)) && [n.x - this._mapOffset[0], n.y - this._mapOffset[1]]
}
GLMapCoordSys.prototype.pointToData = function (pt) {
var mapOffset = this._mapOffset
pt = this._bmap.project(
[pt[0] + mapOffset[0],
pt[1] + mapOffset[1]]
)
return [pt.lng, pt.lat]
}
GLMapCoordSys.prototype.getViewRect = function () {
var api = this._api
return new echarts.graphic.BoundingRect(0, 0, api.getWidth(), api.getHeight())
}
GLMapCoordSys.prototype.getRoamTransform = function () {
return echarts.matrix.create()
}
GLMapCoordSys.dimensions = GLMapCoordSys.prototype.dimensions
GLMapCoordSys.create = function (ecModel, api) {
var coordSys;
ecModel.eachComponent('GLMap', function (GLMapModel) {
var viewportRoot = api.getZr().painter.getViewportRoot()
var GLMap = echarts.glMap;
coordSys = new GLMapCoordSys(GLMap, api)
coordSys.setMapOffset(GLMapModel.__mapOffset || [0, 0])
GLMapModel.coordinateSystem = coordSys
})
ecModel.eachSeries(function (seriesModel) {
if (seriesModel.get('coordinateSystem') === 'GLMap') {
seriesModel.coordinateSystem = coordSys
}
})
}
var EchartsLayer = function (map, options) {
this._map = map;
this._overlay = this._createChartOverlay();
if(options){
this._registerMap();
}
this._overlay.setOption(options||{});
}
EchartsLayer.prototype._registerMap=function(){
if(!this._isRegistered){
echarts.registerCoordinateSystem("GLMap", GLMapCoordSys),
echarts.registerAction({
type: "GLMapRoam", event: "GLMapRoam", update: "updateLayout"
}, function (t, e) { }),
echarts.extendComponentModel({
type: "GLMap", getBMap: function () {
return this.__GLMap
},
defaultOption: { roam: !1 }
}),
echarts.extendComponentView({
type: "GLMap",
init: function (t, e) {
this.api = e, echarts.glMap.postRender.addEventListener(this.moveHandler, this)
},
moveHandler: function (t, e) {
this.api.dispatchAction({ type: "GLMapRoam" })
},
render: function (t, e, i) { },
dispose: function (t) {
echarts.glMap.postRender.removeEventListener(this.moveHandler, this)
}
});
this._isRegistered=true;
}
}
EchartsLayer.prototype._createChartOverlay = function () {
var scene = this._map.scene;
scene.canvas.setAttribute("tabIndex", 0);
const ele = document.createElement('div');
return ele.style.position = "absolute",
ele.style.top = "0px",
ele.style.left = "0px",
ele.style.width = scene.canvas.width + "px",
ele.style.height = scene.canvas.height + "px",
ele.style.pointerEvents = "none",
ele.setAttribute("id", "echarts"),
ele.setAttribute("class", "echartMap"),
this._map.container.appendChild(ele),
this._echartsContainer = ele,
echarts.glMap = scene,
this._chart=echarts.init(ele)
this.resize()
}
EchartsLayer.prototype.dispose = function () {
this._echartsContainer && (this._map.container.removeChild(this._echartsContainer),
this._echartsContainer = null),
this._overlay && (this._overlay.dispose(), this._overlay = null);
}
EchartsLayer.prototype.updateOverlay = function (t) {
this._overlay && this._overlay.setOption(t);
}
EchartsLayer.prototype.getMap = function () {
return this._map;
}
EchartsLayer.prototype.getOverlay = function () {
return this._overlay;
}
EchartsLayer.prototype.show = function () {
this._echartsContainer && (this._echartsContainer.style.visibility = "visible");
}
EchartsLayer.prototype.hide = function () {
this._echartsContainer && (this._echartsContainer.style.visibility = "hidden");
}
EchartsLayer.prototype.remove=function(){
this._chart.clear();
if (this._echartsContainer.parentNode)
this._echartsContainer.parentNode.removeChild(this._echartsContainer);
this._map = undefined;
}
EchartsLayer.prototype.resize=function(){
const me=this;
window.onresize=function(){
const scene = me._map.scene;
me._echartsContainer.style.width = scene.canvas.style.width;
me._echartsContainer.style.height = scene.canvas.style.height;
me._chart.resize();
}
}
export default EchartsLayer