Published
Edited
Jun 24, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height])
.style("padding-top", `${margin.top}px`);
// 2019 paths
svg.append("g")
.attr("class", "temp")
.selectAll("path")
.data(data)
.join("path")
.attr("opacity", 1)
.attr("data-state", d => `${d.state}`)
.attr("fill", (d,i) => "#7f8c8d")
.attr("stroke", "#fff")
.attr("stroke-width", 2)
.attr("d", (d,i) => arc(d,i,1,"sept2019"))
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
// 2020 paths
svg.append("g")
.selectAll("path")
.data(data)
.join("path")
.attr("opacity", 1)
.attr("data-state", d => `${d.state}`)
.attr("fill", "#d35400")
.attr("stroke", "#fff")
.attr("stroke-width", 2)
.attr("d", (d,i) => arc(d,i,-1,"sept2020"))
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
// legend on the side
const states = svg.append("g")
.attr("transform", `translate(-${width/2 - margin.left} -${height/2})`)
.attr("font-size", 10)
.selectAll("text")
.data(data)
.join("text")
.attr("class", "state")
.attr("data-state", d => `${d.state}`)
.attr("opacity", 1)
.attr("y", (d,i) => legendScale(data.length - i))
.text(d => d.state + " +" + d.change + " pts")
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
// years header
const yearHeader = svg.append("g")
.attr("font-size", 25)
.attr("font-weight", "bold")
.attr("transform", `translate(-${width/2} -${height/2 - margin.top})`)
yearHeader
.append("text")
.attr("fill", "#7f8c8d")
.attr("x", width/2 - 60)
.text("2019")
yearHeader
.append("text")
.attr("fill", "#d35400")
.attr("x", width/2 + 5)
.text("2020")
svg.append("g")
.call(yAxis)
d3.selectAll("label").on("click", function() {
activateBtn(this)
});
// I couldn't make the viz responsive for width below 690
if (window.innerWidth > 740) { return svg.node() }
else { return md`**Unfortunately, I couldn't make the viz responsive...**` }
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more