cytoscape / cytosnap

A Node.js package that renders images of Cytoscape.js graphs on the server using Puppeteer
MIT License
58 stars 11 forks source link

Allow options to be specified as non-JSON data #5

Closed maxkfranz closed 8 years ago

maxkfranz commented 8 years ago

e.g.

snap.shot(function(){
  return {
    elements: [ /* ... */ ],
    style: [
      {
        selector: 'node',
        style: {
          'background-color': function( node ){ return Math.random() > 0.5 ? 'red' : 'blue'; }
        }
      }
    ]
  };
});

Function would get executed in phantom context, so there can't be references outside this scope.

didix16 commented 3 years ago

Hi. I had to inspect the code to understand how propertly pass a function for "label" style...

If you pass only the code that @maxkfranz exposed then doest not works. You have to put the whole array inside a function being returned, otherway it won't work.

This is how I manage it to work:

...
snap.shot({

        elements: {
            nodes,
            edges
        },
        style: function(){ return [
            {
                selector: "node",
                css: {
                    label: "data(id)"
                }
            },
            {
                selector: "edge",
                css: {
                    label: function(elem){ return elem.data("weight") > 0 ? elem.data("weight") : " "}
                }
            }
        ]},
...

Docs should tell that... It is very confusing saying "// a cytoscape.js stylesheet in json format (or a function that returns it)" without say if you want to make some css property as functions, you should use a function wrapper for style property option.

(In cytoscape.js you can pass a JSON with property being a function. In cytosnap no. I have to make a wrapper function to get the same result)

Thanks