Graph Visualization and Neo4j – Part Two

If you’re into NoSQL and Graph Databases like Neo4j, then you’ll probably tend to be working on back-end development. If you’re lucky enough to work in a team of specialists, some UX guy will come up with user requirements, hand them off to a UI gal for design, who will then pass it on to a Javascript Ninja to slice it together and they’ll just ask you provide the data and stuff it in a JSON object.

If you’re not so lucky and are working on pet projects by yourself then you’ll have to do it all. So I wanted to give you a little nudge into learning a visualization framework. Since my most popular blog post so far has been Graph Visualization and Neo4j and we’ve already seen one example that you’ll probably want to customize in your projects, we’ll stick with processing.js, and in the future I can do a little intro on D3.js, Unveil.js and maybe something a little crazier like VVVV.js.

So getting started is really easy. We’ll create an html document, add the minified processing javascript library and create a canvas element to put our visualization.

<!DOCTYPE html>
<html>
	<head>
		<title>Hello World - Processing.js</title>
		<script src="processing-1.3.6.min.js"></script>
	</head>
	<body>
		<canvas data-src="helloworld.pjs"></canvas>
	</body>
</html>

All right, let’s create the helloworld.pjs we reference as our canvas data source.

void setup() {
  size(200, 200);
  ellipse(50, 50, 10, 10);
}

The size method is setting up a square 200 pixel by 200 pixel canvas, and we draw an ellipse. The first two values are the x and y coordinates for the center of our ellipse, the second two values are the x and y radius.

void setup() {
  size(200, 200);          // 0,0 starts at top left
  background(120,140,160); // individual RGB values
  stroke(0);               // apply 0 to all values
  ellipse(50, 50, 25, 25); // x, y cntr and x, y radius
}

Processing uses the standard Red Green Blue colors, so we’ll change that default gray background to a nice blue. Stroke is the pen outline of our drawings, and when passing just a single value, it sets all RGB values to it. In this case 3 zeros will give us black.

void setup() {
  size(200, 200);
  background(120,140,160);
  stroke(0);
  frameRate(30);
}

void draw() {
  ellipse(mouseX, mouseY, 25, 25);
}

Processing is mouse aware and automatically populates the mouseX and mouseY values for you. Processing calls draw() right after setup() which then continuously executes the lines of code contained inside its block. The frameRate method sets the number of times per second that draw will be called.

void setup() {
  size(200, 200);
  noStroke();
  frameRate(30);
 }

void draw() {
  background(120,140,160);
  ellipse(mouseX, height/2, mouseY/2+10, mouseY/2+10);
  ellipse(width-mouseX, 
          height/2, 
          ((height-mouseY)/2)+10, 
          ((height-mouseY)/2)+10);
}

You can get rid of the outline with noStroke and get creative with your mouse coordinates and use them to control the size as well as position of your objects.

void setup() {
  size(200, 200);
  noStroke();
  background(120,140,160);
  ellipse(width/2, height/2, 50, 50);
  fill(50,50,180);
  arc(width/2, height/2, 50, 50, 0, PI/4);
 }

The arc method takes x and y coordinates, a height and width like ellipse, but also a start and end point in radians or degrees.

void setup() {
  size(200, 200);
  noStroke();
  background(120,140,160);
  float arc_start;
  float arc_end;
  for(int i=0; i < 8; i = i + 1) {
    fill(i * 20, 120 + ((4-i) * 20),250 - (i * 20));
    arc_start = radians(i * 45);
    arc_end = radians((i + 1) * 45);
    arc(width/2, height/2, 
        100, 100, 
        arc_start, arc_end);
  }
  fill(255);
  ellipse(width/2, height/2, 50, 50);
 }

The fill method colors our objects and we can use a for loop to create multiple arcs as well as rotate their angles. We add a white ellipse at the end and now we’re starting to resemble something we’ve seen before.

You can see the live versions of these canvases on Heroku and download all the code from https://github.com/maxdemarzi/processing_js_intro.

Update: Here is the video from Fosdem.

That was really just a small taste of processing.js. You can read Pomax’s guide to Processing.js to get a more complete introduction, watch a Processing 101 video series or get another quick intro to processing.js on this video:

Tagged , , , ,

3 thoughts on “Graph Visualization and Neo4j – Part Two

  1. dam4222 says:

    amazing. can you go over how to create that rollover effect? Real nice stuff.

Leave a comment