Published
Edited
Oct 4, 2020
Insert cell
md`# Day 42, 2020-10-04`
Insert cell
md`
Resources:
- [1](https://www.codementor.io/@milesbryony/d3-js-join-14gqdz3hfj)
- [2](https://observablehq.com/@d3/selection-join)
- [3](https://github.com/d3/d3-selection/blob/master/README.md#selection_join)
`
Insert cell
d3 = require("d3@6")
Insert cell
w = 600
Insert cell
h = 250
Insert cell
dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13, 11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ]
Insert cell
xScale = d3.scaleBand()
.domain(d3.range(dataset.length))
.range([0, w])
.paddingInner(0.05);
Insert cell
yScale = d3.scaleLinear()
.domain([0, d3.max(dataset)])
.range([0, h]);
Insert cell
svg01 = d3.select("#demo01")
.append("svg")
.attr("width", w)
.attr("height", h);
Insert cell
svg01.selectAll("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))
.attr("fill", d => "rgb("+ Math.round(d * 5) +", "+ Math.round(d * 2) +", "+ Math.floor(d * 9) +")");
Insert cell
d3.select("button01")
.on("click", function() {
updateData();
updateScales();
updateBars_01();
});
Insert cell
function updateData() {
const maxValue = 25;
let newNumber = Math.floor(Math.random() * maxValue);
dataset.push(newNumber)
}
Insert cell
function updateScales() {
xScale.domain(d3.range(dataset.length));
yScale.domain([0, d3.max(dataset)]);
}
Insert cell
function updateBars_01() {
svg01.selectAll("rect")
.data(dataset)
.transition()
.duration(1000)
.attr("x", (d, i) => xScale(i))
.attr("y", d => h - yScale(d))
.attr("width", xScale.bandwidth())
.attr("height", d => yScale(d))
.attr("fill", d => "rgb("+ Math.round(d * 5) +", "+ Math.round(d * 2) +", "+ Math.floor(d * 9) +")");
}
Insert cell
button01 = html`<button type "butoon">Click here to add new data</button>`
Insert cell
html `<section id="demo01"></section>`
Insert cell
md`
We are just adding a new number to the dataset every time you click. Without using \`merge\`, the existing rects will be pushed towards the left. The right of the SVG will be just blank.
`
Insert cell
svg02 = d3.select(body)
.append("svg")
.attr("width", w)
.attr("height", h);
Insert cell
svg02.selectAll("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))
.attr("fill", d => "rgb("+ Math.round(d * 5) +", "+ Math.round(d * 2) +", "+ Math.floor(d * 9) +")");
Insert cell
d3.select("button02")
.on("click", function() {
updateData();
updateScales();
updateBars_02();
});
Insert cell
function updateBars_02() {
let bars = svg02.selectAll("rect").data(dataset)
bars.enter()
.append("rect")
.attr("x", w)
.attr("y", d => h - yScale(d))
.merge(bars)
.transition()
.duration(1000)
.attr("x", (d, i) => xScale(i))
.attr("y", d => h - yScale(d))
.attr("width", xScale.bandwidth())
.attr("height", d => yScale(d))
.attr("fill", d => "rgb(0, 0, "+ Math.floor(d * 10) +")");
}
Insert cell
button02 = html`<button type "butoon">Click here to add new data</button>`
Insert cell
body = html`<body></body>`
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more