有人可以帮我将下图的方向从垂直更改为水平吗?
在SVG中,如何绘制水平图形,我已经阅读了所有文档,但无法实现。
我是D#条形图的初学者,我想实现此水平堆积分组条形图。
Fidler链接如下: Click here
https://jsfiddle.net/yogeeshsj/dr78yzvp/8/
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x0 = d3.scale.ordinal()
.rangeRoundBands([0, width], 0.1);
var x1 = d3.scale.ordinal();
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
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 yBegin;
var innerColumns = {
"column1" : ["Under 5 Years","5 to 13 Years","14 to 17 Years"],
"column2" : ["18 to 24 Years"],
"column3" : ["25 to 44 Years"],
"column4" : ["45 to 64 Years", "65 Years and Over"]
}
var data = [
{
"State": "AL",
"Under 5 Years": 310504,
"5 to 13 Years": 552339,
"14 to 17 Years": 259034,
"18 to 24 Years": 450818,
"25 to 44 Years": 1231572,
"45 to 64 Years": 1215966,
"65 Years and Over": 641667
},
{
"State": "AK",
"Under 5 Years": 52083,
"5 to 13 Years": 85640,
"14 to 17 Years": 42153,
"18 to 24 Years": 74257,
"25 to 44 Years": 198724,
"45 to 64 Years": 183159,
"65 Years and Over": 50277
},
{
"State": "AZ",
"Under 5 Years": 515910,
"5 to 13 Years": 828669,
"14 to 17 Years": 362642,
"18 to 24 Years": 601943,
"25 to 44 Years": 1804762,
"45 to 64 Years": 1523681,
"65 Years and Over": 862573
},
{
"State": "AR",
"Under 5 Years": 202070,
"5 to 13 Years": 343207,
"14 to 17 Years": 157204,
"18 to 24 Years": 264160,
"25 to 44 Years": 754420,
"45 to 64 Years": 727124,
"65 Years and Over": 407205
},
{
"State": "CA",
"Under 5 Years": 2704659,
"5 to 13 Years": 4499890,
"14 to 17 Years": 2159981,
"18 to 24 Years": 3853788,
"25 to 44 Years": 10604510,
"45 to 64 Years": 8819342,
"65 Years and Over": 4114496
},
{
"State": "CO",
"Under 5 Years": 358280,
"5 to 13 Years": 587154,
"14 to 17 Years": 261701,
"18 to 24 Years": 466194,
"25 to 44 Years": 1464939,
"45 to 64 Years": 1290094,
"65 Years and Over": 511094
},
{
"State": "CT",
"Under 5 Years": 211637,
"5 to 13 Years": 403658,
"14 to 17 Years": 196918,
"18 to 24 Years": 325110,
"25 to 44 Years": 916955,
"45 to 64 Years": 968967,
"65 Years and Over": 478007
}
];
var columnHeaders = d3.keys(data[0]).filter(function(key) { return key !== "State"; });
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "State"; }));
data.forEach(function(d) {
var yColumn = new Array();
d.columnDetails = columnHeaders.map(function(name) {
for (ic in innerColumns) {
if($.inArray(name, innerColumns[ic]) >= 0){
if (!yColumn[ic]){
yColumn[ic] = 0;
}
yBegin = yColumn[ic];
yColumn[ic] += +d[name];
return {name: name, column: ic, yBegin: yBegin, yEnd: +d[name] + yBegin,};
}
}
});
d.total = d3.max(d.columnDetails, function(d) {
return d.yEnd;
});
});
x0.domain(data.map(function(d) { return d.State; }));
x1.domain(d3.keys(innerColumns)).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(data, function(d) {
return d.total;
})]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".7em")
.style("text-anchor", "end")
.text("");
var project_stackedbar = svg.selectAll(".project_stackedbar")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x0(d.State) + ",0)"; });
project_stackedbar.selectAll("rect")
.data(function(d) { return d.columnDetails; })
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("x", function(d) {
return x1(d.column);
})
.attr("y", function(d) {
return y(d.yEnd);
})
.attr("height", function(d) {
return y(d.yBegin) - y(d.yEnd);
})
.style("fill", function(d) { return color(d.name); });
var legend = svg.selectAll(".legend")
.data(columnHeaders.slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.x.axis path {
display: none;
}
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</body>