单击按钮打开一个新窗口,并用D3绘制多个大圆

On button click open a new window and draw multiple large circles with D3

本文关键字:并用 D3 绘制 窗口 新窗口 按钮 单击 一个      更新时间:2023-09-26

这个问题以前在这里已经回答过了:点击按钮打开一个新窗口,用D3 画一个大圆

在Mark给出的解决方案中:
http://jsfiddle.net/aj3g5tqg/38/

sampleSVG.append('svg')
.attr('width', 500)
.attr('height', 500) //<-- give the SVG some size
.append("circle")
.style("stroke", "gray")
.style("fill", "red")
.attr("r", 200)
.attr("cx", 250)
.attr("cy", 250)
...

我不知道如何将多个圆附加到同一个svg,比如在for循环中
如何做到这一点?提前感谢!

只需扩展附加一个圆的部分即可附加到循环内部。将第一个svg追加的结果强制转换为变量,然后迭代并追加到它:

var mySVG = sampleSVG.append('svg')
    .attr('width', 500)
    .attr('height', 500); //<-- give the SVG some size
for (var i=0; i < 250; i = i+10){
    mySVG.append("circle")
      .style("stroke", "gray")
      .style("fill", "red")
      .attr("r", 200)
      .attr("cx", i)
      .attr("cy", i)
}