chart = {
const root = d3.create("div")
.attr("class", "root");
const svg = root.append("svg")
.attr("viewBox", [0, 0, width, height]);
const tooltip = root
.append("div")
.attr("class", "cooltip");
svg.append("g")
.call(yAxis);
svg.append("g")
.attr("fill", "#D0B3BC")
.attr("opacity", 1)
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", (d, i) => x(i+2))
.attr("y", d => y(d.new_deaths))
.attr("height", d => y(0) - y(d.new_deaths))
.attr("width", x.bandwidth())
.attr("data-date", d => d.date)
.on('mouseover', function (d) {
this.classList.add('hovered')
d3.select(this)
.attr("fill", "#8B5566")
.raise()
})
.on('mouseout', function() {
this.classList.remove('hovered');
d3.select(this)
.attr("fill", "#D0B3BC")
.lower();
tooltip.style('display', 'none');
});
svg.on('mousemove', function() {
const rootBounds = root.node().getBoundingClientRect();
const mouseX = d3.event.pageX - rootBounds.left;
const displayWidth = rootBounds.right - rootBounds.left
const transformRatio = width / displayWidth;
const heightTransformRatio = height / (rootBounds.bottom - rootBounds.top);
console.log(heightTransformRatio);
let eachBand = x.step();
let i = Math.floor((mouseX * transformRatio - margin.left)/eachBand-2);
let a = avg[i];
let d = data[i];
tooltip
.style('top', (y(a.new_deaths)/heightTransformRatio-100) + 'px')
.style('left', (mouseX + 90 <= displayWidth ? (mouseX + 10) : (mouseX - 90)) + 'px')
.style('display', 'block')
let node = tooltip.node();
node.innerHTML = "";
node.appendChild(getTooltipContents(d));
});
svg.append("path")
.datum(avg)
.attr("fill", "none")
.attr("stroke", color)
.attr("stroke-width", 3)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line);
svg.append("g")
.call(xAxis)
.select('.domain').attr("opacity", 0)
.select('.tick:first-of-type text').remove()
.selectAll('.tick text')
.attr('font-size', 15)
.attr('font-family', 'Helvetica')
.attr('fill', 'gray');
window.addEventListener("resize", () => resized(width, root, svg));
setTimeout(() => resized(width, root, svg), 12);
return root.node();
}