我刚刚开始学习D3。现在我使用D3进行normalized stacked barchart。我的数据集与示例中给出的data.csv
非常相似。只有7个类别,我只有2个。
我的问题是如何旋转xtick标签(例如设置为90度)?当标签文本很长时,它特别有用。
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}
.axis path {
display: none;
}
</style>
<svg width="960" height="500"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 60, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleBand()
.rangeRound([0, width])
.padding(0.1)
.align(0.1);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
var z = d3.scaleOrdinal()
.range(["#9467BD","#ff6666"]);
var stack = d3.stack()
.offset(d3.stackOffsetExpand);
d3.csv("data.csv", type, function(error, data) {
if (error) throw error;
data.sort(function(a, b) { return b[data.columns[1]] / b.total - a[data.columns[1]] / a.total; });
x.domain(data.map(function(d) { return d.Group; }));
z.domain(data.columns.slice(1));
var serie = g.selectAll(".serie")
.data(stack.keys(data.columns.slice(1))(data))
.enter().append("g")
.attr("class", "serie")
.attr("fill", function(d) { return z(d.key); });
serie.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("x", function(d) { return x(d.data.Group); })
.attr("y", function(d) { return y(d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[1]); })
.attr("width", x.bandwidth());
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y).ticks(10, "%"));
var legend = serie.append("g")
.attr("class", "legend")
.attr("transform", function(d) { var d = d[d.length - 1]; return "translate(" + (x(d.data.Group) + x.bandwidth()) + "," + ((y(d[0]) + y(d[1])) / 2) + ")"; });
legend.append("line")
.attr("x1", -6)
.attr("x2", 6)
.attr("stroke", "#000");
legend.append("text")
.attr("x", 9)
.attr("dy", "0.35em")
.attr("fill", "#000")
.style("font", "10px sans-serif")
.text(function(d) { return d.key; });
});
function type(d, i, columns) {
for (i = 1, t = 0; i < columns.length; ++i) t += d[columns[i]] = +d[columns[i]];
d.total = t;
return d;
}
</script>
我尝试添加这段代码,但随后所有标签都消失了:
g.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr("y", 0)
.attr("x", 10)
.attr("dy", ".35em")
.attr("transform", "rotate(90)")
.style("text-anchor", "start");
更新:
Group,VAL 1,VAL 2
XXX XXXXXXXXXXXX,24983,66605
YYY XXXXXXXXXXXX,13522,61334
AAA XXXXXXXXXXXX,15289,37347
BBB XXXXXXXXXXXX,13155,38813
CCC XXXXXXXXXXXX,8374,22656
GGG XXXXXXXXXXXX,7660,21879
ABC XXXXXXXXXXXX,7935,16870
TTT XXXXXXXXXXXX,4831,18409
DDD XXXXXXXXXXXX,6301,16371
ZZZ XXXXXXXXXXXX,7207,13617
YYY XXXXXXXXXXXX,4407,15845
XX2 XXXXXXXXXXXX,3989,15031
FXX XXXXXXXXXXXXGC,3260,13754