Data
D3.js
HTML
SVG
CSS
.data()
.enter()
...
.select()
.selectAll()
...
.attr()
.style()
...
More examples can be found at: https://github.com/d3/d3/wiki/Gallery
D3 uses CSS Selectors
What if we want to select multiple elements at the same time?
What if number of data doesn't match number of items selected?
If the value given to .attr() or .style() is a function, it is evaluated for each selected element, in order, being passed the current datum (d), the current index (i), and the current DOM element (this)
Scales are functions that map from an input domain to an output range
var scale = d3.scaleLinear()
.domain([100, 500])
.range([10, 350]);
// note: use d3.scale().linear() for d3 version 3.x
scale(100); // returns 10
scale(300); // returns 180
scale(500); // returns 350
d3.scaleLog(), d3.scalePow(), d3.scaleTime()
var x = d3.scaleTime()
.domain([new Date(2000, 0, 1), new Date(2000, 0, 2)])
.range([0, 960]);
x(new Date(2000, 0, 1, 5)); // 200
x(new Date(2000, 0, 1, 16)); // 640
var color = d3.scaleOrdinal(d3.schemeCategory10);
D3’s axes are functions generating visual elements of an axis, including lines, labels, and ticks
var scale = d3.scaleLinear()
.domain([100, 500])
.range([10, 350]);
var axis = d3.svg.axisBottom()
.scale(scale);
// Note: use d3.axis().orient("bottom")
// for d3 version 3.x
More ease functions can be found at: https://github.com/d3/d3-ease
More event types can be found at: https://developer.mozilla.org/en-US/docs/Web/Events#Standard_events
var data;
d3.csv("filename.csv", function(d) {
d.someattribute = somevalue;
return d;
}, function(error, rows) {
data = rows;
console.log(data); // returns rows
});
console.log(data); // returns undefined
var data = [1, 1, 2, 3, 5, 8, 13, 21];
d3.pie()(data)
// returns
[
{"data": 1, "value": 1, "startAngle": 6.05, "endAngle": 6.17, "padAngle": 0},
{"data": 1, "value": 1, "startAngle": 6.17, "endAngle": 6.28, "padAngle": 0},
{"data": 2, "value": 2, "startAngle": 5.82, "endAngle": 6.05, "padAngle": 0},
{"data": 3, "value": 3, "startAngle": 5.47, "endAngle": 5.82, "padAngle": 0},
{"data": 5, "value": 5, "startAngle": 4.89, "endAngle": 5.47, "padAngle": 0},
{"data": 8, "value": 8, "startAngle": 3.96, "endAngle": 4.89, "padAngle": 0},
{"data": 13, "value": 13, "startAngle": 2.44, "endAngle": 3.96, "padAngle": 0},
{"data": 21, "value": 21, "startAngle": 0.00, "endAngle": 2.44, "padAngle": 0}
]
arc(arguments…) - generates an arc for the given arguments
var arc = d3.arc();
arc({
innerRadius: 0,
outerRadius: 100,
startAngle: 0,
endAngle: Math.PI / 2
}); // returns "M0,-100A100,100,0,0,1,100,0L0,0Z"
var arc = d3.arc()
.innerRadius(0)
.outerRadius(100)
.startAngle(0)
.endAngle(Math.PI / 2);
arc(); // returns "M0,-100A100,100,0,0,1,100,0L0,0Z"
This is for SVG Path:
- M = move to
- L = line to
- A = arc to
Tree
Pack
Stack