|
@@ -1,7 +1,7 @@
|
|
|
import * as d3 from "https://cdn.jsdelivr.net/npm/d3@7/+esm";
|
|
|
|
|
|
function getOrCreateNode(id, name, nodes, dict) {
|
|
|
- var node = dict[id];
|
|
|
+ let node = dict[id];
|
|
|
if (!node) {
|
|
|
node = {}
|
|
|
node.id = id;
|
|
@@ -13,17 +13,17 @@ function getOrCreateNode(id, name, nodes, dict) {
|
|
|
}
|
|
|
|
|
|
function toGraphData(data) {
|
|
|
- var graph = {};
|
|
|
- var nodes = graph.nodes = [];
|
|
|
- var links = graph.links = [];
|
|
|
- var idToNode = [];
|
|
|
- var columns = data.head.vars;
|
|
|
+ let graph = {};
|
|
|
+ let nodes = graph.nodes = [];
|
|
|
+ let links = graph.links = [];
|
|
|
+ let idToNode = [];
|
|
|
+ let columns = data.head.vars;
|
|
|
data.results.bindings.forEach(item => {
|
|
|
- var srcID = item[columns[0]].value;
|
|
|
- var trgtID = item[columns[1]].value;
|
|
|
- var src = getOrCreateNode(srcID, srcID, nodes, idToNode);
|
|
|
- var trgt = getOrCreateNode(trgtID, trgtID, nodes, idToNode);
|
|
|
- var link = {};
|
|
|
+ const srcID = item[columns[0]].value;
|
|
|
+ const trgtID = item[columns[1]].value;
|
|
|
+ const src = getOrCreateNode(srcID, srcID, nodes, idToNode);
|
|
|
+ const trgt = getOrCreateNode(trgtID, trgtID, nodes, idToNode);
|
|
|
+ let link = {};
|
|
|
link.source = src.id;
|
|
|
link.target = trgt.id;
|
|
|
links.push(link);
|
|
@@ -33,9 +33,9 @@ function toGraphData(data) {
|
|
|
|
|
|
|
|
|
function drawGraph(data, containerID, width = 500, height = 300) {
|
|
|
- var graph = toGraphData(data)
|
|
|
- var container = d3.select(containerID);
|
|
|
- var svg = container.append('svg').attr("width", width).attr("height", height);
|
|
|
+ let graph = toGraphData(data)
|
|
|
+ let container = d3.select(containerID);
|
|
|
+ let svg = container.append('svg').attr("width", width).attr("height", height);
|
|
|
// arrow def
|
|
|
svg.append('defs').append('marker')
|
|
|
.attr('id', 'arrowhead')
|
|
@@ -48,26 +48,27 @@ function drawGraph(data, containerID, width = 500, height = 300) {
|
|
|
.attr('xoverflow', 'visible')
|
|
|
.append('svg:path')
|
|
|
.attr('d', 'M 0,-5 L 10 ,0 L 0,5')
|
|
|
- .attr('fill', '#999')
|
|
|
+ .attr('fill', '#808080')
|
|
|
.style('stroke', 'none');
|
|
|
|
|
|
- var nodes = graph.nodes;
|
|
|
- var links = graph.links;
|
|
|
- var simulation = d3.forceSimulation()
|
|
|
+ let nodes = graph.nodes;
|
|
|
+ let links = graph.links;
|
|
|
+ let simulation = d3.forceSimulation()
|
|
|
.force("link", d3.forceLink().id(function (d) {
|
|
|
return d.id;
|
|
|
}).distance(80))
|
|
|
.force("charge", d3.forceManyBody().strength(-100))
|
|
|
.force("center", d3.forceCenter(width / 2, height / 2));
|
|
|
|
|
|
- var link = svg.selectAll(".link")
|
|
|
+ let link = svg.selectAll(".link")
|
|
|
.data(links)
|
|
|
.enter()
|
|
|
.append("line")
|
|
|
.attr("class", "link")
|
|
|
.attr('marker-end', 'url(#arrowhead)')
|
|
|
+ .style('stroke', '#808080');
|
|
|
|
|
|
- var node = svg.selectAll(".node")
|
|
|
+ let node = svg.selectAll(".node")
|
|
|
.data(nodes)
|
|
|
.enter()
|
|
|
.append("g")
|
|
@@ -79,7 +80,7 @@ function drawGraph(data, containerID, width = 500, height = 300) {
|
|
|
|
|
|
node.append("circle")
|
|
|
.attr("r", 5)
|
|
|
- .style("fill", "red")
|
|
|
+ .attr("fill", "#006496")
|
|
|
|
|
|
node.append("title")
|
|
|
.text(function (d) {
|
|
@@ -113,21 +114,21 @@ function drawGraph(data, containerID, width = 500, height = 300) {
|
|
|
|
|
|
node.attr("transform",
|
|
|
function (d) {
|
|
|
- var dx = getX(d.x);
|
|
|
- var dy = getY(d.y);
|
|
|
+ const dx = getX(d.x);
|
|
|
+ const dy = getY(d.y);
|
|
|
return "translate(" + dx + ", " + dy + ")";
|
|
|
}
|
|
|
);
|
|
|
|
|
|
function getX(x) {
|
|
|
- var dx = x;
|
|
|
+ let dx = x;
|
|
|
if (dx < 20) dx = 20;
|
|
|
if (dx > width - 20) dx = width - 20;
|
|
|
return dx;
|
|
|
}
|
|
|
|
|
|
function getY(y) {
|
|
|
- var dy = y;
|
|
|
+ let dy = y;
|
|
|
if (dy < 20) dy = 20;
|
|
|
if (dy > height - 20) dy = height - 20;
|
|
|
return dy;
|
|
@@ -154,5 +155,5 @@ function drawGraph(data, containerID, width = 500, height = 300) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export { drawGraph, toGraphData };
|
|
|
+export {drawGraph, toGraphData};
|
|
|
export default drawGraph;
|