gpt4 book ai didi

javascript - 如何在 D3.js 中垂直包装 SVG 中的文本?

转载 作者:行者123 更新时间:2023-12-01 16:17:51 25 4
gpt4 key购买 nike

我还没有完全找到使用 D3 的答案。我正在从一个 svg 元素制作一个矩形工具提示,该元素在单击节点时显示在图表上。与工具提示关联的文本,来自图表的 json,目前仅水平扩展,即使工具提示的尺寸是垂直扩展的。我希望矩形垂直环绕文本(如回车),而不是水平环绕,给定矩形的固定宽度。

下面是我在单击节点时创建工具提示的位置:

nodes.on("click", function (d){
if (tip){ tip.remove()};

tip = svg.append("g")
.attr("id", "tip")
.attr("transform", "translate(" + (d3.event.pageX - 10) + "," + (d3.event.pageY - 35) + ")");

let rect = tip.append("rect")
.style("fill", "white")
.style("stroke", "steelblue");

tip.append("text")
.text(d.description)
.attr("dy", "1em")
.attr("x", 5);

let bbox = tip.node().getBBox();

rect.attr("width", bbox.width + 5)
.attr("height", bbox.height)
});

这是一个具有当前功能的 jsfiddle: https://jsfiddle.net/Thegreatmochi/epy6514t/14/

最佳答案

从此bl.ocks.org您可以使用 wrap 功能稍作修改:

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", lineHeight + "em").text(word);
}
}
});
}

这是一个工作片段:

let graph = {
"nodes": [{
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed porta consequat felis, eget varius mi volutpat sit amet. Nullam lobortis vehicula felis, in tempor dui. Sed scelerisque purus ac nisl auctor finibus sit amet nec massa. Duis erat sem, suscipit non molestie vitae, accumsan nec tortor. Nulla sed facilisis ligula, nec interdum elit. Nam tortor lectus, sodales ut nulla ac, feugiat ultrices velit. Vestibulum nec ultricies nisi. Donec leo nibh, fringilla eget pretium nec, condimentum in nulla."
}]
}

let svg = d3.select("body")
.append("svg")
.attr("width", 700)
.attr("height", 800);

let nodes = svg.selectAll("circle")
.data(graph.nodes)
.enter()
.append("circle")
.attr("r", 20)
.attr("cx", function(d) {
return 20;
})
.attr("cy", function(d) {
return 20;
})
.style("fill", "red");
let tip;
nodes.on("click", function(d) {
if (tip) {
tip.remove()
};

tip = svg.append("g")
.attr("id", "tip")
.attr("transform", "translate(" + (d3.event.pageX - 10) + "," + (d3.event.pageY - 35) + ")");

let rect = tip.append("rect")
.style("fill", "white")
.style("stroke", "steelblue");

tip.append("text")
.text(d.description)
.attr("dy", "1em")
.attr("x", 5)
.call(wrap, 300)

let bbox = tip.node().getBBox();

rect.attr("width", bbox.width + 5)
.attr("height", bbox.height + 50)

});


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", lineHeight + "em").text(word);
}
}
});
}
<p>
click the node to show description
</p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.12.2/d3.js"></script>

关于javascript - 如何在 D3.js 中垂直包装 SVG 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61767820/

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