D3.js Tutorial: Download The Code Examples!
D3.js Tutorial: Download The Code Examples!
10
20
45
6
25
D3.js Tutorial
Strata Conference
February 26, 2013
Santa Clara
github.com/alignedleft/strata-d3-tutorial
Download the code examples!
5
10
20
45
6
25
Jrme Cukier
Independent data visualizer
Scott Murray
Assistant Professor, Design
University of San Francisco
github.com/alignedleft/strata-d3-tutorial
Download the code examples!
What is d3.js?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 Page Template</title>
<script type="text/javascript" src="d3.v3.js"></script>
</head>
<body>
<script type="text/javascript">
// Your beautiful D3 code can go here
</script>
</body>
</html>
HTML
CSS
JS
SVG
DOM
Hypertext Markup Language
Cascading Style Sheets
JavaScript
Scalable Vector Graphics
The Document Object Model
all of the above == web standards
HTML
CSS
JS
SVG
DOM
Hypertext Markup Language
Cascading Style Sheets
JavaScript
Scalable Vector Graphics
The Document Object Model
Learning D3 is a process of learning the web
What you need
A text editor,
The d3 library,
A browser.
A text editor
Forms
And it doesnt have to be a full-fledged form: controls like drop-
down menus, tick boxes, sliders etc.
function gores() {
var n = +d3.select("#gores").property("value");
}
When the slider value changes, the "change"
event is triggered and so the gores() function
is called.
Then, this function does stuff depending on
the position of the slider, which is obtained
thusly: by looking up the property "value" of
the slider.
Let's see how it looks like in the code
menu.selectAll("option")
.data(options)
.enter().append("option")
.text(function(d) { return d.name; });
var menu = d3.select("#projection-menu")
.on("change", change);
function change() {
update(options[this.selectedIndex]);
}
Here, the menu is going to be populated with
data.
Then, just as before, a change event will
trigger a function : change.
This function will call another one based on which item
is selected in the drop down menu.
Here we have highlight on mouseover.
http://bl.ocks.org/3709000
Let's see how it looks like in the code
var projection = svg.selectAll(".axis text,.background path,.foreground path")
.on("mouseover", mouseover)
.on("mouseout", mouseout);
function mouseover(d) {
svg.classed("active", true);
projection.classed("inactive", function(p) { return p !== d; });
projection.filter(function(p) { return p === d; }).each(moveToFront);
}
function mouseout(d) {
svg.classed("active", false);
projection.classed("inactive", false);
}
function moveToFront() {
this.parentNode.appendChild(this);
}
This is our on - event function
This function is called upon mouseover.
It uses the underlying value of the line (d).
The formatting is done by
assigning a CSS class.
And this function is called upon mouseout,
it essentially resets the other one.
Questions?
Whats next?
// Generate axes
var axis = d3.svg.axis()
.scale(scale);
svg.append("g")
.call(axis);
200 300 400 500 600 700 800 900 1000
// Layouts
// Geomapping and projections
Scott Murray
@alignedleft
alignedleft.com
An Introduction to Designing With D3
Scott Murray
Interactive Data
Visualization
for the Web
Jrme Cukier
@jcukier
jeromecukier.net
Book signing today!
5:30 pm
OReilly Booth
Expo Hall