Skip to content

Instantly share code, notes, and snippets.

Created April 5, 2014 05:42
Show Gist options
  • Save anonymous/9987916 to your computer and use it in GitHub Desktop.
Save anonymous/9987916 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.chart div {
margin: 3px;
padding: 1px;
}
.chart > div {
background-color: red;
}
.chart > div > div {
background-color: steelblue;
color: white;
}
</style>
<div class="chart"></div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
function draw(data) {
var divs = d3
.select(".chart")
.selectAll(".parent")
.data(data);
var enter = divs
.enter()
.append('div')
.attr('class', 'parent');
enter
.append("div")
.attr('class', 'first');
enter
.append("div")
.attr('class', 'second');
// Feel free to change the method if you want to test
var method = 3;
switch(method) {
case 1:
// This works, obviously:
divs
.select('.first')
.text(function(d) { return d.i; });
divs
.select('.second')
.text(function(d) { return d.i; });
break;
case 2:
// This works also, as I create new propagated data:
divs
.selectAll('div')
.data(function(d) {
var padded = [];
for(var i = 0; i < this.length; ++i) {
padded.unshift(d);
}
return padded;
})
.text(function(d) { return d.i; });
break;
case 3:
// I could do also this:
divs
.selectAll('div')
.call(padData)
.text(function(d) { return d.i; });
break;
default:
// I'd like to do something similar, built-in into d3.js, which selects all the divs and sets their value without manual the propagation and padding:
// divs.selectAll('div').text(function(d) { return d.i; });
break;
}
};
function padData(divs) {
return divs.data(function(d) {
var padded = [];
for(var i = 0; i < this.length; ++i) {
padded.push(d);
}
return padded;
});
}
var d = [];
for (var i = 0; i < 5; ++i) {
d.unshift({i:i});
}
draw(d);
window.setInterval(function() {
var r = Math.random()*100;
d.unshift({i:r});
d.pop();
draw(d);
}, 2000);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment