Published
Edited
Apr 9, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csvParse(await FileAttachment("population-by-age.csv").text(), d3.autoType)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
ensai = [{name: "Femme", value : 50},{name: "Homme", value : 100}]
Insert cell
Insert cell
Insert cell
Insert cell
calcul_angle = d3.pie()
.value(d => d.value)// On lui donne la variable à traiter, ici "value"
.sort(null) //Par défaut le tableau est trié mais on peut désactiver ce paramètre

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
arc = d3.arc()
.outerRadius(Math.min(width, height) / 2 - 1) // On prends un rayon = largeur/2 - 1
.innerRadius(0)
Insert cell
Insert cell
Insert cell
construction_angles = {
// Création de la fenêtre
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height]); // définition de la largueur et hauteur
// Appel à la fonction calcul_angle() de d3.pie() pour avoir le tableau avec les données et les angles
const angles = calcul_angle(ensai);

// On ajoute le cercle construit avec la fonction arc().
svg.append("g") // Creation de l'environnement
.selectAll("path") // selectAll + data + join permet de créer un "path" par ligne du tableau
.data(angles) //
.join("path") //
.attr("d", arc) // arc() fait ensuite tout le travail
return svg.node()
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
color = d3.scaleOrdinal()
.domain(data.map(d => d.name))
.range(d3.quantize(t => d3.interpolateSpectral(t * 0.8 + 0.1), data.length).reverse())
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
arcLabel = {
const radius = Math.min(width, height) / 2 * 0.8; // le rayon est calculé pour etre légèrement inférieur à celui du cercle
return d3.arc().innerRadius(radius).outerRadius(radius);
}
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
d3 = require("d3@6")
Insert cell
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