Using Neo4j labels with KeyLines graph visualization

This page is from our blog archive

It’s still useful, but it’s worth checking out this page on visualizing Neo4j for more up-to-date information.

Fellow fans of the open-source Neo4j graph database will, by now, be pretty familiar with Neo4j 2.0.

The latest Neo4j major release was a big leap forward for the world’s most popular graph database, bringing a host of improvements and enhancements, including:

  • A more concise and declarative Cypher query language
  • A hugely improved Neo4j Browser – a great d3.js tool for developers who need to visualize their data model
  • The addition of labels to the data model

Nothing that KeyLines relies upon was deprecated in the release, so any deployments of KeyLines running from a Neo4j database were virtually unaffected (excluding a need to migrate from the 1.x data format, obviously).

But the addition of those labels opens up new opportunities to KeyLines/Neo4j users.

Using Neo4j labels for visualization

Neo4j is a ‘schema optional’ graph database. Whilst this has its benefits in terms of extensibility, it has had a negative effect on overall performance.

The addition of labels as indexed properties provides a hybrid solution. It keeps the flexibility of the graph model, but still offers a fast way to store and query the attributes of data entities.

For KeyLines applications, this means the developer no longer needs to infer a style from several different properties, but can instead simply have a ‘per-label’ template for your visualization.

For example, our demo uses the (now ubiquitous) Matrix IMDB data. Here’s how it used to look:

Old Neo4j KeyLines demo

The color of the node represents the gender of the actor referenced. To do this with Neo4j 1.x, each node needed a property for gender, role (actor, director, etc) and more:

START m=node(89)
MATCH m<-[r:ACTS_IN*]-a
RETURN m, r, a

With labels, we can now simply tag each node with Actor, Movie or Director and gender tags. As labels are indexed, it means queries can filter nodes with those properties directly, resulting in much faster (and more readable) queries with cleaner results:

START m=node(89)
MATCH (m:Movie)<-[r:ACTS_IN*]-(a:Actor)
RETURN m, r, a

If you want to use labels to style the nodes we’re retrieving we can tweak the Cypher query to return labels as well:

START m=node(89)
MATCH (m:Movie)<-[r:ACTS_IN*]-(a:Actor)
RETURN m, labels(m), r, a, labels(a)

Labels can also be added to edges, meaning we can easily add labels to the links in our KeyLines chart. For example, adding the name of the character each actor played in a movie.

We can also tag our movie with a label then, by binding it to a document store of movie posters, we can easily and more quickly pull in the poster of each title:

Neo4j demo

The use of style templates together with labels can help to write more concise and readable code:

function styleNode(node, labels){
  if( isActor(labels) ){
     // Use style templates for genders
    if( isMale(labels) ){
    } else {
  if( isMovie(labels) ){
    // the addMovieStyle works out the URL for the movie poster
     // and add an oscar glyph if the movie won any
  return node;

There it is. A simple change, but one that offers fresh opportunities to add more contextual information to your chart quickly and easily.

Get in touch if you would like any help or would like to evaluate the KeyLines SDK.

You can persist back to the database

Just a footnote for users of the KeyLines SDK… we’ve also added some source code to the Neo4j demo that explains how to write back to the database. It’s a simple operation, but a useful one!

Get Started with KeyLines and Neo4j

Want to know more about how KeyLines and Neo4j work together? Check out our getting started guide.

How can we help you?

Request trial

Ready to start?

Request a free trial

Learn more

Want to learn more?

Read our white papers


Looking for success stories?

Browse our case studies

Registered in England and Wales with Company Number 07625370 | VAT Number 113 1740 61
6-8 Hills Road, Cambridge, CB2 1JP. All material © Cambridge Intelligence 2024.
Read our Privacy Policy.