1
我需要帮助设置D3堆叠条形图上的标签。我不知道如何将图例中的颜色与数据对象中name属性的引用进行映射。D3在图例上设置标签
我这里有一个的jsfiddle: http://jsfiddle.net/Lhs3e7xk/1/
特别是我需要帮助的代码是传说功能:
function updateLegend(dt) {
var legend = svg.selectAll(".legend")
.data(color.domain()) // I tried dt as well.
.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, i) {
console.log(d)
return color(d.name)
});
}
输出应该在数据集的名称属性的值以及与该组相关的颜色。
固定MBS [Color01]
浮动MBS [Color02]
CMO [Color03]
谢谢!
感谢西里尔,这工作,但有动态设置标签的方法吗?实际上,我可能会有超过3个部分,我也希望将此代码重用于其他图表,以便标签可能会更改。也许我可以将标签提取到不同的数组中,但理想情况下,标签应该来自数据集。没有? – vdiaz1130
检查编辑部分我已经使其动态基于数组 – Cyril