D3.js Template
by allenday
HTML
<svg></svg>
<!--
--second degree
SELECT
tx.from_address, tx.to_address, SUM(tx.value)/POWER(10,18) AS value
FROM `bigquery-public-data.crypto_ethereum.transactions` AS tx
WHERE from_address IN (
SELECT
to_address
FROM `bigquery-public-data.crypto_ethereum.transactions` AS tx
WHERE
from_address = '0x0af5035a8dff53f5a89bfc083fdec94c7332ad7a'
)
GROUP BY from_address, to_address
-- HAVING COUNT(tx.to_address) > 1
UNION ALL
--first degree
SELECT
from_address, to_address, SUM(tx.value)/POWER(10,18)
FROM `bigquery-public-data.crypto_ethereum.transactions` AS tx
WHERE
from_address = '0x0af5035a8dff53f5a89bfc083fdec94c7332ad7a'
GROUP BY from_address, to_address
-->
CSS
.link {
stroke: #000;
}
.node {
stroke: #fff;
}
JavaScript
var width = 400,
height = 400;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.size([width, height]);
d3.csv("https://raw.githubusercontent.com/allenday/force-layout-from-csv/master/graph.csv", function(error, links) {
if (error) throw error;
var nodesByName = {};
// Create nodes for each unique source and target.
links.forEach(function(link) {
link.source = nodeByName(link.source);
link.target = nodeByName(link.target);
});
// Extract the array of nodes from the map by name.
var nodes = d3.values(nodesByName);
// Create the link lines.
var link = svg.selectAll(".link")
.data(links)
.enter().append("line")
.attr("class", "link");
// Create the node circles.
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.call(force.drag);
// Start the force layout.
force
.nodes(nodes)
.links(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 nodeByName(name) {
return nodesByName[name] || (nodesByName[name] = {name: name});
}
});