Skip to content

Instantly share code, notes, and snippets.

@tonysherbondy
Created December 1, 2012 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonysherbondy/4185122 to your computer and use it in GitHub Desktop.
Save tonysherbondy/4185122 to your computer and use it in GitHub Desktop.
Update Pattern
{"description":"Update Pattern","endpoint":"","display":"svg","public":true,"require":[],"tab":"edit","display_percent":0.5502362112855866,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"editor_editor":{"coffee":false,"vim":false,"emacs":false,"width":600,"height":300,"hide":false}}
var alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
var width = 960,
height = 500;
var svg = d3.select('svg');
svg.append("g")
.attr("transform", "translate(32," + 50 + ")");
function update(data) {
// DATA JOIN
// Join new data with old elements, if any.
var text = svg.selectAll("text")
.data(data);
// UPDATE
// Update old elements as needed.
text.attr("class", "update");
// ENTER
// Create new elements as needed.
text.enter().append("text")
.attr("class", "enter")
.attr("x", function(d, i) { return i * 32; })
.attr("dy", ".35em");
// ENTER + UPDATE
// Appending to the enter selection expands the update selection to include
// entering elements; so, operations on the update selection after appending to
// the enter selection will apply to both entering and updating nodes.
text.text(function(d) { return d; });
// EXIT
// Remove old elements as needed.
text.exit().remove();
}
// The initial display.
update(alphabet);
// Grab a random sample of letters from the alphabet, in alphabetical order.
//setInterval(function() {
// update(shuffle(alphabet)
// .slice(0, Math.floor(Math.random() * 26))
// .sort());
//}, 1500);
// Shuffles the input array.
function shuffle(array) {
var m = array.length, t, i;
while (m) {
i = Math.floor(Math.random() * m--);
t = array[m], array[m] = array[i], array[i] = t;
}
return array;
}
text {
font: bold 48px monospace;
}
.enter {
fill: green;
}
.update {
fill: #333;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment