Slides : bit.ly/sfhtml5-d3
Aysegul Yonet / @AysegulYonet
var paragraphs = $("div p");
var paragraphs = d3.select("div").selectAll("p");
jsFiddle
var p = d3.select("body").append('p');
p.html('Hello SFHTML5!').attr('style', 'color: red;');
d3.selectAll("p")
.data([0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
d3.select("body").selectAll("p")
.data([3, 7, 21, 31, 35, 42])
.enter();
d3.select("body").selectAll("p")
.data([3, 7, 21, 31, 35, 42])
.enter().append("p")
.text(function(d) { return "I’m number " + d + "!"; });
var bars = d3.select('body').selectAll('p').data(newData);
//if we have more data, add new 'p' for those data items
bars.enter().append('p')
//if we have less data points, remove the 'p' 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();
d3.select("body").selectAll("p")
.data([3, 7, 21, 31, 35, 42])
.enter().append("p")
.exit().remove();
circles.attr("r", "0")
.attr({
'cx': function(d) {return (d * 2); },
'cy': 10
})
.transition()
.duration(750)
.delay(function(d, i) { return i * 100; })
.attr("r", 10);
var scale = d3.scale.linear();
var xScale = d3.scale.linear()
.domain([0, d3.max(data)])// your data minimum and maximum
.range([0, 420]);//the pixels to map to
d3.select(".chart")
.selectAll("div")
.data([3, 7, 21, 31, 35, 42])
.enter().append("div")
.style("width", function(d) { return xScale(d) + "px"; })
JSFiddle
var xScale = d3.scale.linear()
.domain([0, 1000])
.range([0, 200]);
console.log(xScale(500)); //100
console.log(xScale.domain());//[0, 1000]
Ordinal Scale have a discrete domain, such as a set of names or categories.
var xScale = d3.scale.ordinal()
.domain(["Bob", "Stuart", "Kevin", "Scarlet"])
.rangePoints([0, 100]);
console.log(xScale("Stuart"));//33.333333333333336
xScale.range(); // [0, 33.333333333333336, 66.66666666666667, 100]
d3.scale.category10()
var svg = d3.select('body').append('svg')
.attr({
'width': 250,
'height': 250
});
var xScale = d3.scale.linear()
.domain([0, 5])
.range([0, 200]);
var render = function(data, r, color){
//Bind data
var circles = svg.selectAll('circle').data(data);
//Enter and update
circles.enter().append('circle')
.attr({
'cx': function(d){return xScale(d)},
'cy': 30,
'r': r,
'fill': function(){return color || 'red'}
});
}
var oldData = [1, 2, 3];
var newData = [1, 2, 3, 4, 5];
render(oldData, 10);
render(newData, 15, 'blue');
var render = function(data, r, color){
//Bind Data
var circles = svg.selectAll('circle').data(data);
//Enter
circles.enter().append('circle')
//Update
circles.attr({
'cx': function(d){return xScale(d)},
'cy': 30,
'r': r,
'fill': function(){return color || 'red'}
})
}
var oldData = [1, 2, 3, 4, 5];
var newData = [1, 2, 3];
render(oldData, 10);
render(newData, 15, 'blue');
var render = function(data, r, color){
//Bind Data
var circles = svg.selectAll('circle').data(data);
//Enter
circles.enter().append('circle')
//Update
circles.attr({
'cx': function(d){return xScale(d)},
'cy': 30,
'r': r,
'fill': function(){return color || 'red'}
})
//Remove
circles.exit().remove();
}
day,donut
Monday,91
Tuesday,23
Wednesday,7
Thursday,4
Friday,82
Saturday,39
Sunday,27
var x = "day";
var y = "donut";
var accessor = function(d) {
d[y] = parseInt(d[y]);
return d;
}
d3.csv("data/data.csv", accessor, function(d) {
console.log(d);//Array of objects [{day: "Monday", donut: 91},...]
render(d);
});
[0, 1, 2, 3].reduce(function(a, b) {
return a - b;
});//-6
[0, 1, 2, 3].reduceRight(function(a, b) {
return a - b;
});//0
var donutData = [
{day: 'Monday', donut: 34, variety: "plain"},
{day: 'Tuesday', donut: 41, variety: "glazed"}
];
var nestedData = d3.nest()
.key(function(d){ return d.day;})
.entries(donutData);
[{
key: "Monday",
values: [
{day: 'Monday', donut: 34, variety: "plain"}
]},
{
key: "Tuesday",
values: [
{day: 'Tuesday', donut: 41, variety: "glazed"}
]}
]
var nestedData = d3.nest()
.key(function(d){ return d.day;})
.key(function(d){ return d.variety;})
.entries(donutData);
[{
key: "Monday",
values: [
{ key: "plain", values: [{day: 'Monday', donut: 34, variety: "plain"}]}
]},
{
key:"Tuesday",
values: [
{ key: "glazed", values: [{day: 'Tuesday', donut: 41, variety: "glazed"}]}
]
}
]
d3.time.format("%Y-%m-%d"); //2015-08-26
d3.format("+,%"); //+2,400%
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.
Slides : bit.ly/sfhtml5-d3