Last active
March 13, 2017 23:54
-
-
Save linnnes/22087974e8dfa5c5080b7fdc46e35a65 to your computer and use it in GitHub Desktop.
Ex2: Force Layout 0313
This file contains hidden or 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
license: mit |
This file contains hidden or 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
{ | |
"nodes": [ | |
{"atom": "What are you making?", "size": 36}, | |
{"atom": "App", "size": 12}, | |
{"atom": "Wearables", "size": 8}, | |
{"atom": "Physcial Objects", "size": 4}, | |
{"atom": "Website", "size": 14}, | |
{"atom": "Games", "size": 4}, | |
{"atom": "Poster", "size": 4}, | |
{"atom": "Privacy Seekers", "size": 1}, | |
{"atom": "Elderly People", "size": 1}, | |
{"atom": "Office Workers", "size": 1}, | |
{"atom": "Urban People", "size": 1}, | |
{"atom": "Nervous People", "size": 1}, | |
{"atom": "Children", "size": 1}, | |
{"atom": "Empty Nesters", "size": 1}, | |
{"atom": "Patients", "size": 1}, | |
{"atom": "Empathetic Healthcare", "size": 1} | |
], | |
"links": [ | |
{"source": 0, "target": 1, "bond": 2}, | |
{"source": 0, "target": 2, "bond": 2}, | |
{"source": 0, "target": 3, "bond": 2}, | |
{"source": 0, "target": 4, "bond": 2}, | |
{"source": 0, "target": 5, "bond": 2}, | |
{"source": 0, "target": 6, "bond": 2}, | |
{"source": 1, "target": 7, "bond": 1}, | |
{"source": 2, "target": 8, "bond": 1}, | |
{"source": 3, "target": 9, "bond": 1}, | |
{"source": 4, "target": 10, "bond": 1}, | |
{"source": 5, "target": 11, "bond": 1}, | |
{"source": 6, "target": 12, "bond": 1}, | |
{"source": 6, "target": 13, "bond": 1}, | |
{"source": 6, "target": 14, "bond": 1}, | |
{"source": 1, "target": 15, "bond": 1} | |
] | |
} |
This file contains hidden or 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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.link line { | |
stroke: #696969; | |
} | |
.link line.separator { | |
stroke: #fff; | |
stroke-width: 2px; | |
} | |
.node circle { | |
stroke: #000; | |
stroke-width: 1.5px; | |
} | |
.node text { | |
font: 10px sans-serif; | |
pointer-events: none; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var color = d3.scale.category20(); | |
var radius = d3.scale.sqrt() | |
.range([0, 6]); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var force = d3.layout.force() | |
.size([width, height]) | |
.charge(-400) | |
.linkDistance(function(d) { return radius(d.source.size) + radius(d.target.size) + 20; }); | |
d3.json("graph.json", function(error, graph) { | |
if (error) throw error; | |
force | |
.nodes(graph.nodes) | |
.links(graph.links) | |
.on("tick", tick) | |
.start(); | |
var link = svg.selectAll(".link") | |
.data(graph.links) | |
.enter().append("g") | |
.attr("class", "link"); | |
link.append("line") | |
.style("stroke-width", function(d) { return (d.bond * 2 - 1) * 2 + "px"; }); | |
link.filter(function(d) { return d.bond > 1; }).append("line") | |
.attr("class", "separator"); | |
var node = svg.selectAll(".node") | |
.data(graph.nodes) | |
.enter().append("g") | |
.attr("class", "node") | |
.call(force.drag); | |
node.append("circle") | |
.attr("r", function(d) { return radius(d.size); }) | |
.style("fill", function(d) { return color(d.atom); }) | |
.style("stroke", "yellow") | |
.style("stroke-opacity", ".3"); | |
node.append("text") | |
.attr("dy", ".35em") | |
.attr("text-anchor", "middle") | |
.text(function(d) { return d.atom; }); | |
function tick() { | |
link.selectAll("line") | |
.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("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment