Last active
August 29, 2015 14:10
-
-
Save standarderror/823dc61d719382a11be9 to your computer and use it in GitHub Desktop.
Random Graph D3
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
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
// # groups added by Cam | |
function erdosRenyi(nodesNum,directedness,prob,groups) { | |
// create nodes list | |
var nodes = [] | |
for (var i = 0; i < nodesNum; i++) { | |
var name = function(i) { return toString(i);}; | |
var group = getRandomInt(1,groups); | |
// nodes.push(i); | |
nodes.push({"name":name, "group":group}); | |
} | |
// init edges | |
var edges = []; | |
for (var i = 0; i < nodes.length; i++) { | |
for (var j = i+1; j < nodes.length; j++) { | |
var pchance = Math.random() | |
if (pchance < prob) { // changed by Cam from > to < | |
if (directedness === 0) { | |
edges.push({"source":i,"target":j,"directedness":0}); | |
} | |
else { | |
var dchance = Math.random(); | |
if (dchance > prob) { | |
edges.push({"source":i,"target":j,"directedness":1}) | |
edges.push({"source":j,"target":i,"directedness":1}) | |
} | |
else { | |
edges.push({"source":i,"target":j,"directedness":1}) | |
// need to fix - can't be i or j! | |
var rando = getRandomInt(0,nodes.length); | |
edges.push({"source":getRandomInt(0,nodes.length),"target":i,"directedness":1}) | |
} | |
} | |
} | |
} | |
} | |
return {"nodes":nodes, "links":edges}; | |
//return {"nodes":nodes.map(function(m) { return {"name":m.toString()};}), "links":edges}; | |
} | |
var width = 500; | |
var height = 250; | |
var svg = d3.select("#chart") | |
.append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
// .style('border', '1px solid red') | |
; | |
graph = erdosRenyi(10,0,0.2,5); | |
console.log(graph); | |
var color = d3.scale.category20(); | |
var force = d3.layout.force() | |
.charge(-100) | |
.linkDistance(30) | |
.size([width, height]); | |
force | |
.nodes(graph.nodes) | |
.links(graph.links) | |
.start(); | |
var link = svg.selectAll(".link") | |
.data(graph.links) | |
.enter().append("line") | |
.attr("class", "link") | |
.style("stroke-width", 4) | |
; | |
var node = svg.selectAll(".node") | |
.data(graph.nodes) | |
.enter().append("circle") | |
.attr("class", "node") | |
.attr("r", 5) // function(d) { return d.value; }) | |
.style("fill", function(d) { return color(d.group); }) | |
.call(force.drag); | |
// node.append("title") | |
// .text(function(d) { return d.name; }); | |
force.on("tick", function() { | |
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; }); | |
}); | |
// Button click triggers transition | |
function endall(transition, callback) { | |
var n = 0; | |
transition | |
.each(function() { ++n; }) | |
.each("end", function() { if (!--n) callback.apply(this, arguments); }); | |
} | |
d3.select("#button") | |
.on("click", function() { | |
console.log('click'); | |
force.stop(); | |
// first withdraw all links, then extend them to new spots | |
link.data(graph.links).transition().duration(1000) | |
.attr("x1", function(d) { return d.source.x; }) | |
.attr("y1", function(d) { return d.source.y; }) | |
.attr("x2", function(d) { return d.source.x; }) | |
.attr("y2", function(d) { return d.source.y;}) | |
.call(endall, function() { | |
graph.links = erdosRenyi(10,0,0.2,5).links; | |
link.data(graph.links).transition() | |
.duration(1000) | |
.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; }) | |
.call(endall, function() { | |
force | |
.links(graph.links); | |
force.start(); | |
}); | |
}); | |
// updateChart(); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment