【发布时间】:2021-09-24 15:25:51
【问题描述】:
我正在尝试将由弧连接的圆圈内的文本居中。我看过其他 StackOverflow 答案,但没有一个能解决我的问题。我知道将每个圆形元素包装在一个 g 元素中,然后附加我在下面做的文本。如果有人可以帮助我,将不胜感激。
let svg = d3.select("body").append('svg').attr('viewBox', '0 0 ' + width + ' ' + boxHeight);
let nodes: any = [[width / 2, boxHeight / 1.5], [width / 4, boxHeight / 3], [width / 1.5, boxHeight / 3]];
let g = svg.append('g')
for (let i = 0; i < nodes.length; i++) {
g
.append('circle')
.attr('cx', nodes[i][0])
.attr('cy', nodes[i][1])
.attr('r', 40).style('stroke', 'green')
.style('fill', 'none')
g.append('text').attr('fill', 'black').text('Hello')
}
let links: any = [];
// Link from the first node to the second
links.push(
d3.linkHorizontal()({
source: nodes[0],
target: nodes[1]
})
);
// Link from the first node to the third
links.push(
d3.linkHorizontal()({
source: nodes[0],
target: nodes[2]
})
);
// Append the links to the svg element
for (let i = 0; i < links.length; i++) {
svg
.append('path')
.attr('d', links[i])
.attr('stroke', 'black')
.attr('fill', 'none');
}
【问题讨论】:
标签: javascript typescript d3.js