Created
March 21, 2012 15:29
-
-
Save patrickberkeley/2148469 to your computer and use it in GitHub Desktop.
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
Type | Count | Total | |
---|---|---|---|
A | 604 | 300.864 | |
OP | 438 | 223.673 | |
IAP | 448 | 229.404 | |
AAP | 366 | 207.206 | |
PP | 0 | 0.000 | |
BP | 27 | 0.760 | |
PP | 565 | 298.920 | |
AMP | 12 | 1.184 |
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> | |
<html> | |
<head> | |
<title>stacked test</title> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?1.29.1"></script> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js?1.29.1"></script> | |
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.csv.js?1.29.1"></script> | |
<style type="text/css"> | |
svg { | |
width: 960px; | |
height: 500px; | |
border: solid 1px #ccc; | |
font: 10px sans-serif; | |
shape-rendering: crispEdges; | |
} | |
</style> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
var w = 960, | |
h = 500, | |
p = [20, 50, 30], | |
x = d3.scale.ordinal().rangeRoundBands([0, w - p[1] - p[2]]), | |
y = d3.scale.linear().range([0, h - p[0] - p[2]]), | |
z = d3.scale.ordinal().range(["lightblue", "darkgray"]); | |
var svg = d3.select("body").append("svg:svg") | |
.attr("width", w) | |
.attr("height", h) | |
.append("svg:g") | |
.attr("transform", "translate(" + p[2] + "," + (h - p[2]) + ")"); | |
d3.csv("data.csv", function(data) { | |
// Transpose the data into layers by metric. | |
var metrics = d3.layout.stack()(["Count", "Total"].map(function(metric) { | |
return data.map(function(d) { | |
return {x: d.Type, y: +d[metric]}; | |
}); | |
})); | |
// Compute the x-domain (by type) and y-domain (by top). | |
x.domain(metrics[0].map(function(d) { return d.x; })); | |
y.domain([0, d3.max(metrics[metrics.length - 1], function(d) { return d.y0 + d.y; })]); | |
// Add a group for each metric. | |
var metric = svg.selectAll("g.metric") | |
.data(metrics) | |
.enter().append("svg:g") | |
.attr("class", "metric") | |
.style("fill", function(d, i) { return z(i); }) | |
.style("stroke", function(d, i) { return d3.rgb(z(i)).darker(); }); | |
// Add a rect for each type. | |
var rect = metric.selectAll("rect") | |
.data(Object) | |
.enter().append("svg:rect") | |
.attr("x", function(d) { return x(d.x); }) | |
.attr("y", function(d) { return -y(d.y0) - y(d.y); }) | |
.attr("height", function(d) { return y(d.y); }) | |
.attr("width", x.rangeBand()); | |
// Add a label per type. | |
var label = svg.selectAll("text") | |
.data(x.domain()) | |
.enter().append("svg:text") | |
.attr("x", function(d) { return x(d) + x.rangeBand() / 2; }) | |
.attr("y", 6) | |
.attr("text-anchor", "middle") | |
.attr("dy", ".71em") | |
.text(String); | |
// Add y-axis rules. | |
var rule = svg.selectAll("g.rule") | |
.data(y.ticks(5)) | |
.enter().append("svg:g") | |
.attr("class", "rule") | |
.attr("transform", function(d) { return "translate(0," + -y(d) + ")"; }); | |
rule.append("svg:line") | |
.attr("x2", w - p[1] - p[2]) | |
.style("stroke", function(d) { return d ? "#fff" : "#000"; }) | |
.style("stroke-opacity", function(d) { return d ? .7 : null; }); | |
rule.append("svg:text") | |
.attr("x", w - p[1] - p[2] + 6) | |
.attr("dy", ".35em") | |
.text(d3.format(",d")); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment