## Agenda
- Introduction
- Examples
- A guide of bar chart
- The analysis of source code
- The Wealth & Health of Nations
- An time series visual design of Adserver svn commit record
- Resources
2. AGENDA
Introduction
Examples
A guide of bar chart
The analysis of source code
The Wealth & Health of Nations
An time series visual design of Adserver svn commit
record
Resources
4. D3.JS: DATA-DRIVEN DOCUMENTS
JavaScript Library
Open source (BSD license)
developed by Michael Bostock and others at Stanford
University
former work on Protovis Library
from Intro to d3
5. D3.JS: DATA-DRIVEN DOCUMENTS
not a traditional visualization framework
focus on efficient manipulation of documents based on
data
bind arbitrary data to the DOM
apply data-driven transformations to the document
functional/declarative style
support for interaction and animation
you have to do the graphical representation yourself!
but has helpers for SVG generation
from Intro to d3
8. DYNAMIC PROPERTIES
d3.selectAll("p")
.data([4, 8, 15, 16, 23, 42])
.style("font-size", function(d, i) { return d + "px"; });
bind data to a selection
apply functions to styles, attributes and other
properties
a lot of built-in reusable functions available
bound data available as first argument
you can omit the data operator the second time!
9. ENTER AND EXIT
var p = d3.select("body").selectAll("p") // Update…
.data([4, 8, 15, 16, 23, 42])
.text(String);
p.enter().append("p") // Enter…
.text(String);
p.exit().remove(); // Exit…
enter: selects all data elements without a DOM
representation
exit: selects all DOM elements without data
common pattern
perform only necessary modifications
10. SUBSELECTIONS
d3.selectAll("ul")
.data(questions) // an array of questions
.selectAll("li")
// a nested array of responses
.data(function(d) { return d.responses; })
// the text of the response
.text(function(d) { return d.text; });
generate a subselection by calling selectAll on an
existing selection
maintains the hierarchical structure
11. TRANSITIONS
// fade background from white to black in 1s
d3.select("body").transition()
.duration(1000)
.style("background-color", "black");
with d3.js they are easy!
various tweening functions
interpolation of basic types, numbers and numbers in
strings (e.g. "15px")
18. Agglo merativeCluster
lity
Co mmunityStructure
L inkD e nne ssCe ntra
O pe rato pe rato rL ist
te r
Stac ando m L ayo u t
HierarchicalClus
O pe rato rSw e
ke r
No d
Span e stPaths t
Tre e M
Sho rt lo wMinCu
Rad
So rtO pe ratoh
Ind Icicle cte d L ayyo ut t
ke dA
nce
Visualization
R
B an
Fo ndr ck le L o u ut
e
O
Tre e
MaxF istance
e L in
rSe que nc
Me rge Edg
e nt
rce o g ing ay te
ialTr Pie L ay o ut
De Pa c R o
Cir
r
que
Ob mbixInte to r lato r r
O pe ra
atio
Int te In te rp o lato
re aL ayo ut
Nu atr o la rpo ato
apL a
ed
kTre L ayyo ut
ning
cle
Bu
Dir ra L a o u r
e I la to r
ee
ct Inte te rpo to r
gl o o la lato
Be twe
nSe
e ctR
ol
nd
Tre e L ay o ut
e
r
nt to r
L
to
Tre L ay o ut
Dao lo rIn te rp
ayo u
e L a o ut
Sta
itc
Reo int ctInrInte o la
yo utt
le d A
la
e La ou
to r
ctio
Funsing
er r
Cir ge ay
ck R
r
po
rp
Asp
C rayIn
y
m
M e rp te
Ed xisL
ou
an rp rp
e d ad
Ea
t
Ar ial L a
P je e
Ar
e a L a be o r
le
t
Se cheuse le l lab
L a be le
G
n
be le r
ra
l u
Fis
izatio
Pa ara e d
IO
p
le r
hD
cluste r
he
itio nc r
Vi tan
pe Filtete r
ns ue ule
P ch
r
e
graph
sib c
t
is
ye
er en
ra
IS
Tra q d
r
o ptim
ilit e F
Tre
n
o n nEv rte
te
t
Pr Sha Siz
y il
lay
op p eE eF iti io ve r
o la
S
e r e E nc ilte ns sit on e
o ut
r
Traran e n rs tC rt
e rp
tyE nc o
Co n o de r T we rte Te x nv e r
lo En co de r
int
Fis rEn co de r T v e te d Co rte
o nlimi hML nv e rte r
la
he co de r
b
ye C e p o ve
el
Bif D de r D ra aC o n
cs
o c D isto r G at NC
analyti
alD isto rtio ID SO
filt
isto rtio n ld a
op
Le en J Fie he m
e
ge rtio n
r
co s ataaSc t
e ra
L e ndR n de rte
r D at e ce
g e an D ataS o ur
nd ge r ve
to r
e
L e g Ite m dis on D taS able
at
Visu
en to c Da taT til
im
aliz d rtio
a
T tio n Da taU
an
n Da
Se leo o ltip Ev e nt le g rite
ctio Ev e n ySp
Dat nEv t en
d Dirt e Sprite
aEv e nt L in
e nt
Tre e eve
Build nts
e ta lay
Scale Tre er da disp
Bindin
g
Shape R
e nde re fle x
IRe nd r
Edge Re nd e re r v is
e re r re nde r
Arro wType
physics
Node Sprite data
EdgeSprite
DataSprite fla
re
DataList
Data
ntro l
To o ltipCo ntro l
nCo
Se le ctiomCo ntro l ls
o ol ro
PanZo ICo ntr o l co nt
ntr
e rCo ntro l
Ho v dCo
n ro l
Expa agCo ntlL ist
Dr o ntro o l
C tr qu
Co n tro l is er
kCo
n ax y
Clic
48. PLAY WITH DATA
var dataset = [ 25, 7, 5, 26, 11, 8, 25, 14, 23, 19,
14, 11, 22, 29, 11, 13, 12, 17, 18, 10,
24, 18, 25, 9, 3 ];
d3.select("body").selectAll("div")
.data(dataset) // <-- The answer is here!
.enter()
.append("div")
.attr("class", "bar")
.style("height", function(d) {
var barHeight = d * 5;
return barHeight + "px";
});
49. MAKING A BAR CHART
var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
50. MAKING A BAR CHART
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", 20)
.attr("height", 100);
51. MAKING A BAR CHART
.attr("x", function(d, i) {
return i * 21; //Bar width of 20 plus 1 for padding
})
52. MAKING A BAR CHART
.attr("height", function(d) {
return d;
});
53. MAKING A BAR CHART
.attr("y", function(d) {
return h - d; //Height minus data value
})
.attr("height", function(d) {
return d * 4; //Just the data value
});
54. MAKING A BAR CHART
.attr("fill", function(d) {
return "rgb(0, 0, " + (d * 10) + ")";
});
55. MAKING A BAR CHART
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d) {
return d;
})
.attr("text-anchor", "middle")
.attr("x", function(d, i) {
return i * (w / dataset.length) + (w / dataset.length - barPadd
ing) / 2;
})
.attr("y", function(d) {
return h - (d * 4) + 14;
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "white");
59. TRANSITION
// Start a transition that interpolates the data based on year.
svg.transition()
.duration(duration)
.ease("linear")
.tween("year", tweenYear())
.each("end", enableInteraction);
// Tweens the entire chart by first tweening the year, and then the da
ta.
// For the interpolated data, the dots and label are redrawn.
function tweenYear(start, end) {
start = start || 1800;
end = end || 2009;
return function() {
var year = d3.interpolateNumber(start, end);
return function(t) { displayYear(year(t)); };
}
}
60. INTERPOLATE DATA
// Interpolates the dataset for the given (fractional) year.
function interpolateData(year) {
return nations.map(function(d) {
return {
name: d.name,
income: interpolateValues(d.income, year),
//...
};
});
}
// Finds (and possibly interpolates) the value for the specified year.
function interpolateValues(values, year) {
var i = bisect.left(values, year, 0, values.length - 1),
a = values[i];
if (i > 0) {
var b = values[i - 1],
t = (year - a[0]) / (b[0] - a[0]);
67. RESOURCES
D3js Wiki
Paper:D3: Data-Driven Documents
Mike Bostock
Visual.ly
David McCandless: The beauty of data visualization
GapMinder - for a fact-based world view