【发布时间】:2014-04-30 14:57:48
【问题描述】:
我有一个 X 轴标签相互重叠的 D3 条形图。我有以下内容:
// x 轴自动换行
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
尝试通过添加必要的功能来关注http://bl.ocks.org/mbostock/7555321,但无效。有大神可以指点一下吗?
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
谢谢。
【问题讨论】:
-
您使用该功能时发生了什么?标签是否仍然太长,没有分成单独的行?你的标签是什么样的?
-
是的,如果您提供调用 wrap 函数的完整部分,我们应该能够为您提供帮助。我最近刚刚成功应用它。
标签: javascript css d3.js visualization overlap