go to slides.com/evandana/d3-intro-3/live and play this d3 game
Evan Dana | Sapient
Evan Dana | Sapient
D3.chart.* | Sapient Global Markets | Putnam Investments
Rochester Institute of Technology
November Project | Ironman | Bike commuter
Web basics
(HTML, CSS, DOM)
Programming basics
(JavaScript)
Edit live | Sandboxed
change 'duration' to be 200 or 4000
Evan Dana | Sapient
HTML5
HTML
Library | Notes | Pros |
D3.js | JS SVG Library | Event-driven |
Highcharts | JS SVG Library, Free for non-commercial | IE6+ |
Google Charts | JS SVG Library | Exceptional documentation |
Raphael | JS SVG Library, VML for sub IE9 | Old browsers |
Paper.js | canvas, PaperScript, good for games (frame-based, object-based animations) | Android |
Processing.js | canvas, not easily interactive, meant to teach programming, ProcessingScript based on Java, fast rendering | Android |
SVG | HTML | Canvas
elements | charts | information
general purpose visualization library
Image Credits: www.sitepoint.com/how-to-choose-between-canvas-and-svg/ and http://mbtaviz.github.io/
HiFi & Zoom
Interactive Charts
d3.chart | nvd3.org | c3.js
Evan Dana | Sapient
<svg width="400" height="110">
<rect width="300" height="100"
style="fill:rgb(0,0,255);stroke-width:3;
stroke:rgb(0,0,0)" />
<circle cx="50" cy="50" r="40" stroke="black"
stroke-width="3" fill="red" />
<line x1="0" y1="0" x2="200" y2="200"
style="stroke:rgb(255,0,0);stroke-width:2" />
<text x="0" y="15" fill="red">I love SVG!</text>
</svg>
width, height, fill, stroke
cx, cy, r, fill
x1, y1, x2, y2, stroke
x, y, fill
rect
circle
line
text
SVG Tutorial:
SVG Filters:
SVG Gradients:
SVG Examples:
SVG Reference:
Create the below chart using SVG elements
Click "Save" on left to Clone it
Color: 'blue'.
Longest bar is 300px wide.
10px vertical space between bars.
Tips
Evan Dana | Sapient
An extremely brief review
"a"
1
true
[1, 2, 3]
{a:1}
undefined
null
pro tip: test in your console: 'typeof "a"'
(true)
(0)
(1==1)
(1=='1')
(1==='1')
({a:1}==={a:1})
pro tip: test in your console
function increment(x) {
return x++;
}
increment(0);
pro tip: test in your console
var carFactory = function () {
var thisCar = this;
function repaint (color) {
// repaint thisCar
return thisCar;
}
function changeExhaust () {
// change exhaust
return thisCar;
}
return {
repaint: repaint
};
}
var car = carFactory();
// chain operations
car.repaint().changeExhaust();
pro tip: test in your console
Evan Dana | Sapient
selection
.attr('cx', function (d, i) {return d*10;})
Function that gets your data
(or a static arg that becomes a functor)
Function
d3.selectAll('.chart')
.selectAll('circle')
.data(data)
.enter()
.append('circle')
.attr('cx', function (d) {return d*10;})
.attr('cy', 10)
.attr('r', 20)
.style('fill', 'blue');
In the document,
select every <circle> element
in every element that has a class of 'chart'
d3.selectAll('.chart')
.selectAll('circle')
Bind
to the selection
the data (based on an index)
.data(data)
For the selection of elements bound to data
that do not yet have existing elements
.enter()
To the selection of elements bound to data, for which elements are not created,
add a <circle> element
.append('circle')
To each appended element,
define these attributes
.attr('cx', function (d) {return d*10;})
.attr('cy', 10)
.attr('r', 20)
.style('fill', 'blue');
Evan Dana | Sapient
define group
bind data to group
enter()
append group
define group attributes
append element to group
define element attributes
Partner up!
Create the chart of circles using d3
Click "Save" to Clone it
Evan Dana | Sapient
selection | bind | enter | update | exit }
Use the code on the right as a template to create a d3 graphic with dynamic data
Use the code on the right as a template to
create a d3 graphic with dynamic data
Use the code on the right as a template to
create a d3 graphic with dynamic data
enter, update, remove
transition, duration, delay
constancy
Partner up!
Create a chart with dynamic data
Click "Save" to Clone it
Evan Dana | Sapient
...
1. Bar chart that resizes with the window
2. Updates with dynamic data
3. Handles enter/exit
4. Add bonus!
Click "Save" to Clone it