Built with blockbuilder.org
Last active
September 15, 2017 12:50
-
-
Save RobinL/ff5b2ac8cdf977bfa8cc6f544ba07150 to your computer and use it in GitHub Desktop.
sankey experiment 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://unpkg.com/d3-sankey"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<div id="chart"></div> | |
<script> | |
// Feel free to change or delete any of the code you see in this editor! | |
var margin = {top: 10, right: 10, bottom: 10, left: 10}, | |
width = 700 - margin.left - margin.right, | |
height = 300 - margin.top - margin.bottom; | |
var nodes = [ | |
{"name": "Source1"}, | |
{"name": "Source2"}, | |
{"name": "Source3"}, | |
{"name": "Server1"}, | |
{"name": "Server2"}, | |
{"name": "Analyst1"}, | |
{"name": "Analyst2"}, | |
]; | |
var links = [ | |
{"source": "Source1", "target": "Server1", "value": 10}, | |
{"source": "Source2", "target": "Server1", "value" : 10}, | |
{"source": "Source3", "target": "Server1", "value" : 10}, | |
{"source": "Source1", "target": "Server2", "value" : 10}, | |
{"source": "Source2", "target": "Server2", "value" : 10}, | |
{"source": "Server1", "target": "Analyst1", "value" : 10}, | |
{"source": "Server2", "target": "Analyst2", "value" : 10}, | |
]; | |
var graph = {"nodes": nodes, "links" : links} | |
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(36) | |
.nodePadding(40) | |
.size([width, height]) | |
.nodeId(function(d) {return d.name}) | |
var link = svg.append("g") | |
.attr("class", "links") | |
.attr("fill", "none") | |
.attr("stroke", "#000") | |
.attr("stroke-opacity", 0.2) | |
.selectAll("path"); | |
var node = svg.append("g") | |
.attr("class", "nodes") | |
.attr("font-family", "sans-serif") | |
.attr("font-size", 10) | |
.selectAll("g"); | |
sankey(graph) | |
link = link | |
.data(graph.links) | |
.enter().append("path") | |
.attr("d", d3.sankeyLinkHorizontal()) | |
.attr("stroke-width", function(d) { return Math.max(1, d.width); }); | |
link.append("title") | |
.text(function(d) { return d.source.name + " → " + d.target.name + "\n" + d.value; }); | |
node = node | |
.data(graph.nodes) | |
.enter().append("g"); | |
node.append("rect") | |
.attr("x", function(d) { return d.x0; }) | |
.attr("y", function(d) { return d.y0; }) | |
.attr("height", function(d) { return d.y1 - d.y0; }) | |
.attr("width", function(d) { return d.x1 - d.x0; }) | |
.attr("fill", function(d) { return color(d.name.replace(/ .*/, "")); }) | |
.attr("stroke", "#000"); | |
node.append("text") | |
.attr("x", function(d) { return d.x0 - 6; }) | |
.attr("y", function(d) { return (d.y1 + d.y0) / 2; }) | |
.attr("dy", "0.35em") | |
.attr("text-anchor", "end") | |
.text(function(d) { return d.name; }) | |
.filter(function(d) { return d.x0 < width / 2; }) | |
.attr("x", function(d) { return d.x1 + 6; }) | |
.attr("text-anchor", "start"); | |
node.append("title") | |
.text(function(d) { return d.name + "\n" + format(d.value); }); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment