Skip to content

Commit

Permalink
[feature] Cluster geojson data
Browse files Browse the repository at this point in the history
  • Loading branch information
totallynotvaishnav committed Sep 21, 2022
1 parent aa4d7f3 commit b671597
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ NetJSON format used internally is based on [networkgraph](http://netjson.org/rfc

The `nodeSize` property is used to customize the size of the nodes.

The `clusterConfig` property is used to customize the clusters. You can pass any valid [Echarts options](https://echarts.apache.org/en/option.html#series-scatter.data) in `clusterConfig`.
The `clusterConfig` property is used to customize the clusters. You can pass any valid [Echarts options](https://echarts.apache.org/en/option.html#series-scatter.data) in `clusterConfig`. If you are using GeoJSON data, you can customize the cluster styles by using the CSS classes `marker-cluster` and `.marker-cluster-small`.

`linkConfig` deals with the configuration of the links. You can pass any valid [Echarts options](https://echarts.apache.org/en/option.html#series-lines) in `linkConfig`.

Expand Down
6 changes: 3 additions & 3 deletions dist/netjsongraph.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@types/jest": "^28.1.6",
"acorn": "^8.7.1",
"coveralls": "^3.1.1",
"css-loader": "^6.7.1",
"eslint": "^8.18.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.5.0",
Expand All @@ -54,6 +55,7 @@
"jest-environment-jsdom": "^28.1.1",
"lint-staged": "^13.0.2",
"prettier": "^2.7.1",
"style-loader": "^3.3.1",
"terser-webpack-plugin": "^5.3.3",
"webpack": "^5.73.0",
"webpack-cli": "^4.10.0",
Expand All @@ -64,6 +66,7 @@
"echarts-gl": "^2.0.8",
"kdbush": "^3.0.0",
"leaflet": "^1.8.0",
"leaflet.markercluster": "^1.5.3",
"zrender": "^5.3.2"
}
}
12 changes: 12 additions & 0 deletions src/css/netjsongraph-theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
color: #000;
}

.marker-cluster div {
color: #fff;
}

.marker-cluster-small {
background-color: unset !important;
}

.marker-cluster-small div {
background-color: rgba(21, 102, 169, 0.8) !important;
}

@media only screen and (max-width: 850px) {
.njg-sideBar {
background: rgba(255, 255, 255, 0.96);
Expand Down
23 changes: 18 additions & 5 deletions src/js/netjsongraph.render.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import "echarts/lib/component/tooltip";
import "echarts/lib/component/title";
import "echarts/lib/component/toolbox";
import "echarts/lib/component/legend";
import L from "leaflet/dist/leaflet";
import "leaflet.markercluster";
import "leaflet.markercluster/dist/MarkerCluster.css";
import "leaflet.markercluster/dist/MarkerCluster.Default.css";

import "zrender/lib/svg/svg";

import "../../lib/js/echarts-gl.min";

import L from "leaflet/dist/leaflet";

class NetJSONGraphRender {
/**
* @function
Expand Down Expand Up @@ -353,12 +355,23 @@ class NetJSONGraphRender {
);

if (self.type === "geojson") {
self.leaflet.geoJSON = L.geoJSON(self.data, self.config.geoOptions).addTo(
self.leaflet,
);
self.leaflet.geoJSON = L.geoJSON(self.data, self.config.geoOptions);

if (self.config.clustering) {
self.leaflet.markerClusterGroup = L.markerClusterGroup({
showCoverageOnHover: false,
spiderfyOnMaxZoom: false,
maxClusterRadius: self.config.clusterRadius,
disableClusteringAtZoom: self.config.disableClusteringAtLevel - 1,
}).addTo(self.leaflet);
self.leaflet.markerClusterGroup.addLayer(self.leaflet.geoJSON);
} else {
self.leaflet.geoJSON.addTo(self.leaflet);
}
}

if (
self.type === "netjson" &&
self.config.clustering &&
self.config.clusteringThreshold < JSONData.nodes.length
) {
Expand Down
8 changes: 8 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ module.exports = (env, argv) => ({
}),
],
},
module: {
rules: [
{
test: /\.css$/i,
use: ["style-loader", "css-loader"],
},
],
},
devServer: {
static: "./",
historyApiFallback: true,
Expand Down

0 comments on commit b671597

Please sign in to comment.