Skip to content

Instantly share code, notes, and snippets.

@EmilienDupont
Last active June 6, 2016 15:42
Show Gist options
  • Save EmilienDupont/32cd7f9928798b0e008617106045508a to your computer and use it in GitHub Desktop.
Save EmilienDupont/32cd7f9928798b0e008617106045508a to your computer and use it in GitHub Desktop.
d3 Brain
{
"nodes" : [
{"x" : 442, "y" : 388},
{"x" : 436, "y" : 361},
{"x" : 426, "y" : 339},
{"x" : 415, "y" : 320},
{"x" : 394, "y" : 305},
{"x" : 371, "y" : 296},
{"x" : 357, "y" : 264},
{"x" : 336, "y" : 248},
{"x" : 290, "y" : 249},
{"x" : 255, "y" : 255},
{"x" : 225, "y" : 242},
{"x" : 210, "y" : 207},
{"x" : 193, "y" : 177},
{"x" : 209, "y" : 124},
{"x" : 237, "y" : 86},
{"x" : 275, "y" : 54},
{"x" : 331, "y" : 35},
{"x" : 398, "y" : 19},
{"x" : 431, "y" : 28},
{"x" : 456, "y" : 18},
{"x" : 489, "y" : 22},
{"x" : 535, "y" : 45},
{"x" : 585, "y" : 74},
{"x" : 622, "y" : 95},
{"x" : 640, "y" : 118},
{"x" : 660, "y" : 132},
{"x" : 671, "y" : 167},
{"x" : 678, "y" : 198},
{"x" : 679, "y" : 254},
{"x" : 657, "y" : 283},
{"x" : 598, "y" : 303},
{"x" : 586, "y" : 334},
{"x" : 557, "y" : 347},
{"x" : 526, "y" : 349},
{"x" : 498, "y" : 337},
{"x" : 483, "y" : 356},
{"x" : 477, "y" : 387},
{"x" : 491, "y" : 443},
{"x" : 509, "y" : 461},
{"x" : 490, "y" : 467},
{"x" : 470, "y" : 459},
{"x" : 454, "y" : 421},
{"x" : 233, "y" : 166},
{"x" : 267, "y" : 108},
{"x" : 335, "y" : 85},
{"x" : 401, "y" : 51},
{"x" : 479, "y" : 65},
{"x" : 558, "y" : 105},
{"x" : 608, "y" : 162},
{"x" : 531, "y" : 187},
{"x" : 642, "y" : 222},
{"x" : 591, "y" : 234},
{"x" : 530, "y" : 301},
{"x" : 461, "y" : 370},
{"x" : 464, "y" : 297},
{"x" : 410, "y" : 277},
{"x" : 360, "y" : 232},
{"x" : 416, "y" : 212},
{"x" : 465, "y" : 150},
{"x" : 415, "y" : 98},
{"x" : 369, "y" : 166},
{"x" : 291, "y" : 207},
{"x" : 299, "y" : 155},
{"x" : 417, "y" : 157},
{"x" : 484, "y" : 237},
{"x" : 245, "y" : 208},
{"x" : 267, "y" : 108},
{"x" : 352, "y" : 120},
{"x" : 500, "y" : 116},
{"x" : 542, "y" : 249},
{"x" : 588, "y" : 277},
{"x" : 549, "y" : 140},
{"x" : 446, "y" : 324}
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
var color = d3.scale.category20();
function distance(node1, node2) {
return Math.sqrt( (node1.x - node2.x) * (node1.x - node2.x) + (node1.y - node2.y) * (node1.y - node2.y));
}
function create_close_links(nodes, distance_from_node) {
var links = [];
var num_nodes = nodes.length;
for (i = 0; i < num_nodes; i++) {
var source = i;
for (j = i + 1; j < num_nodes; j++) {
var target = j;
if (distance(nodes[source], nodes[target]) < distance_from_node) {
var value = 2 * Math.random() + 1;
links.push({"source" : source, "target": target, "value" : value});
}
}
}
return links;
}
d3.json("brain_data.json", function(error, graph) {
if (error) throw error;
var nodes = graph.nodes;
var links = create_close_links(nodes, 100);
svg.selectAll(".link")
.data(links)
.enter()
.append("line")
.attr("class", "link")
.attr("x1", function(d) { return nodes[d.source].x; })
.attr("y1", function(d) { return nodes[d.source].y; })
.attr("x2", function(d) { return nodes[d.target].x; })
.attr("y2", function(d) { return nodes[d.target].y; })
.attr("stroke-width", function(d) { return d.value; });
svg.selectAll(".node")
.data(nodes)
.enter()
.append("circle")
.attr("class", "node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", function(d) { return 5 + 4 * Math.random(); })
.attr("fill", function(d) { return color( Math.floor(Math.random() * 6) );});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment