Last active
August 29, 2015 14:02
-
-
Save erizhang/7ef7b43d8fddbdd1eafe to your computer and use it in GitHub Desktop.
d3js StackedBar
This file contains hidden or 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
| build | waiting | duration | |
|---|---|---|---|
| 001 | 3 | 5 | |
| 002 | 6 | 6 | |
| 003 | 13 | 12 | |
| 005 | 7 | 2 | |
| 006 | 5 | 10 |
This file contains hidden or 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"> | |
| <style> | |
| body { | |
| font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; | |
| margin: auto; | |
| position: relative; | |
| width: 960px; | |
| } | |
| text { | |
| font: 10px sans-serif; | |
| } | |
| .axis path, | |
| .axis line { | |
| fill: none; | |
| stroke: #000; | |
| shape-rendering: crispEdges; | |
| } | |
| form { | |
| position: absolute; | |
| right: 10px; | |
| top: 10px; | |
| } | |
| </style> | |
| <script src="http://d3js.org/d3.v3.min.js"></script> | |
| <script> | |
| d3.csv("build.csv", function(data) { | |
| var m = 30; | |
| draw(remap(m, data), m); | |
| }); | |
| function remap(m, data) { | |
| var waiting = []; | |
| var duration = []; | |
| var remapped = []; | |
| for (i in data) { | |
| if (i >= m) | |
| break; | |
| waiting.push({ | |
| "x": i, | |
| "y": parseInt(data[i].waiting) | |
| }); | |
| duration.push({ | |
| "x": i, | |
| "y": parseInt(data[i].duration) | |
| }); | |
| } | |
| remapped[0] = waiting; | |
| remapped[1] = duration; | |
| return remapped; | |
| } | |
| function draw(data, m) { | |
| var n = 2; // number of layers | |
| var stack = d3.layout.stack(); | |
| layers = stack(data), | |
| yStackMax = d3.max(layers, function(layer) { | |
| return d3.max(layer, function(d) { | |
| var b = d.y0 + d.y; | |
| console.log(b) | |
| return b; | |
| }); | |
| }); | |
| var margin = { | |
| top: 40, | |
| right: 10, | |
| bottom: 20, | |
| left: 10 | |
| }, | |
| width = 960 - margin.left - margin.right, | |
| height = 200 - margin.top - margin.bottom; | |
| var x = d3.scale.ordinal() | |
| .domain(d3.range(m)) | |
| .rangeRoundBands([0, width], .08); | |
| var y = d3.scale.linear() | |
| .domain([0, yStackMax]) | |
| .range([height, 0]); | |
| var scale = d3.scale.linear() | |
| .domain([0, yStackMax]) | |
| .range([0, height]); | |
| var color = d3.scale.linear() | |
| .domain([0, n - 1]) | |
| .range(["#aad", "#556"]); | |
| var xAxis = d3.svg.axis() | |
| .scale(x) | |
| .tickSize(0) | |
| .tickPadding(6) | |
| .orient("bottom"); | |
| 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 + ")"); | |
| var layer = svg.selectAll(".layer") | |
| .data(layers) | |
| .enter().append("g") | |
| .attr("class", "layer") | |
| .style("fill", function(d, i) { | |
| return color(i); | |
| }); | |
| var rect = layer.selectAll("rect") | |
| .data(function(d) { | |
| return d; | |
| }) | |
| .enter().append("rect") | |
| .attr("x", function(d) { | |
| return x(d.x); | |
| }) | |
| .attr("y", height) | |
| .attr("width", x.rangeBand()) | |
| .attr("height", 0); | |
| rect.transition() | |
| .delay(function(d, i) { | |
| return i * 20; | |
| }) | |
| .attr("y", function(d) { | |
| return y(d.y0 + d.y); | |
| }) | |
| .attr("height", function(d) { | |
| return scale(d.y); | |
| }); | |
| // Draw X axis | |
| svg.append("g") | |
| .attr("class", "x axis") | |
| .attr("transform", "translate(0," + height + ")") | |
| .call(xAxis); | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment