D3.js Stacked Bar Chart, from vertical to horizontal -
i make vertical bar chart (http://bl.ocks.org/mbostock/3886208) horizontal bar chart.
thanks help!
code examples nice.
it's matter of reversing domains, axis , rect calculations:
var y = d3.scale.ordinal() .rangeroundbands([height, 0], .1); // y becomes ordinal var x = d3.scale.linear() .rangeround([0, width]); // x becomes linear // change state group positioned in y instead of x var state = svg.selectall(".state") .data(data) .enter().append("g") .attr("class", "g") .attr("transform", function(d) { return "translate(0," + y(d.state) + ")"; }); // rect calculations become state.selectall("rect") .data(function(d) { return d.ages; }) .enter().append("rect") .attr("height", y.rangeband()) // height in rangeband .attr("x", function(d) { return x(d.y0); }) // horizontal position in stack .attr("width", function(d) { return x(d.y1) - x(d.y0); }) // horizontal "height" of bar .style("fill", function(d) { return color(d.name); });
here's full working example.
Comments
Post a Comment