A tutorial by Dominik Moritz (@domoritz) and Kanit "Ham" Wongsuphasawat (@kanitw)
Interactive Data Lab, University of Washington
(Based on Vadim Ogievetsky and Scott Murray's work)
D3 uses CSS Selectors
Single selector:
#foo //
foo //
.foo //
[foo=bar] //
foo bar //
foo.bar //
foo#bar //
.select()
.select()
+ .attr()
, .style()
Select and modify element properties
Note: notice chained syntax, object as argument
.select()
only selects one item!
.selectAll()
See also: http://mbostock.github.io/d3/tutorial/circle.html
.data()
.data()
Tell D3 that you want the selected elements to correspond to data!
__data__
property!
.data()
is called, __data__
of each element will contain the data mapped to that element.You should have seen this from last slide!
Data can be objects too!
.data()
Calling .data()
creates three arrays:
enter
(Data without corresponding DOM elements)update
(DOM elements mapped to data)exit
(DOM elements now missing data).data()
returns the update.
.enter()
.enter()
Shorter: append, then update all together!
.enter()
from scratchThis is a common way that people build visualization in D3!
.exit()
.transition()
Note: Transitions in D3 use cubic-in-out as default easing functions. But there are others.
.transition()
& .delay()
The default is index mapping.
.data(...,key)
A key should be a unique string.
http://bost.ocks.org/mike/selection/#key
.data(...,key)
Notice how you can save lines of code with selection.call()
http://bost.ocks.org/mike/selection/#key
New Data?
See even better bars transition.