Introduction to D3 Visualisations

Example Timelines: There are a host of tools available to visualise your data using timelines. Some of these examples afford different levels of flexibility depending on your needs. Some of the more sophisticated ones include:

TimelineJS (http://timeline.knightlab.com)

TimeLineCurator (http://bit.ly/2lGTtxw)

TimeMapper: (http://timemapper.okfnlabs.org)

Chronos Timeline: http://hyperstudio.mit.edu/software/chronos-timeline/

 

Introduction To D3

D3, created by former New York Times developer Mike Bostock, is an abbreviation for Data Driven Documents. Here is a good example of it in use: https://www.nytimes.com/elections/results/president

It began in 2011 as a library of code written in Javascript that enables you to create interactive visualizations in web browsers. Currently in version 4.5, it works by using HTML5 and CSS alongside Scalable Vector Graphics (SVG). D3 allows you to control the result of the representation of your data by giving you the freedom to grab selections of your data and control how it outputs on the browser.

Examples of D3

In order to get going with D3, most developers recommend that you ‘dive in’ and use some of the examples that appear on the http://bl.ocks.org/ website. There are thousands of examples of D3 in use. On this website, developers have displayed their projects, added a description of what they do, and provided code with some sample data. You can re-use this code and add your own data to see how the code works. If you change some of the values in the code, you will be able to see the possibilities you will have for your own visualisations.

Here is a quick example of how to get D3 working. First the HTML code:


<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>D3 Hello World</title>
    
  </head>
  <body>
    
  </body>
</html>

Then we add script tags inside the <head>code goes here...</head> tags like so:

<script src="http://d3js.org/d3.v3.min.js"></script>
<script type="text/javascript">
  d3.select("body").style("background", "#efefef").append("p").text("Hello World");
</script>

Save this as example.html and upload to your hosting space if you haven’t got a server installed on your machine. The result should look like this:


Play around with the values defined above to see how D3 changes the output. Next, we’ll plot some arbitrary data in the browser.Take for example three countries with income, life expectancy and population. We are going to plot these values to get an interactive graph.


<script src="http://d3js.org/d3.v3.min.js"></script>
<div id="example"></div>
<script>
const arbitraryData = [
{name: 'Ireland', income: 53000, life: 78, pop: 5378, color:'green'},
{name: 'Norway', income: 73000, life: 87, pop: 6084, color:'blue'},
{name: 'Tanzania', income: 27000, life: 45, pop: 33407, color:'purple'}
];

// Create SVG - a container to hold our visualisation

const svg = d3.select('#example').append('svg')
.attr('width', 500)
.attr('height', 400)
.style('background-color', '#666666');

svg.selectAll('circle')
.data(arbitraryData)
.enter()
.append('circle')
.attr('id', function(d) { return d.name;})
.attr('cx', function(d){return d.income / 200;})
.attr('cy', function(d){return d.life * 3;})
.attr('r', function(d){return d.pop / 1000 * 3;})
.attr('fill', function(d){return d.color;})

.append('svg:title')
.text(function(d) { return 'Country: ' + d.name + ' ' + d.income});
      </script>

See how the code above constructs the graph. Some of it is self explanatory, but make sure you know what it does before moving on. Save this file as d3-example.html and it should produce a visualisation like this: http://patrickegan.org/d3-example.html

Timelines With D3

In some projects, D3 allows you to create timelines by drawing X and Y axes, adding time markers and then it allows you to feed your data into this timeline using HTML elements. A good example of this would be Timeline For D3 – Proof of Concept by user “Reinhard Rengel” http://bl.ocks.org/rengel-de/5603464. This particular timeline is based on the popular open-source visualisation Simile Timeline from 2005 that was created’ by ‘David François Huynh’ available here: http://www.simile-widgets.org/timeline/

 

Visit the url http://bl.ocks.org/rengel-de/5603464 and follow the instructions for “Create your own timelines”.

To create your own timeline, you need:

A data file (see ‘The file structure’ above).

The file ‘timeline.js’; download and put into your working directory or on your path.

The file ‘timeline.css’; download and put into your working directory or on your path; change settings according to your preferences.

Use ‘index.html’ (without comments) as a template and put in your filenames and paths.

*Hint: When copying these files to your own computer, you will need to have a server. If you haven’t got WAMP or XAMPP installed, you can still try out this code online. I would recommend you put the copied files on your own hosted website (such as Reclaim Hosting), or else use JSFiddle.net

This is just one example of many hundreds of different ways that you can use D3 for interactivity. As you can see from the same website (https://bl.ocks.org/) there are many ways to visualise your data.

1 comment

  1. Patrick Egan - June 1, 2017 10:44 am

    TimelineJS can be found at….

    Reply

Have your say