Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/js/netjsongraph.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,39 @@ const NetJSONGraphDefaultConfig = {
legendHoverLink: true,
emphasis: {
focus: "none",
scale: 2,
lineStyle: {
color: "#3acc38",
opacity: 1,
},
itemStyle: {
//color: "#3acc38",
color: {
type: "radial",
x: 0.5,
y: 0.5,
r: 0.5,
colorStops: [
{
offset: 0,
color: "#ffebc4",
},
{
offset: 0.5,
color: "#ffebc4",
},
{
offset: 0.51,
color: "#ffffff33",
},
{
offset: 1,
color: "#ffffff33",
},
],
opacity: 1,
},
},
},
nodeStyle: {
color: "#ffebc4",
Expand Down Expand Up @@ -184,6 +213,37 @@ const NetJSONGraphDefaultConfig = {
nodeStyle: {
color: "#1566a9",
},
emphasis: {
focus: "none",
scale: 2,
itemStyle: {
color: {
type: "radial",
x: 0.5,
y: 0.5,
r: 0.5,
colorStops: [
{
offset: 0,
color: "#1566a9",
},
{
offset: 0.5,
color: "#1566a9",
},
{
offset: 0.51,
color: "green",
},
{
offset: 1,
color: "green",
},
],
opacity: 1,
},
},
},
nodeSize: "17",
},
linkConfig: {
Expand Down
110 changes: 110 additions & 0 deletions src/js/netjsongraph.core.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,115 @@
import NetJSONGraphDefaultConfig from "./netjsongraph.config";
import NetJSONGraphUpdate from "./netjsongraph.update";

class Selection {
constructor() {
this.selected = new Set();
}

getSetId(item) {
if (item.node) {
// map node
return item.node.id;
} else if (item.link) {
// map link
return (item.link.source + "=>" + item.link.target);
} else if (item.id) {
// graph node
return item.id;
} else {
// graph link
return (item.source + "=>" + item.target);
}
}

isSelected(item) {
let id = this.getSetId(item);
return this.selected.has(id);
}

toggleSelection(item) {
let id = this.getSetId(item);

if (this.selected.has(id)) {
this.selected.delete(id)
return false;
} else {
this.selected.add(id);
return true;
}
}

changeSelection(echarts, params) {
const multiSelectKey = (window.event.ctrlKey || window.event.metaKey);
const isSelectionEmpty = (this.selected.size === 0);

if (multiSelectKey) {
if (this.toggleSelection(params.data)) {
echarts.dispatchAction(
{ type: 'highlight', seriesIndex: params.seriesIndex, dataType: params.dataType, dataIndex: params.dataIndex}
)
} else {
echarts.dispatchAction(
{ type: 'downplay', seriesIndex: params.seriesIndex, dataType: params.dataType, dataIndex: params.dataIndex}
)
}
} else if (!isSelectionEmpty) {
const option = echarts.getOption();
let nodeData = [];
let linkData = [];
if (option.leaflet) {
// map data
nodeData = option.leaflet[0].mapOptions.nodeConfig.data;
linkData = option.leaflet[0].mapOptions.linkConfig.data;
} else {
// graph data
nodeData = option.series[0].nodes;
linkData = option.series[0].links;
}
const nodeIndexes = nodeData.map((node, index) => index);
const linkIndexes = linkData.map((link, index) => index);

// downplay all items
echarts.dispatchAction(
{ type: 'downplay', seriesIndex: 0, batch: [
{dataType: "node", dataIndex: nodeIndexes},
{dataType: "edge", dataIndex: linkIndexes},
]
}
)

this.selected.clear();
}
}

// called when switching between graph/map view
highlightSelected(echarts) {
const option = echarts.getOption();
let nodeData = [];
let linkData = [];
if (option.leaflet) {
// map data
nodeData = option.leaflet[0].mapOptions.nodeConfig.data;
linkData = option.leaflet[0].mapOptions.linkConfig.data;
} else {
// graph data
nodeData = option.series[0].nodes;
linkData = option.series[0].links;
}

const nodeIndexes = nodeData.map((node, index) => this.isSelected(node) ? index : -1);
const linkIndexes = linkData.map((link, index) => this.isSelected(link) ? index : -1);

echarts.dispatchAction(
{ type: 'highlight', seriesIndex: 0, batch: [
{dataType: "node", dataIndex: nodeIndexes},
{dataType: "edge", dataIndex: linkIndexes},
]
}
)
}
}

class NetJSONGraph {
/**
* @constructor
Expand All @@ -10,6 +119,7 @@ class NetJSONGraph {
*/
constructor(JSONParam) {
this.utils = new NetJSONGraphUpdate();
this.selection = new Selection();
this.config = {...NetJSONGraphDefaultConfig};
this.JSONParam = this.utils.isArray(JSONParam) ? JSONParam : [JSONParam];
}
Expand Down
2 changes: 2 additions & 0 deletions src/js/netjsongraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ class NetJSONGraph {
document.querySelector(".leaflet-control-zoom").style.display =
"block";
}

this.selection.highlightSelected(this.echarts);
};
}
this.utils.hideLoading.call(this);
Expand Down
43 changes: 24 additions & 19 deletions src/js/netjsongraph.render.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ echarts.use([
]);

class NetJSONGraphRender {
echartsSetEventHandler(self) {
self.echarts.on(
"click",
(params) => {
self.selection.changeSelection(self.echarts, params);

const clickElement = self.config.onClickElement.bind(self);
if (params.componentSubType === "graph") {
return clickElement(
params.dataType === "edge" ? "link" : "node",
params.data,
);
}
if (params.componentSubType === "graphGL") {
return clickElement("node", params.data);
}
return params.componentSubType === "lines"
? clickElement("link", params.data.link)
: !params.data.cluster && clickElement("node", params.data.node);
},
{passive: true},
);
}

/**
* @function
* @name echartsSetOption
Expand Down Expand Up @@ -92,25 +116,6 @@ class NetJSONGraphRender {
);

echartsLayer.setOption(self.utils.deepMergeObj(commonOption, customOption));
echartsLayer.on(
"click",
(params) => {
const clickElement = configs.onClickElement.bind(self);
if (params.componentSubType === "graph") {
return clickElement(
params.dataType === "edge" ? "link" : "node",
params.data,
);
}
if (params.componentSubType === "graphGL") {
return clickElement("node", params.data);
}
return params.componentSubType === "lines"
? clickElement("link", params.data.link)
: !params.data.cluster && clickElement("node", params.data.node);
},
{passive: true},
);

return echartsLayer;
}
Expand Down
Loading