关于多个饼图动画的问题

huangapple go评论53阅读模式
英文:

Issues with animating multiple pie charts

问题

嗨,StackOverflow 社区,

我试图根据下拉框的值来制作多个饼图(灵感来自此帖子)。然而,每当下拉框的值更改时,我都会遇到以下问题:

  1. 现有的饼图不会更新(重新定位、调整大小或重新切片)。
  2. 新的饼图(在 2004 年)不会平滑进入或退出。

关于多个饼图动画的问题

我想了解我漏掉了什么。有谁知道如何解决上述问题吗?
这里是我一直在使用的笔记本。

英文:

Hi StackOverflow Community,

I'm trying to animate multiple pies (inspired by this post) based on the dropdown value. However, am facing these issues with the pies whenever the dropdown value changes:

  1. the existing pies do not update (reposition, resize or re-slice).
  2. the new pie (on year 2004) does not enter nor exit smoothly.

关于多个饼图动画的问题

I want to understand what it is that I've missed to include. Does anyone know of any way to solve the above challenges?
Here is the notebook that I've been working on.

答案1

得分: 1

你需要为组更新/退出/进入选择,然后为路径进行另一个更新/退出/进入选择。

简而言之,这是组的选择示例:

let pies = g.selectAll("g.pie_ts")
  .data(data);

pies
  .exit()
  // .transition(transition_duration)
  .remove();

pies = pies
  .enter()
  .append("g")
  .attr("class", "pie_ts")
  .property("inner_radius", function(_d) {
    return 0.3 * resize_radius(_d.total_slice);
  })
  .property("outer_radius", function(_d) {
    return resize_radius(_d.total_slice);
  })
  .property("x_value", function(_d) {
    return _d[parameter_x];
  })
  .property("y_value", function(_d) {
    return _d[parameter_y];
  })
  .merge(pies);

而这是路径的选择示例:

let piesArc = pies.selectAll(".pie_ts_slice")
  .data(d => {
    let array = pie([d.total_slice_disposal, d.total_slice_diverted]);
    return array;
  })

piesArc.exit().remove();

piesArc = piesArc.enter()
  .append("path")
  .attr("class", "pie_ts_slice")
  .merge(piesArc)
  .attr("transform", function(_d) {
    let x_value = d3.select(this.parentNode).property("x_value");
    let y_value = d3.select(this.parentNode).property("y_value");
    return `translate(${x_scale(x_value)}, ${y_scale(y_value)})`;
  })
  .attr("d", function(_d) {
    let inner_radius = d3.select(this.parentNode).property("inner_radius");
    let outer_radius = d3.select(this.parentNode).property("outer_radius");
    arc
      .innerRadius(inner_radius)
      .outerRadius(outer_radius);
    return arc(_d)
  })
  .attr("fill", function(_d, _i) {
    return color[_i];
  });

希望这有所帮助。

英文:

You need to have an update/exit/enter selection for the groups, then another update/exit/enter selection for the paths.

In short, this is how the groups' selections looks like:

let pies = g.selectAll("g.pie_ts")
  .data(data);

pies
  .exit()
  // .transition(transition_duration)
  .remove();

pies = pies
  .enter()
  .append("g")
  .attr("class", "pie_ts")
  .property("inner_radius", function(_d) {
    return 0.3 * resize_radius(_d.total_slice);
  })
  .property("outer_radius", function(_d) {
    return resize_radius(_d.total_slice);
  })
  .property("x_value", function(_d) {
    return _d[parameter_x];
  })
  .property("y_value", function(_d) {
    return _d[parameter_y];
  })
  .merge(pies);

And this is the paths' selections:

let piesArc = pies.selectAll(".pie_ts_slice")
  .data(d => {
    let array = pie([d.total_slice_disposal, d.total_slice_diverted]);
    return array;
  })

piesArc.exit().remove();

piesArc = piesArc.enter()
  .append("path")
  .attr("class", "pie_ts_slice")
  .merge(piesArc)
  .attr("transform", function(_d) {
    let x_value = d3.select(this.parentNode).property("x_value");
    let y_value = d3.select(this.parentNode).property("y_value");
    return `translate(${x_scale(x_value)}, ${y_scale(y_value)})`;
  })
  .attr("d", function(_d) {
    let inner_radius = d3.select(this.parentNode).property("inner_radius");
    let outer_radius = d3.select(this.parentNode).property("outer_radius");
    arc
      .innerRadius(inner_radius)
      .outerRadius(outer_radius);
    return arc(_d)
  })
  .attr("fill", function(_d, _i) {
    return color[_i];
  });

huangapple
  • 本文由 发表于 2023年3月1日 10:28:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定