使用 d3 过渡增加和减少圆的半径

Increase and decrease radius of a circle using d3 transition

本文关键字:d3 增加 使用      更新时间:2023-09-26

我试图通过增加和减少圆的半径来对圆产生脉冲效果。我希望圆圈根据给定的数据集增长和缩小。我只能获得以太增加或减少半径的过渡函数,但不能同时获得两者。

D3 会自动为数组中的每个值创建一个不同的圆圈。如何使一个圆的半径在遍历数组时增大和缩小?到目前为止,我所拥有的简单版本如下。 感谢您提供的任何帮助。

dataset = [30, 80, 150, 90, 20, 200, 180]
var svg = d3.select("body")
  .append("svg")
  .attr("width", w)
  .attr("height", h);
var circle = svg.selectAll("circle")
  .data(dataset)
  .enter()
  .append("circle");
circle
  .attr("cx", 500)
  .attr("cy", h/2)
  .attr("r", dataset[0])
  .attr("fill", "orange");

这并不适合一般的 D3 data/enter/update/exit 模式,因为您不是在控制多个 DOM 元素,而是在更改单个元素的属性。但是,您可以使用按指定添加过渡的循环轻松完成此操作。代码如下所示。

dataset.forEach(function(d, i) {
    circle.transition().duration(duration).delay(i * duration)
        .attr("r", d);
});

有关完整示例,请参阅此处。

另一个创建连续脉冲圆的选项:

http://bl.ocks.org/chiester/11267307