gpt4 book ai didi

javascript - 在 D3js 中沿圆弧旋转矩形

转载 作者:行者123 更新时间:2023-12-03 01:12:02 27 4
gpt4 key购买 nike

我正在尝试将矩形放置在 d3js 弧上的 0、90、180 和 270 度处。这是我放在一起的一个简单示例,它使用圆弧生成器创建 4 条圆弧,并使用 arc.centroid 函数将矩形放置在每个圆弧的中心点:

<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Rotate rectangles to arc positions: 0, 90, 180 & 270 </title>
</head>

<style>
path {
fill: white;
stroke: black;
}
</style>

<body>
<svg width="700" height="220">
<g transform="translate(200, 110)"></g>
</svg>

<script src="https://d3js.org/d3.v5.min.js" charset="utf-8"></script>

<script>
// Create an arc generator with configuration
var arcGenerator = d3.arc()
.innerRadius(80)
.outerRadius(100);

var arcData = [
{startAngle: 0, endAngle: Math.PI * 2 / 4, degree: 0},
{startAngle: Math.PI * 2 / 4, endAngle: Math.PI, degree: 90},
{startAngle: Math.PI, endAngle: Math.PI * 3 / 2, degree: 180},
{startAngle: Math.PI * 3 / 2, endAngle: Math.PI * 2, degree: 270},
];

// Create a path element and set its d attribute
d3.select('g')
.selectAll('path')
.data(arcData)
.enter()
.append('path')
.attr('d', arcGenerator);

// Add labels, using .centroid() to position
d3.select('g')
.selectAll('rect')
.data(arcData)
.enter()
.append('rect')
.each(function(d) {
var centroid = arcGenerator.centroid(d);
d3.select(this)
.attr('x', centroid[0])
.attr('y', centroid[1])
.attr('width', 10)
.attr('height', 35)
});
</script>
</body>
</html>

结果如下:image showing rectangles at the centroid position of each arc

相反,我希望将每个矩形放置在每个圆弧的开始或结束位置并旋转,以便每个矩形都是纵向的(10 px 宽和 35 px 长)。

这是我的:jsfiddle

最佳答案

为您想要的 Angular 创建弧数据记录,并为这些虚拟弧使用centroid()

g 元素后面有一些非法 html。

// Create an arc generator with configuration
var arcGenerator = d3.arc()
.innerRadius(80)
.outerRadius(100);

var arcData = [
{startAngle: 0, endAngle: Math.PI * 2 / 4, degree: 0},
{startAngle: Math.PI * 2 / 4, endAngle: Math.PI, degree: 90},
{startAngle: Math.PI, endAngle: Math.PI * 3 / 2, degree: 180},
{startAngle: Math.PI * 3 / 2, endAngle: Math.PI * 2, degree: 270},
];

var rectData = arcData.map(d => { return {startAngle: d.startAngle, endAngle:d.startAngle}; });

// Create a path element and set its d attribute
d3.select('g')
.selectAll('path')
.data(arcData)
.enter()
.append('path')
.attr('d', arcGenerator);

// Add labels, using .centroid() to position
d3.select('g')
.selectAll('.grect')
.data(rectData)
.enter()
.append('g')
.attr('class', 'grect')
.attr('transform', d => {
var centroid = arcGenerator.centroid(d);
return `translate(${centroid[0]},${centroid[1]})`;
})
.append('rect')
.attr('x', -5)
.attr('y', -17)
.attr('width', 10)
.attr('height', 35);
path {
fill: white;
stroke: black;
}
<meta charset="utf-8">
<head>
<title>Rotate rectangles to arc positions: 0, 90, 180 and 270 </title>
</head>

<body>
<svg width="700" height="220">
<g transform="translate(200, 110)"></g>
</svg>

<script src="https://d3js.org/d3.v5.min.js" charset="utf-8"></script>
</body>

编辑

您可以即时创建rectData

    d3.select('g')
.selectAll('.grect')
.data(arcData)
.enter()
.append('g')
.attr('class', 'grect')
.attr('transform', d => {
var centroid = arcGenerator.centroid({startAngle: d.startAngle, endAngle:d.startAngle});
return `translate(${centroid[0]},${centroid[1]})`;
})
.append('rect')
.attr('x', -5)
.attr('y', -17)
.attr('width', 10)
.attr('height', 35);

关于javascript - 在 D3js 中沿圆弧旋转矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52174343/

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