admin管理员组

文章数量:1352788

In d3, I have an app that allows users to show and hide elements. When hiding elements, I have it animate out for a few seconds. However, during this time, the user could instead hit show again, and the items should reappear.

The issue is that it seems until the exit has finished, only update is called (which does make sense, the elements are still in the DOM until they are finally removed after the transition). However, I want to have them go through the re-enter loop if possible or handle it another way rather than going through update, since I use a separate transition in update to animate when the data changes, and for rescaling, etc. while panning.

Here's a simple example that demonstrates what I mean. You can clear circles and then during the transition, hitting show circles will do nothing since enter is not being called, just update.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>D3 Circle Toggle</title>
  <script src=".v7.min.js"></script>
  <style>
    svg {
      border: 1px solid #ccc;
      margin-top: 10px;
    }
    button {
      margin: 5px;
    }
  </style>
</head>
<body>

  <button id="showBtn">Show Circles</button>
  <button id="clearBtn">Clear Circles</button></br>
  <svg width="500" height="200"></svg>

  <script>
    const svg = d3.select("svg");
    const cx = 50, cy = 100;
    const data = [80, 120, 60, 150, 200];

    function render(data) {
      const circles = svg.selectAll("circle").data(data, (d, i) => i);

      // Exit: remove circles no longer in data
      circles.exit()
        .transition()
        .duration(5000)
        .attr("r", 0)
        .remove();

      // Enter: new circles
      circles.enter()
        .append("circle")
        .attr("cx", (d, i) => cx + i * 80)
        .attr("cy", cy)
        .attr("r", 0)
        .attr("fill", "tomato")
        .transition()
        .duration(1000)
        .attr("r", d => Math.sqrt(d));
    }

    document.getElementById("showBtn").addEventListener("click", () => render(data));
    document.getElementById("clearBtn").addEventListener("click", () => render([]));

    // Optional: initial render
    render(data);
  </script>

</body>
</html>

In d3, I have an app that allows users to show and hide elements. When hiding elements, I have it animate out for a few seconds. However, during this time, the user could instead hit show again, and the items should reappear.

The issue is that it seems until the exit has finished, only update is called (which does make sense, the elements are still in the DOM until they are finally removed after the transition). However, I want to have them go through the re-enter loop if possible or handle it another way rather than going through update, since I use a separate transition in update to animate when the data changes, and for rescaling, etc. while panning.

Here's a simple example that demonstrates what I mean. You can clear circles and then during the transition, hitting show circles will do nothing since enter is not being called, just update.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>D3 Circle Toggle</title>
  <script src="https://d3js./d3.v7.min.js"></script>
  <style>
    svg {
      border: 1px solid #ccc;
      margin-top: 10px;
    }
    button {
      margin: 5px;
    }
  </style>
</head>
<body>

  <button id="showBtn">Show Circles</button>
  <button id="clearBtn">Clear Circles</button></br>
  <svg width="500" height="200"></svg>

  <script>
    const svg = d3.select("svg");
    const cx = 50, cy = 100;
    const data = [80, 120, 60, 150, 200];

    function render(data) {
      const circles = svg.selectAll("circle").data(data, (d, i) => i);

      // Exit: remove circles no longer in data
      circles.exit()
        .transition()
        .duration(5000)
        .attr("r", 0)
        .remove();

      // Enter: new circles
      circles.enter()
        .append("circle")
        .attr("cx", (d, i) => cx + i * 80)
        .attr("cy", cy)
        .attr("r", 0)
        .attr("fill", "tomato")
        .transition()
        .duration(1000)
        .attr("r", d => Math.sqrt(d));
    }

    document.getElementById("showBtn").addEventListener("click", () => render(data));
    document.getElementById("clearBtn").addEventListener("click", () => render([]));

    // Optional: initial render
    render(data);
  </script>

</body>
</html>

Share Improve this question edited Apr 1 at 13:16 Łukasz Daniel Mastalerz 2,4282 gold badges7 silver badges28 bronze badges asked Apr 1 at 2:12 rb612rb612 5,6033 gold badges35 silver badges80 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Try use interrupt() function

https://d3js./d3-transition/control-flow#selection_interrupt

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>D3 Circle Toggle with Interrupt</title>
  <script src="https://d3js./d3.v7.min.js"></script>
  <style>
svg {
  border: 1px solid #ccc;
  margin-top: 10px;
}
button {
  margin: 5px;
}
  </style>
</head>
<body>

  <button id="showBtn">Show Circles</button>
  <button id="clearBtn">Clear Circles</button></br>
  <svg width="500" height="200"></svg>

  <script>
const svg = d3.select("svg");
const cx = 50, cy = 100;
const data = [80, 120, 60, 150, 200];

function render(data) {
  svg.selectAll("circle").interrupt();

  const circles = svg.selectAll("circle").data(data, (d, i) => i);

  circles.exit()
    .transition()
    .duration(5000)
    .attr("r", 0)
    .remove();

  circles.enter()
    .append("circle")
    .attr("cx", (d, i) => cx + i * 80)
    .attr("cy", cy)
    .attr("r", 0)
    .attr("fill", "tomato")
    .transition()
    .duration(1000)
    .attr("r", d => Math.sqrt(d));

  circles.transition()
    .duration(1000)
    .attr("r", d => Math.sqrt(d));
}

document.getElementById("showBtn").addEventListener("click", () => render(data));
document.getElementById("clearBtn").addEventListener("click", () => render([]));

render(data);
  </script>

</body>
</html>

本文标签: javascriptd3 handling data reenter while exitingStack Overflow