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