Search code examples
jquerycytoscape.jscytoscape

CytoscapeJS background color grey


I have a little example of a Cytoscape JS website with adjusted background color. However, to make background visible one first has to click one of the nodes. Why is that ? How can I see the background color immediately ?

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://pagecdn.io/lib/cytoscape/3.10.2/cytoscape.min.js"></script>



<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    <style media="screen">
      #cy {
        width: 100%;
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="cy"></div>


  </body>
</html>

<script type="text/javascript">
    $(document).ready(function(){
      var cy = cytoscape({
      container: document.getElementById('cy'),
      elements: {
        nodes: [
          { data: { id: 'n1'}, style: {  'pie-size': '80%',
                                'pie-1-background-color': '#E8747C',
                                'pie-1-background-size': '75%',
                                'pie-2-background-color': '#74CBE8',
                                'pie-2-background-size': '25%'}  },
          { data: { id: 'n2'}, style: {'background-color': '#E8747C'}  }
        ],
        edges: [
          { data: { source: 'n1', target: 'n2' } }
        ]
      }
    });
    })
</script>

Solution

  • Normally, you specify these styles in the cytoscape.js stylesheet (see this section in the docs):

    $(document).ready(function() {
      var cy = cytoscape({
        container: document.getElementById('cy'),
        style: [{
            selector: '.quarter',
            css: {
              'pie-size': '80%',
              'pie-1-background-color': '#E8747C',
              'pie-1-background-size': '75%',
              'pie-2-background-color': '#74CBE8',
              'pie-2-background-size': '25%'
            }
          },
          {
            selector: '.uniform',
            css: {
              'background-color': '#E8747C'
            }
          }
        ],
        elements: {
          nodes: [{
              data: {
                id: 'n1'
              },
              classes: ['quarter']
            },
            {
              data: {
                id: 'n2'
              },
              classes: ['uniform']
            }
          ],
          edges: [{
            data: {
              source: 'n1',
              target: 'n2'
            }
          }]
        }
      });
    })
    body {
      font: 14px helvetica neue, helvetica, arial, sans-serif;
    }
    
    #cy {
      height: 100%;
      width: 100%;
      left: 0;
      top: 0;
      float: left;
      position: absolute;
    }
    <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/cytoscape@3.3.0/dist/cytoscape.min.js">
      </script>
      <script src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
    </head>
    
    <body>
      <div id="cy"></div>
    </body>
    
    </html>