carz
carz

Reputation: 11

Change edge label after graph was rendered

I'd like to change the label of a few specific edges after a certain event. What would be the best way to do so?

Do I need to remove these edges, change the label tag and then add them again to cytoscape?

Best way would be something like "refresh graph".

Thanks in advance!

Upvotes: 1

Views: 559

Answers (1)

Stephan T.
Stephan T.

Reputation: 6074

Here is a simple way to edit the label of any cy element:


// unbind first to prevent issues with binding conflicts
cy.unbind('click');

// change label of node to new text
cy.bind('click', 'node', function (evt) {
  var target = evt.target;
  target.data('label', 'new node label');
});

// change label of node to new text
cy.bind('click', 'edge', function (evt) {
  var target = evt.target;
  target.data('label', 'new edge label');
});

Here is a working demonstration:

var cy = (window.cy = cytoscape({
  container: document.getElementById("cy"),

  boxSelectionEnabled: false,
  autounselectify: true,

  style: [{
      selector: "node",
      css: {
        "label": "data(label)",
        "text-valign": "center",
        "text-halign": "center",
        "height": "60px",
        "width": "60px"
      }
    },
    {
      selector: "edge",
      css: {
        "target-arrow-shape": "triangle"
      }
    },
    {
      selector: "edge[label]",
      css: {
        label: "data(label)",
        "text-rotation": "autorotate",
        "text-margin-x": "0px",
        "text-margin-y": "0px"
      }
    }
  ],
  elements: {
    nodes: [{
        data: {
          id: "Peter",
          label: "Peter"
        }
      },
      {
        data: {
          id: "Claire",
          label: "Claire"
        }
      },
      {
        data: {
          id: "Mike",
          label: "Mike"
        }
      },
      {
        data: {
          id: "Rosa",
          label: "Rosa"
        }
      }
    ],
    edges: [{
        data: {
          source: "Peter",
          target: "Claire",
          label: "edge 01"
        }
      },
      {
        data: {
          source: "Claire",
          target: "Mike",
          label: "edge 02"
        }
      },
      {
        data: {
          source: "Mike",
          target: "Rosa",
          label: "edge 03"
        }
      },
      {
        data: {
          source: "Rosa",
          target: "Peter",
          label: "edge 04"
        }
      }
    ]
  },
  layout: {
    name: "circle"
  }
}));

// This is the important part
cy.unbind('click');
cy.bind('click', 'node', function(evt) {
  var target = evt.target;
  target.data('label', 'new node label');
});
cy.bind('click', 'edge', function(evt) {
  var target = evt.target;
  target.data('label', 'new edge label');
});
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 100%;
  width: 75%;
  position: absolute;
  left: 0;
  top: 0;
  float: left;
}
<html>

<head>
  <meta charset=utf-8 />
  <meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
  <script src="https://unpkg.com/[email protected]/dist/cytoscape.min.js"></script>
</head>

<body>
  <div id="cy"></div>
</body>

</html>

All you have to do now is to call the .data() function on the edges array (one by one).

Upvotes: 2

Related Questions