Reputation: 73
On the localhost page launched to use Neo4j, it is easy to color specific nodes in a different way. Following the movie example included in Neo4j; persons in red, movies in purple and with a click of the button change one to orange.
As an expansion on this, I would like to color nodes of the same type, but with a different property in a different way. Following the example; add gender as a property to each person node, followed by coloring men blue and women red.
Is it possible to realize this in the current build? I am working with Neo4j Community 2.1.7.
Edit: There was a request for an piece of the .grass file responsible for the visualization. Here is a small excerpt responsible for coloring a type of node.
node.Page {
color: #30B6AF;
border-color: #46A39E;
text-color-internal: #FFFFFF;
diameter: 80px;
border-width: 2px;
caption: '{pagename}';
font-size: 10px;}
Upvotes: 4
Views: 5803
Reputation: 6818
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):
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.
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.
Please leave an up vote if this was helpful and want to let others know that this is an acceptable answer. Thank you.
Upvotes: 3
Reputation: 31
I have found a workaround - virtual subgraph. Based on the APOC page:
"you can create visual representation of data that are not in the graph"
Basically you can create virtual copy of nodes using apoc.create.vNode and assign different types to those virtual nodes based on the property value. In your case there would be two new types of nodes men and women. Then you are free to assign different colour/style to the 'men' nodes and 'women' nodes.
Your code should be something like:
UNWIND ['men', 'women'] as gender
MATCH (n:Person {Gender:gender})
CALL apoc.create.vNode([gender],{id:n.id}) yield node as vPerson
RETURN vPerson
if you want to show the relationships (between same sex), comment out the last line above and add the following query:
WITH apoc.map.groupBy(collect(vPerson),'id') as vPersons, gender
WITH vPersons, [(p1:Person {Gender:gender})-[r]->(p2:Person {Gender:gender}) | apoc.create.vRelationship(vPersons[p1.id], type(r), {}, vPersons[p2.id])] as rels
RETURN vPersons, rels
Upvotes: 0
Reputation: 10856
I'm pretty sure this is not possible. If you click on the "Style" tab of the pop-up and click on "View stylesheet" you'll see in the GRASS file that the colors are tied to node labels. If you want a different sort of visualization, you could submit a pull request:
https://github.com/neo4j/neo4j/tree/master/community/browser
It's important to note that the GRASS format was created for Neo4j and, as far as I know, isn't used anywhere else, so don't look for standards documentation anywhere.
Alternatively, depending on what you're doing, you could create your own visualization. For the web there are a number of libraries like Sigma.js or D3. I've also been wanting to play with this one (based on Sigma.js), but haven't gotten a chance yet:
https://github.com/Linkurious/linkurious.js
Upvotes: 1