[英]d3 SVG text element background-color with getBBox() wrong order
我想為文本元素添加背景色。 我意識到我無法使用CSS樣式設置背景顏色,因為這是SVG文本,因此我嘗試附加矩形但成功了:
let g = svg.selectAll("g")
.data(["1 year", "2 years", "3 years", "4 years", "5 years"])
.enter()
.append("g")
.attr("transform", "translate(" + (markerCirclesScale(name) + 330) + "," + (fullSVGHeight / 2 - 60) + ")" );
g.append("text")
.attr("text-anchor", "middle")
.style("font-size", 10)
.style("fill", "black")
.attr("y", function(d,i){
return i * (-65);
})
.text(function(d){
return d;
})
g.append("rect")
.attr("x", function(d){ return this.parentNode.getBBox().x - 10;})
.attr("y", function(d, i){ return this.parentNode.getBBox().y })
.attr("width", function(d){ return this.parentNode.getBBox().width + 20;})
.attr("height", function(d) {return 40;})
.style("fill", "#80d6c7");
但是我意識到改變DOM中的順序並不能解決問題,為什么改變代碼順序不起作用?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.