Javascript /退出选择。代码中发生的情况是: 在初始化函数中为每个图表元素执行一次数据联接。该数据联接将每个节点标记为新节点并返回每个节点,然后缓存这些结果。 在更新函数中,每次都使用这些缓存的结果。
相反,每次图形更改时,在更新时执行数据联接,而不是在初始化时。节点元素的示例如下:Javascript /退出选择。代码中发生的情况是: 在初始化函数中为每个图表元素执行一次数据联接。该数据联接将每个节点标记为新节点并返回每个节点,然后缓存这些结果。 在更新函数中,每次都使用这些缓存的结果。,javascript,node.js,reactjs,typescript,d3.js,Javascript,Node.js,Reactjs,Typescript,D3.js,相反,每次图形更改时,在更新时执行数据联接,而不是在初始化时。节点元素的示例如下: private initializeGraph(): void { const mainGroup = select(this.svgElement.current) .append("g") .attr("id", "main"); // append nodes svg group this.
private initializeGraph(): void {
const mainGroup = select(this.svgElement.current)
.append("g")
.attr("id", "main");
// append nodes svg group
this.nodeElements = mainGroup.append("g")
.attr("id", "nodes")
}
private updateGraph(): void {
// select nodes & edges
const graphNodes = this.nodeElements
.selectAll<SVGCircleElement, Node>(".directed-graph-node")
.data<Node>(this.state.g.nodes, _ => _.id);
// update nodes with their current position
graphNodes.attr("cx", node => node.x)
.attr("cy", node => node.y);
// add newly added nodes if any
graphNodes.enter()
.append("circle")
.attr("class", ".directed-graph-node")
.attr("stroke", "steelblue")
.attr("cx", node => node.x)
.attr("cy", node => node.y)
.attr("r", 2.5)
.call(drag<SVGCircleElement, Node>());
// remove nodes that don't exist anymore
graphNodes.exit().remove();
}
private updateGraph(): void {
const graphNodes = this.nodeElements
.selectAll<SVGCircleElement, Node>(".directed-graph-node")
// data() join()
.data<Node>(this.state.g.nodes, _ => _.id)
.join(
enter => enter.append("circle")
.attr("class", ".directed-graph-node")
.attr("stroke", "steelblue")
.attr("r", 2.5)
.call(drag<SVGCircleElement, Node>()),
update => update,
exit => exit.remove();
)
// enter + update past this point
.attr("cx", node => node.x)
.attr("cy", node => node.y)
}