Contents
What is D3?
What can D3 do? [click]
D3 makes graphics within a web page from data
It does data visualisations
- D3 is an abbreviation of Data-Driven Documents
- It’s another JavaScript library file
- See and get latest version 4 [here]
- Link to D3 using <script> element like this:
<script src="https://d3js.org/d3.v4.min.js"></script>
- There are no predefined charts in D3
- Just shapes, lines and text
- For example, to create a bar chart you must create the bars, axes, and text yourself
- This allows you to devise/create new types of custom charts
- But you build from the ground up
- D3 uses SVG to do the drawing
SVG
- SVG is Scalable Vector Graphics
- There are two kinds of digital graphic – raster and vector
- Raster images like photographs use many colored pixels to form an image, e.g. JPEGs,
- GIFs and PNGs
- They can’t be resized much without loss of resolution
- A vector graphic appears like any image, but it is a mathematical definition
- Vector images never loses definition
- SVG is ideal for data visualisations
- HTML has a <svg> tag
- It can be placed anywhere inside the <body> tag
- D3 uses the <svg> tag as a container to put charts into
- The <svg> tag can only contain specific graphical elements like lines, rectangles, etc.
- D3 adds graphical elements to the SVG and binds data to them
- For example, D3 could read 50 data values from an array and add 50 circles to the SVG. Have a look at this SVG Tutorial
SVG Example
<html>
<body>
<svg width="300" height="200">
<rect x="0" y="0" width="300" height="200" fill="red" />
<circle cx="150" cy="100" r="80" fill="green" />
<text x="150" y="125" font-size="60" text-anchor="middle">SVG</text>
</svg>
</body>
</html>
SVG has some basic shapes:
Rectangles
Circle
Ellipse
Line
Polyline
Polygon
Path (alternative to Polyline and Polygon)
See and learn them here
SVG Bar Chart
And the code…
<html>
<body>
<svg width="300" height="200">
<rect x="0" y="0" width="100" height="50" fill="steelblue" />
<rect x="0" y="60" width="200" height="50" fill="steelblue" />
<rect x="0" y="120" width="300" height="50" fill="steelblue" />
<text x="10" y="35" font-size="20" fill="white">100</text>
<text x="10" y="95" font-size="20" fill="white">200</text>
<text x="10" y="155" font-size="20" fill="white">300</text>
</svg>
</body>
</html>
- The values (in blue) are very specific and static
- JavaScript and D3 could create this dynamically using a dataset
- Previously, the values in our bar chart were static
- JavaScript could be used to draw it dynamically like this…
SVG Bar Chart Version 2
svgBarChart2.html
<html>
<head>
<script type="text/javascript" src="svgBarChart2.js"></script>
</head>
<body>
<svg id="chart">
</svg>
</body>
</html>
svgBarChart2.js
var data = [100,200,300]; // can add new values
window.onload = function() {
document.getElementById('chart').setAttribute('width',300);
document.getElementById('chart').setAttribute('height',data.length*60);
for(var i=0;i<data.length;i++) {
var rect=document.createElementNS("http://www.w3.org/2000/svg","rect");
rect.setAttribute('x',0);
rect.setAttribute('y',i*60);
rect.setAttribute('width',data[i]);
rect.setAttribute('height',50);
rect.setAttribute('fill','steelblue');
document.getElementById('chart').appendChild(rect);
}
}
Do Exercise 1 - SVG - Part 1
It is important you know how to render SVG graphics BEFORE using D3. Its similar to having to know HTML before using JavaScript
1. Use the code below called svgBarChart.html that uses SVG to render a simple bar chart. Save the program into a d3 folder in htdocs:
<html>
<body>
<svg width="300" height="200">
<rect x="0" y="0" width="100" height="50" fill="steelblue" />
<rect x="0" y="60" width="200" height="50" fill="steelblue" />
<rect x="0" y="120" width="300" height="50" fill="steelblue" />
<text x="10" y="35" font-size="20" fill="white">100</text>
<text x="10" y="95" font-size="20" fill="white">200</text>
<text x="10" y="155" font-size="20" fill="white">300</text>
</svg>
</body>
</html>
Open it using your browser. Experiment/change some of the values, colours, etc. to see the outcome
2. Modify the code above so that bar chart looks like this:
Set the <svg> width to 300.
3. Create a new SVG rendered chart in a file called svgCircles.html to look like this:
The diameter of the circles are shown in the text values inside the circle. Set the <svg>
 height to 300.
4. Create a new SVG rendered column chart in a file called svgColumnChart.html to look like this:
Set the <svg> height to 300.
5. Create a new SVG rendered line chart in a file called svgLineChart.html to look like this:
Solution 1
svgBarChart.html
<html>
<body>
<svg width="300" height="200">
<rect x="0" y="0" width="100" height="50" fill="steelblue" />
<rect x="0" y="60" width="200" height="50" fill="steelblue" />
<rect x="0" y="120" width="300" height="50" fill="steelblue" />
<text x="10" y="35" font-size="20" fill="white">100</text>
<text x="10" y="95" font-size="20" fill="white">200</text>
<text x="10" y="155" font-size="20" fill="white">300</text>
</svg>
</body>
</html>
Solution 2
svgBarChart.html
<html>
<body>
<svg width="300" height="200">
<rect x="0" y="0" width="300" height="50" fill="steelblue" />
<rect x="0" y="60" width="200" height="50" fill="steelblue" />
<rect x="0" y="120" width="100" height="50" fill="steelblue" />
<text x="10" y="35" font-size="20" fill="white">300</text>
<text x="10" y="95" font-size="20" fill="white">200</text>
<text x="10" y="155" font-size="20" fill="white">100</text>
</svg>
</body>
</html>
Solution 3
svgCircle.html
<html>
<body>
<svg width="650" height="300">
<circle cx="50" cy="150" r="50" fill="steelblue" />
<circle cx="225" cy="150" r="100" fill="steelblue" />
<circle cx="500" cy="150" r="150" fill="steelblue" />
<text x="50" y="150" fill="#FFF" font-size="20px" text-anchor="middle">100</text>
<text x="225" y="150" fill="#FFF" font-size="20px" text-anchor="middle">200</text>
<text x="500" y="150" fill="#FFF" font-size="20px" text-anchor="middle">300</text>
</svg>
</body>
</html>
Solution 4
columnChart.html
<html>
<body>
<svg width="300" height="300">
<rect x="0" y="200" width="50" height="100" fill="steelblue" />
<rect x="75" y="100" width="50" height="200" fill="steelblue" />
<rect x="150" y="0" width="50" height="300" fill="steelblue" />
<text x="10" y="290" font-size="20" text-anchor="left" fill="white">100</text>
<text x="85" y="290" font-size="20" text-anchor="left" fill="white">200</text>
<text x="160" y="290" font-size="20" text-anchor="left" fill="white">300</text>
</svg>
</body>
</html>
Solution 5
lineChart.html
<html>
<body>
<svg width="400" height="400">
<!-- Left & Bottom Bar -->
<line x1="0" y1="0" x2="0" y2="400" stroke="Black" stroke-width="2"/><!-- Left Bar -->
<line x1="400" y1="400" x2="0" y2="400" stroke="Black" stroke-width="2"/><!-- Bottom Bar -->
<!-- Indents on bottom bar -->
<line x1="100" y1="400" x2="100" y2="390" stroke="Black" stroke-width="2"/>
<line x1="200" y1="400" x2="200" y2="390" stroke="Black" stroke-width="2"/>
<line x1="300" y1="400" x2="300" y2="390" stroke="Black" stroke-width="2"/>
<text x="100" y="385" font-size="20" text-anchor="middle" fill="Black">100</text>
<text x="200" y="385" font-size="20" text-anchor="middle" fill="Black">200</text>
<text x="300" y="385" font-size="20" text-anchor="middle" fill="Black">300</text>
<!-- Indents on Left bar -->
<line x1="0" y1="100" x2="10" y2="100" stroke="Black" stroke-width="2"/>
<line x1="0" y1="200" x2="10" y2="200" stroke="Black" stroke-width="2"/>
<line x1="0" y1="300" x2="10" y2="300" stroke="Black" stroke-width="2"/>
<text x="15" y="300" font-size="20" text-anchor="left" fill="Black">100</text>
<text x="15" y="200" font-size="20" text-anchor="left" fill="Black">200</text>
<text x="15" y="100" font-size="20" text-anchor="left" fill="Black">300</text>
<!-- Graph Line -->
<line x1="100" y1="300" x2="300" y2="100" stroke="red" stroke-width="2"/>
<!-- Graph Circle -->
<circle cx="100" cy="300" r="5" fill="steelblue" />
<circle cx="200" cy="200" r="5" fill="steelblue" />
<circle cx="300" cy="100" r="5" fill="steelblue" />
</svg>
</body>
</html>
Do Exercise 1 - SVG - Part 2
1. Use the code below called svgBarChart2.html and svgBarChart2.js to use SVG to render a simple bar chart dynamically using JavaScript.
svgBarChart2.html
<html>
<head>
<script type="text/javascript" src="svgBarChart2.js"></script>
</head>
<body>
<svg id="chart">
</svg>
</body>
</html>
svgBarChart2.js
var data = [100,200,300];
window.onload = function() {
document.getElementById('chart').setAttribute('width',300);
document.getElementById('chart').setAttribute('height',data.length*60);
for(var i=0;i<data.length;i++)
{
var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
rect.setAttribute('x',0);
rect.setAttribute('y',i*60);
rect.setAttribute('width',data[i]);
rect.setAttribute('height',50);
rect.setAttribute('fill','steelblue');
document.getElementById('chart').appendChild(rect);
}
}
Run the code. Add another value to the data array and rerun the code.
2. Modify the code so the output includes the text like this:
Add the text dynamically using JavaScript.
Solution 1
HTML
<html>
<head>
<script type="text/javascript" src="svgBarChart2.js"></script>
</head>
<body>
<svg id="chart">
</svg>
</body>
</html>
JS
var data = [100,200,250,300]; // can add new values
window.onload = function() {
document.getElementById('chart').setAttribute('width',300);
document.getElementById('chart').setAttribute('height',data.length*60);
for(var i=0;i<data.length;i++) {
var rect=document.createElementNS("http://www.w3.org/2000/svg","rect");
rect.setAttribute('x',0);
rect.setAttribute('y',i*60);
rect.setAttribute('width',data[i]);
rect.setAttribute('height',50);
rect.setAttribute('fill','steelblue');
document.getElementById('chart').appendChild(rect);
}
}
Solution 2
HTML
– incomplete
<html>
<head>
<script type="text/javascript" src="svgBarChart2.js"></script>
</head>
<body>
<svg id="chart">
</svg>
</body>
</html>
JS
– incomplete
var data = [100,200,250,300]; // can add new values
window.onload = function() {
data.sort();
data.reverse();
document.getElementById('chart').setAttribute('width',300);
document.getElementById('chart').setAttribute('height',data.length*60);
for(var i=0;i<data.length;i++) {
var rect=document.createElementNS("http://www.w3.org/2000/svg","rect");
rect.setAttribute('x',0);
rect.setAttribute('y',i*60);
rect.setAttribute('width',data[i]);
rect.setAttribute('height',50);
rect.setAttribute('fill','steelblue');
document.getElementById('chart').appendChild(rect);
}
}
Setup D3
Download D3 from here
Add d3.js or d3.min.js into a new folder called d3
Use it with a <script> tag like so:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="d3.min.js"></script>
</head>
...
...
D3 Bar Chart
Video….