Вы можете использовать transition.end()
, который
Возвращает обещание, которое разрешается после завершения перехода каждого выбранного элемента.
Поскольку у вас есть три выборки - а именно выборки ввода, обновления и выхода в .join()
- вы можете создать массив для сбора этих обещаний и передать его в Promise.all()
, чтобы дождаться разрешения этих обещаний, когда все соответствующие переходы завершатся. Следующий код показывает, как это можно сделать для выбора ввода; очевидно, тот же шаблон может быть применен для сбора обещаний из обновления и выбора выхода.
const transitions = []; // Collect promises for enter, update, exit transitions.
someSelection.data(someData)
.join(
enter => enter.append("whatever")
.attr("", "") // Modify some attributes.
.call(enter => transitions.push( // Push the returned promise to the collector array.
enter.transition() // Create a new transition.
.attr("", "") // Attributes to be transitioned.
.end() // Get the promise.
))
);
Promise.all(transitions)
.then(() => {
console.log("Done"); // This logs when all transitions have finished.
});
Посмотрите на следующий фрагмент рабочего демо:
const svg = d3.select('body')
.append("svg")
.attr("width", 600)
.attr("height", 200);
const transitions = [];
svg.selectAll(null)
.data([500, 1500, 1000])
.join(
enter => enter.append('circle')
.attr('cx', (_, i) => i * 50 + 100)
.attr('cy', 100)
.attr("r", 20)
.attr("fill", "red")
.call(enter => transitions.push(
enter.transition()
.duration(d => d)
.attr('fill', "blue")
.end()
))
);
Promise.all(transitions)
.then(() => {
console.log("Done");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.15.0/d3.js"></script>