Published
Edited
Sep 22, 2020
Insert cell
md`# Day Twenty Six, 2020-09-19`
Insert cell
md`
In Chapter 9, \`25_adding_values.html\`, assign a name of "previous_rects" to the rect generating codes before \`d3.select("p")\`. Then, modify the \`d3.select("p").on("click", function(){})\` this way, and explain what happens:

d3.select("p")
.on("click", function() {

var maxValue = 25;
var newNumber = Math.round(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 / dataset.length)
.attr("y", function(d) {
return h - yScale(d);
})
.attr("width", xScale.bandwidth())
.attr("height", function(d) {
return yScale(d);
})
.attr("fill", "red")
.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) +")"
});
});

Try to understand how the above codes worked.
`
Insert cell
md`
Modify the above codes this way, and explain what happens:

svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.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);
})
.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("+ Math.round(d * 10) +", "+ Math.round(d * 10) +", "+ Math.round(d * 10) +")"
});
`
Insert cell
md`
What I really don't get is why the newly generated bars sometimes are moving towards the left in comparison to previously generated bars? This literally drove me crazy!

I know that the \`.attr()\` right after \`.append("rect")\` are assigning attributions to the new generated bar (just one), rather than all the bars. And after \`.merge()\`, the \`attr()\` are assigning attributions to the bars in \`circles\` and the newly generated bar.

However, isn't the \`xScale(i)\` before \`.merge()\` equal to the \`xScale(i)\` after \`merge()\`? If yes, why are the positions of newly generated bars moving with the transition? If not, why aren't they equal??
`
Insert cell
md`
Go to [Day 27](https://observablehq.com/@hongtaoh/day-twenty-seven-2020-09-20).
`
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more