animateChart = {
const rollupEVdata = d3.rollup(d3.filter(EVdata, d => d.State == "WA"), v => v.length, d => d.County)
const aggData = d3.filter(Array.from(rollupEVdata, ([county, count]) => ({ county, count })), d => d.count > 600)
const height = 500
const margins = ({
top: 30,
right: 10,
bottom: 20,
left: 45
})
const x = d3.scaleBand()
.domain(d3.groupSort(aggData, ([d]) => -d.count, (d) => d.county))
.range([margins.left, width - margins.right])
.padding(0.1)
const y = d3.scaleLinear()
.domain([0, d3.max(aggData, d=> d.count)])
.range([height - margins.bottom, margins.top])
.nice()
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;");
const gy = svg.append("g")
.attr("transform", `translate(${margins.left}, 0)`)
.call(d3.axisLeft(y).tickSizeOuter(0))
.call(g => g.select(".domain").remove()) // remove the axis but preserve the ticks
.call(g => g.append("text")
.attr("x", -margins.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text("↑ Count"))
const barColor = "pink"
const barHighlightColor = "lightblue"
const bars = svg.append("g")
.selectAll()
.data(aggData)
.join("rect")
.attr("x", d => x(d.county))
.attr("y", d => y(d.count))
.attr("height", d => y(0) - y(d.count)) // must be y(0) - y(d.country)
.attr("width", x.bandwidth())
.attr("fill", barColor)
.attr("class", "bars")
const label = svg.append("text")
bars
.on("mouseover", function(e,d){
d3.selectAll(".bars")
.transition()
.duration(200)
.style("opacity", 0.5)
d3.select(this)
.transition()
.duration(200)
.attr("fill", barHighlightColor)
.attr("opacity", 1)
const labelText = `${d.count}`
label
.attr("display", null)
.attr("font-size", "13px")
.style("opacity", 1)
.attr("transform", `translate(${d3.select(this).attr("x")},${d3.select(this).attr("y")})`)
.attr("dx", +33)
.attr("dy", -5)
.text(d => labelText)
.attr("text-anchor", "end")
})
.on("mousemove", function(e, d){
d3.selectAll(".bars")
.transition()
.duration(10)
.attr("fill", barColor)
.style("opacity", 1)
d3.select(this)
.transition()
.duration(10)
.attr("fill", barHighlightColor)
.attr("opacity", 1)
})
.on("mouseleave", function(e, d){
d3.selectAll(".bars")
.transition()
.duration(200)
.attr("fill", barColor)
.style("opacity", 1)
label
.attr("display", "none")
})
const xAxis = d3.axisBottom(x).tickSizeOuter(0);
// Add x-axis and label
const gx = svg.append("g")
.attr("transform", `translate(0,${height - margins.bottom})`)
.call(xAxis)
// Add a title
const title = svg.append("g")
.append("text")
.attr("x", width / 2)
.attr("y", (margins.top / 2))
.attr("text-anchor", "middle")
.attr("front-weight", "bold")
.attr("font-family", "Helvetica Neue, Arial")
.attr("font-size", "20px")
.text("Electric vehicle ownership distribution, Washington State")
// update chart
function updateChart(order) {
x.domain(aggData.sort(order).map(d => d.county));
const t = svg.transition()
.duration(750);
bars.data(aggData, d => d.county)
.order()
.transition(t)
.delay((d, i) => i * 20)
.attr("x", d => x(d.county));
gx.transition(t)
.call(xAxis)
.selectAll(".tick")
.delay((d, i) => i * 20);
}
return Object.assign(svg.node(), {updateChart});
}