Public
Edited
Sep 30, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// myLinearScaleE = d3.scaleLinear().domain([0, 1]).range([0, 100]) // ecriture 1
myLinearScaleE = d3.scaleLinear([0, 1], [0, 100]) // écriture raccourci
Insert cell
myLinearScaleE(0.7)
Insert cell
iris
Insert cell
// sélection des valeurs la colonne longueur pétale dans la base iris
myValeurs = iris.map((d) => d.sepalLength)
Insert cell
minValeur = d3.min(myValeurs)
Insert cell
maxValeur = d3.max(myValeurs)
Insert cell
extentValeurs = d3.extent(myValeurs)
Insert cell
d3.scaleLinear().domain(extentValeurs).range([0, 1])
Insert cell
// autre méthode pour trouver les valeurs extrêmes directement dans la base de données
extentValeurs1 = d3.extent(iris, d => d.sepalLength)
Insert cell
// récupération d'une valeur d'un tableau
d3
.scaleLinear()
.domain(d3.extent(extract("sepalLength", iris)))
.range([0, 1])(5)
Insert cell
extract = (value, data) => {
return data.map((d) => d[value]);
}
Insert cell
Insert cell
Insert cell
myOrdinalScale = d3
.scaleOrdinal()
.domain(["apple", "orange", "banana"])
.range(["red", "orange", "yellow"])
Insert cell
myOrdinalScale("orange")
Insert cell
// renvoyer une couleur de fond en rapport avec la variable appelée
htl.html`<div style='background-color: ${myOrdinalScale(
"apple"
)}'>${"apple"}</div>`
Insert cell
myDataOrdinalScale = d3
.scaleOrdinal()
.domain(Array.from(new Set(iris.map((y) => y.species))))
.range(["red", "orange", "yellow"])
Insert cell
myDataOrdinalScale("virginica")
Insert cell
iris
Insert cell
// extraction de la colonne catégorie
// iris.map((y) => y.species)
// stockage dans set des différentes catégories du tableau
new Set(iris.map((y) => y.species))
Insert cell
// met les catégories dans un tableau
Array.from(new Set(iris.map((y) => y.species)))
Insert cell
Insert cell
Insert cell
Insert cell
{
const width = 600;
const height = 400;
const margin = {top: 20, bottom: 20, left: 20, right: 20};
const svg = d3.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("border", "1px dotted #000")
const g = svg
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

g.append("rect").attr("width", width).attr("height", height);
return svg.node();
}
Insert cell
iris
Insert cell
{
const width = 600;
const height = 400;
const margin = { top: 20, bottom: 20, left: 50, right: 50 };

const svg = d3
.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("border", "1px dotted #000");

const g = svg
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

// g.append("rect").attr("width", width).attr("height", height);

const x = d3
.scaleLinear()
.domain(d3.extent(iris, (d) => d["sepalLength"]))
.range([0, width]);

const y = d3
.scaleLinear()
.domain(d3.extent(iris, (d) => d["sepalWidth"]))
.range([height, 0]);

const colorScale = d3
.scaleOrdinal()
.domain(Array.from(new Set(iris.map((d) => d.species))))
.range(["red", "orange", "yellow"]);

g.append("g")
.selectAll("circle")
.data(iris)
.join("circle")
.attr("r", 3)
.attr("cx", (d) => x(d.sepalLength))
.attr("cy", (d) => y(d.sepalWidth))
.style("fill", (d) => colorScale(d.species));

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const width = 600;
const height = 400;
const margin = { top: 20, bottom: 20, left: 50, right: 50 };

const svg = d3
.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("border", "1px dotted #000");

const g = svg
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

// g.append("rect").attr("width", width).attr("height", height).style("fill", "#ccc");

const x = d3
.scaleLinear()
.domain(d3.extent(iris, (d) => d["sepalLength"]))
.range([0, width]);

const y = d3
.scaleLinear()
.domain(d3.extent(iris, (d) => d["sepalWidth"]))
.range([height, 0]);

const colorScale = d3
.scaleOrdinal()
.domain(Array.from(new Set(iris.map((d) => d.species))))
.range(["red", "orange", "yellow"]);

g.append("g")
.selectAll("circle")
.data(iris)
.join("circle")
.attr("r", 3)
.attr("cx", (d) => x(d.sepalLenght))
.attr("cy", (d) => y(d.sepalWidht))
.style("fill", (d) => colorScale(d.species));
return svg.node();
}
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
iris.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

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