Sadique Ali
Mike Bostock
No new representation
Construct DOM from data
HTML
SVG
Well understood
Yes!
<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="500">
<circle cx="480" cy="250" r="200" fill="steelblue">
</circle>
</svg>
python -m SimpleHTTPServer 7654
You can thank me later!
01-barchart.html
<div id="barchart">
</div>
var data = [820, 883, 903, 836,
915, 981, 892, 761, 858, 757, 363];
var chart = d3.select("#barchart") .append("svg") .attr ("class", "chart") .attr ("width", 1000) .attr ("height", 20 * data.length);
select
selectAll
#foo //<any id="foo">
foo //<foo>
.foo //<anyclass="foo">
[foo=bar] //<any foo="bar">
foo bar //<foo><bar></foo>
selection.append("svg")
selection.attr("width", 1000)
chart.selectAll ("rect")
.data (data)
.enter ()
.append ("rect")
.attr ("width", function (d) {
return d;
})
var rectangles = chart.selectAll("rect")
.data(data)
rects correspond to data
data
provides enter
and exit
var rectangles = chart.selectAll("rect")
.data(data)
rectangles.enter()
.append("rect")
append
on enter()
adds missing elements
var rectangles = chart.selectAll("rect")
.data(data)
rectangles.enter()
.append("rect")
.attr("y", function(d, i) {
return i * 20;
})
Elements are bound to data
Heights and Widths
Size of dataset
var x = d3.scale.linear()
.domain([12, 24])
.range([0, 720]);
x(16); // 240
Linear scale: Translate and Scale
var x = d3.scale.sqrt()
.domain([12, 24])
.range([0, 720]);
x(16); // 268.9056992603583
sqrt - exponential
Useful for circles
var x = d3.scale.log()
.domain([12, 24])
.range([0, 720]);
x(16); // 298.82699948076737
log - you get the idea
var x = d3.scale.linear()
.domain([12, 24])
.range(["steelblue", "brown"]);
x(16); // #666586
Isn't that cool?
var x = d3.scale.ordinal()
.domain(["A", "B", "C", "D"])
.range([0, 10, 20, 30]);
x("B"); // 10
Explicit mapping
02-barchart2.html
Using scales
Axes
Labeling
Labeling for scales
03-barchart3.html
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
Create an Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
Append it to g
04-barchart4.html
var yRule = chart.append("g")
.selectAll(".rule")
.data(xScale.ticks(20))
.enter()
.append("g");
Create g
using Ticks
var yRule = chart.append("g")
.selectAll(".rule")
.data(xScale.ticks(20))
.enter()
.append("g");
yRule.append("line")
.attr("y2", height - 2 * margins.bottom);
Draw a line for each tick
Scatterplot
Scale
Axes
Rules
scatterplot.html
bowlers.json
{
"cap"=>1, "name"=>"Amar Singh",
"debut_year"=>1932,
"series"=>"india in england, 1932",
"winner"=>"eng",
"performance"=>{
"Mat"=>"1", "Runs"=>"56",
"HS"=>"51", "BatAv"=>"28.00",
"100s"=>"0", "50s"=>"1",
"W"=>"4", "BB"=>"2/75", "Ct"=>"0"
"BowlAv"=>"39.75", "5w"=>"0",
}
}
d.name
d.country
d.performance.BowlAv
I need feedback!