jqueryvis.jsvis.js-network

How to change node image in vis.js when node clicked?


I am trying to replicate script in vis.js library for grey node when clicked, it is successfully but that is actually for shape type. but my case is I am using image type. What happen is the image is not change. How to implement below code for my case (using image).

Let say I have 2 images (Image_A and Image_B), Image_A is for default color image, Image_B is grey image. So when clicked at specific node, the node will use Image_A and other is Image_B. When clicked outside all nodes will use Image_A.

Example only:

enter image description here

Source link: Grey out other node

var network;
var allNodes;
var highlightActive = false;
var nodes;

function redrawAll() {
    var allNodes = [{
        id: 1,
        label: "Node A_1",
        image: "https://www.freeiconspng.com/uploads/letter-a-icon-4.png"
    },{
        id: 2,
        label: "Node A_2",
        image: "https://www.freeiconspng.com/uploads/letter-a-icon-4.png"
    },{
        id: 3,
        label: "Node B_1",
        image: "https://www.freeiconspng.com/uploads/letter-b-icon-png-23.png"
    },{
        id: 4,
        label: "Node B_2",
        image: "https://www.freeiconspng.com/uploads/letter-b-icon-png-23.png"
    },
    ];
    // Create edge data array
    var allEdges = [
        { id: "1_2", from: 1, to: 2 },
        { id: "2_3", from: 2, to: 3 },
        { id: "2_4", from: 2, to: 4 },
        { id: "4_1", from: 4, to: 1 }
    ]
    var container = document.getElementById('mynetwork');
    var nodes = new vis.DataSet(allNodes);
    var edges = new vis.DataSet(allEdges);
    var data = { nodes: nodes, edges: edges };
    var options = {};
    network = new vis.Network(container, data, options);

    // get a JSON object
    allNodes = nodes.get({ returnType: "Object" });
    network.on("click", neighbourhoodHighlight);
}

function neighbourhoodHighlight(params) {
    // if something is selected:
    if (params.nodes.length > 0) {
        highlightActive = true;
        var i, j;
        var selectedNode = params.nodes[0];
        var degrees = 2;
  
        // mark all nodes as hard to read.
        for (var nodeId in allNodes) {
            allNodes[nodeId].color = "rgba(200,200,200,0.5)";
            if (allNodes[nodeId].hiddenLabel === undefined) {
                allNodes[nodeId].hiddenLabel = allNodes[nodeId].label;
                allNodes[nodeId].label = undefined;
            }
        }
        var connectedNodes = network.getConnectedNodes(selectedNode);
        var allConnectedNodes = [];
    
        // get the second degree nodes
        for (i = 1; i < degrees; i++) {
            for (j = 0; j < connectedNodes.length; j++) {
                allConnectedNodes = allConnectedNodes.concat(network.getConnectedNodes(connectedNodes[j]));
            }
        }
  
        // all second degree nodes get a different color and their label back
        for (i = 0; i < allConnectedNodes.length; i++) {
            allNodes[allConnectedNodes[i]].color = "rgba(150,150,150,0.75)";
            if (allNodes[allConnectedNodes[i]].hiddenLabel !== undefined) {
                allNodes[allConnectedNodes[i]].label = allNodes[allConnectedNodes[i]].hiddenLabel;
                allNodes[allConnectedNodes[i]].hiddenLabel = undefined;
            }
        }
    
        // all first degree nodes get their own color and their label back
        for (i = 0; i < connectedNodes.length; i++) {
            allNodes[connectedNodes[i]].color = undefined;
            if (allNodes[connectedNodes[i]].hiddenLabel !== undefined) {
                allNodes[connectedNodes[i]].label = allNodes[connectedNodes[i]].hiddenLabel;
                allNodes[connectedNodes[i]].hiddenLabel = undefined;
            }
        }
    
        // the main node gets its own color and its label back.
        allNodes[selectedNode].color = undefined;
        if (allNodes[selectedNode].hiddenLabel !== undefined) {
            allNodes[selectedNode].label = allNodes[selectedNode].hiddenLabel;
            allNodes[selectedNode].hiddenLabel = undefined;
        }
    }
    else if (highlightActive === true) {
        // reset all nodes
        for (var nodeId in allNodes) {
            allNodes[nodeId].color = undefined;
            if (allNodes[nodeId].hiddenLabel !== undefined) {
                allNodes[nodeId].label = allNodes[nodeId].hiddenLabel;
                allNodes[nodeId].hiddenLabel = undefined;
            }
        }
        highlightActive = false;
    }
  
    // transform the object into an array
    var updateArray = [];
    for (nodeId in allNodes) {
        if (allNodes.hasOwnProperty(nodeId)) {
            updateArray.push(allNodes[nodeId]);
        }
    }
    nodes.update(updateArray);
}

redrawAll();

Solution

  • You need to use shape :"image" to display node as images. Then , to make image blur i have use opacity instead of color . So , whenever you need to blur other images use allNodes[nodeId].opacity = any value same for all other nodes.

    Demo Code :

    var network;
    var allNodes;
    var highlightActive = false;
    var nodes;
    
    function redrawAll() {
      allNodes = [{
        id: 1,
        label: "Node A_1",
        shape: "image", //added shape..to show image.. in nodes..
        image: "https://www.freeiconspng.com/uploads/letter-a-icon-4.png"
      }, {
        id: 2,
        label: "Node A_2",
        shape: "image",
        image: "https://www.freeiconspng.com/uploads/letter-a-icon-4.png"
      }, {
        id: 3,
        label: "Node B_1",
        shape: "image",
        image: "https://www.freeiconspng.com/uploads/letter-b-icon-png-23.png"
      }, {
        id: 4,
        label: "Node B_2",
        shape: "image",
        image: "https://www.freeiconspng.com/uploads/letter-b-icon-png-23.png"
      }, ];
      // Create edge data array
      allEdges = [{
          id: "1_2",
          from: 1,
          to: 2
        },
        {
          id: "2_3",
          from: 2,
          to: 3
        },
        {
          id: "1_3",
          from: 1,
          to: 3
        },
        {
          id: "4_1",
          from: 4,
          to: 1
        }
      ]
      var container = document.getElementById('mynetwork');
      nodes = new vis.DataSet(allNodes);
      var edges = new vis.DataSet(allEdges);
      var data = {
        nodes: nodes,
        edges: edges
      };
      //just added some styles..
      var options = {
        edges: {
          color: {
            color: '#CCC',
            highlight: '#A22'
          },
          width: 3,
          length: 275,
          hoverWidth: .05
        }
      };
      network = new vis.Network(container, data, options);
    
      //get a JSON object
      allNodes = nodes.get({
        returnType: "Object"
      });
      network.on("click", neighbourhoodHighlight);
    }
    
    function neighbourhoodHighlight(params) {
      // if something is selected:
      if (params.nodes.length > 0) {
        highlightActive = true;
        var i, j;
        var selectedNode = params.nodes[0];
        var degrees = 2;
    
        // mark all nodes as hard to read.
        for (var nodeId in allNodes) {
          allNodes[nodeId].opacity = 0.2; //change to opacity..
          if (allNodes[nodeId].hiddenLabel === undefined) {
            allNodes[nodeId].hiddenLabel = allNodes[nodeId].label;
            allNodes[nodeId].label = undefined;
          }
        }
        var connectedNodes = network.getConnectedNodes(selectedNode);
        var allConnectedNodes = [];
    
        // get the second degree nodes
        for (i = 1; i < degrees; i++) {
          for (j = 0; j < connectedNodes.length; j++) {
            allConnectedNodes = allConnectedNodes.concat(network.getConnectedNodes(connectedNodes[j]));
          }
        }
        // all second degree nodes get a different opacity and their label back
        for (i = 0; i < allConnectedNodes.length; i++) {
          allNodes[allConnectedNodes[i]].opacity = 0.10
          if (allNodes[allConnectedNodes[i]].hiddenLabel !== undefined) {
            allNodes[allConnectedNodes[i]].label = allNodes[allConnectedNodes[i]].hiddenLabel;
            allNodes[allConnectedNodes[i]].hiddenLabel = undefined;
          }
        }
    
        // all first degree nodes get their own opacity and their label back
        for (i = 0; i < connectedNodes.length; i++) {
          allNodes[connectedNodes[i]].opacity = undefined;
          if (allNodes[connectedNodes[i]].hiddenLabel !== undefined) {
            allNodes[connectedNodes[i]].label = allNodes[connectedNodes[i]].hiddenLabel;
            allNodes[connectedNodes[i]].hiddenLabel = undefined;
          }
        }
    
        // the main node gets its own opacity and its label back.
        allNodes[selectedNode].opacity = undefined;
        if (allNodes[selectedNode].hiddenLabel !== undefined) {
          allNodes[selectedNode].label = allNodes[selectedNode].hiddenLabel;
          allNodes[selectedNode].hiddenLabel = undefined;
        }
      } else if (highlightActive === true) {
        // reset all nodes
        for (var nodeId in allNodes) {
          allNodes[nodeId].opacity = undefined;
          if (allNodes[nodeId].hiddenLabel !== undefined) {
            allNodes[nodeId].label = allNodes[nodeId].hiddenLabel;
            allNodes[nodeId].hiddenLabel = undefined;
          }
        }
        highlightActive = false;
      }
    
      // transform the object into an array
      var updateArray = [];
      for (nodeId in allNodes) {
        if (allNodes.hasOwnProperty(nodeId)) {
          updateArray.push(allNodes[nodeId]);
        }
      }
      nodes.update(updateArray);
    }
    
    redrawAll();
    #mynetwork {
      width: 500px;
      height: 500px;
      border: 1px solid lightgray;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://visjs.github.io/vis-network/standalone/umd/vis-network.min.js"></script>
    
    <div id="mynetwork"></div>