Published
Edited
Nov 2, 2021
2 forks
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function colorByContinent2(expr){
switch (expr) {
case 'Asia':
return c[0]
break;
case 'Europe':
return c[1]
break;
case 'North America':
return c[2]
break;
case 'Northern Africa & Middle East':
return c[3]
break;
case 'Sub-saharan Africa':
return c[4]
break;
case 'South America':
return c[5]
break;
case 'Central America and the Caribbean':
return c[6]
break;
case 'Oceania':
return c[7]
break;
default:
return "olive"
}}
Insert cell
Insert cell
Insert cell
deathsSortByRegion.filter(d => d.region === regions[1])
Insert cell
// index of countries of max deaths in each region
deathsSortByRegion.filter(d => d.region === regions[6])
Insert cell
container = { let i = 0
let container = []
for (let i = 0; i < regions.length; i++) {
let maxInRegion = deathsSortByRegion.filter(d => d.region === regions[i])[0]
container.push(maxInRegion)
}
return container
}
Insert cell
container
Insert cell
chart5 = {
const arcs = pie_deaths(container);

const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height]);

svg.append("g")
.attr("stroke", "white")
.selectAll("path")
.data(arcs)
.join("path")
// .attr("fill", d => color(d.data.region))
.attr("fill", d => colorByContinent2(d.data.region))
.attr("d", arc_normal)


return svg.node();
}
Insert cell
chart6 = {
const arcs = pie_deaths(container);

const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height]);

svg.append("g")
.attr("stroke", "white")
.selectAll("path")
.data(arcs)
.join("path")
// .attr("fill", d => color(d.data.region))
.attr("fill", d => colorByContinent2(d.data.region))
.attr("d", arc_normal)


return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
regions = [...new Set(deaths.map(obj => obj.region))]
Insert cell
Insert cell
// data = d3.csvParse(await FileAttachment("population-by-age.csv").text(), d3.autoType)
Insert cell
data_lagest = [ {name: "Africa", value: 800},]
Insert cell
data = [ {name: "Africa", value: 950},
{name: "Africa", value: 550},
{name: "Americas", value: 1500},
{name: "Americas", value: 500},
{name: "Asia", value: 1000},
{name: "Asia", value: 800},
{name: "Europe", value: 300},
{name: "Europe", value: 1000},
{name: "Europe", value: 200},
{name: "Middle East", value: 350},
{name: "Middle East", value: 700},
{name: "Middle East", value: 650}]
Insert cell
value = Array.from({length: 100}, () => Math.floor(Math.random() * 10000));
Insert cell
color = d3.scaleOrdinal().domain(data)
.range(d3.schemePastel1);
Insert cell
color2 = d3.scaleOrdinal()
.domain(data.map(d => d.name))
.range(d3.quantize(t => d3.interpolateSpectral(t * 0.8 + 0.1), data.length).reverse())
Insert cell
height = Math.min(width, 500)
Insert cell
padAngel = 0.01
Insert cell
arc_normal = d3.arc()
.innerRadius(20)
.outerRadius(Math.min(width, height) / 2 - 1)
.cornerRadius(60)
.padAngle(padAngel)
// .startAngle([Math.PI/5])
// .endAngle([7*Math.PI/5])
Insert cell
arc2 = d3.arc()
.innerRadius(20)
.outerRadius(Math.min(width, height) / 2.5 - 1)
.cornerRadius(60)
.padAngle(padAngel)
// .startAngle([Math.PI/5])
// .endAngle([2*Math.PI/5])

// .centroid([10,20])
Insert cell
arc3 = d3.arc()
.innerRadius(20)
.outerRadius(Math.min(width, height) / 3 - 1)
.cornerRadius(60)
.padAngle(padAngel)
Insert cell
arc = d3.arc()
.innerRadius(20)
.outerRadius(Math.min(width, height) / 2 - 1)
.cornerRadius(60)
.padAngle(0.5)
// .startAngle([Math.PI/5])
// .endAngle([7*Math.PI/5])
Insert cell
arc_largest = d3.arc()
.innerRadius(20)
.outerRadius(Math.min(width, height) / 2 - 1)
.cornerRadius(80)
.padAngle(0.5)
// .startAngle([Math.PI/5])
// .endAngle([7*Math.PI/5])
Insert cell
arcLabel = {
const radius = Math.min(width, height) / 2 * 0.8;
return d3.arc().innerRadius(radius).outerRadius(radius);
}
Insert cell
pie_deaths = d3.pie()
.sort(null)
.value(d => d.deaths)
Insert cell
pie_normal = d3.pie()
.sort(null)
.value(d => d.value)
Insert cell
pie = d3.pie()
.startAngle(Math.PI / 4)
.endAngle(3.5* Math.PI / 4)
.sort(null)
.value(d => d.value)
Insert cell
pie2 = d3.pie()
.startAngle(3 * Math.PI / 4 + 1 )
.endAngle(6 * Math.PI / 4)
.sort(null)
.value(d => d.value)
Insert cell
pie3 = d3.pie()
.startAngle(6 * Math.PI / 4 + 1 )
.endAngle(9 * Math.PI / 4)
.sort(null)
.value(d => d.value)
Insert cell
d3 = require("d3@6")
Insert cell
http://using-d3js.com/05_07_arcs_pie_charts.html
https://github.com/d3/d3-shape#arc_outerRadius
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