修改网络中为vis.js选择的特定节点的样式

时间:2016-07-27 20:38:15

标签: javascript vis.js

是否有办法仅更改所选节点的节点大小,而不是更改选项中所有节点的大小? 这些是我的节点选项

nodes:{
                borderWidth: 1,
                borderWidthSelected: 2,
                physics: true,
                color: {
                    border: '#000000',
                    background: '#ffffff',
                    highlight: {
                        border: '#000000',
                        background: '#B9B9BF'
                    }
                },
                shadow:{
                    enabled: false,
                    color: '#C11818',
                    size:10,
                    x:5,
                    y:5
                },
                shape: 'circularImage',
                mass: 2,
                size: 25

我想扩大所选节点,使其比其他节点更明显。

network.on("selectNode", function (params) {
        var nodeId = params.nodes[0];
        var node = nodes.get(nodeId);
        nodeClick(nodeId, nodes, edges, network);
        // var options= {
        // nodes: {
        // size: 40
        // }
        // };
        // network.setOptions(options);
    });

注释部分设置所有节点的大小而不是所选节点的大小,节点对象也没有任何句柄处理。

2 个答案:

答案 0 :(得分:2)

您可以更改所选节点的字体大小以增加其大小:



var nodes = new vis.DataSet([
  {id: 1, label: 'Node 1'},
  {id: 2, label: 'Node 2'},
  {id: 4, label: 'Node 4'},
  {id: 5, label: 'Node 5'}
]);
var edges = new vis.DataSet([
  {from: 1, to: 2},
  {from: 2, to: 4},
  {from: 2, to: 5}
]);

var container = document.getElementById('mynetwork');
var data = { 
  nodes: nodes,
  edges: edges
};
var options = {
  interaction: { hover:true },
  nodes: { font: { size: 14 }}
};
var network = new vis.Network(container, data, options);

network.on("selectNode", function (params) {
  var selectedNodeId = params.nodes[0];
  var node = network.body.nodes[selectedNodeId];
  node.setOptions({
    font: {
      size: 20
    }
  });
});

network.on("deselectNode", function (params) {
  var deselectedNodeId = params.previousSelection.nodes[0];
  var node = network.body.nodes[deselectedNodeId];
  node.setOptions({
    font: {
      size: options.nodes.font.size
    }
  });
});

#mynetwork {
  width: 100%;
  height: 100%;
  border: 1px solid lightgray;
}

<!DOCTYPE html>
<html>
<head>
  <script src="//cdnjs.cloudflare.com/ajax/libs/vis/4.16.1/vis.min.js"></script>
  <link href="//cdnjs.cloudflare.com/ajax/libs/vis/4.16.1/vis.min.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="mynetwork"></div>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

如果启用了多选,则可以循环使用params.nodes

for (id in params.nodes){
    var node = network.body.nodes[params.nodes[id]];
    ...
}

(分别取消选择)