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 ressource 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:
First html document
This is my first sentence
This is a link to the d3 graph gallery
→ 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:
First html document
This is my first sentence
This is a link to the d3 graph gallery
→ 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:
First html document
This is my first sentence
→ Try:
svg
call to understand what feature they control.
Javascript
and D3.js
→ Explanation:
target
and modify its stroke-width
.→ Example:
First html document
This is my first sentence
→ 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:
→ 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:
→ 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:
→ 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:
→ 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:
→ 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.