Published
Edited
May 24, 2021
2 stars
Insert cell
Insert cell
Insert cell
url = "https://cdn.jsdelivr.net/gh/EE2dev/d3-indented-tree/test/data/countries.csv" // put your data here
Insert cell
data = {
let ar = await d3.csv(url, d3.autoType);
ar.forEach(d => { d.name = d.country; d.value = d.population_2020;}); // adjust accordingly to create d.name and d.value
ar.format = ",d";
ar.groupColumn = "region"; // set your group column here
return ar.sort(countriesSort);
}
Insert cell
groups = [...new Set(data.map(d => d[data.groupColumn]))]
Insert cell
Insert cell
spacing = 50
Insert cell
format = x.tickFormat(20, data.format)
Insert cell
x = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([margin.left, width - margin.right])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${margin.top})`)
.call(d3.axisTop(x).ticks(width / 150, data.format))
.call(g => g.select(".domain").remove())
Insert cell
barHeight = 25
Insert cell
height = Math.ceil((data.length + 0.1) * barHeight) + groups.length * spacing + margin.top + margin.bottom
Insert cell
margin = ({top: 30, right: 0, bottom: 10, left: 160})
Insert cell
d3 = require("d3@6")
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