Skip to content

Instantly share code, notes, and snippets.

@fogonwater
Last active July 2, 2018 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fogonwater/379ceff4f30a44d41a427c937b570677 to your computer and use it in GitHub Desktop.
Save fogonwater/379ceff4f30a44d41a427c937b570677 to your computer and use it in GitHub Desktop.
ignore
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<title>Web referrals</title>
<style>
body {
font-family: Helvetica;
font-size:0.8em;
}
.node rect {
cursor: move;
fill-opacity: .9;
shape-rendering: crispEdges;
}
.node text {
pointer-events: none;
text-shadow: 0 1px 0 #fff;
}
.link {
fill: none;
stroke: #000;
stroke-opacity: .2;
}
.link:hover {
stroke-opacity: .5;
}
</style>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="sankey.js"></script>
<script>
console.clear()
// set the dimensions and margins of the graph
var margin = {top: 10, right: 10, bottom: 10, left: 10},
width = 550 - margin.left - margin.right,
height = 800 - margin.top - margin.bottom;
// format variables
var format = d3.format(",.0f"),// zero decimal places
color = d3.scaleOrdinal(d3.schemeCategory10);
// append the svg object to the body of the page
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Set the sankey diagram properties
var sankey = d3.sankey()
.nodeWidth(36)
.nodePadding(15)
.size([width, height]);
var path = sankey.link();
// load the data
d3.json("sankey.json", function(error, graph) {
sankey
.nodes(graph.nodes)
.links(graph.links)
.layout(0);
// add in the links
var link = svg.append("g").selectAll(".link")
.data(graph.links.filter((d)=> d.group != 'Ignore'))
.enter().append("path")
.attr("class", "link")
.attr("d", path)
.style("stroke-width", (d) => Math.max(1, d.dy))
.style("stroke", (d) => color(d.group))
.sort(function(a, b) { return b.dy - a.dy; });
// add the link titles
link.append("title")
.text(function(d) {
return d.source.name + " → " +
d.target.name + "\n" + format(d.value); });
// add in the nodes
var node = svg.append("g").selectAll(".node")
.data(graph.nodes.filter((d)=> d.group != 'Ignore'))
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")"; })
.call(d3.drag()
.subject(function(d) {
return d;
})
.on("start", function() {
this.parentNode.appendChild(this);
})
.on("drag", dragmove));
// add the rectangles for the nodes
node.append("rect")
.attr("height", function(d) { return d.dy; })
.attr("width", sankey.nodeWidth())
.style("fill", (d) => color(d.group))
//.style("stroke", (d) => d3.rgb(d.color).darker(2))
.append("title")
.text((d) => d.name + "\n" + format(d.value));
// add in the title for the nodes
node.append("text")
.attr("x", -6)
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "end")
.attr("transform", null)
.text((d) => d.name + " " + format(Math.round(d.value)))
.filter((d) => d.x < width / 2)
.attr("x", 6 + sankey.nodeWidth())
.attr("text-anchor", "start");
// the function for moving the nodes
function dragmove(d) {
d3.select(this)
.attr("transform",
"translate("
+ d.x + ","
+ (d.y = Math.max(
0, Math.min(height - d.dy, d3.event.y))
) + ")");
sankey.relayout();
link.attr("d", path);
}
});
</script>
</body>
d3.sankey = function() {
var sankey = {},
nodeWidth = 24,
nodePadding = 8,
size = [1, 1],
nodes = [],
links = [];
sankey.nodeWidth = function(_) {
if (!arguments.length) return nodeWidth;
nodeWidth = +_;
return sankey;
};
sankey.nodePadding = function(_) {
if (!arguments.length) return nodePadding;
nodePadding = +_;
return sankey;
};
sankey.nodes = function(_) {
if (!arguments.length) return nodes;
nodes = _;
return sankey;
};
sankey.links = function(_) {
if (!arguments.length) return links;
links = _;
return sankey;
};
sankey.size = function(_) {
if (!arguments.length) return size;
size = _;
return sankey;
};
sankey.layout = function(iterations) {
computeNodeLinks();
computeNodeValues();
computeNodeBreadths();
computeNodeDepths(iterations);
computeLinkDepths();
return sankey;
};
sankey.relayout = function() {
computeLinkDepths();
return sankey;
};
sankey.link = function() {
var curvature = .5;
function link(d) {
var x0 = d.source.x + d.source.dx,
x1 = d.target.x,
xi = d3.interpolateNumber(x0, x1),
x2 = xi(curvature),
x3 = xi(1 - curvature),
y0 = d.source.y + d.sy + d.dy / 2,
y1 = d.target.y + d.ty + d.dy / 2;
return "M" + x0 + "," + y0
+ "C" + x2 + "," + y0
+ " " + x3 + "," + y1
+ " " + x1 + "," + y1;
}
link.curvature = function(_) {
if (!arguments.length) return curvature;
curvature = +_;
return link;
};
return link;
};
// Populate the sourceLinks and targetLinks for each node.
// Also, if the source and target are not objects, assume they are indices.
function computeNodeLinks() {
nodes.forEach(function(node) {
node.sourceLinks = [];
node.targetLinks = [];
});
links.forEach(function(link) {
var source = link.source,
target = link.target;
if (typeof source === "number") source = link.source = nodes[link.source];
if (typeof target === "number") target = link.target = nodes[link.target];
source.sourceLinks.push(link);
target.targetLinks.push(link);
});
}
// Compute the value (size) of each node by summing the associated links.
function computeNodeValues() {
nodes.forEach(function(node) {
node.value = Math.max(
d3.sum(node.sourceLinks, value),
d3.sum(node.targetLinks, value)
);
});
}
// Iteratively assign the breadth (x-position) for each node.
// Nodes are assigned the maximum breadth of incoming neighbors plus one;
// nodes with no incoming links are assigned breadth zero, while
// nodes with no outgoing links are assigned the maximum breadth.
function computeNodeBreadths() {
var remainingNodes = nodes,
nextNodes,
x = 0;
while (remainingNodes.length) {
nextNodes = [];
remainingNodes.forEach(function(node) {
node.x = x;
node.dx = nodeWidth;
node.sourceLinks.forEach(function(link) {
if (nextNodes.indexOf(link.target) < 0) {
nextNodes.push(link.target);
}
});
});
remainingNodes = nextNodes;
++x;
}
//
moveSinksRight(x);
scaleNodeBreadths((size[0] - nodeWidth) / (x - 1));
}
function moveSourcesRight() {
nodes.forEach(function(node) {
if (!node.targetLinks.length) {
node.x = d3.min(node.sourceLinks, function(d) { return d.target.x; }) - 1;
}
});
}
function moveSinksRight(x) {
nodes.forEach(function(node) {
if (!node.sourceLinks.length) {
node.x = x - 1;
}
});
}
function scaleNodeBreadths(kx) {
nodes.forEach(function(node) {
node.x *= kx;
});
}
function computeNodeDepths(iterations) {
var nodesByBreadth = d3.nest()
.key(function(d) { return d.x; })
.sortKeys(d3.ascending)
.entries(nodes)
.map(function(d) { return d.values; });
//
initializeNodeDepth();
resolveCollisions();
for (var alpha = 1; iterations > 0; --iterations) {
relaxRightToLeft(alpha *= .99);
resolveCollisions();
relaxLeftToRight(alpha);
resolveCollisions();
}
function initializeNodeDepth() {
var ky = d3.min(nodesByBreadth, function(nodes) {
return (size[1] - (nodes.length - 1) * nodePadding) / d3.sum(nodes, value);
});
nodesByBreadth.forEach(function(nodes) {
nodes.forEach(function(node, i) {
node.y = i;
node.dy = node.value * ky;
});
});
links.forEach(function(link) {
link.dy = link.value * ky;
});
}
function relaxLeftToRight(alpha) {
nodesByBreadth.forEach(function(nodes, breadth) {
nodes.forEach(function(node) {
if (node.targetLinks.length) {
var y = d3.sum(node.targetLinks, weightedSource) / d3.sum(node.targetLinks, value);
node.y += (y - center(node)) * alpha;
}
});
});
function weightedSource(link) {
return center(link.source) * link.value;
}
}
function relaxRightToLeft(alpha) {
nodesByBreadth.slice().reverse().forEach(function(nodes) {
nodes.forEach(function(node) {
if (node.sourceLinks.length) {
var y = d3.sum(node.sourceLinks, weightedTarget) / d3.sum(node.sourceLinks, value);
node.y += (y - center(node)) * alpha;
}
});
});
function weightedTarget(link) {
return center(link.target) * link.value;
}
}
function resolveCollisions() {
nodesByBreadth.forEach(function(nodes) {
var node,
dy,
y0 = 0,
n = nodes.length,
i;
// Push any overlapping nodes down.
nodes.sort(ascendingDepth);
for (i = 0; i < n; ++i) {
node = nodes[i];
dy = y0 - node.y;
if (dy > 0) node.y += dy;
y0 = node.y + node.dy + nodePadding;
}
// If the bottommost node goes outside the bounds, push it back up.
dy = y0 - nodePadding - size[1];
if (dy > 0) {
y0 = node.y -= dy;
// Push any overlapping nodes back up.
for (i = n - 2; i >= 0; --i) {
node = nodes[i];
dy = node.y + node.dy + nodePadding - y0;
if (dy > 0) node.y -= dy;
y0 = node.y;
}
}
});
}
function ascendingDepth(a, b) {
return a.y - b.y;
}
}
function computeLinkDepths() {
nodes.forEach(function(node) {
node.sourceLinks.sort(ascendingTargetDepth);
node.targetLinks.sort(ascendingSourceDepth);
});
nodes.forEach(function(node) {
var sy = 0, ty = 0;
node.sourceLinks.forEach(function(link) {
link.sy = sy;
sy += link.dy;
});
node.targetLinks.forEach(function(link) {
link.ty = ty;
ty += link.dy;
});
});
function ascendingSourceDepth(a, b) {
return a.source.y - b.source.y;
}
function ascendingTargetDepth(a, b) {
return a.target.y - b.target.y;
}
}
function center(node) {
return node.y + node.dy / 2;
}
function value(link) {
return link.value;
}
return sankey;
};
{
"links": [
{
"group": "Archives",
"source": 0,
"target": 1,
"value": 174113.0
},
{
"group": "Archway",
"source": 0,
"target": 2,
"value": 136212.0
},
{
"group": "DigitalNZ.org",
"source": 0,
"target": 3,
"value": 66720.0
},
{
"group": "National Library Online",
"source": 0,
"target": 4,
"value": 1646557.0
},
{
"group": "NZHISTORY",
"source": 0,
"target": 5,
"value": 3074722.0
},
{
"group": "TE ARA",
"source": 0,
"target": 6,
"value": 4642029.0
},
{
"group": "Archives",
"source": 7,
"target": 1,
"value": 58389.0
},
{
"group": "Archway",
"source": 7,
"target": 2,
"value": 121363.0
},
{
"group": "DigitalNZ.org",
"source": 7,
"target": 3,
"value": 34648.0
},
{
"group": "National Library Online",
"source": 7,
"target": 4,
"value": 259410.0
},
{
"group": "NZHISTORY",
"source": 7,
"target": 5,
"value": 374508.0
},
{
"group": "TE ARA",
"source": 7,
"target": 6,
"value": 552176.0
},
{
"group": "Archives",
"source": 8,
"target": 1,
"value": 9227.0
},
{
"group": "Archway",
"source": 8,
"target": 2,
"value": 6972.0
},
{
"group": "DigitalNZ.org",
"source": 8,
"target": 3,
"value": 1878.0
},
{
"group": "National Library Online",
"source": 8,
"target": 4,
"value": 32771.0
},
{
"group": "NZHISTORY",
"source": 8,
"target": 5,
"value": 97572.0
},
{
"group": "TE ARA",
"source": 8,
"target": 6,
"value": 135906.0
},
{
"group": "Archives",
"source": 9,
"target": 1,
"value": 448.0
},
{
"group": "Archway",
"source": 9,
"target": 2,
"value": 110782.0
},
{
"group": "DigitalNZ.org",
"source": 9,
"target": 3,
"value": 18346.0
},
{
"group": "National Library Online",
"source": 9,
"target": 4,
"value": 1380.0
},
{
"group": "NZHISTORY",
"source": 9,
"target": 5,
"value": 1478.0
},
{
"group": "TE ARA",
"source": 9,
"target": 6,
"value": 51.0
},
{
"group": "Archives",
"source": 10,
"target": 1,
"value": 2678.0
},
{
"group": "Archway",
"source": 10,
"target": 2,
"value": 10005.0
},
{
"group": "DigitalNZ.org",
"source": 10,
"target": 3,
"value": 24690.0
},
{
"group": "National Library Online",
"source": 10,
"target": 4,
"value": 27941.0
},
{
"group": "NZHISTORY",
"source": 10,
"target": 5,
"value": 48752.0
},
{
"group": "TE ARA",
"source": 10,
"target": 6,
"value": 41652.0
},
{
"group": "Archives",
"source": 11,
"target": 1,
"value": 2137.0
},
{
"group": "Archway",
"source": 11,
"target": 2,
"value": 1401.0
},
{
"group": "DigitalNZ.org",
"source": 11,
"target": 3,
"value": 407.0
},
{
"group": "National Library Online",
"source": 11,
"target": 4,
"value": 7031.0
},
{
"group": "NZHISTORY",
"source": 11,
"target": 5,
"value": 28315.0
},
{
"group": "TE ARA",
"source": 11,
"target": 6,
"value": 44378.0
},
{
"group": "Archives",
"source": 12,
"target": 1,
"value": 516.0
},
{
"group": "Archway",
"source": 12,
"target": 2,
"value": 45671.0
},
{
"group": "DigitalNZ.org",
"source": 12,
"target": 3,
"value": 126.0
},
{
"group": "National Library Online",
"source": 12,
"target": 4,
"value": 1125.0
},
{
"group": "NZHISTORY",
"source": 12,
"target": 5,
"value": 1065.0
},
{
"group": "TE ARA",
"source": 12,
"target": 6,
"value": 701.0
},
{
"group": "Archives",
"source": 13,
"target": 1,
"value": 650.0
},
{
"group": "Archway",
"source": 13,
"target": 2,
"value": 238.0
},
{
"group": "DigitalNZ.org",
"source": 13,
"target": 3,
"value": 181.0
},
{
"group": "National Library Online",
"source": 13,
"target": 4,
"value": 2049.0
},
{
"group": "NZHISTORY",
"source": 13,
"target": 5,
"value": 9807.0
},
{
"group": "TE ARA",
"source": 13,
"target": 6,
"value": 27384.0
},
{
"group": "Archives",
"source": 14,
"target": 1,
"value": 187.0
},
{
"group": "Archway",
"source": 14,
"target": 2,
"value": 231.0
},
{
"group": "DigitalNZ.org",
"source": 14,
"target": 3,
"value": 2574.0
},
{
"group": "National Library Online",
"source": 14,
"target": 4,
"value": 10892.0
},
{
"group": "NZHISTORY",
"source": 14,
"target": 5,
"value": 9202.0
},
{
"group": "TE ARA",
"source": 14,
"target": 6,
"value": 2831.0
},
{
"group": "Archway",
"source": 15,
"target": 2,
"value": 40.0
},
{
"group": "DigitalNZ.org",
"source": 15,
"target": 3,
"value": 22.0
},
{
"group": "National Library Online",
"source": 15,
"target": 4,
"value": 606.0
},
{
"group": "NZHISTORY",
"source": 15,
"target": 5,
"value": 14210.0
},
{
"group": "TE ARA",
"source": 15,
"target": 6,
"value": 6619.0
},
{
"group": "Archives",
"source": 16,
"target": 1,
"value": 451.0
},
{
"group": "Archway",
"source": 16,
"target": 2,
"value": 175.0
},
{
"group": "DigitalNZ.org",
"source": 16,
"target": 3,
"value": 225.0
},
{
"group": "National Library Online",
"source": 16,
"target": 4,
"value": 2429.0
},
{
"group": "NZHISTORY",
"source": 16,
"target": 5,
"value": 5638.0
},
{
"group": "TE ARA",
"source": 16,
"target": 6,
"value": 9546.0
},
{
"group": "Archives",
"source": 17,
"target": 1,
"value": 234.0
},
{
"group": "Archway",
"source": 17,
"target": 2,
"value": 772.0
},
{
"group": "DigitalNZ.org",
"source": 17,
"target": 3,
"value": 43.0
},
{
"group": "National Library Online",
"source": 17,
"target": 4,
"value": 1980.0
},
{
"group": "NZHISTORY",
"source": 17,
"target": 5,
"value": 6599.0
},
{
"group": "TE ARA",
"source": 17,
"target": 6,
"value": 7600.0
},
{
"group": "NZHISTORY",
"source": 18,
"target": 5,
"value": 14738.0
},
{
"group": "TE ARA",
"source": 18,
"target": 6,
"value": 202.0
},
{
"group": "Archives",
"source": 19,
"target": 1,
"value": 514.0
},
{
"group": "National Library Online",
"source": 19,
"target": 4,
"value": 2345.0
},
{
"group": "NZHISTORY",
"source": 19,
"target": 5,
"value": 5617.0
},
{
"group": "TE ARA",
"source": 19,
"target": 6,
"value": 3766.0
},
{
"group": "Archives",
"source": 20,
"target": 1,
"value": 25.0
},
{
"group": "Archway",
"source": 20,
"target": 2,
"value": 645.0
},
{
"group": "DigitalNZ.org",
"source": 20,
"target": 3,
"value": 411.0
},
{
"group": "National Library Online",
"source": 20,
"target": 4,
"value": 6017.0
},
{
"group": "NZHISTORY",
"source": 20,
"target": 5,
"value": 1713.0
},
{
"group": "TE ARA",
"source": 20,
"target": 6,
"value": 2211.0
},
{
"group": "Archway",
"source": 21,
"target": 2,
"value": 2498.0
},
{
"group": "DigitalNZ.org",
"source": 21,
"target": 3,
"value": 1750.0
},
{
"group": "National Library Online",
"source": 21,
"target": 4,
"value": 1678.0
},
{
"group": "NZHISTORY",
"source": 21,
"target": 5,
"value": 1056.0
},
{
"group": "TE ARA",
"source": 21,
"target": 6,
"value": 3744.0
},
{
"group": "Archives",
"source": 22,
"target": 1,
"value": 229.0
},
{
"group": "National Library Online",
"source": 22,
"target": 4,
"value": 163.0
},
{
"group": "NZHISTORY",
"source": 22,
"target": 5,
"value": 2103.0
},
{
"group": "TE ARA",
"source": 22,
"target": 6,
"value": 7289.0
},
{
"group": "Archives",
"source": 23,
"target": 1,
"value": 45.0
},
{
"group": "Archway",
"source": 23,
"target": 2,
"value": 4.0
},
{
"group": "DigitalNZ.org",
"source": 23,
"target": 3,
"value": 168.0
},
{
"group": "National Library Online",
"source": 23,
"target": 4,
"value": 2290.0
},
{
"group": "NZHISTORY",
"source": 23,
"target": 5,
"value": 1144.0
},
{
"group": "TE ARA",
"source": 23,
"target": 6,
"value": 5893.0
},
{
"group": "Archives",
"source": 24,
"target": 1,
"value": 115.0
},
{
"group": "Archway",
"source": 24,
"target": 2,
"value": 5.0
},
{
"group": "DigitalNZ.org",
"source": 24,
"target": 3,
"value": 402.0
},
{
"group": "National Library Online",
"source": 24,
"target": 4,
"value": 754.0
},
{
"group": "NZHISTORY",
"source": 24,
"target": 5,
"value": 4256.0
},
{
"group": "TE ARA",
"source": 24,
"target": 6,
"value": 3765.0
},
{
"group": "Archives",
"source": 25,
"target": 1,
"value": 87.0
},
{
"group": "Archway",
"source": 25,
"target": 2,
"value": 16.0
},
{
"group": "DigitalNZ.org",
"source": 25,
"target": 3,
"value": 26.0
},
{
"group": "National Library Online",
"source": 25,
"target": 4,
"value": 587.0
},
{
"group": "NZHISTORY",
"source": 25,
"target": 5,
"value": 3679.0
},
{
"group": "TE ARA",
"source": 25,
"target": 6,
"value": 3947.0
},
{
"group": "Archway",
"source": 26,
"target": 2,
"value": 202.0
},
{
"group": "DigitalNZ.org",
"source": 26,
"target": 3,
"value": 4618.0
},
{
"group": "National Library Online",
"source": 26,
"target": 4,
"value": 1787.0
},
{
"group": "NZHISTORY",
"source": 26,
"target": 5,
"value": 174.0
},
{
"group": "TE ARA",
"source": 26,
"target": 6,
"value": 672.0
},
{
"group": "Archives",
"source": 27,
"target": 1,
"value": 21638.0
},
{
"group": "Archway",
"source": 27,
"target": 2,
"value": 47177.0
},
{
"group": "DigitalNZ.org",
"source": 27,
"target": 3,
"value": 22714.0
},
{
"group": "National Library Online",
"source": 27,
"target": 4,
"value": 105015.0
},
{
"group": "NZHISTORY",
"source": 27,
"target": 5,
"value": 121748.0
},
{
"group": "TE ARA",
"source": 27,
"target": 6,
"value": 168762.0
}
],
"nodes": [
{
"group": "mixed",
"name": "google",
"node": 0
},
{
"group": "Archives",
"name": "Archives",
"node": 1
},
{
"group": "Archway",
"name": "Archway",
"node": 2
},
{
"group": "DigitalNZ.org",
"name": "DigitalNZ.org",
"node": 3
},
{
"group": "National Library Online",
"name": "National Library Online",
"node": 4
},
{
"group": "NZHISTORY",
"name": "NZHISTORY",
"node": 5
},
{
"group": "TE ARA",
"name": "TE ARA",
"node": 6
},
{
"group": "mixed",
"name": "(direct)",
"node": 7
},
{
"group": "mixed",
"name": "bing",
"node": 8
},
{
"group": "mixed",
"name": "archives.govt.nz",
"node": 9
},
{
"group": "mixed",
"name": "facebook",
"node": 10
},
{
"group": "mixed",
"name": "yahoo",
"node": 11
},
{
"group": "mixed",
"name": "aucklandmuseum.com",
"node": 12
},
{
"group": "mixed",
"name": "wikipedia",
"node": 13
},
{
"group": "mixed",
"name": "twitter",
"node": 14
},
{
"group": "mixed",
"name": "classroom.google.com",
"node": 15
},
{
"group": "mixed",
"name": "duckduckgo.com",
"node": 16
},
{
"group": "mixed",
"name": "ask.com",
"node": 17
},
{
"group": "mixed",
"name": "buzzfeed.com",
"node": 18
},
{
"group": "mixed",
"name": "teara.govt.nz",
"node": 19
},
{
"group": "mixed",
"name": "digitalnz.org",
"node": 20
},
{
"group": "mixed",
"name": "nzetc.victoria.ac.nz",
"node": 21
},
{
"group": "mixed",
"name": "newzealandnow.govt.nz",
"node": 22
},
{
"group": "mixed",
"name": "pinterest.com",
"node": 23
},
{
"group": "mixed",
"name": "reddit.com",
"node": 24
},
{
"group": "mixed",
"name": "ecosia.org",
"node": 25
},
{
"group": "mixed",
"name": "aucklandcity.govt.nz",
"node": 26
},
{
"group": "mixed",
"name": "other",
"node": 27
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment