gpt4 book ai didi

javascript - 堆积条形图标签 - D3

转载 作者:行者123 更新时间:2023-11-29 17:13:01 24 4
gpt4 key购买 nike

我正在尝试将数据标签添加到 d3 中的堆叠条形图。

我希望数据标签位于栏的中间。到目前为止,我只是想出了如何在每个栏的顶部添加数据标签。但实际上我希望这些标签位于每个栏的中间。

这是我的代码:

var width = 750,
height = 500;

var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);

var y = d3.scale.linear()
.rangeRound([height, 0]);

var color = d3.scale.ordinal()
.range(["#D70B16", "#154CEF", "#1A8A55"]);

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");

var yAxis = d3.svg.axis()
.scale(y)
.orienta("left")
.tickFormat(d3.format(".2s"));

var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "Value: " + (d.y1 - d.y0) + "";
})

var svgContainer = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + 30 + "," + 30 + ")");


d3.csv("data.csv", function(error, data) {
color.domain(d3.keys(data[0]).filter(function(key) {
return key !== "circle";
}));

data.forEach(function(d) {
var y0 = 0;
d.hours = color.domain().map(function(name) {
return {name: name, y0: y0, y1: y0 += +d[name]};
});

d3.select('body').append('pre')
.text(JSON.stringify(d.hours, null, ' '));

d.total = d.hours[d.hours.length - 1].y1;
});

x.domain(data.map(function(d) {return d.circle;}));
y.domain([0, d3.max(data, function(d) {return d.total;})])

svgContainer.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);

svgContainer.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("Login Hours");

var circle = svgContainer.selectAll(".circle")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) {
return "translate(" + x(d.circle) + ",0)";
});

circle.selectAll("rect")
.data(function(d) { return d.hours; })
.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); })
.on("mouseover", tip.show)
.on("mouseout", tip.hide)
.style("fill", function(d) { return color(d.name); });


circle.selectAll("text")
.data(function(d) { return d.hours; })
.enter()
.append("text")
.attr("x", 75)
.attr("y", function(d, i) { return y(d.y1) ; })
.style("text-anchor", "middle")
.text("test")

})

数据:

state,value1, value2, value3
state1, 80, 10, 20
state2, 90, 5, 10
state3, 70, 15, 35
state4, 90, 3, 27
state5, 50, 25, 55
state6, 85, 8, 27

我实际上不知道如何获得每个条形的 X 和 Y 坐标的中点。

帮我安排每个栏中间的标签。

最佳答案

你只需要调整text元素的y坐标:

circle.selectAll("text")
.data(function(d) { return d.hours; })
.enter()
.append("text")
.attr("x", 75)
.attr("y", function(d, i) { return y(d.y1) + (y(d.y0) - y(d.y1))/2; })
.style("text-anchor", "middle")
.text("test")

关于javascript - 堆积条形图标签 - D3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20400292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com