a journey of pain, joy, frustration and enlightenment
Chris Price / @100pxls
var selection = d3.select('#id');
selection.select('div')
.selectAll('.class-name');
selection.text('inner text')
.attr('class', 'class-name')
.style({
height: 'auto'
})
.each(function(data) {
var element = this;
// ...
})
.append('span');
selection.on('click.foo',
function(data) {
var element = this;
// ...
});
d3.json('/some/url',
function(error, data) {
// ...
});
selection.style('background', 'blue')
.transition()
.duration(100)
.ease('linear')
.style('background', 'red');
var scale = d3.scale.linear()
.domain([-10, 10])
.range([0, 500]);
scale(-5); // 125
scale.invert(125); // -5
scale.ticks(3); // [-10, 0, 10]
var layout = d3.layout.pie()
.padAngle(Math.PI/10);
layout([3,1,2]);
// [ {
// "data":3,
// "value":3,
// "padAngle":0.314...,
// "startAngle":0,
// "endAngle":2.984...
// }, ... ]
var polygon = d3.geom.polygon(
[[0,0],[0,1],[1,1],[1,0]]);
polygon.area(); // 1
polygon.centroid(); // [0.5,0.5]
var mercator = d3.geo.projection(
function(λ, φ) {
return [
λ,
Math.log(Math.tan(π / 4 + φ / 2))
];
});
var scale = d3.linear.scale()
.domain([-10, 10])
.range([0, 100]);
var axis = d3.svg.axis()
.scale(scale);
d3.select('svg')
.call(axis);
var line = d3.svg.line();
line([[0,0],[50,50]]);
// "M0,0L50,50"
d3.select('svg')
.append('path')
.datum([[0,0],[50,50]])
.attr('d', line);
// <path d="M0,0L50,50"></path>
var format = d3.time.format("%a, %e %B %Y");
format(new Date(2015, 4, 7));
// "Thu, 7 May 2015"
var d = format.parse("Fri, 8 May 2015");
d.toString();
// "Fri May 08 2015 00:00:00 GMT+0000 (GMT)"
d3.time.year.ceil(d, 1);
// "Fri Jan 01 2016 00:00:00 GMT+0000 (GMT)"
<h1>USS Missouri Crew List</h1>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
var data = [
{
name: 'Adams', rank: 'Captain'
},
{
name: 'Krill', rank: 'Commander'
},
{
name: 'Ryback', rank: 'Chief Petty Officer'
}
];
// ...
function render() {
requestAnimationFrame(render);
}
requestAnimationFrame(render);
// ...
function render() {
var container = d3.select('ul');
container.selectAll('li');
requestAnimationFrame(render);
}
// ...
// ...
var container = d3.select('ul');
container.selectAll('li')
.data(data);
requestAnimationFrame(render);
// ...
// ...
var updateSelection = container.selectAll('li')
.data(data);
updateSelection.text(function(d) {
return d.name + ' (' + d.rank + ')';
});
// ...
<h1>USS Missouri Crew List</h1>
<ul>
<li>Adams (Captain)</li>
<li>Krill (Commander)</li>
<li>Ryback (Chief Petty Officer)</li>
</ul>
var data = [
{
name: 'Adams', rank: 'Captain'
},
{
name: 'Krill', rank: 'Commander'
},
{
name: 'Ryback', rank: 'Chief Petty Officer'
},
{
name: 'Strannix', rank: 'Civilian'
}
];
<h1>USS Missouri Crew List</h1>
<ul>
<li>Adams (Captain)</li>
<li>Krill (Commander)</li>
<li>Ryback (Chief Petty Officer)</li>
</ul>
// ...
var updateSelection = container.selectAll('li')
.data(data);
updateSelection.enter()
.append('li');
updateSelection.text(function(d) {
return d.name + ' (' + d.rank + ')';
});
// ...
<h1>USS Missouri Crew List</h1>
<ul>
<li>Adams (Captain)</li>
<li>Krill (Commander)</li>
<li>Ryback (Chief Petty Officer)</li>
<li>Strannix (Civilian)</li>
</ul>
var data = [
{
name: 'Krill', rank: 'Commander'
},
{
name: 'Ryback', rank: 'Chief Petty Officer'
},
{
name: 'Strannix', rank: 'Civilian'
}
];
<h1>USS Missouri Crew List</h1>
<ul>
<li>Krill (Commander)</li>
<li>Ryback (Chief Petty Officer)</li>
<li>Strannix (Civilian)</li>
<li>Strannix (Civilian)</li>
</ul>
// ...
updateSelection.enter()
.append('li');
updateSelection.text(function(d) {
return d.name + ' (' + d.rank + ')';
});
updateSelection.exit()
.remove();
// ...
<h1>USS Missouri Crew List</h1>
<ul>
<li>Krill (Commander)</li>
<li>Ryback (Chief Petty Officer)</li>
<li>Strannix (Civilian)</li>
</ul>
<h1>USS Missouri Crew List</h1>
<ul>
<li>Ryback (Chief Petty Officer)</li>
</ul>
var data = [
{
name: 'Ryback', rank: 'Chief Petty Officer'
}
];
function caseyRyback(selection) {
selection.text('Another cold day in Hell.');
}
function dramaticEffect(selection) {
selection.style('fontWeight', 'bold');
}
var selection = d3.select('span');
caseyRyback(selection);
dramaticEffect(selection);
function caseyRyback(selection) {
selection.text('Another cold day in Hell.');
}
function dramaticEffect(selection) {
selection.style('fontWeight', 'bold');
}
var selection = d3.select('span')
.call(caseyRyback)
.call(dramaticEffect);
function caseyRybackFactory() {
function caseyRyback(selection) {
selection.text('Another cold day in Hell.');
}
return caseyRyback;
}
var component = caseyRybackFactory();
d3.select('span')
.call(component);
function caseyRybackFactory() {
var quote = 'Another cold day in Hell.';
function caseyRyback(selection) {
selection.text(quote);
}
caseyRyback.quote = function(value) {
if (!arguments.length) { return quote; }
quote = value;
return caseyRyback;
};
return caseyRyback;
}
var component = caseyRybackFactory()
.quote('Keep the faith, Strannix.');
d3.select('span')
.call(component);
Chris Price / @100pxls
d3fc.io
slides.com/chrisprice/creating-d3-components-1-4