neo4j-contrib / neovis.js

Neo4j + vis.js = neovis.js. Graph visualizations in the browser with data from Neo4j.
Apache License 2.0
1.59k stars 324 forks source link

How to change the color shade of nodes according to a node attribute? #370

Closed hantongyou closed 10 months ago

hantongyou commented 10 months ago

For example, I want to use neovis to draw a influence graph of each node. And there is an attribute "influence" which is between 0 and 1, now how to use this attribute to adjust the node color shape? When influence is smaller the color would be lighter.

thebestnom commented 10 months ago

You can use advanved config to put a function in the color that the color would be a function that's get the attribute value and the output would be a color probably from a color Maybe somthing like this https://stackoverflow.com/questions/10848990/rgb-values-to-0-to-1-scale

hantongyou commented 10 months ago

however my code did not work like I expected. Here is my code:

{
[NeoVis.NEOVIS_ADVANCED_CONFIG]:{
                static:{
                    // group:labels[i],
                },
                function:{
                    title:(node)=>{
                        // console.log(Object.keys(node.properties));
                        return NeoVis.objectToTitleHtml(node, Object.keys(node.properties))},
                    label:(node)=>{
                        if(node.properties.indexName.length>=10)
                            return (node,node.properties.indexName.substr(0,10)+"...")
                        else
                            return (node,node.properties.indexName+"...")
                    },
                    color:(node)=>{
                        return (node,(0,0,255*node.properties.Contribution))
                    }

                    }
            }
}
hantongyou commented 10 months ago

It seems like nothing has changed.

image

thebestnom commented 10 months ago

The color function should return a string https://visjs.github.io/vis-network/docs/network/nodes.html

hantongyou commented 10 months ago

Thanks! Now i use opacity instead of color the problem has been fixed perfectly. image