Search code examples
javascriptcytoscape.js

Conditional styling for cytoscape


I want to add conditional styles for cytoscape elements (nodes and edges). I read conditional styles for cytoscape but this seems to add style conditionals after the graph is rendered which I think is not very good when there are a lot of conditions.

        style: {
            "content": "data(label)",
            "shape": "round-rectangle",
            "width": "150px",
            "text-valign": "center",
            "background-color": "data(error)" ? "red" : "green"
        }

I want to set background based on the value of error property. This does not work as "data(error)" is a string. I tried "data(error)" === "true" but that also did not work. Can you please help me out?


Solution

  • You can use a simple function block to achieve this. The function has one parameter, which is the current node. You can access the node by calling the .data(attr) method:

    document.addEventListener("DOMContentLoaded", function() {
      var cy = (window.cy = cytoscape({
        container: document.getElementById("cy"),
    
        layout: {
          name: "klay"
        },
    
        style: [{
            selector: "node",
            style: {
              // You can use function(node){} instead of ES6 syntax here
              "background-color": node => node.data('error') ? 'red' : 'green',
              "content": "data(label)",
              "shape": "round-rectangle",
              "width": "50px",
              "text-valign": "center"
            }
          },
          {
            selector: "edge",
            style: {
              "curve-style": "bezier",
              "target-arrow-shape": "triangle",
              "line-color": "#dd4de2",
              "target-arrow-color": "#dd4de2",
              opacity: 0.5
            }
          }
        ],
        elements: {
          nodes: [{
              data: {
                id: "n0",
                label: "n0",
                error: true
              }
            },
            {
              data: {
                id: "n1",
                label: "n1",
                error: false
              }
            },
            {
              data: {
                id: "n2",
                label: "n2",
                error: false
              }
            },
            {
              data: {
                id: "n3",
                label: "n3",
                error: false
              }
            },
            {
              data: {
                id: "n4",
                label: "n4",
                error: true
              }
            },
            {
              data: {
                id: "n5",
                label: "n5",
                error: true
              }
            },
            {
              data: {
                id: "n6",
                label: "n6",
                error: false
              }
            },
            {
              data: {
                id: "n7",
                label: "n7",
                error: false
              }
            },
            {
              data: {
                id: "n8",
                label: "n8",
                error: false
              }
            },
            {
              data: {
                id: "n9",
                label: "n9",
                error: true
              }
            },
            {
              data: {
                id: "n10",
                label: "n10",
                error: true
              }
            },
            {
              data: {
                id: "n11",
                label: "n11",
                error: false
              }
            },
            {
              data: {
                id: "n12",
                label: "n12",
                error: true
              }
            },
            {
              data: {
                id: "n13",
                label: "n13",
                error: false
              }
            },
            {
              data: {
                id: "n14",
                label: "n14",
                error: true
              }
            },
            {
              data: {
                id: "n15",
                label: "n15",
                error: false
              }
            }
          ],
          edges: [{
              data: {
                source: "n0",
                target: "n1"
              }
            },
            {
              data: {
                source: "n1",
                target: "n2"
              }
            },
            {
              data: {
                source: "n1",
                target: "n3"
              }
            },
            {
              data: {
                source: "n2",
                target: "n4"
              }
            },
            {
              data: {
                source: "n4",
                target: "n5"
              }
            },
            {
              data: {
                source: "n4",
                target: "n6"
              }
            },
            {
              data: {
                source: "n6",
                target: "n7"
              }
            },
            {
              data: {
                source: "n6",
                target: "n8"
              }
            },
            {
              data: {
                source: "n8",
                target: "n9"
              }
            },
            {
              data: {
                source: "n8",
                target: "n10"
              }
            },
            {
              data: {
                source: "n10",
                target: "n11"
              }
            },
            {
              data: {
                source: "n11",
                target: "n12"
              }
            },
            {
              data: {
                source: "n12",
                target: "n13"
              }
            },
            {
              data: {
                source: "n13",
                target: "n14"
              }
            },
            {
              data: {
                source: "n13",
                target: "n15"
              }
            }
          ]
        }
      }));
    });
    body {
      font-family: helvetica neue, helvetica, liberation sans, arial, sans-serif;
      font-size: 14px;
    }
    
    #cy {
      position: absolute;
      left: 0;
      top: 0;
      bottom: 0;
      right: 0;
      z-index: 999;
    }
    <html>
    
    <head>
      <script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script>
      <script src="https://unpkg.com/[email protected]/klay.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/cytoscape-klay.min.js"></script>
    </head>
    
    <body>
      <div id="cy"></div>
    </body>
    
    </html>

    I would generally suggest to use classes instead of conditional css though:

    document.addEventListener("DOMContentLoaded", function() {
      var cy = (window.cy = cytoscape({
        container: document.getElementById("cy"),
    
        layout: {
          name: "klay"
        },
    
        style: [{
            selector: "node",
            style: {
              "background-color": 'green',
              "content": "data(label)",
              "shape": "round-rectangle",
              "width": "50px",
              "text-valign": "center"
            }
          },
          {
            selector: ".danger",
            style: {
              "background-color": 'red'
            }
          },
          
          {
            selector: "edge",
            style: {
              "curve-style": "bezier",
              "target-arrow-shape": "triangle",
              "line-color": "#dd4de2",
              "target-arrow-color": "#dd4de2",
              opacity: 0.5
            }
          }
        ],
        elements: {
          nodes: [{
              data: {
                id: "n0",
                label: "n0",
                error: true
              },
              classes: 'danger'
            },
            {
              data: {
                id: "n1",
                label: "n1",
                error: false
              }
            },
            {
              data: {
                id: "n2",
                label: "n2",
                error: false
              }
            },
            {
              data: {
                id: "n3",
                label: "n3",
                error: false
              }
            },
            {
              data: {
                id: "n4",
                label: "n4",
                error: true
              }
            },
            {
              data: {
                id: "n5",
                label: "n5",
                error: true
              },
              classes: 'danger'
            },
            {
              data: {
                id: "n6",
                label: "n6",
                error: false
              }
            },
            {
              data: {
                id: "n7",
                label: "n7",
                error: false
              }
            },
            {
              data: {
                id: "n8",
                label: "n8",
                error: false
              }
            },
            {
              data: {
                id: "n9",
                label: "n9",
                error: true
              }
            },
            {
              data: {
                id: "n10",
                label: "n10",
                error: true
              }
            },
            {
              data: {
                id: "n11",
                label: "n11",
                error: false
              }
            },
            {
              data: {
                id: "n12",
                label: "n12",
                error: true
              }
            },
            {
              data: {
                id: "n13",
                label: "n13",
                error: false
              }
            },
            {
              data: {
                id: "n14",
                label: "n14",
                error: true
              }
            },
            {
              data: {
                id: "n15",
                label: "n15",
                error: false
              }
            }
          ],
          edges: [{
              data: {
                source: "n0",
                target: "n1"
              }
            },
            {
              data: {
                source: "n1",
                target: "n2"
              }
            },
            {
              data: {
                source: "n1",
                target: "n3"
              }
            },
            {
              data: {
                source: "n2",
                target: "n4"
              }
            },
            {
              data: {
                source: "n4",
                target: "n5"
              }
            },
            {
              data: {
                source: "n4",
                target: "n6"
              }
            },
            {
              data: {
                source: "n6",
                target: "n7"
              }
            },
            {
              data: {
                source: "n6",
                target: "n8"
              }
            },
            {
              data: {
                source: "n8",
                target: "n9"
              }
            },
            {
              data: {
                source: "n8",
                target: "n10"
              }
            },
            {
              data: {
                source: "n10",
                target: "n11"
              }
            },
            {
              data: {
                source: "n11",
                target: "n12"
              }
            },
            {
              data: {
                source: "n12",
                target: "n13"
              }
            },
            {
              data: {
                source: "n13",
                target: "n14"
              }
            },
            {
              data: {
                source: "n13",
                target: "n15"
              }
            }
          ]
        }
      }));
    });
    body {
      font-family: helvetica neue, helvetica, liberation sans, arial, sans-serif;
      font-size: 14px;
    }
    
    #cy {
      position: absolute;
      left: 0;
      top: 0;
      bottom: 0;
      right: 0;
      z-index: 999;
    }
    <html>
    
    <head>
      <script src="https://unpkg.com/cytoscape/dist/cytoscape.min.js"></script>
      <script src="https://unpkg.com/[email protected]/klay.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/cytoscape-klay.min.js"></script>
    </head>
    
    <body>
      <div id="cy"></div>
    </body>
    
    </html>