chart = {
const width = 640;
const height = 400;
const marginTop = 20;
const marginRight = 0;
const marginBottom = 30;
const marginLeft = 40;
const default_color = "steelblue";
const highlight_color = "orange";
const x = d3.scaleBand()
.domain(data.map(d => d.letter))
.range([marginLeft, width - marginRight])
.padding(0.1);
const xAxis = d3.axisBottom(x).tickSizeOuter(0);
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.frequency)]).nice()
.range([height - marginBottom, marginTop]);
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("style", `max-width: ${width}px; height: auto; font: 10px sans-serif; overflow: visible;`);
const tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0)
.style("position", "absolute")
.style("background-color", "rgba(0, 0, 0, 0.7)")
.style("color", "white")
.style("border-radius", "5px")
.style("padding", "10px")
.style("font-size", "12px")
.style("pointer-events", "none");
// Create a bar for each letter.
// TODO: 1. Add tooltip functionalities
// 2. Add an hover and on-click highlight color change to bars
const bar = svg.append("g")
.attr("fill", default_color)
.selectAll("rect")
.data(data)
.join("rect")
.style("mix-blend-mode", "multiply") // Darker color when bars overlap during the transition.
.attr("x", d => x(d.letter))
.attr("y", d => y(d.frequency))
.attr("height", d => y(0) - y(d.frequency))
.attr("width", x.bandwidth())
.on("mouseover", function(event, d) {
const bar = d3.select(this);
if (!bar.classed("clicked")) {
bar.attr("fill", highlight_color);
}
tooltip.transition()
.duration(200)
.style("opacity", 0.9);
tooltip.html("Letter: " + d.letter + "<br>Frequency: " + (d.frequency * 100).toFixed(2) + "%")
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 28) + "px");
})
.on("mousemove", function(event, d) {
tooltip.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 28) + "px");
})
.on("mouseout", function() {
tooltip.transition()
.duration(500)
.style("opacity", 0);
const bar = d3.select(this);
if (!bar.classed("clicked")) {
bar.attr("fill", default_color);
}
})
.on("click", function() {
const bar = d3.select(this);
bar.classed("clicked", !bar.classed("clicked"));
// Set fill color based on clicked state
if (bar.classed("clicked")) {
bar.attr("fill", highlight_color);
} else {
bar.attr("fill", default_color);
}
});
// Create the axes.
const gx = svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(xAxis);
const gy = svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y).tickFormat((y) => (y * 100).toFixed()))
.call(g => g.select(".domain").remove());
function update(order) {
// Get selected ordered data mapping
x.domain(data.sort(order).map(d => d.letter));
// TODO: Add animation to bars before setting new x positions of bars
bar.data(data, d => d.letter)
.order()
.transition()
.duration(750)
.delay((d, i) => i * 20)
.attr("x", d => x(d.letter));
// TODO: Add a similar animation to x ticks before changing ticks
gx.transition()
.duration(750)
.call(xAxis)
.selectAll(".tick")
.delay((d, i) => i * 20);
}
// Return the chart, with an update function that takes as input a domain
// comparator and transitions the x axis and bar positions accordingly.
return Object.assign(svg.node(), {update});
}