Skip to content

Instantly share code, notes, and snippets.

@deathbob
Created May 25, 2012 03:37
Show Gist options
  • Save deathbob/2785614 to your computer and use it in GitHub Desktop.
Save deathbob/2785614 to your computer and use it in GitHub Desktop.
Overly complicated d3
<script>
var trans = [["2-21","129.68"],["2-22","92.79"],["2-23","196.22"],["2-24","187.71"],["2-25","16.47"],["2-26","18.5"],["2-27","110.48"],["2-28","122.61"],["2-29","114.11"],["3-01","121.74"],["3-02","267.23"],["3-03","167.61"],["3-04","10.26"],["3-05","46.32"],["3-06","40.76"],["3-07","309.39"],["3-08","71.91"],["3-09","246.11"],["3-10","148.21"],["3-11","10.01"],["3-12","118.58"],["3-13","109.69"],["3-14","125.81"],["3-15","116.23"],["3-16","201.83"],["3-17","64.68"],["3-19","44.28"],["3-20","118.25"],["3-21","365.65"],["3-22","216.62"]];
var vans = [["3-23","105.78"],["3-24","46.01"],["3-26","144.34"],["3-27","193.03"],["3-28","112.92"],["3-29","147.45"],["3-30","103.91"],["3-31","70.93"],["4-02","153.63"],["4-03","116.56"],["4-04","124.04"],["4-05","135.46"],["4-06","200.77"],["4-07","141.15"],["4-09","145.97"],["4-10","218.34"],["4-11","169.59"],["4-12","235.91"],["4-13","200.32"],["4-14","207.49"],["4-16","230.42"],["4-17","254.29"],["4-18","186.4"],["4-19","164.49"],["4-20","235.65"],["4-21","110.9"],["4-22","25.81"],["4-23","236.41"],["4-24","245.86"],["4-25","206.35"],["4-26","196.54"],["4-27","439.04"],["4-28","147.73"],["4-29","18.78"],["4-30","226.39"],["5-01","144.6"],["5-02","221.85"],["5-03","275.24"],["5-04","299.16"],["5-05","127.31"],["5-06","38.74"],["5-07","340.82"],["5-08","212.36"],["5-09","162.26"],["5-10","249.45"],["5-11","236.92"],["5-12","200.32"],["5-13","125.44"],["5-14","144.52"],["5-15","294.36"],["5-16","197.5"],["5-17","214.0"],["5-18","296.85"],["5-19","134.28"],["5-20","49.47"]];
$(function(){
setInterval(function() {
trans.shift();
var cow = vans.shift();
trans.push(cow);
vans.push(cow)
redraw();
}, 1500);
var w = 40,
h = 600;
var x = d3.scale.linear()
.domain([0, 1])
.range([0, w]);
var y = d3.scale.linear()
.domain([0, 400])
.rangeRound([0, h]);
var chart = d3.select("body").append("svg")
.attr("class", "chart")
.attr("width", w * trans.length - 1)
.attr("height", h)
.style("padding-bottom", '50px');
// holder for rects
body = chart.append("g")
.attr("transform", "translate(0,0)");
// holder for the second label
booty = chart.append("g")
.attr("id", 'booty')
body.selectAll("rect")
.data(trans)
.enter().append("rect")
.attr("x", function(d, i) { return x(i) - .5; })
.attr("y", function(d) { return h - y(d[1]) - .5; })
.attr("width", w)
.attr("height", function(d) { return y(d[1]); });
body.append("line")
.attr("x1", 0)
.attr("x2", w * trans.length)
.attr("y1", h - .5)
.attr("y2", h - .5)
.style("stroke", "#000");
function redraw() {
// rectangles
var rect = body.selectAll("rect")
.data(trans, function(d) { return d[0]; });
rect.enter().insert("rect", "line")
.attr("x", function(d, i) { return x(i + 1) - .5; })
.attr("y", function(d) { return h - y(d[1]) - .5; })
.attr("width", w)
.attr("height", function(d) { return y(d[1]); })
.transition()
.duration(1000)
.attr("x", function(d, i) { return x(i) - .5; });
rect.transition()
.duration(1000)
.attr("x", function(d, i) { return x(i) - .5; });
rect.exit().transition()
.duration(1000)
.attr("x", function(d, i) { return x(i - 1) - .5; })
.remove();
var text = body.selectAll("text")
.data(trans, function(d) { return d[0]; })
text.enter().insert('text')
.attr('x', function(d, i) { return x(i + 1) - .5; })
.attr('y', function(d){return h - y(d[1]) - .5; })
.attr('dx', 8)
.attr("dy", "1.2em")
.attr("text-anchor", 'start')
.text(function(d, i){
return Math.round(d[1]);
});
text.transition()
.duration(1000)
.attr("x", function(d, i){return x(i) - .5});
text.exit().transition()
.duration(1000)
.attr("x", function(d, i) {return x(i - 1) - .5})
.remove();
bang = booty.selectAll("text")
.data(trans, function(d) {return d[1];})
bang.enter().append("text")
.attr("text-anchor", 'start')
.attr('x', function(d, i) { return x(i + 1) - .5; })
.attr('y', 605)
.attr("dx", 4)
.attr('dy', '.71em')
.text(function(d){
return d[0];
});
bang.transition()
.duration(1000)
.attr("x", function(d, i){return x(i) - .5});
bang.exit().transition()
.duration(1000)
.attr("x", function(d, i) {return y(i - 1) - .5})
.remove();
} // closes redraw
}); // document ready
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment