16

我正在尝试绘制 vis.js 网络图并让 vis 加载和定位节点。然后我希望禁用物理,以便用户可以移动节点。我已经尝试过了,但它不起作用。

var options = {

    nodes: {
      borderWidth:4,
      size:60,
      color: {
        border: '#222222',
        background: 'grey'
      },
      font:{color:'black'}
    },
    edges: {
      arrows: {
        to:     {enabled: false, scaleFactor:1},
        middle: {enabled: false, scaleFactor:1},
        from:   {enabled: false, scaleFactor:1}
      },
      color: 'black'
    },

    { physics: enabled: false; };

有人做过吗?如果是这样,您能否提供有关实现此目的的最佳方法的示例或建议。我也阅读了位于此处的解释,但对 java 不太熟悉,我无法弄清楚这些步骤。

谢谢

4

3 回答 3

24

经过 vis.js 开发人员的更多工作和帮助,这里是完整的代码,减去 json 数据和一些选项。诀窍是使用"stabilizationIterationsDone"事件并禁用物理:

 // create a network
var container = document.getElementById('mynetwork');
var data = {
    nodes: nodes,
    edges: edges
};
var options = {

    nodes: ...,
    edges: ...,

    physics: {
        forceAtlas2Based: {
            gravitationalConstant: -26,
            centralGravity: 0.005,
            springLength: 230,
            springConstant: 0.18,
            avoidOverlap: 1.5
        },
        maxVelocity: 146,
        solver: 'forceAtlas2Based',
        timestep: 0.35,
        stabilization: {
            enabled: true,
            iterations: 1000,
            updateInterval: 25
        }
    }
};

network = new vis.Network(container, data, options);

network.on("stabilizationIterationsDone", function () {
    network.setOptions( { physics: false } );
});
于 2015-09-11T11:56:59.823 回答
4

我想你首先想让网络稳定,然后才禁用物理?在这种情况下,您可以加载网络physicsstabilization启用。一旦节点稳定下来,stabilized就会触发事件。然后你可以通过禁用物理network.setOptions

于 2015-09-07T08:20:49.743 回答
4

我能够弄清楚这一点,代码现在看起来像这样

// create a network
var container = document.getElementById('mynetwork');
var data = {
    nodes: nodes,
    edges: edges
};
var options = {

    nodes: {
      borderWidth:1,
      size:45,
      color: {
        border: '#222222',
        background: 'grey'
      },
      font:{color:'black',
       size: 11,
       face :'arial',
       },
    },

    edges: {

        arrows: {
            to:     {enabled: false, scaleFactor:1},
            middle: {enabled: false, scaleFactor:1},
            from:   {enabled: false, scaleFactor:1}
        },
        color: {
            color:'#848484',
            highlight:'#848484',
            hover: '#848484',
        },
        font: {
            color: '#343434',
            size: 11, // px
            face: 'arial',
            background: 'none',
            strokeWidth: 5, // px
            strokeColor: '#ffffff',
            align:'vertical'
        },
        smooth: {
            enabled: false, //setting to true enables curved lines
            //type: "dynamic",
            //roundness: 0.5
        },
    }
};

network = new vis.Network(container, data, options);
    network.setOptions({
            physics: {enabled:false}
    });
}

我必须network.setOptions()按照新代码所示移动它,它现在可以按需要工作。

于 2015-09-09T13:47:47.683 回答