In this tutorial, we will focus on creating an interactive network visualization that will allow us to get details about the nodes in the network, rearrange the network into different layouts, and sort, filter, and search through our data.

In this example, each node is a song. The nodes are sized based on popularity, and colored by artist. Links indicate two songs are similar to one another.

Try out the visualization on different songs to see how the different layouts and filters look with the different graphs.

Technology

This visualization is a JavaScript based web application written using the powerful D3 visualization library. jQuery is also used for some DOM element manipulation. Both frameworks are included in the js/libs directory of the source code.

If you hate CoffeeScript, you can always compile the code to JavaScript and start there.The code itself is actually written in CoffeeScript, a little language that is easy to learn, and compiles down to regular JavaScript. Why use CoffeeScript? I find that the reduced syntax makes it easier to read and understand what the code is doing. While it may seem a bit intimidating to learn a whole new ‘language’, there are just a few things you need to know about CoffeeScript to be a pro.

Quick CoffeeScript Notes

Functions

First and foremost, This is what a function looks like:

functionName = (input) ->
results = input * 2
results

So the input parameters are inside the parentheses. The -> indicates the start of the implementation. If a function’s implementation is super simple, this can all go on one line.

cube = (x) -> x * x * x

A function returns the last thing executed, so you typically don’t need a return statement, but you can use one if you like.

Indentation matters

The other main syntactical surprise is that, similar to Python, indentation is significant and used to denote code hierarchy and scope. We can see an example of this in the function above: The implementation is indented.

In practice, this isn’t too big of an issue: just hit the Tab key instead of using curly braces – {, } – and you are set.

Semicolons and Parentheses

Taking a page from Ruby, semicolons are not needed and should be avoided in CoffeeScript.

Also, parentheses are optional in many places. While this can get confusing, I typically use parentheses unless they are around a multi-line function that is an input argument into another function. If that doesn’t make sense, don’t worry – the code below should still be easy to follow.

In-browser CoffeeScript

Using CoffeeScript is made simpler by the fact that our CoffeeScript code can be compiled into JavaScript right in the browser. Our index.html includes the CoffeeScript compiler which in turn compiles to JavaScript any script listed as text/coffeescript :

It’s just that simple. When vis.coffee loads, it will be parsed and compiled into JavaScript before running. For production, we would want to do this compilation beforehand, but this let’s us get started right away.

Setting up the Network

You don’t have to organize your code like this, if it seems like too much work. But the encapsulation makes it easier to change your input data laterOk, let’s get started with coding this visualization. We are going to be using a simplified version of Mike Bostock’s (the creator of D3) reusable chart recommendations to package our implementation. What this means for us is that our main network code will be encapsulated in a function, with getters and setters to allow interaction with the code from outside. Here is the general framework:

So the Network function defines a closure which scopes all the variables used in the visualization, like width and height. The network function is where the main body of the code goes, and is returned by Network at the end of the implementation.

Functions defined on network, like network.toggleLayout() can be called externally while functions like update are ‘private’ functions and can only be called by other functions inside Network. You can think of it as the same abstraction that classes provide us in object-oriented programing. Here is how we create a new network :

So here, myNetwork is the value Network() returns – namely the function called network. Then we call this network function, passing in the id of the div where the visualization will live, and the data to visualize.

Network Data Format

From above, we see we are passing in the data from songs.json to be visualized. How is this data organized?

Since a network defines connections between nodes as well as the data contained in the nodes themselves, it would be difficult to define as a simple ‘spreadsheet’ or table of values. Instead, we use JSON to capture this structure with as little overhead as possible.

The input data for this visualization is expected to follow this basic structure:</p

This is a JSON object (just like a JavaScript object). If you haven’t looked at JSON before, then let’s take a look now! The format is pretty straight forward (as it’s just JavaScript).

This object requires two names, nodes and links. Both of these store arrays of other objects. nodes is an array of nodes. Each node object needs some fields used in the visualization as well as an id which uniquely identifies that node.

The objects in the links array just need source and target. Both of these point to @id@’s of nodes.

The traditional/default method in D3 of defining a link’s source and target is to use their position in the nodes array as the value. Since we are going to be filtering and rearranging these nodes, I thought it would be best to use values independent of where the nodes are stored. We will see how to use these id’s in a bit.

Moved by the Force

We will start with the default force-directed layout that is built into D3 . Force-based network layouts are essentially little physics simulations. Each node has a force associated with it (hence the name), which can repel (or attract) other nodes. Links between nodes act like springs to draw them back together. These pushing and pulling forces work on the network over a number of iterations, and eventually the system finds an equilibrium.

Force-directed layouts usually result in pretty good looking network visualizations, which is why they are so popular. D3’s implementation does a lot of work to make the physics simulation efficient, so it stays fast in the browser.

Example of force-directed layout from our song network demo

To start, as is typical with most D3 visualizations, we need to create a svg element in our page to render to. Lets look at the network() function which performs this action:

First we declare a bunch of variables that will be available to us inside the Network closure. They are ‘global’ and available anywhere in Network. Note that our D3 force directed layout is one such global variable called force.

Inside our network function, we start by tweaking the input data. Then we use D3 to append an svg element to the input selection element. linksG and nodesG are group elements that will contain the individual lines and circles used to create the links and nodes. Grouping related elements is a pretty common strategy when using D3. Here, we create the linksG before the nodesG because we want the nodes to sit on top of the links.

The update function is where most of the action happens, so let’s look at it now.

# The update() function performs the bulk of the
# work to setup our visualization based on the
# current layout/sort/filter.
#
# update() is called everytime a parameter changes
# and the network needs to be reset.
update = () ->
# filter data to show based on current filter settings.
curNodesData = filterNodes(allData.nodes)
curLinksData = filterLinks(allData.links, curNodesData)
# sort nodes based on current sort and update centers for
# radial layout
if layout == "radial"
artists = sortedArtists(curNodesData, curLinksData)
updateCenters(artists)
# reset nodes in force layout
force.nodes(curNodesData)
# enter / exit for nodes
updateNodes()
# always show links in force layout
if layout == "force"
force.links(curLinksData)
updateLinks()
else
# reset links so they do not interfere with
# other layouts. updateLinks() will be called when
# force is done animating.
force.links([])
# if present, remove them from svg
if link
link.data([]).exit().remove()
link = null
# start me up!
force.start()

The final version of this visualization will have filtering and sorting capabilities, so update starts with filtering the nodes and links of the total dataset. Then sorts if necessary. We will come back and hit these functions later. For the basic force-directed layout without all these bells and whistles to come, all we really care about is:

The force’s nodes array is set to our currently displayed nodes, erasing any previous nodes in the simulation. Then we update the visual display of the nodes in the visualization. This same pattern is then followed for the links.

Remember: the force layout doesn’t add circles and lines for you. It just tells you where to put them.It is important to realize that in D3, the nodes and links in the force layout don’t automatically get visualized in any way. This is to say that there is a separation between the force-directed physics simulation and any visual mapped to that simulation.

To create this visual representation associated with this force-directed simulation, we will need to bind to the same data being used, which we do in updateNodes and updateLinks:

Finally, some D3 visualization code! Looking at updateNodes, we select all circle.node elements in our nodeG group (which at the very start of the execution of this code, will be empty). Then we bind our filtered node data to this selection, using the data function and indicating that data should be identified by its id value.

The enter() function provides an access point to every element in our data array that does not have a circle associated with it. When append is called on this selection, it creates a new circle element for each of these representation-less data points. The attr and style functions set values for each one of these newly formed circles. When a function is used as the second parameter, like:

.attr("r", (d) -> d.radius)

The d is the data associated with the visual element, which is passed in automatically by D3. So with just a few lines of code we create and style all the circles we need.

Because we will be filtering our data to add and remove nodes, there will be times where there is a circle element that exists on screen, but there is no data behind it. This is where the exit() function comes into play. exit() provides a selection of elements which are no longer associated with data. Here we simply remove them using the remove function.

If the concepts of enter() and exit() are still not clicking, check out the Thinking With Joins and three little circles tutorials. These selections are a big part of D3, so it is worth having a feel for what they do.

Configuring the Force

There has to be a ton of Star Wars jokes I should be making… but I can’t think of any.In order to get the force-directed graph working the way we want, we need to configure the force layout a bit more. This will occur in the setLayout function. For the force-directed layout, our force configuration is pretty simple:

force.on("tick", forceTick)
.charge(-200)
.linkDistance(50)

Here, charge is the repulsion value for nodes pushing away from one another and linkDistance is the maximum length of each link. These values allow the nodes to spread out a bit.

The forceTick function will be called each iteration (aka ‘tick’) of the simulation. This is where we need to move our visual representations of the nodes and links of the network to where they are in the simulation after this tick. Here is forceTick:

Pretty straightforward. The D3 simulation is modifying the x and y values of each node during the simulation. Thus, for each tick, we simply need to move the circles representing our nodes to where x and y are. The links can be moved based on where their source and target nodes are.

Setting Up Data

Speaking of source and target, we need to go back and see how to deal with our initial data where we were using the id of a node in place of the node’s index in the nodes array. Here is setupData which is the very first thing executed in our network code:

setupData is doing a few things for us, so let’s go through it all. First, we are using a d3.scale to specify the possible values that the circle radii can take, based on the extent of the playcount values. Then we iterate through all the nodes, setting their radius values, as well as setting their x and y values to be within the current visualization size. Importantly, nodes are not automatically sized by any particular data value they contain. We are just adding radius to our data so we can pull it out in updateNodes. The x and y initialization is just to reduce the time it takes for the force-directed layout to settle down.

Finally, we map node id’s to node objects and then replace the source and target in our links with the node objects themselves, instead of the id’s that were in the raw data. This allows D3’s force layout to work correctly, and makes it possible to add/remove nodes without worrying about getting our nodes array and links array out of order.

Radializing the Force

Now we have everything needed to display our network in a nice looking, fast, force-directed layout. It might have been a lot of explanation, but we did it in a pretty small amount of code.

The force-directed layout is a great start, but also a bit limiting. Sometimes you want to see your network in a different layout – to find patterns or trends that aren’t readily apparent in a force-directed one. In fact, it would be really cool if we could toggle between different layouts easily – and allow our users to see the data in a number of different formats. So, lets do that!

Here’s a basic idea: we are going to hijack D3’s force-directed layout and tell it where we want the nodes to end up. This way, D3 will still take care of all the physics and animations behind the scenes to make the transitions between layouts look good without too much work. But we will get to influence where the nodes go, so their movements will no longer be purely based on the underlying simulation.

Radial layout example, grouping song nodes by artist

Always force-jack with extreme cautionTo aid in our force-jacking, I’ve created a separate entity to help position our nodes in a circular fashion called RadialPlacement. Its not really a full-on layout, but just tries to encapsulate the complexity of placing groups of nodes. Essentially, we will provide it with an array of keys. It will calculate radial locations for each of these keys. Then we can use these locations to position our nodes in a circular fashion (assuming we can match up our nodes with one of the input keys).

RadialPlacement is a little clumsy looking, but gets the job done. The bulk of the work occurs in setKeys and radialLocation :

Hopefully the comments help walk you through the code. In setKeys our goal is to break up the total set of keys into an inner circle and an outer circle. We use slice to pull apart the array, after we figure out how many locations can fit in the inner circle.

radialLocation does the actual polar coordinate conversion to get a radial location. It is called from place, which is in turn called from setKeys.

Toggling Between Layouts

Lets the user explore different layouts interactively

With RadialPlacement in tow, we can now create a toggle between our force-directed layout and a new radial layout. The radial layout will use the song’s artist field as keys so the nodes will be grouped by artist.

In the update function described above, we saw a mention of the radial layout:

We can see that we just pass our artists array to the RadialPlacement function. It calculates locations for all keys and stores them until we want to position our nodes.

Now we just need to work on this node positioning and move them towards their artist’s location. To do this, we change the tick function for the D3 force instance to use radialTick when our radial layout is selected:

We can see that radialTick calls moveToRadialLayout which simply looks up the location for the node’s artist location from the previously computed groupCenters. It then moves the node towards this center.

This movement is dampened by the alpha parameter of the force layout. alpha represents the cooling of the physics simulation as it reaches equilibrium. So it gets smaller as the animation continues. This dampening allows the nodes repel forces to impact the position of the nodes as it nears stopping – which means the nodes will be allowed to push away from each other and cause a nice looking clustering effect without node overlap.

We also use the alpha value inside radialTick to stop the simulation after it has cooled enough and to have an easy opportunity to redisplay the links.

Because the nodes are different sizes, we want them to have different levels of repelling force to push on each other with. Luckily, the force’s charge function can itself take a function which will get the current node’s data to calculate the charge. This means we can base the charge off of the node’s radius, as we’ve stored it in the data:

charge = (node) -> -Math.pow(node.radius, 2.0) / 2

The specific ratio is just based on experimentation and tweaking. You are welcome to play around with what other effects you can come up with for charge.

Filter and Sort

Force-directed and Radial layouts after filtering for obscure songs

The filter and sort functionality works how you would expect: we check the networks current setting and perform operations on the nodes and links based on these settings. Let’s look at the filter functionality that deals with popular and obscure songs, as it uses a bit of D3 array functionality:

filterNodes defaults to returning the entire node array. If popular or obscure is selected, it uses D3’s quantile function to get the median value. Then it filters the node array based on this cutoff. I’m not sure if the median value of playcounts is a good indicator of the difference between ‘popular’ and ‘obscure’, but it gives us an excuse to use some of the nice data wrangling built into D3.

Bonus: Search

Search bar – Simple searching made easy

Search is a feature that is often needed in networks and other visualizations, but often lacking. Given a search term, one way to make a basic search that highlights the matched nodes would be:

We just create a regular expression out of the search, then compare it to the value in the nodes that we want to search on. If there is a match, we highlight the node. Nothing spectacular, but its a start to a must-have feature in network visualizations.

We can see that updateSearch is a public function, so how do we connect it to the UI on our network visualization code?

Wiring it Up

The other button groups use very similar code.There are a lot of ways we could connect our buttons and other UI to the network functionality. I’ve tried to keep things simple here and just have a separate section for each button group. Here is the layout toggling code:

It just uses jQuery to watch for a key-up event, then re-runs the updateSearch function.

Thanks and Goodnight

Hopefully we have hit all the highlights of this visualization. Interactive networks are a powerful visual, and this code should serve as a jumping off point for your own amazing network visualizations.

Other placement functions could be easily developed for more interesting layouts, like spirals, sunflowers, or grids. Filtering and sorting could be extended in any number of ways to get more insight into your particular dataset. Finally, labelling could be added to each node to see what is present without mousing over.

I hope you enjoyed this walk-through, and I can’t wait to see your own networks!

About the Author

Jim Vallandingham is a programmer and data analyst currently working in the Nordstrom Data Lab as a data visualization engineer. He is interested in interactive interfaces to data that allow for exploration and insight. You can find him on Twitter @vlandham and his personal site, vallandingham.me.

Nice post! Great to see some code on the site and demonstration for creating a graph. VivaGraphJS is another JS force graph that I’ve used recently. I choose it over D3 in this instance – very simple to use. https://github.com/anvaka/VivaGraphJS

I wish D3 had integrated support for pan/zoom. Seems like such a common thing that requires looking all over for examples. Another one that I thought was more difficult that it should have been was simply using pictures instead of ellipses.

Due to the way D3 pulls in data, you typically need to run the site from a local webserver. I apologize for not including a README in the download to explain this. I believe I created one, but must have missed it in the zip file.

If you have python installed (which should be true for Mac and Linux systems) you can run python’s simple http server by running the following command in the terminal:

python -m SimpleHTTTPServer -p 3000

If you have ruby installed, you can get the ‘thin’ web server gem, and then run:

thin start

inside the tutorial directory. In both cases, you should be able to open up

still need help. i really have no idea what i’m doing, i think i’m way out of my depth…
I downloaded python for windows but when i try to run the server command i just get syntax errors. is this because i’m using version 3.2.3 rather than 2, or is there just something else i need to do as well?

oh yes, the new syntax worked, thank you! (it took me a while to realise that i had to run it in cmd without running the interpreter first)
i’ve now got the demo vis to run in my browser, thanks so much!
now it remains to see if i can create one of my own…

I’m running on Windows and haven’t been able to update nodes and adding new nodes and graphs of my own. The demo renders, and I see the names of my new graphs in the song selection menu. After updating the graph .json and songs.json, however, my new graphs still don’t render. The terminal window also shows a string of 200 and 404 messages.

I haven’t been able to trace the code execution flow very well or determine where my new .jsons are being read, etc. (this is probably my inexperience with JS). Any insight on that is also appreciated.

Sounds like an interesting issue. I don’t have a lot of developing on Windows experience, but from it sounds like, this probably isn’t a Windows-specific problem.

Error messages in the terminal would imply a problem not necessarily related to the javascript code. The only problem that I can think of that would cause errors in the terminal would be when the server itself cannot find a resource it is trying to access.

Is it possible that the path to your new song .json file is incorrect – or incomplete?

If this is not the case, are any errors or warning messages presented in your web browser’s console? In Google Chrome, you can access the console by selecting View -> Developer -> JavaScript Console.

Have the console open. Reload the page, then select your new entry again, to see if any errors are displayed.

If this troubleshooting leads to a dead end, you might need to post some of your code changes, or some of the error messages you are receiving.

I think you have the start of one way to add a new layout grouping, but have some issues. For one, it would appear you now have 2 different functions with the name updateCenters, that both take the same number of arguments. This should not work, as the system won’t know which to choose.

If you are really wanting to use a radial layout – but with a different grouping besides ‘artist’ – I have an alternative approach to suggest: You can abstract out the value used to group the bubbles to its own little function, then just change the function to return whatever value you want to group by.

The function is called ‘groupBy’ and it is at the top of the Network function. Then say we had another value, ‘year’, that we wanted to group by instead. Now its as easy as changing the return value of the groupBy function:

So I made the changes you suggested, such that anything that follows d. will be the JSON value by which the nodes are grouped i.e.

groupBy = (d) ->
d.type

I was wondering if you could help once more and tell me how I can make the d.VALUE dynamic. So basically if I added a Group By in the index.html file……how could I activate that and read the “id” of the selected tag so that …………… d.valueofselectedgroupbyoption/

I hope this makes sense. To summarize I basically want to add a drop down menu where a user can selected how they want the radial layout to group the nodes i.e. by genre or by year published. This value will then append the n.artist (where artist is the value replaced).

To dynamically change the grouping method, we can pretty much follow the system used by the other controls: make a public function to change the grouping value and then provide some UI to trigger that function.

Hi guys–
Like Simon, I am having trouble getting my data into the right JSON structure. I’m trying to export a Gephi graph CSV, then use Mr.DataConverter to turn it into a JSON which seems to be working however, what I can’t figure out is how to supplement my own node attributes.

For example, each one of your nodes have attributes of “name”, “artist”, “id”, “playcount”

And then in the ‘groupBy’ function, use the node attribute you are interested in grouping by – like ‘Label’. As an aside, in javascript, you can use an array-like syntax to access values in an object – so you could write:

groupBy = (d) ->
d[“Label”]

to group by a node’s ‘Label’ attribute.

In the code, I use an attribute called ‘id’ to uniquely access each node. This is used by the ‘mapNodes’ function to associate nodes with their appropriate links, as well as in a number of different places. In your node example, it looks like you are using ‘Id’ – with a capital ‘I’.

Perhaps the easiest way to get around this would be to add an ‘id’ attribute that mirrors your ‘Id’ to each node, before any other work occurs in the visualization. This can be done in the ‘setupData’ function. We already are iterating through each node to provide an initial x/y value, so you could add something like:

data.nodes.forEach (n) ->
n.id = n[“Id”]
# rest of the loop here

That way you won’t have to worry about changing all those ‘id”s to ‘Id”s in the code.

To switch ‘name’ to ‘Label’ a quick find and replace should work. OR you can add ‘name’ into your nodes – just like ‘id’ in the setupData function:

data.nodes.forEach (n) ->
n.name = n[“Label”]
# rest of the loop here

So its really a trade off between modifying the code to accomodate your node structure, or modifying your node structure to accomodate the code.

The later might be easier then the former, and would probably incur a very minimal amount of delay in the rendering of the visualization (the slow step isn’t the preprocessing – its the creation of the nodes).

Hope this gets you on the right track for creating your own network visualizations with this example.

I keep saying this but genuinely thank you for your help! I have now got me quite a nice network up and running.
In fact it’s almost working exactly as I’d planned but I’d love to be able to implement a couple of additional features and any input would be greatly appreciated….I’ll even promise to pest no more!

Firstly, I was hoping to add direction to the relationships (inbound/ outbound/bi-directional). I was wondering if I’d simply add this information to each edge in the JSON file? Once this information has been added I was wondering how I would go about adding arrow heads to the connecting lines?

Secondly, I was hoping to represent frequency of ‘knowledge flowing between nodes’ and thought the best way would be to have varied width lines between the nodes or have use a grayscale for the lines colour. Could you advise on how I could go about implementing this?

Yes, you should be able to add more attributes to each edge in the json file and then use this information to determine how to visualize the edges.

The edges are, in fact, already directional – each edge has a source and a destination. So you have two options: you could use this built in directionality directly, or you could add a separate attribute to each of your edges to indicate directionality.

I myself would lean towards using the first approach, as it is cleaner and more direct. If you need to support bi-directional edges, you could add a separate edge for each direction. This may or may not have the look you are wanting. If you want to keep bi-directional-ness to a single edge, you could still use the built in directionality of edges and then just have an extra attribute – ‘bidirectional’ that would be true or false.

Once you have the data in place, adding the arrow heads looks to be a bit more tricky.

Here is an example using arrow heads, as well as different types of lines in a network visualization in d3:

It might take a bit of study and experimentation to get it working in your project, but I think it is doable. I believe in this example, the different arrow types are created separately and then used as the end markers of the lines by linking to their url.

I’d suggest getting a local copy of this example working and then trying to copy over the functionality to your graphic.

You can see how the different line types are done using css. Your grayscale lines could be accomplished the same way.

Hey, this is a bit of a tangent, however, a network visualization related question….
Does anyone know how to acces public Facebook page data? Like Obama or Absolut Vodka, or Nike etc…
I’ve toyed around with Facebook’s GraphAPI, however, you can only get 100 results per request which is pointless.
I mean, the data is public. Just how to get it…….
thx. weeezzzyyy

Thanks for all your help so far! I’ve got a rich visualization that’s close to completion with lots of groupBy functionality and on the whole it’s very swell! However I’ve amassed a few questions that I’d love to swing your way to get your feedback!

1. I was hoping to add a new search field where you can search for all nodes based on another attribute i.e. year of release, how would I go about setting up this new search field and setting what JSON field it should be searching through?

2. Is their a maximum size (no of nodes) the visualization will accept before crashing? I’m making a JSON file from DB queries which are then written to JSON format using jsencode. 50% of the time the files open no problem, however for the larger files there tends to be problems. The error I get in the console reads as follows:

Uncaught TypeError: Cannot read property ‘id’ of undefined

If you can offer an insight on my queries it would be much appreciated as ever!
Cheers

For your additional search – question #1 – you could do this a couple different ways. One way would be to duplicate the ‘search_section’ portion of index.html to have two search forms.

I would switch to using a ‘.search’ class instead of a search id – so you can use the same stylings for both searches. Then you could pass the field you are searching for (‘name’ or ‘year’) as a second parameter to the network’s search.

I’ve created a gist here that tries to capture the changes that would be required. Note that this is untested code, but should point you in the right direction:

Also note that you would need to modify the css for the search box to make them use the class names instead of id’s.

If you were planning on just searching for year’s you might consider a drop down list – with all possible years listed, instead of just a text box.

For issue #2 – This doesn’t sound like a problem with the number of nodes. Instead, it sounds like somewhere in the visualization there is an issue of passing a bad node (an undefined node) to a function that is trying to access the ‘id’ parameter of the node. Since the node here is undefined, it gives this error.

What would cause an undefined node? Well, I would start by looking at the mapping we use to connect the links to the nodes.

If you recall from the tutorial, we are using the ‘id’ field of the node to reference specific nodes in the ‘links’ section of our json data files. So, if you had a typo, or mismatch in one of your id’s used in the links section compared to the node id’s, then this mapping would be unsuccessful, and you would end up with links with no actual nodes associated with them.

So in short, check your links and make sure that all the id’s in the ‘source’ and ‘target’ sections have corresponding nodes with the same ids.

In the network.updateData function, you could add some console logs to see that each link gets a source and target node

Hey Jim, I’ve been following Tim’s question as I have also been having some difficulty trying to add an additional search box but I managed to get it working today using your code – Thanks! One thing I’d like to do is get the information from the JSON into another div on the index page. Looking at your code for the tooltip has been useful but I can’t work out the process involved in trying to get info from the JSON into a different div. If you have any thoughts on how I could go about achieving this, I’d be really grateful!

This showInfo function will have access to all the json data for that particularly clicked node as one of its input parameters. The idea for this function is to build up the html content we want to display (as in the tool tip) and then use d3’s ‘html’ function to replace the contents of the ‘#info’ div with this new content.

This is truly wonderful! Pardon me if you have covered this elsewhere, but I have a quick question that I haven’t seen directly covered elsewhere.

Background:

I want to create a visualization of my posts on a wordpress.org blog. It is a private journal-like blog that I am using to store my thoughts and what not. I have my posts categorized as quotes, poems, personal thoughts, etc. but they are all tagged with potentially similar tags.

I was thinking it would be cool to create a visualization similar to what you produced above, but with the following principles:

a) Quotes are circles, Poems are triangles, my own posts are squares
b) Links are made by commonly shared tags
c) Title and author would be used similarly to the way you used them above.

Content of the post itself could be displayed
1) under the author on the rollover text and
2) on the side for the center node — this second part a bonus, not necessarily essential

Finally, the last thing to implement would be to have a given force-directed network, but switch the center node, maybe by double-clicking?

As it pertains to the data, WordPress uses a MySql database, but it can also export as XML or as a CSV file that contains all the posts.

I have coded to a reasonable degree in each of Java, C, C++, Python, R, and Matlab, but haven’t done any web applications or databases. I am very excited to use this to learn a bit about data visualization. I am on a mac.

Question(s):

I would appreciate advice on getting started.

My biggest concern is getting the data into your program. Any advice on turning the WordPress data into a nodes and edges between all nodes of the same tags?

Also, I would love to hear other advice on any and all other aspects of the plan I listed above, including suggested (free) text editors, etc.

Sounds like a great project. It could reveal some insights into related posts/poems, like a mind map of your site. I think it would take a bit of work to utilize this code to create what you are talking about, but it could provide a good starting point.

I don’ t have a lot of experience extracting data from wordpress sites, but the first thing I thought of when you mentioned wanting to extract the data was converter scripts that move posts from one platform to another. Specifically, Jekyll, the blogging platform I use, has a ruby script that pulls out data from a wordpress site to organize and insert into plain text files:

It might be a bit more difficult attempting to use a ruby script if you are more familiar with python, but this at least gives you some of the database queries that could be used to extract tags, titles, etc.

I don’t think it would be too much of a stretch to use this script and tweak it so that in the end it builds up a data structure that can be saved off for your nodes and edges.

You would want to encode the type of the node (poem,quote,post) into each node element. Then you could look at the type when creating nodes, so you could create the right shape.

Part of what I enjoy about force-directed layouts is the ‘organic’ nature of the layout and how it shifts over time.

Initialization of fixed nodes would have some drawbacks, if I’m understanding what you are saying:
* You would have to pre-calculate all the node’s locations somehow and store that in the data, instead of letting the layout do that for you
* If you wanted to add dragging to the nodes, you would not have any re-positioning capabilities.
* Transitioning from one layout to another would require storing multiple locations per node?

Thanks for the quick reply. Yes, it does involve pre-computing all the node locations but it looks like that’s what you do anyway for your radial placement since you store all the locations in your “groupCenters” object.?

Nodes with the “fixed” property set to true can still be moved by dragging I’m pretty sure, but you may have to alter the dragging behavior.

This is an awesome tutorial, and thank you so much for putting it up. I just seem to have a problem right now with adding an additional JSON file that I created to view a different network. The format of the JSON file is identical to those that currently exist (i.e., uses “id”, “name”, etc.), yet when I try and view the program the network does not get generated. The only changes I made to the source code were to add my new JSON file to the data directory, and then I changed the last line of the coffee code so that it would read from the data file that I added instead of the “Call Me Al” file. Any pointers?

Thanks a million, I’ve been pulling my hair out trying to get this working.
-DM

Great tutorial. I’ve been using your demo as a template to map genetic and physical interactions between yeast genes and have come quite a long way. I am having one issue though. One your features I have modified is the filter function. I changed it to allow filtering based on node degree (i.e. node “weight” in D3 terms). This works great when in the default force layout, however when I switch to radial layout (in both my implementation and your demo) all of the nodes have their weight value converted to 0. This, of course, renders filtering by degree impossible when in radial view. When you switch back to the force layout the weights are restored.

I imagine this is because the nodes are somehow disconnected from their edges (i.e. links) when in radial mod.e Unfortunately, I cannot figure out how to solve this issue. Any ideas?

I want to develop application with d3.js. I use my own function for coordiantes and also want to add a simple drag drop feature for the visualization. I am able to drag nodes but how to adjust edges when I drag nodes.