Skip to content

Commit b66082b

Browse files
committed
[fix] Reduce size due to leaflet
Signed-off-by: Sankalp <[email protected]>
1 parent 9598d7c commit b66082b

12 files changed

+60
-103
lines changed

.prettierrc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
"trailingComma": "all",
33
"bracketSpacing": false,
44
"tabWidth": 2,
5-
"printWidth": 88,
6-
"insertFinalNewLine": true
7-
}
5+
"printWidth": 88
6+
}

lib/js/echarts-leaflet/LeafletCoordSys.js renamed to src/js/echarts-leaflet/LeafletCoordSys.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
import { util, graphic, matrix } from "echarts/core";
1+
import {util, graphic, matrix} from "echarts/core";
2+
import {
3+
Layer,
4+
DomUtil,
5+
Projection,
6+
LatLng,
7+
map as lMap,
8+
control,
9+
tileLayer as lTileLayer,
10+
} from "leaflet";
211

312
/* eslint-disable no-underscore-dangle */
413
// Underscore dangling allowed to identify internal methods and variable
514
/**
615
* generate leaflet coord system
7-
* @param {object} L leaflet
816
*
917
* @return {function} LeafletCoordSys
1018
*/
11-
function createLeafletCoordSystem(L) {
12-
const CustomOverlay = L.Layer.extend({
19+
function createLeafletCoordSystem() {
20+
const CustomOverlay = Layer.extend({
1321
initialize(container) {
1422
this._container = container;
1523
},
@@ -31,7 +39,7 @@ function createLeafletCoordSystem(L) {
3139
},
3240

3341
onRemove() {
34-
L.DomUtil.remove(this._container);
42+
DomUtil.remove(this._container);
3543
// map.off('zoomend viewreset', this._update, this);
3644
},
3745

@@ -52,7 +60,7 @@ function createLeafletCoordSystem(L) {
5260
this.dimensions = ["lng", "lat"];
5361
this._mapOffset = [0, 0];
5462
this._api = api;
55-
this._projection = L.Projection.Mercator;
63+
this._projection = Projection.Mercator;
5664
}
5765

5866
function doConvert(methodName, ecModel, finder, value) {
@@ -62,10 +70,9 @@ function createLeafletCoordSystem(L) {
6270
const coordSys = leafletModel
6371
? leafletModel.coordinateSystem
6472
: seriesModel
65-
? seriesModel.coordinateSystem || // For map.
66-
(seriesModel.getReferringComponents("leaflet")[0] || {})
67-
.coordinateSystem
68-
: null;
73+
? seriesModel.coordinateSystem || // For map.
74+
(seriesModel.getReferringComponents("leaflet")[0] || {}).coordinateSystem
75+
: null;
6976
return coordSys === this ? coordSys[methodName](value) : null;
7077
/* eslint-enable */
7178
}
@@ -75,38 +82,46 @@ function createLeafletCoordSystem(L) {
7582
LeafletCoordSys.dimensions = ["lng", "lat"];
7683
LeafletCoordSys.prototype.dimensions = ["lng", "lat"];
7784

85+
// eslint-disable-next-line func-names
7886
LeafletCoordSys.prototype.setZoom = function (zoom) {
7987
this._zoom = zoom;
8088
};
8189

90+
// eslint-disable-next-line func-names
8291
LeafletCoordSys.prototype.setCenter = function (center) {
83-
this._center = this._projection.project(new L.LatLng(center[1], center[0]));
92+
this._center = this._projection.project(new LatLng(center[1], center[0]));
8493
};
8594

95+
// eslint-disable-next-line func-names
8696
LeafletCoordSys.prototype.setMapOffset = function (mapOffset) {
8797
this._mapOffset = mapOffset;
8898
};
8999

100+
// eslint-disable-next-line func-names
90101
LeafletCoordSys.prototype.getLeaflet = function () {
91102
return this._map;
92103
};
93104

105+
// eslint-disable-next-line func-names
94106
LeafletCoordSys.prototype.getViewRect = function () {
95107
const api = this._api;
96108
return new graphic.BoundingRect(0, 0, api.getWidth(), api.getHeight());
97109
};
98110

111+
// eslint-disable-next-line func-names
99112
LeafletCoordSys.prototype.getRoamTransform = function () {
100113
return matrix.create();
101114
};
102115

116+
// eslint-disable-next-line func-names
103117
LeafletCoordSys.prototype.dataToPoint = function (data) {
104-
const point = new L.LatLng(data[1], data[0]);
118+
const point = new LatLng(data[1], data[0]);
105119
const px = this._map.latLngToLayerPoint(point);
106120
const mapOffset = this._mapOffset;
107121
return [px.x - mapOffset[0], px.y - mapOffset[1]];
108122
};
109123

124+
// eslint-disable-next-line func-names
110125
LeafletCoordSys.prototype.pointToData = function (pt) {
111126
const mapOffset = this._mapOffset;
112127
const coord = this._map.layerPointToLatLng({
@@ -116,16 +131,11 @@ function createLeafletCoordSystem(L) {
116131
return [coord.lng, coord.lat];
117132
};
118133

119-
LeafletCoordSys.prototype.convertToPixel = util.curry(
120-
doConvert,
121-
"dataToPoint",
122-
);
134+
LeafletCoordSys.prototype.convertToPixel = util.curry(doConvert, "dataToPoint");
123135

124-
LeafletCoordSys.prototype.convertFromPixel = util.curry(
125-
doConvert,
126-
"pointToData",
127-
);
136+
LeafletCoordSys.prototype.convertFromPixel = util.curry(doConvert, "pointToData");
128137

138+
// eslint-disable-next-line func-names
129139
LeafletCoordSys.create = function (ecModel, api) {
130140
let leafletCoordSys;
131141
const leafletList = [];
@@ -156,13 +166,13 @@ function createLeafletCoordSystem(L) {
156166
mapRoot.classList.add("ec-extension-leaflet");
157167
root.appendChild(mapRoot);
158168

159-
leafletModel.__map = L.map(mapRoot, leafletModel.get("mapOptions"));
169+
leafletModel.__map = lMap(mapRoot, leafletModel.get("mapOptions"));
160170
const map = leafletModel.__map;
161171
const tiles = leafletModel.get("tiles");
162172
const baseLayers = {};
163173
let baseLayerAdded = false;
164174
tiles.forEach((tile) => {
165-
const tileLayer = L.tileLayer(tile.urlTemplate, tile.options);
175+
const tileLayer = lTileLayer(tile.urlTemplate, tile.options);
166176
if (tile.label) {
167177
// only add one baseLayer
168178
if (!baseLayerAdded) {
@@ -178,7 +188,7 @@ function createLeafletCoordSystem(L) {
178188
// add layer control when there are more than two layers
179189
if (tiles.length > 1) {
180190
const layerControlOpts = leafletModel.get("layerControl");
181-
L.control.layers(baseLayers, {}, layerControlOpts).addTo(map);
191+
control.layers(baseLayers, {}, layerControlOpts).addTo(map);
182192
}
183193

184194
/*
File renamed without changes.

lib/js/echarts-leaflet/LeafletView.js renamed to src/js/echarts-leaflet/LeafletView.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import {extendComponentView, getInstanceByDom} from "echarts/core";
22
/* eslint-disable no-underscore-dangle */
33
/**
44
* extend echarts view
5-
* @param {object} L
65
*/
7-
export default function extendLeafletView(L) {
6+
export default function extendLeafletView() {
87
extendComponentView({
98
type: "leaflet",
109

@@ -36,6 +35,7 @@ export default function extendLeafletView(L) {
3635
/**
3736
* handler for map move event.
3837
*/
38+
// eslint-disable-next-line no-unused-vars
3939
function moveHandler(e) {
4040
if (rendering) {
4141
return;
@@ -47,14 +47,14 @@ export default function extendLeafletView(L) {
4747
let dy = 0;
4848
if (transformStyle) {
4949
transformStyle = transformStyle.replace("translate3d(", "");
50-
let parts = transformStyle.split(",");
50+
const parts = transformStyle.split(",");
5151
dx = -parseInt(parts[0], 10);
5252
dy = -parseInt(parts[1], 10);
5353
} else {
5454
dx = -parseInt(offsetEl.style.left, 10);
5555
dy = -parseInt(offsetEl.style.top, 10);
5656
}
57-
let mapOffset = [dx, dy];
57+
const mapOffset = [dx, dy];
5858
moveContainer.style.left = `${mapOffset[0]}px`;
5959
moveContainer.style.top = `${mapOffset[1]}px`;
6060

lib/js/echarts-leaflet/index.js renamed to src/js/echarts-leaflet/index.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1+
import {registerCoordinateSystem, registerAction} from "echarts/core";
12
import createLeafletCoordSystem from "./LeafletCoordSys";
23
import extendLeafletModel from "./LeafletModel";
34
import extendLeafletView from "./LeafletView";
4-
import {registerCoordinateSystem, registerAction} from "echarts/core";
55

66
/**
77
* echarts register leaflet coord system
8-
* @param {object} L
9-
* @param {object} API {
10-
* colorTool: "zrender/lib/tool/color",
11-
* { each }: "zrender/lib/core/util",
12-
* env: "zrender/lib/core/env",
13-
* }
148
*/
15-
function registerLeafletSystem(L, API) {
9+
export function registerLeafletSystem() {
1610
extendLeafletModel();
17-
extendLeafletView(L);
11+
extendLeafletView();
1812

19-
registerCoordinateSystem(
20-
"leaflet",
21-
createLeafletCoordSystem(L),
22-
);
13+
registerCoordinateSystem("leaflet", createLeafletCoordSystem());
2314

2415
registerAction(
2516
{
@@ -31,10 +22,7 @@ function registerLeafletSystem(L, API) {
3122
ecModel.eachComponent("leaflet", (leafletModel) => {
3223
const leaflet = leafletModel.getLeaflet();
3324
const center = leaflet.getCenter();
34-
leafletModel.setCenterAndZoom(
35-
[center.lng, center.lat],
36-
leaflet.getZoom(),
37-
);
25+
leafletModel.setCenterAndZoom([center.lng, center.lat], leaflet.getZoom());
3826
});
3927
},
4028
);

src/js/leaflet-loader.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/js/netjsongraph.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import L from "./leaflet-loader";
1+
import {CRS} from "leaflet";
22

33
/**
44
* Default options
@@ -43,7 +43,7 @@ const NetJSONGraphDefaultConfig = {
4343
clusterSeparation: 20,
4444
showMetaOnNarrowScreens: false,
4545
showLabelsAtZoomLevel: 13,
46-
crs: L ? L.CRS.EPSG3857 : null,
46+
crs: CRS.EPSG3857,
4747
echartsOption: {
4848
aria: {
4949
show: true,

src/js/netjsongraph.geojson.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import {geoJSON} from "leaflet";
12
/*
23
* Dedicated GeoJSON utilities for netjsongraph.js.
34
*/
45

5-
import L from "./leaflet-loader";
6-
76
/**
87
* Convert a GeoJSON FeatureCollection into a NetJSON-style object
98
* (nodes / links arrays) so that the rest of the rendering pipeline can work
@@ -155,7 +154,7 @@ export function addPolygonOverlays(self) {
155154
fillOpacity: 0.6,
156155
};
157156

158-
const polygonLayer = L.geoJSON(
157+
const polygonLayer = geoJSON(
159158
{type: "FeatureCollection", features: polygonFeatures},
160159
{
161160
pane: "njg-polygons",

src/js/netjsongraph.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
/* eslint-disable no-undef */
22
import {init} from "echarts/core";
33
import NetJSONGraphCore from "./netjsongraph.core";
4-
import {NetJSONGraphRender} from "./netjsongraph.render";
4+
import NetJSONGraphRender from "./netjsongraph.render";
55
import NetJSONGraphGUI from "./netjsongraph.gui";
66
import attachClientsOverlay from "./netjsongraph.clients";
7-
import L from "./leaflet-loader";
8-
9-
const colorTool = require("zrender/lib/tool/color");
10-
const {each} = require("zrender/lib/core/util");
11-
const env = require("zrender/lib/core/env");
7+
import {registerLeafletSystem} from "./echarts-leaflet";
128

139
/**
1410
* @class
@@ -41,7 +37,7 @@ class NetJSONGraph {
4137
return {
4238
...config,
4339
render:
44-
config.render === "map" && L
40+
config.render === "map"
4541
? NetJSONGraphRender.prototype.mapRender
4642
: NetJSONGraphRender.prototype.graphRender,
4743
onInit: this.onInit,
@@ -124,7 +120,7 @@ class NetJSONGraph {
124120
} else {
125121
this.gui.nodeLinkInfoContainer = this.gui.createNodeLinkInfoContainer();
126122
}
127-
if (this.config.switchMode && this.utils.isNetJSON(this.data) && L) {
123+
if (this.config.switchMode && this.utils.isNetJSON(this.data)) {
128124
this.gui.renderModeSelector.onclick = () => {
129125
// Switch from map to graph mode, first clear canvasContainer and then render
130126
if (this.config.render === this.utils.mapRender) {
@@ -162,16 +158,6 @@ class NetJSONGraph {
162158
}
163159
}
164160

165-
if (L) {
166-
// eslint-disable-next-line import/no-dynamic-require,global-require
167-
const leafletModule = require("../../lib/js/echarts-leaflet/index");
168-
leafletModule.default(L, {
169-
colorTool,
170-
each,
171-
env,
172-
});
173-
window.L = L;
174-
}
161+
registerLeafletSystem();
175162

176163
window.NetJSONGraph = NetJSONGraph;
177-
window.L = L;

src/js/netjsongraph.render.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {use} from "echarts/core";
2+
import {circleMarker, latLngBounds} from "leaflet";
23
import {install as LinesChart} from "echarts/lib/chart/lines/install";
34
import {install as GraphChart} from "echarts/lib/chart/graph/install";
45
import {install as ScatterChart} from "echarts/lib/chart/scatter/install";
@@ -10,7 +11,6 @@ import {install as GraphicComponent} from "echarts/lib/component/graphic/install
1011
import {install as SVGRenderer} from "echarts/lib/renderer/installSVGRenderer";
1112
import {install as CanvasRenderer} from "echarts/lib/renderer/installCanvasRenderer";
1213
import {addPolygonOverlays} from "./netjsongraph.geojson";
13-
import L from "./leaflet-loader";
1414

1515
use([
1616
GraphChart,
@@ -412,7 +412,7 @@ class NetJSONGraphRender {
412412
self.config.geoOptions = self.utils.deepMergeObj(
413413
{
414414
pointToLayer: (feature, latlng) =>
415-
L.circleMarker(latlng, self.config.geoOptions.style),
415+
circleMarker(latlng, self.config.geoOptions.style),
416416
onEachFeature: (feature, layer) => {
417417
layer.on("click", () => {
418418
const properties = {
@@ -451,7 +451,7 @@ class NetJSONGraphRender {
451451
if (bounds) {
452452
latlngs.forEach((ll) => bounds.extend(ll));
453453
} else {
454-
bounds = L.latLngBounds(latlngs);
454+
bounds = latLngBounds(latlngs);
455455
}
456456
}
457457

@@ -729,4 +729,4 @@ class NetJSONGraphRender {
729729
}
730730
}
731731

732-
export {NetJSONGraphRender, L};
732+
export default NetJSONGraphRender;

0 commit comments

Comments
 (0)