-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
479 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Modified from the example at of D3 | ||
http://mbostock.github.com/d3/ex/force.html | ||
|
||
Run the file force.py to generate the force.json data file needed for this to work. | ||
|
||
Then copy all of the files in this directory to a webserver and load force.html. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.nodes circle { | ||
cursor: pointer; | ||
fill: #ff3399; | ||
stroke: #000; | ||
stroke-width: .5px; | ||
} | ||
|
||
.links line { | ||
fill: none; | ||
stroke: #9ecae1; | ||
stroke-width: .5px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Force-Directed Layout</title> | ||
<script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script> | ||
<link type="text/css" rel="stylesheet" href="graph/graph.css"/> | ||
</head> | ||
<body> | ||
<svg width="1000" height="2000"></svg> | ||
<script type="text/javascript" src="graph/graph.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// This is adapted from https://bl.ocks.org/mbostock/2675ff61ea5e063ede2b5d63c08020c7 | ||
|
||
const getDescription = (d) => Object.keys(d) | ||
.map((k)=>k+": "+d[k]+"<br>") | ||
.join(""); | ||
|
||
var svg = d3.select("svg"), | ||
width = +svg.attr("width"), | ||
height = +svg.attr("height"); | ||
|
||
var simulation = d3.forceSimulation() | ||
.force("link", d3.forceLink().id(function (d) { | ||
return d.id; | ||
}).distance(350)) | ||
.force("charge", d3.forceManyBody().strength(-300)) | ||
.force("center", d3.forceCenter(500, 500)); | ||
|
||
d3.json("graph/graph.json", function (error, graph) { | ||
if (error) throw error; | ||
|
||
svg.append("svg:defs").append("svg:marker") | ||
.attr("id", "triangle") | ||
.attr("refX", 100) | ||
.attr("refY", 5) | ||
.attr("markerWidth", 30) | ||
.attr("markerHeight", 30) | ||
.attr("markerUnits","userSpaceOnUse") | ||
.attr("orient", "auto") | ||
.append("path") | ||
.attr("d", "M 0 0 10 5 0 10") | ||
.style("fill", "black"); | ||
|
||
var label = svg.append("g") | ||
.attr("class", "labels") | ||
.append("foreignObject") | ||
.attr("width", "100%") | ||
.attr("height", "100%"); | ||
|
||
var link = svg.append("g") | ||
.attr("class", "links") | ||
.selectAll("line") | ||
.data(graph.links) | ||
.enter().append("line") | ||
.attr("marker-end", "url(#triangle)"); | ||
|
||
var node = svg.append("g") | ||
.attr("class", "nodes") | ||
.selectAll("circle") | ||
.data(graph.nodes) | ||
.enter().append("circle") | ||
.attr("r", 10) | ||
.call(d3.drag() | ||
.on("start", dragstarted) | ||
.on("drag", dragged) | ||
.on("end", dragended)); | ||
|
||
|
||
|
||
var labelDiv = label | ||
.selectAll("div") | ||
.data(graph.nodes) | ||
.enter() | ||
.append("xhtml:div") | ||
.html(function (d) { return d.name; }) | ||
.style("position", "absolute") | ||
.style("text-anchor", "middle") | ||
.style("fill", "#555") | ||
.style("font-family", "Arial") | ||
.style("font-size", 20) | ||
.style("z-index", 10) | ||
.style("cursor", "pointer") | ||
.on("click", function(d) { | ||
let element = d3 | ||
.select(this); | ||
if(element.html()===d.name) { | ||
element | ||
.html(function (d) { return getDescription(d) }); | ||
} else { | ||
element | ||
.html(function (d) { return d.name; }); | ||
} | ||
}); | ||
|
||
node.append("title") | ||
.text(function (d) { | ||
return d.id; | ||
}); | ||
|
||
simulation | ||
.nodes(graph.nodes) | ||
.on("tick", ticked); | ||
|
||
simulation.force("link") | ||
.links(graph.links); | ||
|
||
function ticked() { | ||
link | ||
.attr("x1", function (d) { | ||
return d.source.x; | ||
}) | ||
.attr("y1", function (d) { | ||
return d.source.y; | ||
}) | ||
.attr("x2", function (d) { | ||
return d.target.x; | ||
}) | ||
.attr("y2", function (d) { | ||
return d.target.y; | ||
}); | ||
|
||
node | ||
.attr("cx", function (d) { | ||
return d.x; | ||
}) | ||
.attr("cy", function (d) { | ||
return d.y; | ||
}); | ||
labelDiv | ||
.style("left", function(d){return (d.x + 20) + "px"; }) | ||
.style("top", function(d){return (d.y + 10) + "px"; }); | ||
} | ||
}); | ||
|
||
function dragstarted(d) { | ||
if (!d3.event.active) simulation.alphaTarget(0.3).restart(); | ||
d.fx = d.x; | ||
d.fy = d.y; | ||
} | ||
|
||
function dragged(d) { | ||
d.fx = d3.event.x; | ||
d.fy = d3.event.y; | ||
} | ||
|
||
function dragended(d) { | ||
if (!d3.event.active) simulation.alphaTarget(0); | ||
d.fx = null; | ||
d.fy = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"directed": true, "multigraph": false, "graph": {}, "nodes": [{"name": "Activity 1", "desc": "This is a cool 1", "id": 1}, {"name": "Activity 2", "desc": "This is a cool 2", "id": 2}, {"name": "Activity 3", "desc": "This is a cool 3", "id": 3}, {"name": "Activity 4", "desc": "This is a cool 4", "id": 4}, {"name": "Activity 5", "desc": "This is a cool 5", "id": 5}], "links": [{"source": 1, "target": 2}, {"source": 2, "target": 3}, {"source": 2, "target": 4}, {"source": 3, "target": 5}, {"source": 4, "target": 5}]} |
Oops, something went wrong.