tombrus
tombrus

Reputation: 369

Is it possible to set the font color on nodes when selected in vis.js?

I would like to change the appearance of nodes whenever they are selected. I can change the color of the node as well as the border, but I would like to change the color of the text as well. Am I missing something or is it an enhancement request?

Upvotes: 3

Views: 1997

Answers (1)

Robin Mackenzie
Robin Mackenzie

Reputation: 19289

Adapted from this answer relating to setting the size of a selected node:

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, color: "#000" }}
};
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,
      color: "#ff0000"
    }
  });
});

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,
      color: options.nodes.font.color
    }
  });
});
#mynetwork {
  width: 100%;
  height: 100%;
  border: 1px solid lightgray;
}
<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"/>
<div id="mynetwork"></div>

Upvotes: 3

Related Questions