smallDetails = {
const timeSpent = [
{ year: 2009, Estimate: 1.11 },
{ year: 2010, Estimate: 1.13 },
{ year: 2011, Estimate: 1.12 },
{ year: 2012, Estimate: 1.12 },
{ year: 2013, Estimate: 1.11 },
{ year: 2014, Estimate: 1.07 },
{ year: 2015, Estimate: 1.07 },
{ year: 2016, Estimate: 1.06 },
{ year: 2017, Estimate: 1.06 },
{ year: 2018, Estimate: 1.07 },
{ year: 2019, Estimate: 1.06 }
];
let height = 400;
let margin = 75;
let svg = d3.create("svg").attr("width", width).attr("height", height);
svg
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", height)
.attr("fill", "#23395D");
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!");
const barWidth = (height - margin * 2) / timeSpent.length;
const popExtents = d3.extent(timeSpent, (d) => d.Estimate);
const popScale = d3
.scaleLinear()
.domain(popExtents)
.range([100, width - margin * 2]);
svg
.selectAll(".yearBars")
.data(timeSpent)
.enter()
.append("rect")
.attr("x", margin)
.attr("y", (d, i) => i * barWidth + margin)
.attr("width", (d) => popScale(d.Estimate))
.attr("height", barWidth)
.attr("fill", "#9370D8")
.on("mouseover", (e) => {
d3.select(e.target).attr("stroke", "white").attr("stroke-width", "2");
})
.on("mouseout", (e) => {
d3.select(e.target).attr("stroke", "none").attr("stroke-width", "0");
})
.on("click", (e, d, i) => {
d3.select("#infoText").text(
"In the year " +
d.year +
", people spent " +
d.Estimate.toLocaleString() +
" hour" +
" in eating and drinking."
);
});
svg
.selectAll(".yearLabels")
.data(timeSpent)
.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", (e, d, i) => {
d3.select(e.target).attr("stroke", "white").attr("stroke-width", "1");
})
.on("mouseout", (e, d, i) => {
d3.select(this).attr("stroke", "none").attr("stroke-width", "0");
});
return svg.node();
}