{
var w = 600;
var h = 250;
var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15,
13, 11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
var xScale = d3.scaleBand()
.domain(d3.range(dataset.length))
.rangeRound([0, w])
.paddingInner(0.05);
var yScale = d3.scaleLinear()
.domain([0, d3.max(dataset)])
.range([0, h]);
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
var previous_rects = svg.selectAll("placeholder_for_rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", (d, i) => xScale(i))
.attr("y", d => h - yScale(d))
.attr("width", xScale.bandwidth())
.attr("height", d => yScale(d))
.style("fill", d => "rgb(0, 0, "+ Math.floor(d * 10) +")");
d3.select("p")
.on("click", function(){
var maxValue = 25
var newnumber = Math.floor(Math.random() * maxValue);
dataset.push(newnumber)
xScale.domain(d3.range(dataset.length))
yScale.domain([0, d3.max(dataset)]);
//var bars = svg.selectAll("rect")
//.data(dataset)
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", w)
.attr("y", function(d) {
return h - yScale(d);
})
.attr("width", xScale.bandwidth())
.attr("height", function(d) {
return yScale(d);
})
.attr("fill", "green")
.merge(previous_rects)
.transition()
.duration(500)
.attr("x", function(d, i){
return xScale(i);
})
.attr("y", function(d){
return h - yScale(d);
})
.attr("width", xScale.bandwidth())
.attr("height", function(d){
return yScale(d);
})
.style("fill", function(d){
return "rgb(0, 0, "+ Math.round(d * 10) +")"
});
})
}