gpt4 book ai didi

javascript - 当文本变长时,我的 d3 y 轴溢出,如何在图表中打破单词?

转载 作者:行者123 更新时间:2023-12-03 00:51:15 25 4
gpt4 key购买 nike

svg.append("g").attr("class", "y axis").transition().call(yAxis);

如何为 y 轴上生成的每个文本添加换行符。我完全无法更新js中的文本

最佳答案

可以使用here使用的换行函数将长文本包裹在轴标签中

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);
}
}
});
}

这样调用。

svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll(".tick text")
.call(wrap, x.rangeBand());

这适用于 x 轴,如果您需要 y 轴,则需要将其应用于 y 轴

enter image description here

关于javascript - 当文本变长时,我的 d3 y 轴溢出,如何在图表中打破单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53031828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com