Created
September 18, 2014 15:40
-
-
Save joshkh/335b374ce6a4c7d96da7 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.link { | |
stroke: #000; | |
stroke-width: 1.5px; | |
} | |
.node { | |
fill: #000; | |
stroke: #fff; | |
stroke-width: 1.5px; | |
} | |
.node.a { fill: #1f77b4; } | |
.node.b { fill: #ff7f0e; } | |
.node.c { fill: #2ca02c; } | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var color = d3.scale.category10(); | |
var nodes = [], | |
links = []; | |
var a = {id: "a"}, b = {id: "b"}, c = {id: "c"}; | |
nodes.push(a, b, c); | |
links.push({source: a, target: b}, {source: a, target: c}, {source: b, target: c}); | |
var force = d3.layout.force() | |
.nodes(nodes) | |
.links(links) | |
.charge(-400) | |
.linkDistance(500) | |
.size([width, height]) | |
.on("tick", tick); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var node = svg.selectAll(".node"), | |
link = svg.selectAll(".link"); | |
function start() { | |
link = link.data(force.links(), function(d) { return d.source.id + "-" + d.target.id; }); | |
link.enter().insert("line", ".node").attr("class", "link"); | |
link.exit().remove(); | |
node = node.data(force.nodes(), function(d) { return d.id;}); | |
node.enter().append("circle").attr("class", function(d) { return "node " + d.id; }).attr("r", 8); | |
node.exit().remove(); | |
force.start(); | |
} | |
function tick() { | |
node.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }) | |
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; }); | |
} | |
function updateSize(value) { | |
console.log("using value", value); | |
force.linkDistance(value * 10); | |
force.start(); | |
} | |
start(); | |
</script> | |
<form> | |
<input id="slide" type="range" name="points" min="0" max="50" step="5" onchange="updateSize(this.value);"> | |
</form> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment