首页 > 解决方案 > 更新 d3 强制有向图链接不起作用

问题描述

我正在尝试更新我的力有向图,但只有节点在更新(而不是链接)。我基本上是在尝试使用 css 类更新链接的颜色,但它没有设置。每当有内容更新时,都会调用更新函数。

我的代码有点复杂,所以我只是粘贴了它的要点:

let update = () => {

    // [...]
    // Update nodes and their css classes works perfectly
    
    // But updating link css class is not updating at all

    link = link
        .data(links)
        .join(enter => {
            return enter
                .append("path")
                .attr("class", d => {

                    return "someclass"; // <-- This is not constant!
                });
        },
            update => {
                return update
            }
        );

        this._simulation.nodes(nodes);
        this._simulation.force("link").links(links);
        this._simulation.alpha(1).restart();
};

请特别注意该return "someclass";行。返回的字符串可能会更改,但不会应用该类。

这不起作用有什么明显的原因吗?

使用以下版本:

d3:6.7.0,d3-力:2.1.1

标签: javascriptd3.jsd3-force-directed

解决方案


推荐阅读