我想在其旁邊顯示一個rect
,其標籤爲text
。 rect
的寬度應該拉伸到svg容器的寬度,小於文本的寬度,這是動態的,可以是任何可變長度。在繪製文本之前計算文本的寬度
var text = 'Foobar';
var textWidth = 50; //how to calculate this?
var plotWidth = 400;
var barWidth = plotWidth-textWidth;
var plot = d3.select(container)
.insert("svg")
.attr('width', plotWidth)
.attr('height', 50);
plot.append("rect")
.style("fill", "steelblue")
.attr("x", 0)
.attr("width", barWidth)
.attr("y", 0)
.attr("height", 50);
plot.append("text")
.attr("x", barWidth)
.attr("y", 28)
.text(text);
如何計算使用D3文本的寬度,繪製之前呢?或者,如何確定取決於可變長度文本的維度的元素的位置和大小?
見http://stackoverflow.com/questions/20224611/d3-position-text-element-dependent-on-length-of-element-before – 2015-03-13 12:07:49