Published
Edited
Apr 22, 2019
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));
svg.append("g")
.attr("fill", "steelblue")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", x(0))
.attr("y", d => y(d.name))
.attr("width", d => x(d.value) - x(0))
.attr("height", y.bandwidth());
svg.append("g")
.attr("fill", "white")
.attr("text-anchor", "end")
.style("font", "24px sans-serif")
.selectAll("text")
.data(data)
.join("text")
.attr("x", d => x(d.value) - 4)
.attr("y", d => y(d.name) + y.bandwidth() / 2)
.attr("dy", "0.35em")
.text(function(d) {
var formatted_num = String(d3.format("$,.3s")(d.value).replace("G","b"));
return (formatted_num)});
svg.append("g")
.attr("fill", "white")
.attr("text-anchor", "start")
.style("font", "24px sans-serif")
.selectAll("text")
.data(data)
.join("text")
// .attr("x", d => x(d.value) - 4)
.attr("x", 12)
.attr("y", d => y(d.name) + y.bandwidth() / 2)
.attr("dy", "0.35em")
.text(function(d) {
return d.name});
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
return svg.node();
}
Insert cell
data = (await d3.csv("https://raw.githubusercontent.com/greencracker/georgia-budget-2020/master/five-comparisions-num.csv", ({name, value}) => ({name: name, value: +value}))).sort((a, b) => b.value - a.value)
Insert cell
format = d3.format(".3f")
Insert cell
x = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleBand()
.domain(data.map(d => d.name))
.range([margin.top, height - margin.bottom])
.padding(0.1)
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${margin.top})`)
.call(d3.axisTop(x).ticks(0))
.call(g => g.select(".domain").remove())
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
// .attr("style",
// .call(d3.axisLeft(y).tickSizeOuter(0))
.call(d3.axisLeft(y).tickFormat("").tickSizeOuter(0))
Insert cell
height = data.length * 50 + margin.top + margin.bottom
Insert cell
margin = ({top: 30, right: 0, bottom: 10, left: 10})
Insert cell
d3 = require("d3@5")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more