我正在D3.js中设计带有双轴折线图的堆叠条形图,但无法在其上对齐标签。
下面是我尝试过的代码- https://plnkr.co/edit/doobXBC5hgzvGwDLvArF?p=preview
我需要将标签垂直放置在每个堆栈的中间,并且每个刻度线都需要在行上方添加标签。有人可以帮忙吗,因为我是D3.js的新手,对我可以在此处使用的函数/样式技术了解不多。
下面给出了Javascript代码-
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 660 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width-150], .1);
y = d3.scale.linear()
.rangeRound([height, 0]).domain([0,100]);
y1 = d3.scale.linear().range([height, 0]).domain([0,1000]);//marks can have min 0 and max 100
var yAxisRight = d3.svg.axis().scale(y1)
.orient("right").ticks(10);
var color = d3.scale.ordinal()
.range(["#ADD8E6", "#FF7F50", "lightgreen" ]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
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 + ")");
d3.csv("class.csv", function(error, data) {
if (error) throw error;
//filter out name and average
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "Name" && key !=="Average"; }));
data.forEach(function(d) {
var y0 = 0;
d.group = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
d.total = d.group[d.group.length - 1].y1;
});
x.domain(data.map(function(d) { return d.Name; }));
//stores toltal headcount
y.domain([0, d3.max(data, function(d) { return d.total; })]);
//line function for averageLine
var averageline = d3.svg.line()
.x(function(d) { return x(d.Name) + x.rangeBand()/2; })
.y(function(d) { return y1(d.Average); });
//this will make the y axis to teh right
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (width-100) + " ,0)")
.style("fill", "red")
.call(yAxisRight);
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", ".71em")
.style("text-anchor", "end")
.text("Bookings");
var state = svg.selectAll(".state")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x(d.Name) + ",0)"; });
var ages_enter = state.selectAll("rect")
.data(function(d) { return d.group; })
.enter();
ages_enter.append("rect")
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.y1); })
.attr("height", function(d) { return y(d.y0) - y(d.y1); })
.style("fill", function(d) { return color(d.name); });
ages_enter.append("text")
.text(function(d) { return d3.format(".2s")(d.y1-d.y0 ); })
.attr("y", function(d) { return y(d.y1); })
//.attr("text-anchor", "right")
.style("stroke", 'black')
.style("font-family", "calibri")
.style("font-size", "12px")
.style("text-anchor",'left')
.attr("dominant-baseline", "middle");
//.attr("y", function(d) { return y(d.frequency/2); })
//.attr("y", height)
//.attr("y", function(d) { return y(d.frequency); })
//.attr("y", function(d) { return y(d.frequency); })
//.attr("y", function(d) { return Math.min(y(d.frequency) + 10, height); });
svg.append("path") // Add the valueline path.
.attr("d", averageline(data));
//add teh legend
var legend = svg.selectAll(".legend")
.data(color.domain().slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 30 + ")"; });
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; });
});
答案 0 :(得分:0)
可以将以下两行添加到ages_enter.append(“ text”)
.attr(“ x”,function(d){return -y(d.y1)-20;}) .attr(“ transform”,“ rotate(-90)”)
所以您的代码将如下所示;
ages_enter.append("text")
.text(function(d) { return d3.format(".2s")(d.y1-d.y0 ); })
.attr("x", function(d) { return -y(d.y1)-20; })
.attr("transform","rotate(-90)")
//.attr("text-anchor", "right")
.style("stroke", 'black')
.style("font-family", "calibri")
.style("font-size", "12px")
.style("text-anchor",'left')
.attr("dominant-baseline", "middle");