D3.js is a JavaScript library for manipulating documents based on data. It allows to build absolutely any type of data visualization. This document displays 10 interactive examples illustrating the key concepts of d3, leading to a first basic scatterplot. Note that this online course is a great resource to get you started with d3.js.
HTML
?→ Explanation:
h1
tag, a paragraph with the
p
tag, an image by the img
tag and so on.
→ Example:
<!DOCTYPE html>
<!-- Add a title -->
<h1>First html document</h1>
<!-- Add a bit of text -->
<p>This is my first sentence</p>
<!-- Add a link -->
<p>This is <a href="https://www.d3-graph-gallery.com">a link to the d3 graph gallery</a></p>
→ Try:
test.html
. Open it with a browser. You've created
your first website!
CSS
→ Explanation:
html
before.
css
. If it is new for you,
check this
tutorial.
→ Example:
<!DOCTYPE html>
<!-- Apply specific style to the elements that have the class `inGreen` -->
<style>
.inGreen { color: green; }
</style>
<!-- Add a title. Note that the class 'inGreen' is given to this title -->
<h1 class="inGreen">First html document</h1>
<!-- Add a bit of text -->
<p>This is my first sentence</p>
<!-- Add a link -->
<p>This is <a href="https://www.d3-graph-gallery.com">a link to the d3 graph gallery</a></p>
→ Try:
inGreen
to one of the paragraph
p
font-size: 20px
in css.
SVG
→ Explanation:
svg
shapes put
together. For instant, a scatterplot is just composed by several
circles as the one shown below.
→ Example:
This is my first sentence
<!DOCTYPE html>
<!-- Add a title -->
<h1>First html document</h1>
<!-- Add a bit of text -->
<p>This is my first sentence</p>
<!-- Add a svg shape -->
<svg>
<circle style="fill: #69b3a2" stroke="black" cx=50 cy=50 r=40></circle>
</svg>
→ Try:
svg
call to understand
what feature they control.
<line x1="0" y1="0" x2="10" y2="10" stroke="black"></line>
<rect x="0" y="0" width="10" height="10"></rect>
<circle cx="5" cy="5" r="5"></circle>
<ellipse cx="10" cy="5" rx="10" ry="5"></ellipse>
<polygon points="0,0 10,5 20,0 20,20 10,15 0,20"></polygon>
<polyline points="0,0 10,5 20,0 20,20 10,15 0,20" stroke="black"></polyline>
<path d="M65,10 a50,25 0 1,0 50,25"></path>
Javascript
and D3.js
→ Explanation:
target
and modify its stroke-width
.
→ Example:
This is my first sentence
<!DOCTYPE html>
<h1>First html document</h1>
<!-- Add a bit of text -->
<p>This is my first sentence</p>
<!-- Add a svg shape. Note that the 'target' class is attributed to the circle -->
<svg>
<circle class="target" style="fill: #69b3a2" stroke="black" cx=50 cy=50 r=40></circle>
</svg>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<script>
d3
.select(".target") // select the elements that have the class 'target'
.style("stroke-width", 8) // change their style: stroke width is not equal to 8 pixels
</script>
→ Try:
opacity
style that
goes between 0 and 1.
Console.log()
is your friend.→ Explanation:
Html
, Css
and
Javascript
code and shows the result as a webpage.
console.log("sometext")
in your javascript code
→ Explanation:
svg
element. This element has a width
and
a height
, given in pixels.
x=0
and y=0
. The bottom left
corner has the coordinate x=0
and
y=height
. The top right corner has the coordinate
x=width
and height=0
→ Example:
<!DOCTYPE html>
<!-- Add a svg area, empty -->
<svg id="dataviz_area" height=200 width=450></svg>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<script>
var svg = d3.select("#dataviz_area")
svg.append("circle")
.attr("cx", 2).attr("cy", 2).attr("r", 40).style("fill", "blue");
svg.append("circle")
.attr("cx", 140).attr("cy", 70).attr("r", 40).style("fill", "red");
svg.append("circle")
.attr("cx", 300).attr("cy", 100).attr("r", 40).style("fill", "green");
</script>
→ Try:
→ Explanation:
scale
svg
area is 400px
width. 0% → 0px. 100% → 400px. 50% → 200px.
domain
(0 to 100% here) and a
range
(0 to 400px here)
x
. If you
run x(10)
, d3 returns the position in px for this value
→ Example:
<!DOCTYPE html>
<!-- Add a svg area, empty -->
<svg id="viz_area" height=200 width=450></svg>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<script>
// Select the svg area
var svg = d3.select("#viz_area")
// Create a scale: transform value in pixel
var x = d3.scaleLinear()
.domain([0, 100]) // This is the min and the max of the data: 0 to 100 if percentages
.range([0, 400]); // This is the corresponding value I want in Pixel
// Try console.log( x(25) ) to see what this x function does.
// Add 3 dots for 0, 50 and 100%
svg.append("circle")
.attr("cx", x(10)).attr("cy", 100).attr("r", 40).style("fill", "blue");
svg.append("circle")
.attr("cx", x(50)).attr("cy", 100).attr("r", 40).style("fill", "red");
svg.append("circle")
.attr("cx", x(100)).attr("cy", 100).attr("r", 40).style("fill", "green");
</script>
→ Try:
domain
and range
values to
understand how it works.
→ Explanation:
scale
. This
scale specifies where the axis must be placed, and
what range it should indicate.
axisBottom()
creates a horizontal axis,
with ticks and labels at the bottom. axisLeft()
will be
used later for the Y axis
→ Example:
<!DOCTYPE html>
<!-- Add a svg area, empty -->
<svg id="Viz_area" height=200 width=450></svg>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<script>
// Select the svg area
var svg = d3.select("#Viz_area")
// Create a scale: transform value in pixel
var x = d3.scaleLinear()
.domain([0, 100]) // This is the min and the max of the data: 0 to 100 if percentages
.range([0, 400]); // This is the corresponding value I want in Pixel
// Show the axis that corresponds to this scale
svg.call(d3.axisBottom(x));
// Add 3 dots for 0, 50 and 100%
svg.append("circle")
.attr("cx", x(10)).attr("cy", 100).attr("r", 40).style("fill", "blue");
svg.append("circle")
.attr("cx", x(50)).attr("cy", 100).attr("r", 40).style("fill", "red");
svg.append("circle")
.attr("cx", x(100)).attr("cy", 100).attr("r", 40).style("fill", "green");
</script>
→ Try:
domain
and range
values to
understand how it works.
→ Explanation:
.attr("transform", "translate(20,50)")
to an
element with translate it 20px to the right and 50px to the bottom.
svg
area, creating a bit of margin around the chart
without having to think about it in the rest of the code. It is
important to understand how it works since almost all d3.js chart
start that way.
→ Example:
<!DOCTYPE html>
<!-- Add a svg area, empty -->
<div id="Area"></div>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<script>
// set the dimensions and margins of the graph
var margin = {top: 10, right: 40, bottom: 30, left: 30},
width = 450 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
var sVg = d3.select("#Area")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
// translate this svg element to leave some margin.
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// X scale and Axis
var x = d3.scaleLinear()
.domain([0, 100]) // This is the min and the max of the data: 0 to 100 if percentages
.range([0, width]); // This is the corresponding value I want in Pixel
sVg
.append('g')
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// X scale and Axis
var y = d3.scaleLinear()
.domain([0, 100]) // This is the min and the max of the data: 0 to 100 if percentages
.range([height, 0]); // This is the corresponding value I want in Pixel
sVg
.append('g')
.call(d3.axisLeft(y));
</script>
→ Try:
svg
variable creation.→ Explanation:
svg
: this select the svg area where the chart takes
place
.selectAll("whatever")
: select all the elements that
have not be created yet, I know it is weird.
.data(data)
: specify the data to use..enter()
: start a loop for the data. Following code
will be applied to data[0]
, data[1]
and
so on.
.append("circle")
: for each iteration, add a circle.
.attr("cx", function(d){ return x(d.x) })
: gives the
x
position of the circle. Here d
will be
data[0]
, then data[1]
and so on. Thus
d.x
is the value of x
, and
x(d.x)
is the corresponding position in pixel found
thanks to the x scale
.
→ Example:
<!DOCTYPE html>
<!-- Add a svg area, empty -->
<div id="scatter_area"></div>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<script>
// set the dimensions and margins of the graph
var margin = {top: 10, right: 40, bottom: 30, left: 30},
width = 450 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svG = d3.select("#scatter_area")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Create data
var data = [ {x:10, y:20}, {x:40, y:90}, {x:80, y:50} ]
// X scale and Axis
var x = d3.scaleLinear()
.domain([0, 100]) // This is the min and the max of the data: 0 to 100 if percentages
.range([0, width]); // This is the corresponding value I want in Pixel
svG
.append('g')
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// X scale and Axis
var y = d3.scaleLinear()
.domain([0, 100]) // This is the min and the max of the data: 0 to 100 if percentages
.range([height, 0]); // This is the corresponding value I want in Pixel
svG
.append('g')
.call(d3.axisLeft(y));
// Add 3 dots for 0, 50 and 100%
svG
.selectAll("whatever")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d){ return x(d.x) })
.attr("cy", function(d){ return y(d.y) })
.attr("r", 7)
</script>
→ Note:
This is probably the most difficult concept in d3.js. And it is used in almost every single chart. It is actually the power of d3: binding data to elements
It is probably a good idea to read more on this topic. Check d3 in depth and Dashing d3.js.
next
?This document is a very very short intro to d3.js. However, it describes the basic concepts that are used in almost every chart.
You're now probably ready to explore the gallery. For each chart section, there is a very basic example to start with.