The University of Florida Sparse Matrix Collection provides a vast collection of interesting graphs in Matrix Market format. This example shows how to adapt this format to D3's force layout.
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
margin: 0;
}
.node {
fill: steelblue;
}
.link {
stroke: #ccc;
}
</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 force = d3.layout.force()
.size([width, height]);
d3.text("can_161.mtx", function(error, text) {
if (error) throw error;
var graph = parseMtx(text);
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link");
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 4.5)
.call(force.drag);
force
.nodes(graph.nodes)
.links(graph.links)
.on("tick", tick)
.start();
function tick() {
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; });
}
});
function parseMtx(text) {
var nodes = [];
var links = text
.split(/\n/g) // split lines
.filter(function(d) { return d.charAt(0) != "%"; }) // skip comments
.slice(1, -1) // skip header line, last line
.map(function(d) {
d = d.split(/\s+/g);
var source = d[0] - 1, target = d[1] - 1;
return {
source: nodes[source] || (nodes[source] = {index: source}),
target: nodes[target] || (nodes[target] = {index: target})
};
});
return {
nodes: nodes,
links: links
};
}
</script>
https://d3js.org/d3.v3.min.js