Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change node color based on properties - neo4j

Tags:

neo4j

py2neo

I want to change the color of my nodes based on their properties:

Say I have many "Person" nodes. And I want those who live in New York to be red and those who live in Los Angeles to be blue. How would I write that. In cypher or in py2neo?

like image 583
Nicky Feller Avatar asked Nov 01 '15 20:11

Nicky Feller


4 Answers

The styling of nodes and relationships in Neo4j Browser is controlled by a graph style sheet (GRASS), a cousin of CSS. You can view the current style by typing :style in the browser. To edit it, you can click on nodes and relationships and pick colors and sizes, or you can view the style sheet (:style), download it, make changes, and drag-n-drop it back into the view window.

Unfortunately for your case, color can only be controlled a) for all nodes and all relationships or b) for nodes by label and relationships by type. Properties can only be used for the text displayed on the node/rel.

like image 120
jjaderberg Avatar answered Oct 16 '22 06:10

jjaderberg


It is not possible to interact with neo4j browser pro-grammatically. But the end goal could be achieved through a hack.

Even though I am a bit late here want to help others who might be finding a way. It is not possible to change the color of the nodes based on the property but there is a way it can be achieved by creating nodes based on the property. Keep in mind that after applying these queries your data wont be the same. So it is always a good idea to keep a backup of your data.

This is how labels are colored by default (Before):

enter image description here

Color based on the property

Suppose there is a label called Case with a property nationality and you want to color the nodes based on nationality. So following query could be used to create labels out of nationality property. For this you will need to install apoc library. check here for installation.

// BY NATIONALITY
MATCH (n:Case)
WITH DISTINCT n.nationality AS nationality, collect(DISTINCT n) AS persons
CALL apoc.create.addLabels(persons, [apoc.text.upperCamelCase(nationality)]) YIELD node
RETURN *

This will return all the people by nationality. Now you can color by country of nationality. Below shows an example.

enter image description here

Color based on the property and load with other labels

Lets say you also have a label called Cluster.The cases are attached to clusters via relationships. Just change the query to following to get the clusters with their relationships to cases.

//BY NATIONALITY WITH CLUSTERS
MATCH (n:Case),(c:Cluster)
WITH DISTINCT n.nationality AS nationality, 
    collect(DISTINCT n) AS persons,
    collect(DISTINCT c) AS clusters
CALL apoc.create.addLabels(persons, [apoc.text.upperCamelCase(nationality)]) YIELD node
RETURN *

It will return cases and clusters with all the relationships. Below shows example.

enter image description here

Please leave an up vote if this was helpful and want to let others know that this is an acceptable answer. Thank you.

like image 27
aimme Avatar answered Oct 16 '22 06:10

aimme


You cannot include formatting of the output in Cypher queries in the neo4j browser. Currently, the only way is to change the graph view manually or load a graph style file.

See tutorial here: http://neo4j.com/developer/guide-neo4j-browser/

Also, you cannot interact with the neo4j browser from py2neo.

like image 44
Martin Preusse Avatar answered Oct 16 '22 05:10

Martin Preusse


If you are happy setting the color through a graphical user interface rather than programatically, Neo4j also supplies a data exploration addon named bloom. When using this addon (now automatically installed when using neo4j desktop), it is possible to set node color based on its properties. In the example below, movies released after 2002 are colored green.

enter image description here

like image 22
Len Blokken Avatar answered Oct 16 '22 06:10

Len Blokken