Aysegul Yonet / @AysegulYonet
Slides : bit.ly/Neohack-d3
Not a prototyping tool.
Lot's of examples
Interesting fact: if a properly specified value is provided for rx but not for ry (or the opposite), then the browser will consider the missing value equal to the defined one.
Unless you set the style of the SVG text, it inherits font-styles from CSS.
Any transformations (positioning, scaling, rotating) that are applied to the group element will also be applied to each of the child elements.
var paragraphs = $("div p");
var paragraphs = d3.select("div").selectAll("p");
Bind array of data to child nodes
d3.selectAll("p")
.data([3, 7, 21, 31, 35, 42])
d3.select("body").selectAll("p")
.data([3, 7, 21, 31, 35, 42])
.enter().append("p");
var bars = d3.select('svg').selectAll('rect').data(someData);
//if we have more data, add new rectangles for those data items
bars.enter().append('rect')
//if we have less data points, remove the rectangles that no longer have a data pairing.
bars.exit().remove();
// Update…
var p = d3.select("body").selectAll("p")
.data([3, 7, 21, 31, 35, 42]);
// Enter…
p.enter().append("p")
// Exit…
p.exit().remove();
Exercise: bit.ly/enter-exit-exercise
If you get stuck...Sotlution
You can operate over the nodes in a declarative way using selector methods.
You can use data values or index in a callback to define any property.
d3.select("div").selectAll("p").style("color", function(data, i) {
return i % 2 ? "red" : "blue";
});
d3.selectAll("circle")
.attr("r", "0");
.transition()
.duration(750)
.delay(function(d, i) { return i * 10; })
.attr("r", function(d) { return Math.sqrt(d); });
Exercise 0: Look at the example and create a transition to change the size of the circles:bit.ly/d3-transitions
Exercise 1: bit.ly/d3exercise1
In case you get stuck... Solution for Exercise 1
Scales and Domains
Axis
Loading external data
Scales transform a number in a certain interval (called the domain) into a number in another interval (called the range).
var x = d3.scale.linear()
.domain([0, d3.max(data)])
.range([0, 420]);
d3.select(".chart")
.selectAll("div")
.data([3, 7, 21, 31, 35, 42])
.enter().append("div")
.style("width", function(d) { return x(d) + "px"; })
JSFiddle
Ordinal Scale have a discrete domain, such as a set of names or categories.
var x = d3.scale.ordinal()
.domain(["A", "B", "C", "D", "E", "F"])
.rangeRoundBands([0, width], .1);
d3.scale.category10() - Constructs a new ordinal scale with a range of ten categorical colors:
Solution: Bar Charts with Scale
d3.svg.axis creates a new axis generator.
var xAxis = d3.svg.axis()
.scale(x)
.orient('bottom')
.tickValues(data);
JSFiddle
d3.time.format - creates a new local time formatter for a given specifier.
d3.time.format("%Y-%m-%d");
JSFiddle
d3.time.scale - constructs a linear time scale.
var xScale = d3.time.scale()
.domain([2009-07-13T00:02, 2009-07-13T23:48])
.rangeRound([0, width]);
//rangeRound does the same thing as range but rounds the values to integers or begining of dates.
JSFiddle
d3.time.intervals - a time interval in local time.
Most commonly used functions
Slides : bit.ly/Neohack-d3