On Github chrisuehlinger / IntroToD3
7/18/2015
Reveal.js comes with a few themes built in: Default - Sky - Beige - Simple - Serif - Night Moon - Solarized
* Theme demos are loaded after the presentation which leads to flicker. In production you should load your theme in the <head> using a <link>.
SVG is basically HTML with shapes:
Similar to jQuery Selectors:
var jQuerySelection = $(".foo #bar div:first-child");
// is equivalent to:
var d3Selection = d3.selectAll(".foo #bar div:first-child");
Many similar abilities:
$(".foo").append("ul");
d3.selectAll(".foo").append("ul");
SVG is free out-of-the-box!
$("svg.myPicture").append("circle");
d3.selectAll("svg.myPicture").append("circle");
Get used to seeing things like this:
d3.selectAll("svg.my-picture")
.append("g")
.attr("class", "circle-group")
.append("circle")
.attr("cx", 100)
.attr("y", 200)
.attr("r", 50);
To create things like this:
<svg class="my-picture">
<g class="circle-group">
<circle cx="100" cy="200" r="50"></circle>
</g>
</svg>
Roll your own chained method:
function foo(){
return {
bar:function(n){
return n+1;
}
};
};
And then use it like this:
var baz = foo().bar(3); // -> 4 console.log(baz);
Capture chains in variables:
var group = d3.selectAll("svg.foo")
.append("g")
.attr("class", "bar");
To build nested stuff:
<svg class="foo"> <g class="bar"></g> </svg>
group.append("circle")
.attr("cx", 100)
.attr("y", 200)
.attr("r", 50);
<svg class="foo">
<g class="bar">
<circle cx="100" cy="200" r="50">
</circle>
</g>
</svg>
group.append("text")
.text("Hello World");
<svg class="foo">
<g class="bar">
<circle cx="100" cy="200" r="50">
</circle>
<text>Hello World</text>
</g>
</svg>
Here's some data:
var data = [
{
xPos:120,
yPos:200
},
{
xPos:220,
yPos:100
},
{
xPos:150,
yPos:80
}
];
And here's our markup:
<svg></svg>
Let's make a circle for each data point:
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", function(d) { return d.xPos; })
.attr("cy", function(d) { return d.yPos; })
.attr("r", 25);
Whoa there...
Data
var data = [
{
xPos:120,
yPos:200
},
{
xPos:220,
yPos:100
},
{
xPos:150,
yPos:80
}
];
D3 Code
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", function(d) { return d.xPos; })
.attr("cy", function(d) { return d.yPos; })
.attr("r", 25);
Markup
<svg> </svg>
var data = [
{
xPos:120,
yPos:200
},
{
xPos:220,
yPos:100
},
{
xPos:150,
yPos:80
}
];
var circle = svg.selectAll("circle")
.data(data);
// Enter
circle.enter()
.append("circle")
.attr("cx", function(d) { return d.xPos; })
.attr("cy", function(d) { return d.yPos; })
.attr("r", 25);
// Update
circle
.attr("cx", function(d) { return d.xPos; })
.attr("cy", function(d) { return d.yPos; })
.attr("r", 25);
// Exit
circle.exit()
.remove();
<svg>
<circle cx="120" cy="200" r="25"></circle>
<circle cx="220" cy="100" r="25"></circle>
<circle cx="150" cy="80" r="25"></circle>
</svg>
Scales are functions that map from an input domain to an output range. That's it.
var myScale = d3.scale.linear()
.domain([0, 100]) // Takes in a number from 1 to 100
.range([0, 1]); // Outputs a number between 0 and 1
myScale(50) // -> 0.5
myScale(22) // -> 0.22
myScale(101) // -> 1.01
D3 handles colors really well out of the box.
var color = d3.scale.linear()
.domain([0, 100]) // Takes in a number from 1 to 100
.range(["#000", "#fff"]); // Outputs a color from black to white
color(0) // -> "#000" (black)
color(100) // -> "#fff" (white)
color(50) // -> "#808080" (medium gray)
Here's a CodePen that demonstrates color scales.