Partial forces on nodes in D3.js

前端 未结 2 1708
小蘑菇
小蘑菇 2021-01-14 19:54

I want to apply several forces (forceX and forceY) respectively to several subparts of nodes.

To be more explanatory, I have this JSON as data for my nodes:

2条回答
  •  灰色年华
    2021-01-14 20:27

    I fixed my own implementation of altocumulus' second solution :

    In my case, I had to create two forces per centroid. It seems like we can't share the same initializer for all the forceX and forceY functions.

    I had to create local variables for each centroid on the loop :

    Emi.nodes.centroids.forEach((centroid, i) => {
    
        let forceX = d3.forceX(centroid.fx);
        let forceY = d3.forceY(centroid.fy);
        let forceXInit = forceX.initialize;
        let forceYInit = forceY.initialize;
        forceX.initialize = nodes => {
            forceXInit(nodes.filter(n => n.theme === centroid.label))
        };
        forceY.initialize = nodes => {
            forceYInit(nodes.filter(n => n.theme === centroid.label))
        };
    
        Emi.simulation.force("X" + i, forceX);
        Emi.simulation.force("Y" + i, forceY);
    });
    

提交回复
热议问题