chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const tooltip = d3.select("body")
.append("div")
.attr("class", "svg-tooltip")
.style("position", "absolute")
.style("visibility", "hidden");
const bar = svg.append("g")
.selectAll("rect")
.data(data)
.join("rect")
.style("mix-blend-mode", "multiply")
.attr("x", d => x(d.name))
.attr("y", d => y(d.value))
.attr("height", d => y(0) - y(d.value))
.attr("width", x.bandwidth())
.attr("fill", d => color(d.name))
.on("mouseover", d => tooltip.style("visibility", "visible").text(`${d.value} new ${d.name} characters`))
.on("mousemove", d => tooltip.style("top", (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px"))
.on("mouseout", d => tooltip.style("visibility", "hidden").text(""));
const gx = svg.append("g")
.call(xAxis);
const gy = svg.append("g")
.call(yAxis);
return Object.assign(svg.node(), {
update(order) {
x.domain(data.sort(order).map(d => d.name));
const t = svg.transition()
.duration(750);
bar.data(data, d => d.name)
.order()
.transition(t)
.delay((d, i) => i * 20)
.attr("x", d => x(d.name));
gx.transition(t)
.call(xAxis)
.selectAll(".tick")
.delay((d, i) => i * 20);
}
});
}