Skip to content

Instantly share code, notes, and snippets.

@tomshanley
Last active May 15, 2018 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomshanley/11277583 to your computer and use it in GitHub Desktop.
Save tomshanley/11277583 to your computer and use it in GitHub Desktop.
Sankey - highlight links by doubleclicking nodes
license: mit

An adaptation of the Sankey diagram to show traceability of project outputs through to benefits. Double clicking on a node shows the links up and down the model.

We adapted the sankey.js file so that node heights weren't determined by the number and size of incoming/outgoing links, and aligned the ends of the links to the middle of the nodes.

Thanks to Simon Titheridge!

{
"nodes":[
{"name":"Project 13", "types": ["carrier"]},
{"name":"Project 1", "types": ["carrier"]},
{"name":"Project 2", "types": ["carrier"]},
{"name":"Project 3", "types": ["carrier"]},
{"name":"Project 4", "types": ["carrier"]},
{"name":"Project 5", "types": ["carrier"]},
{"name":"Project 6", "types": ["carrier"]},
{"name":"Project 7", "types": ["carrier"]},
{"name":"Project 8", "types": ["carrier"]},
{"name":"Project 9", "types": ["carrier"]},
{"name":"Capability 1", "types": ["carrier"]},
{"name":"Capability 2", "types": ["carrier"]},
{"name":"Capability 3", "types": ["carrier"]},
{"name":"Capability 4", "types": ["carrier"]},
{"name":"Capability 5", "types": ["carrier"]},
{"name":"Capability 6", "types": ["carrier"]},
{"name":"Capability 7", "types": ["carrier"]},
{"name":"Capability 8", "types": ["carrier"]},
{"name":"Capability 9", "types": ["carrier"]},
{"name":"Capability 10", "types": ["carrier"]},
{"name":"Capability 11", "types": ["carrier"]},
{"name":"Capability 12", "types": ["carrier"]},
{"name":"Capability 13", "types": ["carrier"]},
{"name":"Capability 14", "types": ["carrier"]},
{"name":"Capability 15", "types": ["carrier"]},
{"name":"Capability 16", "types": ["carrier"]},
{"name":"Capability 17", "types": ["carrier"]},
{"name":"Capability 18", "types": ["carrier"]},
{"name":"Capability 19", "types": ["carrier"]},
{"name":"Capability 20", "types": ["carrier"]},
{"name":"Capability 21", "types": ["carrier"]},
{"name":"Capability 22", "types": ["carrier"]},
{"name":"Capability 23", "types": ["carrier"]},
{"name":"Capability 24", "types": ["carrier"]},
{"name":"Capability 25", "types": ["carrier"]},
{"name":"Capability 26", "types": ["carrier"]},
{"name":"Capability 27", "types": ["carrier"]},
{"name":"Capability 28", "types": ["carrier"]},
{"name":"Capability 29", "types": ["carrier"]},
{"name":"Capability 30", "types": ["carrier"]},
{"name":"Capability 31", "types": ["carrier"]},
{"name":"Capability 32", "types": ["carrier"]},
{"name":"Capability 33", "types": ["carrier"]},
{"name":"Capability 34", "types": ["carrier"]},
{"name":"Capability 35", "types": ["carrier"]},
{"name":"Capability 36", "types": ["carrier"]},
{"name":"Capability 37", "types": ["carrier"]},
{"name":"Capability 38", "types": ["carrier"]},
{"name":"Capability 39", "types": ["carrier"]},
{"name":"Capability 40", "types": ["carrier"]},
{"name":"Capability 41", "types": ["carrier"]},
{"name":"Outcome 1", "types": ["carrier"]},
{"name":"Intermediate Outcome 1", "types": ["carrier"]},
{"name":"Intermediate Outcome 2", "types": ["carrier"]},
{"name":"Deliverable 1", "types": ["carrier"]},
{"name":"Outcome 2", "types": ["carrier"]},
{"name":"Deliverable 2", "types": ["carrier"]},
{"name":"Deliverable 3", "types": ["carrier"]},
{"name":"Deliverable 4", "types": ["carrier"]},
{"name":"Intermediate Outcome 3", "types": ["carrier"]},
{"name":"Intermediate Outcome 4", "types": ["carrier"]},
{"name":"Intermediate Outcome 5", "types": ["carrier"]},
{"name":"Intermediate Outcome 6", "types": ["carrier"]},
{"name":"Outcome 3", "types": ["carrier"]},
{"name":"Outcome 4", "types": ["carrier"]},
{"name":"Intermediate Outcome 7", "types": ["carrier"]},
{"name":"Intermediate Outcome 8", "types": ["carrier"]},
{"name":"Outcome 5", "types": ["carrier"]},
{"name":"Intermediate Outcome 9", "types": ["carrier"]},
{"name":"Intermediate Outcome 10", "types": ["carrier"]},
{"name":"Intermediate Outcome 11", "types": ["carrier"]},
{"name":"Outcome 6", "types": ["carrier"]},
{"name":"Deliverable 5", "types": ["carrier"]},
{"name":"Deliverable 6", "types": ["carrier"]},
{"name":"Deliverable 7", "types": ["carrier"]},
{"name":"Intermediate Outcome 12", "types": ["carrier"]},
{"name":"Deliverable 8", "types": ["carrier"]},
{"name":"Project 10", "types": ["carrier"]},
{"name":"Outcome 7", "types": ["carrier"]},
{"name":"Project 11", "types": ["carrier"]},
{"name":"Deliverable 9", "types": ["carrier"]},
{"name":"Deliverable 10", "types": ["carrier"]},
{"name":"Deliverable 11", "types": ["carrier"]},
{"name":"Intermediate Outcome 13", "types": ["carrier"]},
{"name":"Outcome 11", "types": ["carrier"]},
{"name":"Benefit 2", "types": ["carrier"]},
{"name":"Benefit 3", "types": ["carrier"]},
{"name":"Benefit 4", "types": ["carrier"]},
{"name":"Intermediate Outcome 14", "types": ["carrier"]},
{"name":"Intermediate Outcome 15", "types": ["carrier"]},
{"name":"Benefit 5", "types": ["carrier"]},
{"name":"Outcome 8", "types": ["carrier"]},
{"name":"Benefit 6", "types": ["carrier"]},
{"name":"Intermediate Outcome 16", "types": ["carrier"]},
{"name":"Intermediate Outcome 17", "types": ["carrier"]},
{"name":"Intermediate Outcome 18", "types": ["carrier"]},
{"name":"Deliverable 12", "types": ["carrier"]},
{"name":"Deliverable 13", "types": ["carrier"]},
{"name":"Deliverable 14", "types": ["carrier"]},
{"name":"Project 12", "types": ["carrier"]},
{"name":"Deliverable 15", "types": ["carrier"]},
{"name":"Intermediate Outcome 19", "types": ["carrier"]},
{"name":"Deliverable 16", "types": ["carrier"]},
{"name":"Intermediate Outcome 20", "types": ["carrier"]},
{"name":"Intermediate Outcome 21", "types": ["carrier"]},
{"name":"Intermediate Outcome 22", "types": ["carrier"]},
{"name":"Deliverable 17", "types": ["carrier"]},
{"name":"Intermediate Outcome 23", "types": ["carrier"]},
{"name":"Deliverable 18", "types": ["carrier"]},
{"name":"Deliverable 19", "types": ["carrier"]},
{"name":"Deliverable 20", "types": ["carrier"]},
{"name":"Deliverable 21", "types": ["carrier"]},
{"name":"Deliverable 22", "types": ["carrier"]},
{"name":"Benefit 7", "types": ["carrier"]},
{"name":"Benefit 8", "types": ["carrier"]},
{"name":"Outcome 9", "types": ["carrier"]},
{"name":"Intermediate Outcome 24", "types": ["carrier"]},
{"name":"Benefit 9", "types": ["carrier"]},
{"name":"Intermediate Outcome 25", "types": ["carrier"]},
{"name":"Intermediate Outcome 26", "types": ["carrier"]},
{"name":"Benefit 1", "types": ["carrier"]},
{"name":"Deliverable 23", "types": ["carrier"]},
{"name":"Deliverable 24", "types": ["carrier"]},
{"name":"Deliverable 25", "types": ["carrier"]},
{"name":"Deliverable 26", "types": ["carrier"]},
{"name":"Capability 42", "types": ["carrier"]},
{"name":"Deliverable 27", "types": ["carrier"]},
{"name":"Deliverable 28", "types": ["carrier"]},
{"name":"Deliverable 29", "types": ["carrier"]},
{"name":"Deliverable 30", "types": ["carrier"]},
{"name":"Deliverable 31", "types": ["carrier"]},
{"name":"Intermediate Outcome 27", "types": ["carrier"]},
{"name":"Deliverable 32", "types": ["carrier"]},
{"name":"Deliverable 33", "types": ["carrier"]},
{"name":"Intermediate Outcome 28", "types": ["carrier"]},
{"name":"Deliverable 34", "types": ["carrier"]},
{"name":"Deliverable 35", "types": ["carrier"]},
{"name":"Deliverable 36", "types": ["carrier"]},
{"name":"Outcome 10", "types": ["carrier"]},
{"name":"Deliverable 37", "types": ["carrier"]},
{"name":"Deliverable 38", "types": ["carrier"]},
{"name":"Deliverable 39", "types": ["carrier"]}
],
"links":[
{"source":123,"target":22,"value":1},
{"source":111,"target":22,"value":1},
{"source":98,"target":16,"value":1},
{"source":128,"target":22,"value":1},
{"source":76,"target":36,"value":1},
{"source":58,"target":32,"value":1},
{"source":84,"target":117,"value":1},
{"source":115,"target":85,"value":1},
{"source":115,"target":120,"value":1},
{"source":55,"target":117,"value":1},
{"source":91,"target":87,"value":1},
{"source":55,"target":113,"value":1},
{"source":78,"target":113,"value":1},
{"source":63,"target":113,"value":1},
{"source":51,"target":113,"value":1},
{"source":64,"target":113,"value":1},
{"source":51,"target":92,"value":1},
{"source":64,"target":92,"value":1},
{"source":71,"target":92,"value":1},
{"source":91,"target":92,"value":1},
{"source":24,"target":138,"value":1},
{"source":18,"target":138,"value":1},
{"source":38,"target":138,"value":1},
{"source":45,"target":55,"value":1},
{"source":35,"target":55,"value":1},
{"source":23,"target":78,"value":1},
{"source":24,"target":78,"value":1},
{"source":47,"target":78,"value":1},
{"source":26,"target":67,"value":1},
{"source":37,"target":67,"value":1},
{"source":21,"target":67,"value":1},
{"source":47,"target":71,"value":1},
{"source":37,"target":71,"value":1},
{"source":44,"target":84,"value":1},
{"source":63,"target":87,"value":1},
{"source":78,"target":87,"value":1},
{"source":67,"target":113,"value":1},
{"source":71,"target":113,"value":1},
{"source":138,"target":113,"value":1},
{"source":138,"target":92,"value":1},
{"source":78,"target":92,"value":1},
{"source":21,"target":138,"value":1},
{"source":30,"target":63,"value":1},
{"source":24,"target":51,"value":1},
{"source":37,"target":51,"value":1},
{"source":21,"target":51,"value":1},
{"source":13,"target":64,"value":1},
{"source":23,"target":67,"value":1},
{"source":47,"target":84,"value":1},
{"source":38,"target":84,"value":1},
{"source":31,"target":91,"value":1},
{"source":31,"target":115,"value":1},
{"source":93,"target":30,"value":1},
{"source":58,"target":28,"value":1},
{"source":135,"target":42,"value":1},
{"source":141,"target":42,"value":1},
{"source":136,"target":42,"value":1},
{"source":106,"target":16,"value":1},
{"source":42,"target":105,"value":1},
{"source":27,"target":94,"value":1},
{"source":50,"target":93,"value":1},
{"source":17,"target":60,"value":1},
{"source":34,"target":131,"value":1},
{"source":39,"target":131,"value":1},
{"source":14,"target":61,"value":1},
{"source":12,"target":134,"value":1},
{"source":12,"target":68,"value":1},
{"source":49,"target":118,"value":1},
{"source":11,"target":70,"value":1},
{"source":19,"target":83,"value":1},
{"source":43,"target":107,"value":1},
{"source":68,"target":30,"value":1},
{"source":140,"target":42,"value":1},
{"source":132,"target":46,"value":1},
{"source":112,"target":27,"value":1},
{"source":112,"target":50,"value":1},
{"source":124,"target":34,"value":1},
{"source":46,"target":104,"value":1},
{"source":27,"target":69,"value":1},
{"source":12,"target":94,"value":1},
{"source":27,"target":68,"value":1},
{"source":12,"target":69,"value":1},
{"source":12,"target":66,"value":1},
{"source":34,"target":75,"value":1},
{"source":12,"target":95,"value":1},
{"source":48,"target":75,"value":1},
{"source":28,"target":75,"value":1},
{"source":28,"target":89,"value":1},
{"source":32,"target":89,"value":1},
{"source":28,"target":88,"value":1},
{"source":125,"target":88,"value":1},
{"source":22,"target":59,"value":1},
{"source":33,"target":88,"value":1},
{"source":29,"target":52,"value":1},
{"source":15,"target":52,"value":1},
{"source":22,"target":52,"value":1},
{"source":25,"target":119,"value":1},
{"source":16,"target":103,"value":1},
{"source":40,"target":116,"value":1},
{"source":50,"target":119,"value":1},
{"source":131,"target":23,"value":1},
{"source":60,"target":13,"value":1},
{"source":134,"target":45,"value":1},
{"source":66,"target":30,"value":1},
{"source":131,"target":18,"value":1},
{"source":105,"target":18,"value":1},
{"source":83,"target":18,"value":1},
{"source":103,"target":35,"value":1},
{"source":65,"target":35,"value":1},
{"source":62,"target":35,"value":1},
{"source":95,"target":31,"value":1},
{"source":101,"target":21,"value":1},
{"source":53,"target":21,"value":1},
{"source":104,"target":47,"value":1},
{"source":69,"target":47,"value":1},
{"source":61,"target":37,"value":1},
{"source":69,"target":31,"value":1},
{"source":68,"target":31,"value":1},
{"source":59,"target":18,"value":1},
{"source":108,"target":34,"value":1},
{"source":56,"target":46,"value":1},
{"source":97,"target":46,"value":1},
{"source":132,"target":39,"value":1},
{"source":108,"target":39,"value":1},
{"source":80,"target":39,"value":1},
{"source":57,"target":43,"value":1},
{"source":97,"target":12,"value":1},
{"source":56,"target":12,"value":1},
{"source":81,"target":48,"value":1},
{"source":56,"target":48,"value":1},
{"source":97,"target":48,"value":1},
{"source":74,"target":41,"value":1},
{"source":110,"target":40,"value":1},
{"source":98,"target":36,"value":1},
{"source":139,"target":36,"value":1},
{"source":127,"target":33,"value":1},
{"source":72,"target":29,"value":1},
{"source":28,"target":53,"value":1},
{"source":10,"target":101,"value":1},
{"source":41,"target":62,"value":1},
{"source":36,"target":62,"value":1},
{"source":36,"target":65,"value":1},
{"source":20,"target":88,"value":1},
{"source":61,"target":23,"value":1},
{"source":60,"target":45,"value":1},
{"source":131,"target":45,"value":1},
{"source":94,"target":45,"value":1},
{"source":66,"target":26,"value":1},
{"source":68,"target":26,"value":1},
{"source":107,"target":44,"value":1},
{"source":116,"target":45,"value":1},
{"source":75,"target":23,"value":1},
{"source":101,"target":30,"value":1},
{"source":119,"target":30,"value":1},
{"source":88,"target":23,"value":1},
{"source":89,"target":21,"value":1},
{"source":75,"target":21,"value":1},
{"source":75,"target":37,"value":1},
{"source":70,"target":38,"value":1},
{"source":118,"target":38,"value":1},
{"source":68,"target":47,"value":1},
{"source":88,"target":18,"value":1},
{"source":52,"target":18,"value":1},
{"source":109,"target":26,"value":1},
{"source":102,"target":30,"value":1},
{"source":81,"target":30,"value":1},
{"source":73,"target":49,"value":1},
{"source":137,"target":11,"value":1},
{"source":129,"target":34,"value":1},
{"source":133,"target":14,"value":1},
{"source":126,"target":17,"value":1},
{"source":130,"target":39,"value":1},
{"source":96,"target":12,"value":1},
{"source":141,"target":19,"value":1},
{"source":122,"target":48,"value":1},
{"source":96,"target":48,"value":1},
{"source":128,"target":20,"value":1},
{"source":121,"target":10,"value":1},
{"source":54,"target":29,"value":1},
{"source":54,"target":15,"value":1},
{"source":72,"target":15,"value":1},
{"source":111,"target":125,"value":1},
{"source":82,"target":16,"value":1},
{"source":100,"target":25,"value":1},
{"source":138,"target":114,"value":1},
{"source":55,"target":114,"value":1},
{"source":63,"target":114,"value":1},
{"source":51,"target":114,"value":1},
{"source":64,"target":114,"value":1},
{"source":67,"target":114,"value":1},
{"source":78,"target":114,"value":1},
{"source":2,"target":102,"value":1},
{"source":78,"target":86,"value":1},
{"source":63,"target":86,"value":1},
{"source":91,"target":86,"value":1},
{"source":1,"target":140,"value":1},
{"source":3,"target":124,"value":1},
{"source":3,"target":132,"value":1},
{"source":3,"target":108,"value":1},
{"source":3,"target":80,"value":1},
{"source":4,"target":112,"value":1},
{"source":4,"target":81,"value":1},
{"source":4,"target":56,"value":1},
{"source":4,"target":97,"value":1},
{"source":6,"target":137,"value":1},
{"source":6,"target":57,"value":1},
{"source":0,"target":106,"value":1},
{"source":5,"target":130,"value":1},
{"source":5,"target":129,"value":1},
{"source":5,"target":126,"value":1},
{"source":5,"target":98,"value":1},
{"source":5,"target":128,"value":1},
{"source":5,"target":110,"value":1},
{"source":5,"target":74,"value":1},
{"source":7,"target":127,"value":1},
{"source":8,"target":121,"value":1},
{"source":99,"target":100,"value":1},
{"source":79,"target":109,"value":1},
{"source":77,"target":82,"value":1},
{"source":77,"target":76,"value":1},
{"source":138,"target":90,"value":1},
{"source":78,"target":90,"value":1},
{"source":51,"target":90,"value":1},
{"source":64,"target":90,"value":1},
{"source":71,"target":90,"value":1},
{"source":91,"target":90,"value":1},
{"source":4,"target":58,"value":1},
{"source":6,"target":73,"value":1},
{"source":5,"target":133,"value":1},
{"source":5,"target":135,"value":1},
{"source":5,"target":141,"value":1},
{"source":5,"target":136,"value":1},
{"source":5,"target":122,"value":1},
{"source":5,"target":96,"value":1},
{"source":9,"target":139,"value":1},
{"source":9,"target":72,"value":1},
{"source":9,"target":111,"value":1},
{"source":9,"target":54,"value":1},
{"source":88,"target":24,"value":1},
{"source":75,"target":24,"value":1},
{"source":9,"target":123,"value":1}
]}
<!DOCTYPE html>
<meta charset="utf-8">
<title>Traceability</title>
<link href="sankey_style.css" rel="stylesheet">
<style media="screen" type="text/css">
.sankey-graph {
height: 1000px;
}
.sankey-graph .node rect {
shape-rendering: crispEdges;
pointer-events:all;
fill: none;
stroke: rgb(160, 160,160);
stroke-width: 2px;
}
.sankey-graph .nodeInfo {
pointer-events:none;
background-color: white;
font: 9px;
font-family: Calibri, sans-serif;
}
.sankey-graph #foreignObject {
pointer-events:none;
}
.sankey-graph .node text {
pointer-events: none;
text-shadow: 0 1px 0 #fff;
}
.sankey-graph .bar rect {
stroke: none;
}
.sankey-graph .link {
fill: none;
stroke: #000;
stroke-opacity: .2;
}
.sankey-graph div.nodeInfo {
text-align: center;
line-height: 110%;
vertical-align: middle;
display: table-cell;
}
</style>
<body>
<header>
</header>
<p id="chart" class="sankey-graph">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="sankey-bm.js"></script>
<script>
var margin = {top: 1, right: 0, bottom: 6, left: 0},
width = 1895, //also change CSS file to change Width
height = 1030 - margin.top - margin.bottom,
node_Width = 200;
node_Stroke = 2;
var formatNumber = d3.format(",.0f"),
format = function(d) { return formatNumber(d) + " TODO TODO TODO"; },
color = d3.scale.category20();
var svg = d3.select("#chart").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 + ")");
var sankey = d3.sankey()
.nodeWidth(node_Width) //Node width
.nodePadding(8) //spacing between nodes
.size([width, height]);
var path = sankey.link();
d3.json("benefits.json", function(graph) {
sankey.nodes(graph.nodes)
.links(graph.links)
.layout(101); //iterations for layout algorithm
var link = svg.append("g").selectAll(".link")
.data(graph.links)
.enter().append("path")
.attr("class", "link")
.attr("d", path)
.style("stroke-width", function(d) { return Math.max(1, d.dy); })
.sort(function(a, b) { return b.dy - a.dy; });
//.on("mouseover", highlightLink)
//.on("mouseout", unhighlightLink);
var node = svg.append("g").selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.call(d3.behavior.drag()
.origin(function(d) { return d; })
.on("dragstart", function() { this.parentNode.appendChild(this); })
.on("drag", dragmove));
node.append("foreignObject")
.attr("y", 0)
.attr("height", function(d) { return d.dy; })
.attr("width", sankey.nodeWidth())
.attr("transform", null)
.append("xhtml:div")
.attr("class", "nodeInfo")
.html(function(d) {
var types = d['types'];
var properName = '';
var typeNames = '';
var htmlString = d.name;
return htmlString;
})
.style("height", function(d) { return d.dy + "px"; })
.style("width", sankey.nodeWidth() + "px");
//Add node clicking
//Adding color bar for Node TYPE:
var typeDict = {
'cdn': '#00778c',
'analytics': '#ff9b03',
'security': '#58008c',
'advertising': '#506da5',
'api': '#11462c',
'social': '#ef5f00',
'tracking': '#58008c',
'datacenter': '#11462c',
'hosting': '#a5c1b3',
'cloud': '#11462c'
};
node.append("g")
.attr("class", "bar")
.call(addTypeBars);
function addTypeBars(sel) {
sel.each(function(d) {
var bars = d3.select(this);
var types = d.types;
if (types) {
var colorbars = [];
//Loop through to pull the colors this set of types does have
for(var j = 0; j < types.length; j++) {
if (typeDict[types[j]]) {
colorbars.push(types[j]);
}
}
for (var i = 0; i < colorbars.length; i++) {
bars.append("rect")
.attr("x", i * (sankey.nodeWidth()/colorbars.length) + 1)
.attr("y", 1)
.attr("height", 2.5)
.attr("width", function (d) {
if (i == colorbars.length - 1)
return (sankey.nodeWidth()/colorbars.length) - 2;
else
return (sankey.nodeWidth()/colorbars.length);
})
.style("fill", function(d) {
return typeDict[colorbars[i]]
});
}
}
})
}//end of addTypeBars
node.append("rect")
.attr("height", function(d) { return d.dy; })
.attr("width", sankey.nodeWidth())
.on("mouseover", showConnections)
.on("mouseout", hideConnections);
d3.selectAll(".node").on("click", handleNodeClick);
function showConnections(d) {
//this highlights the node
d3.select(this).style("stroke","black")
.style("stroke-opacity", 0.6);
//If this link is connected to the node
link.style("stroke-opacity", function(l) {
if (l.source.name == d.name || l.target.name == d.name) {
return 0.6;
}
else
return 0.2;
});
}
function hideConnections(d) {
//This unhighlights the node
d3.select(this).style("stroke",rgbToHex(160, 160,160))
.style("stroke-opacity", 0.6);
link.style("stroke-opacity", 0.2);
}
function highlightLink(d) {
//changes styling on selected link
d3.select(this).style("stroke","red")
.style("stroke-opacity", 0.6);
}
function unhighlightLink(d) {
//changes styling on selected link
d3.select(this).style("stroke","black")
.style("stroke-opacity", 0.2);
}
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
function rgbToHex(r, g, b) {
var rgb = b | (g << 8) | (r << 16);
return (0x1000000 | rgb).toString(16).substring(1);
}
//Recursive highlighting of nodes
//---------------------------
var leftDelay;
var rightDelay;
var delayIncrement
var durationTime;
delayIncrement = 75;
durationTime = 400; //millisecs
function handleNodeClick(d) {
console.log(delayIncrement)
leftDelay = 0;
rightDelay = 0;
//Reset colour of nodes to black
d3.selectAll(".node")
.transition()
.duration(250)
.style("color","black");
d3.selectAll(".link")
.transition()
.duration(250)
.style("stroke","black");
//Highlight clicked node
d3.selectAll(".node").filter(function(d2,i) {
return d.name == d2.name;
})
.style("color","green")
iterateLinkedLinksRight(d); //Recurse source direction
iterateLinkedLinksLeft(d); //Recurse target direction
}
function iterateLinkedLinksRight(pStartNode) {
rightDelay = rightDelay + delayIncrement;
//Select links that have a given source name
d3.selectAll("path.link").filter(function(pLinkedLink,i) {
return pLinkedLink.source.name == pStartNode.name;
})
.transition()
.duration(durationTime)
.delay(rightDelay)
.style("stroke","red")
.style("stroke-opacity", 0.6)
.each(iterateLinkedNodesRight);
}
function iterateLinkedNodesRight(pStartLink) {
//Select nodes that have a given source name
d3.selectAll(".node").filter(function(pLinkedNode,i) {
return pLinkedNode.name == pStartLink.target.name;
})
.transition()
.duration(durationTime)
.delay(rightDelay)
.style("color","red")
.each(iterateLinkedLinksRight);
rightDelay = 0;
}
function iterateLinkedLinksLeft(pStartNode) {
leftDelay = leftDelay + delayIncrement;
//Select links that have a given source name
d3.selectAll("path.link").filter(function(pLinkedLink,i) {
return pLinkedLink.target.name == pStartNode.name;
})
.transition()
.duration(durationTime)
.delay(leftDelay)
.style("stroke","red")
.style("stroke-opacity", 0.6)
.each(iterateLinkedNodesLeft);
}
function iterateLinkedNodesLeft(pStartLink) {
//Select nodes that have a given source name
d3.selectAll(".node").filter(function(pLinkedNode,i) {
return pLinkedNode.name == pStartLink.source.name;
})
.transition()
.duration(durationTime)
.delay(leftDelay)
.style("color","red")
.each(iterateLinkedLinksLeft);
leftDelay = 0;
}
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>
d3.sankey = function() {
var sankey = {},
nodeWidth = 24,
nodePadding = 8,
size = [1, 1],
nodes = [],
links = []
//heightConstant = 80;
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;
y0 = d.source.y + d.source.dy / 2,
y1 = d.target.y + d.target.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) {
nextNodes.push(link.target);
});
});
remainingNodes = nextNodes;
++x;
}
//
moveSinksRight(x);
scaleNodeBreadths((width - 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;
node.dy = setNodeHeight(node);
});
});
links.forEach(function(link) {
link.dy = link.value * ky;
});
}
function setNodeHeight(node) {
var actualHeight = (node.name.length / node.dx);
var setHeight;
if (actualHeight < 0.2249) { setHeight = 13; }
else if (actualHeight < 0.41) { setHeight = 26; }
else if (actualHeight < 0.551) { setHeight = 39; }
else if (actualHeight < 0.71) { setHeight = 52; }
return setHeight;
}
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;
};
html {
min-width: 1040px;
}
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;
position: relative;
width: 1895px;
}
h1 {
font-size: 32px;
font-weight: 250;
letter-spacing: -2px;
margin: .3em 0 .1em 0;
}
h2 {
margin-top: 2em;
}
h1, h2 {
text-rendering: optimizeLegibility;
}
h2 a {
color: #ccc;
left: -20px;
position: absolute;
width: 740px;
}
footer {
font-size: small;
margin-top: 8em;
}
header aside {
margin-top: 88px;
}
header aside,
footer aside {
color: #636363;
text-align: right;
}
aside {
font-size: small;
right: 0;
position: absolute;
width: 180px;
}
.attribution {
font-size: small;
margin-bottom: 2em;
}
body > p, li > p {
line-height: 1.5em;
}
body > p {
width: 720px;
}
body > blockquote {
width: 640px;
}
li {
width: 680px;
}
a {
color: steelblue;
}
a:not(:hover) {
text-decoration: none;
}
pre, code, textarea {
font-family: "Menlo", monospace;
}
code {
line-height: 1em;
}
textarea {
font-size: 100%;
}
body > pre {
border-left: solid 2px #ccc;
padding-left: 18px;
}
.html .value,
.javascript .string,
.javascript .regexp {
color: #756bb1;
}
.html .tag,
.css .tag,
.javascript .keyword {
color: #3182bd;
}
.comment {
color: #636363;
}
.html .doctype,
.javascript .number {
color: #31a354;
}
.html .attribute,
.css .attribute,
.javascript .class,
.javascript .special {
color: #e6550d;
}
svg {
font: 11px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
sup, sub {
line-height: 0;
}
q:before,
blockquote:before {
content: "“";
}
q:after,
blockquote:after {
content: "”";
}
blockquote:before {
position: absolute;
left: 2em;
}
blockquote:after {
position: absolute;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment