Skip to content

Commit

Permalink
[feature] Add support for GeoJSON #116
Browse files Browse the repository at this point in the history
closes #116
  • Loading branch information
totallynotvaishnav committed Aug 10, 2022
1 parent 1606d69 commit 2f62118
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/js/netjsongraph.config.js
Expand Up @@ -139,6 +139,16 @@ const NetJSONGraphDefaultConfig = {
},
},
],
geoOptions: {
style: {
fillColor: "#d94f34",
color: "#d94f34",
weight: 2,
opacity: 1,
fillOpacity: 0.3,
radius: 10,
},
},
nodeCategories: [],
linkCategories: [],

Expand Down
10 changes: 8 additions & 2 deletions src/js/netjsongraph.core.js
Expand Up @@ -8,11 +8,13 @@ class NetJSONGraph {
* @param {string} JSONParam The NetJSON file param
* @param {Object} config
*/
constructor(JSONParam) {
constructor(JSONParam, GeoJSONParam) {
this.utils = new NetJSONGraphUpdate();

this.config = {...NetJSONGraphDefaultConfig};

this.GeoJSONParam = GeoJSONParam;

this.JSONParam = this.utils.isArray(JSONParam) ? JSONParam : [JSONParam];
}

Expand Down Expand Up @@ -62,7 +64,11 @@ class NetJSONGraph {
this.event.once("onLoad", this.config.onLoad.bind(this));

this.utils
.JSONParamParse(JSONParam)
.JSONParamParse(this.GeoJSONParam)
.then((data) => {
this.geoData = data;
return this.utils.JSONParamParse(JSONParam);
})
.then((JSONData) => {
this.config.prepareData.call(this, JSONData);
this.data = JSONData;
Expand Down
5 changes: 3 additions & 2 deletions src/js/netjsongraph.js
Expand Up @@ -14,15 +14,16 @@ class NetJSONGraph {
* @param {string} JSONParam The NetJSON file param
* @param {Object} config
*/
constructor(JSONParam, config) {

constructor(JSONParam, config, GeoJSONParam = {}) {
if (config && config.render === "map") {
config.render = NetJSONGraphRender.prototype.mapRender;
} else if (!config || !config.render || config.render === "graph") {
config = config || {};
config.render = NetJSONGraphRender.prototype.graphRender;
}

const graph = new NetJSONGraphCore(JSONParam);
let graph = new NetJSONGraphCore(JSONParam, GeoJSONParam);

Object.setPrototypeOf(NetJSONGraphRender.prototype, graph.utils);
graph.gui = new NetJSONGraphGUI(graph);
Expand Down
8 changes: 8 additions & 0 deletions src/js/netjsongraph.render.js
Expand Up @@ -322,6 +322,14 @@ class NetJSONGraphRender {
// eslint-disable-next-line no-underscore-dangle
self.leaflet._zoomAnimated = false;

if (Object.keys(self.geoData).length) {
L.geoJSON(self.geoData, {
...self.config.geoOptions,
pointToLayer: (feature, latlng) =>
L.circleMarker(latlng, self.config.geoOptions.style),
}).addTo(self.leaflet);
}

self.event.emit("onLoad");
self.event.emit("onReady");
self.event.emit("renderArray");
Expand Down

0 comments on commit 2f62118

Please sign in to comment.