Escolar Documentos
Profissional Documentos
Cultura Documentos
Homework 3
Create a bar chart with D3
Due September 30
Fall 2014
CS 4460
Assumptions
You have a basic understanding of the
following topics
HTML
CSS
Javascript
CS 4460
CS 4460
What is D3 Doing?
HTML elements => Webpage
Manipulate HTML elements dynamically
with Javascript based on input data to
create visualizations
CS 4460
Reference Tutorial
Lets Make a Bar Chart by Mike Bostock
Use examples in this tutorial
Part 2: http://bost.ocks.org/mike/bar/2/
Part 3: http://bost.ocks.org/mike/bar/3/
Fall 2014
CS 4460
Selections
Fall 2014
e.g. d3.select(this)
https://github.com/mbostock/d3/wiki/Selections
CS 4460
10
Loading Data
Fall 2014
https://github.com/mbostock/d3/wiki/Requests
CS 4460
11
12
CS 4460
13
14
Linear Scale
420
var x = d3.scale.linear()
.domain([1,5]).range([0,420]);
x(3); // 210
Fall 2014
Range
15
Ordinal Scales
var x = d3.scale.ordinal()
.domain([a, b, c]).range([0,210,420]);
x(b); // 210
color(a); // #1f77b4
Fall 2014
CS 4460
16
17
18
Fall 2014
CS 4460
19
20
Fall 2014
CS 4460
21
22
Enter()
Fall 2014
group.append(g)
.attr(transform, ...);
CS 4460
23
What if we need to
update the data?
Fall 2014
CS 4460
24
Three groups
http://bost.ocks.org/mike/join/
New data
Enter group
Fall 2014
Existing Elements
Update
group
CS 4460
Exit group
25
Fall 2014
group.enter()
group.exit()
26
.data(data)
.enter().append(g)
.attr(transform, ...);
CS 4460
27
Index
0
1
2
3
4
5
6
Fall 2014
CS 4460
28
29
What is bar?
Fall 2014
CS 4460
30
Adding Rects
bar is a group of <g> elements with data!
bar.append(rect)
.attr(width, function(d){
return x(d.value);
})
.attr(height, barHeight);
http://www.w3.org/TR/SVG/shapes.html
Fall 2014
CS 4460
31
Method Chaining
D3 extensively uses it
Fall 2014
group.attr(...);
group.attr(...);
group.attr(...).attr(...);
33
34
Adding Text
http://www.w3.org/TR/SVG/text.html
?
Fall 2014
CS 4460
35
36
Another Example
Fall 2014
CS 4460
37
38
RangeBand()
var x = d3.scale.ordinal()
.domain(A, B, C)
.rangeBands([0,width], 0.3); // range with padding
https://github.com/mbostock/d3/wiki/Ordinal-Scales
Padding
x.rangeBand()
range()
Fall 2014
CS 4460
39
40
Axes
https://github.com/mbostock/d3/wiki/SVG-Axes
Fall 2014
CS 4460
41
42
Mouse Interactions
CSS
D3
d3.selectAll(.bar)
.on("mouseover", function(){
d3.select(this).style("fill","brown");
})
.on("mouseout", function(){
d3.select(this).style("fill","steelblue");
});
https://github.com/mbostock/d3/wiki/Selections
Fall 2014
CS 4460
43
Transitions
d3.selectAll(.bar)
.on("mouseout", function(){
d3.select(this)
.transition()
.duration(500)
.style("fill","steelblue");
});
https://github.com/mbostock/d3/wiki/Transitions
Fall 2014
CS 4460
44
45
Debugging
Google Chrome Developer Tools
Inspect HTML elements
Debug Javascript
Fall 2014
CS 4460
46
Examples
Example 1
http://bl.ocks.org/mbostock/7341714
Example 2
http://bl.ocks.org/mbostock/3885304
Fall 2014
CS 4460
47
Homework 3
Create a bar chart with D3
Due September 30
Fall 2014
CS 4460
48
Project Feedback
Fall 2014
CS 4460
49
Upcoming
Systems & Toolkits
InfoVis
Reading:
CS 4460
50