+ All Categories
Home > Documents > Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects...

Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects...

Date post: 19-Jul-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
30
Introduction to the D3 library Presenter Barnabé Monnot Contents The anatomy of a webpage First D3.js script for SVG manipulation Some cool layouts 1
Transcript
Page 1: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

Introduction to the D3 libraryPresenter Barnabé Monnot

Contents

› The anatomy of a webpage

› First D3.js script for SVG manipulation

› Some cool layouts

1

Page 2: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

D3 is awesome for...

› Creating interactive data visualization

› Tying the visualizations with maps, geographical data

› Manipulating webpages à la jQuery

› With D3 available on Node.js, access to cool data manipulation procedures

2

Page 3: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

Some examples

3

Page 4: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

Some examples

4

Page 5: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

Some examples

5

Page 6: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

Some examples

6

Page 7: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

Typical workflow

7

R D3 PS, AIGet the data Get the SVG

Page 8: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› D3 was created by Mike Bostock

› Evolved from the subject of his PhD thesis, working on a language, Protovis, to visualize data

› He is now in charge of making great interactive data visualizations for the New York Times(example, example)

Mike Bostock

8

Page 9: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› Nothing fancy, but introduces key concepts such as:

» Drawing in a webpage

» Selections

» Data-binding

» Enter/Update/Exit pattern

» Scales

First example: Bar chart

9

Page 10: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› Let’s create a JSFiddle, go to jsfiddle.net

First example: Bar chart

10

Page 11: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› What we want

First example: Bar chart

11

Page 12: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› What we want

First example: Bar chart

12My eyes are burning!

Page 13: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› What we want

First example: Bar chart

13

Notice 10 px margin here

Top bar takes all the height

Page 14: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› HTML is made up of tags, such as h1 or div

› div is a particular one: defines a box in which to draw (text, image...)

› Tags can be given id’s (unique) and classes (reusable)<h1 id=‘title’></h1><p class=‘myp’></p>

› When we refer to them (in CSS or with D3), use # for titles and . for classes#title // refers to the h1 title tagged #title.myp // refers to all elements classed .myp

HTML first

14

Page 15: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› D3 acts on selections of objectsd3.select(”#title”)

selects the DOM element named #title

› Once we have selected an object, we can change its attributes// Set content of h1 tagd3.select(”#title”).text(“Introduction”)

› We can also select all objects that have a certain property// Select all elements classed bard3.selectAll(”.bar”)

Selections

15

Page 16: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

SVG

16

x

y

var svg = d3.append(“svg”).attr(“width”,200).attr(“height”,100)// creates <svg width=200 height=100>

Page 17: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

SVG

17

x

y

svg.append(“rect”).attr(“height”, 20).attr(“width”, 40).attr(“x”, 50).attr(“y”, 40).attr(“fill”, “blue”)

// creates<svg width=200 height=100>

<rect x=50 y=40 height=20 width=40></svg>

50 90

40

60

Page 18: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› We want to draw the data var data = [1, 3, 2, 4];

› Each piece of data will be represented by a rectangle classed .bar

› We select all .bar elements and attach the data to itvar barsvg = d3.selectAll(”.bar”).data(data);

Data-binding

18

barsvg

data 1 3 2 4

Page 19: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› We can get the enter selection via the enter() function applied to our selection barsvg

barsvg.enter()

› The enter selection corresponds to the dashed boxes below: data that is yet to be drawn

Enter selection

19

barsvg.enter()

data 1 3 2 4

Page 20: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› Scales are an easy way to map inputs from a domain to outputs in a range

› There are a couple of cool options to work with: linear scales, logarithmic, qualitative...

Scales

20

0 3

10 100

21

7040

var scale = d3.scale.linear().domain([0,3]).range([10,100])

scale(1) = 40 scale(2) = 70

Page 21: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› We can get the exit selection via the exit() function applied to our selection barsvg

barsvg.exit()

› The exit selection corresponds to the dashed boxes below: data that is yet to be removed from the drawing

Exit selection

21

barsvg.exit()

data 3 2 4

Page 22: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› D3 comes packaged with lots of nice layouts: force (network), dendogram (shown in the first slides) ...

› What this means is it computes boundaries for you, and your job is simply to draw them! => can then animate, make interactive ...

› Here we learn:

» Using a layout

» Color scales

» Not drawing SVG (using standard HTML elements)

Example: drawing a treemap

22

Page 23: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› We can’t use JSFiddle anymore: can’t load external resources in there...

› Fire up your favorite code editor (Notepad++, Sublime, Atom, TextMate, ...)

› Create a d3 folder somewhere (e.g, on your Desktop)

› Create a new file treemap.html

No more Fiddle

23

Page 24: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› We can’t use JSFiddle anymore, so we write a bit more HTML!

The HTML boilerplate

24

<!DOCTYPE html><html>

<meta><title>D3 is awesome!</title><script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>

</meta><body>

// Where JSFiddle HTML was</body>

</html>

Page 25: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› We add a script tag under the body one

The HTML boilerplate

25

<!DOCTYPE html><html>

<meta><title>Malta</title><script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>

</meta><body>

// Where JSFiddle HTML was</body><script>

// Your code goes here</script>

</html>

Page 26: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› Go to barnabemonnot.com/d3/data

› Decompress the zip in your D3 folder

› Open RStudio (yes! yes!)

› Use setwd() to set the directory to your D3 folder

› Remember last week? Use dplyr

› If you don’t have R, or I don’t have enough time, skip this part J

Getting the data

26

Page 27: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

R code

27

install.packages("dplyr")library("dplyr")

dat <- read.csv("imports_manufactures.csv")names <- read.csv("importkey.csv")total <- dat %>%

group_by(from) %>%summarise(sum(size)) %>%left_join(names, by=c("from"="id")) %>%select(name,2)

write.table(total, file="imports.csv", row.names=FALSE, col.names=FALSE, sep=",")

Page 28: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› D3 is also a breeze to work with maps.

› Here we learn:

» Loading external resources

» Create groups in our SVG

» Work with map data

» Where Malta is

» Respond to events and transitions

Example: drawing a map

28

Page 29: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› Go to barnabemonnot.com/d3/malta

› Get the code and paste it in a new file, malta.html, inside your D3 folder

› We are going to complete parts of it

Example: drawing a map

29

Page 30: Introduction to the D3 library - Amazon S3 · ›D3 acts on selectionsof objects d3.select(”#title”) selects the DOM element named #title ›Once we have selected an object, we

› Groups are nice to create layers to your drawing, easier then to reorder them if necessary

SVG groups

30

<svg width=200 height=100><g class=“borders”></g> // Borders are drawn first<g class=“pois”></g> // Then the points of interest<g class=“names”></g> // Then the names

</svg>


Recommended