Created
October 19, 2013 15:58
-
-
Save jleonard99/7057780 to your computer and use it in GitHub Desktop.
sample HTML file using d3 library.
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> | |
<meta charset = 'utf-8'> | |
<html> | |
<head> | |
<link rel='stylesheet' href='http://timelyportfolio.github.io/rCharts_d3_sankey/libraries/widgets/d3_sankey/css/sankey.css'> | |
<script src='http://timelyportfolio.github.io/rCharts_d3_sankey/libraries/widgets/d3_sankey/js/d3.v3.js' type='text/javascript'></script> | |
<script src='http://timelyportfolio.github.io/rCharts_d3_sankey/libraries/widgets/d3_sankey/js/sankey.js' type='text/javascript'></script> | |
<style> | |
.rChart { | |
display: block; | |
margin-left: auto; | |
margin-right: auto; | |
width: 512px; | |
height: 400px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id='sankey1' class='rChart d3_sankey'></div> | |
<!--Attribution: | |
Mike Bostock https://github.com/d3/d3-plugins/tree/master/sankey | |
Mike Bostock http://bost.ocks.org/mike/sankey/ | |
--> | |
<script> | |
(function(){ | |
var params = { | |
"dom": "sankey1", | |
"width": 512, | |
"height": 400, | |
"data": { | |
"source": [ "DSS ($155M 9%)", "GENX-ICR ($143M 9%)", "GENX-Other ($48M 3%)", "GENX-State Approp ($201M 12%)", "GENX-Tuition ($283M 17%)", "GTF ($98M 6%)", "DSS ($155M 9%)", "DSS ($155M 9%)", "DSS ($155M 9%)", "DSS ($155M 9%)", "GENX-ICR ($143M 9%)", "GENX-Other ($48M 3%)", "GENX-Tuition ($283M 17%)", "GTF ($98M 6%)", "DSS ($155M 9%)", "GENX-Other ($48M 3%)", "GENX-State Approp ($201M 12%)", "GENX-Tuition ($283M 17%)", "GTF ($98M 6%)", "Sponsored ($482M 29%)", "GTAA ($16M 1%)", "Agency Funds ($158M 10%)", "Endowments ($2M 0%)", "DSS ($155M 9%)", "DSS ($155M 9%)", "GENX-ICR ($143M 9%)", "GENX-Other ($48M 3%)", "GENX-ResCons ($6M 0%)", "GENX-State Approp ($201M 12%)", "GENX-Tuition ($283M 17%)", "GTF ($98M 6%)", "Plant Funds ($30M 2%)", "Sponsored ($482M 29%)", "Student activities ($20M 1%)", "DSS ($155M 9%)", "GENX-State Approp ($201M 12%)", "GTF ($98M 6%)", "GTRC ($15M 1%)", "Sponsored ($482M 29%)", "DSS ($155M 9%)", "GENX-ICR ($143M 9%)", "GENX-Other ($48M 3%)", "GENX-State Approp ($201M 12%)", "GENX-Tuition ($283M 17%)", "GTF ($98M 6%)", "GTRC ($15M 1%)", "Sponsored ($482M 29%)", "DSS ($155M 9%)", "Agency Funds ($158M 10%)", "GTF ($98M 6%)", "Sponsored ($482M 29%)", "GENX-Tuition ($283M 17%)", "GTF ($98M 6%)", "Student activities ($20M 1%)", "DSS ($155M 9%)" ], | |
"target": [ "Academic Support ($53M 3%)", "Academic Support ($53M 3%)", "Academic Support ($53M 3%)", "Academic Support ($53M 3%)", "Academic Support ($53M 3%)", "Academic Support ($53M 3%)", "Food Services ($23M 1%)", "Health Services ($7M 0%)", "Housing ($45M 3%)", "Institute Support ($66M 4%)", "Institute Support ($66M 4%)", "Institute Support ($66M 4%)", "Institute Support ($66M 4%)", "Institute Support ($66M 4%)", "Instruction ($239M 14%)", "Instruction ($239M 14%)", "Instruction ($239M 14%)", "Instruction ($239M 14%)", "Instruction ($239M 14%)", "Instruction ($239M 14%)", "Intercollegiate Athletics ($16M 1%)", "Other codes ($111M 7%)", "Other codes ($111M 7%)", "Other Service Units ($5M 0%)", "Plant ($231M 14%)", "Plant ($231M 14%)", "Plant ($231M 14%)", "Plant ($231M 14%)", "Plant ($231M 14%)", "Plant ($231M 14%)", "Plant ($231M 14%)", "Plant ($231M 14%)", "Plant ($231M 14%)", "Plant ($231M 14%)", "Public Service ($36M 2%)", "Public Service ($36M 2%)", "Public Service ($36M 2%)", "Public Service ($36M 2%)", "Public Service ($36M 2%)", "Research ($671M 40%)", "Research ($671M 40%)", "Research ($671M 40%)", "Research ($671M 40%)", "Research ($671M 40%)", "Research ($671M 40%)", "Research ($671M 40%)", "Research ($671M 40%)", "Stores and Shops ($14M 1%)", "Student aid ($92M 6%)", "Student aid ($92M 6%)", "Student aid ($92M 6%)", "Student Services ($35M 2%)", "Student Services ($35M 2%)", "Student Services ($35M 2%)", "Transportation and Parking ($14M 1%)" ], | |
"value": [ 1.041, 1.073, 1.13, 5.539, 41.977, 2.3, 22.636, 7.066, 45.419, 2.497, 22.333, 23.261, 42.023, 2.786, 18.765, 7.695, 106.17, 86.878, 13.082, 6.257, 16.272, 109.16, 1.664, 5.275, 3.73, 49.107, 13.944, 5.897, 17.825, 64.19, 37.783, 30.027, 6.48, 1.574, 2.5, 8.274, 3.05, 1.182, 19.595, 17.826, 70.608, 1.643, 90.404, 33.911, 17.531, 13.752, 425.62, 13.812, 49.079, 19.944, 23.106, 13.977, 1.941, 18.396, 13.869 ] | |
}, | |
"nodeWidth": 25, | |
"nodePadding": 8, | |
"layout": 32, | |
"id": "sankey1" | |
}; | |
params.units ? units = " " + params.units : units = ""; | |
//hard code these now but eventually make available | |
var formatNumber = d3.format("0,.0f"), // zero decimal places | |
format = function(d) { return formatNumber(d) + units; }, | |
color = d3.scale.category20(); | |
if(params.labelFormat){ | |
formatNumber = d3.format(".2%"); | |
} | |
var svg = d3.select('#' + params.id).append("svg") | |
.attr("width", params.width) | |
.attr("height", params.height); | |
var sankey = d3.sankey() | |
.nodeWidth(params.nodeWidth) | |
.nodePadding(params.nodePadding) | |
.layout(params.layout) | |
.size([params.width,params.height]); | |
var path = sankey.link(); | |
var data = params.data, | |
links = [], | |
nodes = []; | |
//get all source and target into nodes | |
//will reduce to unique in the next step | |
//also get links in object form | |
data.source.forEach(function (d, i) { | |
nodes.push({ "name": data.source[i] }); | |
nodes.push({ "name": data.target[i] }); | |
links.push({ "source": data.source[i], "target": data.target[i], "value": +data.value[i] }); | |
}); | |
//now get nodes based on links data | |
//thanks Mike Bostock https://groups.google.com/d/msg/d3-js/pl297cFtIQk/Eso4q_eBu1IJ | |
//this handy little function returns only the distinct / unique nodes | |
nodes = d3.keys(d3.nest() | |
.key(function (d) { return d.name; }) | |
.map(nodes)); | |
//it appears d3 with force layout wants a numeric source and target | |
//so loop through each link replacing the text with its index from node | |
links.forEach(function (d, i) { | |
links[i].source = nodes.indexOf(links[i].source); | |
links[i].target = nodes.indexOf(links[i].target); | |
}); | |
//now loop through each nodes to make nodes an array of objects rather than an array of strings | |
nodes.forEach(function (d, i) { | |
nodes[i] = { "name": d }; | |
}); | |
sankey | |
.nodes(nodes) | |
.links(links) | |
.layout(params.layout); | |
var link = svg.append("g").selectAll(".link") | |
.data(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; }); | |
link.append("title") | |
.text(function (d) { return d.source.name + " → " + d.target.name + "\n" + format(d.value); }); | |
var node = svg.append("g").selectAll(".node") | |
.data(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("rect") | |
.attr("height", function (d) { return d.dy; }) | |
.attr("width", sankey.nodeWidth()) | |
.style("fill", function (d) { return d.color = color(d.name.replace(/ .*/, "")); }) | |
.style("stroke", function (d) { return d3.rgb(d.color).darker(2); }) | |
.append("title") | |
.text(function (d) { return d.name + "\n" + format(d.value); }); | |
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(function (d) { return d.name; }) | |
.filter(function (d) { return d.x < params.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 = Math.max(0, Math.min(params.width - d.dx, d3.event.x)) | |
) + "," + ( | |
d.y = Math.max(0, Math.min(params.height - d.dy, d3.event.y)) | |
) + ")"); | |
sankey.relayout(); | |
link.attr("d", path); | |
} | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment