Maarten Lambrechts
CIMAT
INEGI
8 October 2021
A bubble chart with D3, part 2
Examples of D3 charts
References for learning D3
HyperText Markup Language
Structure and content of a webpage, rendered by a browser
Hierarchy of elements/tags
.html
<h1>Hello class!</h1>
<p>Today we are going to make awesome graphics <span>with D3!</span></p>
<img src="https://media.giphy.com/media/QHE5gWI0QjqF2/giphy.gif">
Scalable Vector Graphics
Scalable images for the web
Part of html
Text based
Animatable
<svg width="100"height="100">
<circle
cx="50"
cy="50"
r="40"
stroke="green"
stroke-width="4"
fill="yellow"/>
</svg>
Cascading Style Sheets
How html elements should be displayed
In separate .css file, or inline in html
body {
font-family: Arial;
}
h1 {
font-size: 52px;
}
p {
font-size: 24px;
}
span {
background-color: #910000;
padding-left: 4px;
padding-right: 4px;
color: white;
}
Language to make webpages dynamic
React to user actions, update content
Animations
Control multimedia
.js files
const circle = document.querySelector("circle");
circle.addEventListener("click", function(){
circle.setAttribute("fill", "blue")
}
);
Files (.html, .css, .js, images, ...) are served to the client by a server
To develop a web visualization, you need a (local) server
But we'll use JS Bin instead
Open a web page
View source: click right => View page source
Inspect element: click right => Inspect element
In Developer tools:
Open jsbin.com/copokiv/1/edit and play around a little bit
Add
to the javascript, and open the console tab
console.log("Logging to the console");
Add
to the javascript, and open the console tab
var sum = 4 + 12;
console.log(sum);
Data Driven Documents
Bind data to html elements
"A JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG, and CSS"
Selecting elements
Creating elements
Data loading
Data transformations
Scales
Colors
Chart layouts
Maps
Number and date/time formatting
Animations
... a charting library, it has no ready made charts
... very easy to learn
... a data analytics tool
Basic example: basic pie
More examples:
Advanced:
Animated charts
Dynamic charts
Custom chart types
Visualizing data on the web
Go to jsbin.com
Keep 1 jsbin tab open, and try to follow me. Use the solutions if you get stuck
Load version 7 of D3 in the html (see D3js.org homepage)
Set up svg, 800 pixels wide and 600 pixels high
Solution: jsbin.com/coxefax/1/edit
Append a circle to the svg with D3
Solution: jsbin.com/coxefax/2/edit
Inline data
Load csv file with d3.csv
Log the data
const data = [
{"name": "datapoint1", "value": 20},
{"name": "datapoint2", "value": 33}
]
d3.csv("my-data-file.csv";).then(data => {
console.log(data)
});
Select elements
(even when they are not existing yet!)
Bind data with data()
Append elements with join()
svg.selectAll("circle")
.data(circledata)
.join("circle")
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y)
.attr("r", (d) => d.radius)
Draw 2 or more circles from data with D3
Map data to visual properties of elements
From input domain to output range
Load the data with d3.csv(), and convert to numbers
Define a linear X scale
Define a linear Y scale (from bottom to top)
d3.scaleSqrt() for radius
d3.scaleOrdinal() for colors
stroke & fill attributes
2 ways of adding color: as a style attribute, or through css with classes
d3.scaleLog()
Scale circles with d3.scaleSqrt()
Convert X scale to a logarithmic scale with d3.scaleLog()
Add a color scale to fill by continent with d3.scaleOrdinal()
Sort data to show small bubbles on top
No D3, just "vanilla" javascript
data.sort(function(a,b){
return b.population - a.population;
})
Sort the data (also try to revert the sorting)
Add a <g> element
Transform
Adjust scales
d3.axisBottom()
d3.axisLeft()
Refactor to implement the margins convention
Add axes
Add grid with negative inner tick size + css for colors
Text annotation for the year
Tooltips
Animation