TO THE
HELP YOURSELF TO PIZZA!
github.com/newcastle-digital/code-club-d3js
Got all these? Let's get started!
Provided for you in the ZIP from GitHub is an extremely basic webpage with D3.js ready to go. It includes:
X
Y
Column Names
Column
Row
When we ask D3 to load an external data file, it takes some time to be returned. We provide a callback function that runs once it does so.
The callback function will include everything that involves using the data to draw our graph.
So far we've only been displaying data at a 1:1 ratio - dots are placed in pixels only. This means if X is 200, the dot is 200 pixels horizontally on the chart.
But what if our data has large (millions) or small (decimals) values?
We need to scale how the graph displays our values.
For each axis we create a range object:
Domain
Range
0
500
0
750
382
573
Instead of manually defining the range of data to be displayed in our graph (0 to 500 for both axes), we can use the data to programmatically define a domain.
This can't be done when the linear scale is defined, as the data has not loaded yet! So it must happen in our CSV callback.
Calling d3.extent on our data returns an Array with the lowest and highest value in the data set.
Nice rounds the domain to whole, sensible numbers.
For example:
This makes for better axis ticks when we render our axes around the graph...
D3 provides us a way to easily render our X and Y axis. This uses our linear scales to draw each tick - the numbers laid out along the axis.
Each axis is then added to the SVG and positioned, with a label so that we know exactly what each axis refers to.
Instead of generic X and Y values, let's turn our scatterplot into a visualisation of films released in 2016, with data gathered from IMDB.
imdb.csv includes the following data about each film:
Now that we're using big numbers, our axis ticks won't fit on the screen neatly.
Do we really need to show 1 million as 1,000,000, when all of the numbers are in millions?
Now that we're not using arbitrary data, it'd be useful to know more about each data point (dot).
How do we know which film a dot is?
A tooltip will tell us more info when we mouse over each dot. Each time this happens we need to:
When creating a linear scale, we can have the range be colours, not just numbers!
This could be used to colour each dot by metascore:
Domain
Range
0
100
Red
Yellow
Green
50