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

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