Skip to content

Instantly share code, notes, and snippets.

@mtaptich
Last active December 10, 2015 21:07
Show Gist options
  • Select an option

  • Save mtaptich/f1373d7ee8990353a885 to your computer and use it in GitHub Desktop.

Select an option

Save mtaptich/f1373d7ee8990353a885 to your computer and use it in GitHub Desktop.
Append/Push

The fifth of a series of data visualizations on basic coding concepts. When you append or push data to a list, all you do is add it to the last position!

<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
width: 1024px;
margin-top: 0;
margin: auto;
font-family: "Lato", "PT Serif", serif;
color: #222222;
padding: 0;
font-weight: 300;
line-height: 33px;
-webkit-font-smoothing: antialiased;
}
.tile {
stroke: #000;
stroke-width: 2px;
fill: #ecf0f1;
-webkit-font-smoothing: antialiased;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 200,
translate_speed = 1500;
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
function makelist(){
var g = svg.append('g').attr('transform','translate(0,0)');
var data = [[Math.floor(Math.random()*10)], [Math.floor(Math.random()*10)], [Math.floor(Math.random()*10)]]
svg.append('text')
.attr('x', 80)
.attr('y', 130)
.style('text-anchor', 'middle')
.style('font-size', '90')
.text("A")
svg.append('text')
.attr('x', 160)
.attr('y', 130)
.attr('class', 'temp')
.style('text-anchor', 'middle')
.style('font-size', '90')
.text("=").transition()
.duration(translate_speed)
.transition()
.duration(translate_speed/2)
.style('opacity', '0')
.remove()
svg.append('text')
.attr('x', 220)
.attr('y', 130)
.attr('class', 'temp')
.style('text-anchor', 'middle')
.style('font-size', '120')
.text("[").transition()
.duration(translate_speed)
.transition()
.duration(translate_speed/3)
.style('opacity', '0')
.remove()
svg.append('text')
.attr('x', 680)
.attr('y', 130)
.attr('class', 'temp')
.style('text-anchor', 'middle')
.style('font-size', '120')
.text("]").transition()
.duration(translate_speed)
.transition()
.duration(translate_speed/3)
.style('opacity', '0')
.remove()
g.selectAll('.newarray')
.data(data).enter()
.append('rect')
.attr('x', function(d,i){ return 150*i + 250 })
.attr('y', 50)
.attr('width', 100)
.attr('height', 100)
.attr('class', 'tile')
g.selectAll('rect')
.transition()
.duration(translate_speed)
.transition()
.duration(translate_speed)
.attr('x', 30)
.transition()
.duration(translate_speed)
.style('opacity', '0')
g.transition().duration(translate_speed*4).each("end", function(){
new_append(g)
});
}
function new_append(g){
svg.append('text')
.attr('x', 650)
.attr('y', 110)
.attr('class', 'temp')
.style('text-anchor', 'middle')
.style('font-size', '30')
.text("{ Append/Push }").transition()
.duration(translate_speed*2)
.transition()
.duration(translate_speed/2)
.style('opacity', '0')
.remove()
svg.append('text')
.attr('x', 160)
.attr('y', 130)
.attr('class', 'temp')
.style('text-anchor', 'middle')
.style('font-size', '90')
.text("=").transition()
.duration(translate_speed*2)
.transition()
.duration(translate_speed/2)
.style('opacity', '0')
.remove()
svg.append('text')
.attr('x', 220)
.attr('y', 130)
.attr('class', 'temp')
.style('text-anchor', 'middle')
.style('font-size', '120')
.text("[").transition()
.duration(translate_speed*2)
.transition()
.duration(translate_speed/2)
.style('opacity', '0')
.remove()
g.append('text')
.attr('x', 280)
.attr('y', 130)
.style('text-anchor', 'middle')
.style('font-size', '90')
.text("A")
g.append('rect')
.attr('x', 350)
.attr('y', 50)
.attr('width', 100)
.attr('height', 100)
.attr('class', 'tile')
.style('fill', '#fdb03f')
g.selectAll('rect').transition()
.duration(translate_speed*2)
.transition()
.duration(translate_speed)
.attr('x', 30)
.style('opacity', '0');
svg.append('text')
.attr('x', 490)
.attr('y', 130)
.style('text-anchor', 'middle')
.style('font-size', '120')
.text("]")
.attr('class', 'temp')
.transition()
.duration(translate_speed*2)
.transition()
.duration(translate_speed/2)
.style('opacity', '0')
.remove()
g.select('text').transition()
.duration(translate_speed*2)
.transition()
.duration(translate_speed)
.attr('x', 80)
.each("end", function(){
expand(g)
});
}
function expand(g){
d3.selectAll('.temp').remove()
g.append('text')
.attr('x', 160)
.attr('y', 130)
.attr('class', 'temp')
.style('text-anchor', 'middle')
.style('font-size', '90')
.style('opacity', '0')
.text("=").transition().delay(1000).duration(translate_speed).style('opacity', '1')
g.append('text')
.attr('x', 220)
.attr('y', 130)
.attr('class', 'temp')
.style('text-anchor', 'middle')
.style('font-size', '120')
.style('opacity', '0')
.text("[").transition().delay(1000).duration(translate_speed).style('opacity', '1')
g.append('text')
.attr('x', 830)
.attr('y', 130)
.attr('class', 'temp')
.style('text-anchor', 'middle')
.style('font-size', '120')
.style('opacity', '0')
.text("]").transition().delay(1000).duration(translate_speed).style('opacity', '1')
g.selectAll('rect').transition().delay(500).duration(translate_speed).style('opacity', '1')
.attr('x', function(d,i){ return 150*i + 250 })
setTimeout(function(){
d3.selectAll('g').remove();
d3.selectAll('svg text').remove();
makelist()}, translate_speed*4);
}
makelist()
d3.select(self.frameElement).style("height", (height) + "px");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment