smallDetails = {
let usPopulation = [
{ year: 1960, usPop: 180671000 },
{ year: 1970, usPop: 205052000 },
{ year: 1980, usPop: 227225000 },
{ year: 1990, usPop: 249623000 },
{ year: 2000, usPop: 282162411 },
{ year: 2010, usPop: 309321666 },
{ year: 2020, usPop: 326687501 }
];
let width = 800;
let height = 400;
let margin = 75;
let svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
.attr("height", height);
let bg = svg
.append('rect')
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", height)
.attr('fill', 'blue');
let barWidth = (height - margin * 2) / usPopulation.length;
let popExtents = d3.extent(usPopulation, d => d.usPop);
let barLengthExtents = [100, width - margin * 2];
let popScale = d3
.scaleLinear()
.domain(popExtents)
.range(barLengthExtents);
let bars = svg
.selectAll('.yearBars')
.data(usPopulation)
.enter()
.append('rect')
.attr('x', margin)
.attr('y', (d, i) => i * barWidth + margin)
.attr('width', d => popScale(d.usPop))
.attr('height', barWidth)
.attr('fill', 'red')
.on('mouseover', function(d) {
d3.select(this)
.attr('stroke', 'white')
.attr('stroke-width', '2');
})
.on('mouseout', function(d) {
d3.select(this)
.attr('stroke', 'none')
.attr('stroke-width', '0');
})
.on('click', function(d, i) {
d3.select('#infoText').text(
"In the year " +
d.year +
", item " +
i +
" of dataset, the US had " +
d.usPop.toLocaleString() +
" people."
);
});
let labels = svg
.selectAll('.yearLabels')
.data(usPopulation)
.enter()
.append('text')
.attr('x', margin - 5)
.attr('y', (d, i) => i * barWidth + margin + barWidth / 1.5)
.attr('fill', 'white')
.style('text-anchor', 'end')
.text(d => d.year)
.attr('font-family', 'courier')
.on('mouseover', function(d, i) {
d3.select(this)
.attr('stroke', 'white')
.attr('stroke-width', '1');
})
.on('mouseout', function(d) {
d3.select(this)
.attr('stroke', 'none')
.attr('stroke-width', '0');
});
let infoText = svg
.append('text')
.attr('x', width - margin / 2)
.attr('y', margin / 2)
.attr('fill', 'white')
.style('text-anchor', 'end')
.attr('font-size', '12')
.attr('font-family', 'courier')
.attr('id', 'infoText')
.text("Click on a red bar!");
return svg.node();
}