Public
Edited
Nov 28, 2023
Insert cell
Insert cell
{
let svg = d3.create("svg").attr("width", 150).attr("height", 150);
svg
.append("rect")
.attr("x", 20)
.attr("y", 20)
.attr("height", 60)
.attr("width", 60)
.attr("fill", "pink")
.attr("stroke", "orange");

svg
.append("circle")
.attr("cx", 100)
.attr("cy", 100)
.attr("r", 20)
.attr("fill", "yellow")
.attr("stroke", "orange")
.attr("stroke-width", 5)
.attr("opacity", 0.2);

return svg.node();
}
Insert cell
{
let svg = d3.create("svg")
.attr("width", 600)
.attr("height", 410)

svg.append("rect")
.attr("width", 200)
.attr("height", 200)
.attr("fill", "pink")
.attr("opacity", 0.5)

svg.append("circle")
.attr("cx", 105)
.attr("cy", 305)
.attr("r", 100)
.attr("fill", "orange")
.attr("stroke", "yellow")
.attr("stroke-width", 5)
.attr("opacity", 0.8)

return svg.node()
}
Insert cell
viewof radius = Inputs.range([0, 90], { label: "Radius", step: 1 })
Insert cell
viewof hue = Inputs.range([0, 360], { label: "Hue", step: 1 })
Insert cell
mySvg = {
let svg = d3.create("svg").attr("height", 500).attr("width", 1000);

for (let i = 0; i < 900; i++) {

let color = d3.hsl(Math.random() * 50 + hue, 0.5, 0.5);

svg
.append("circle")
.attr("cx", Math.random() * 1000)
.attr("cy", Math.random() * 1000)
.attr("r", Math.random() * radius)
.attr("fill", "pink")
.attr("opacity", Math.random());
}
svg
.append("rect")
.attr("width", 1000)
.attr("height", 500)
.attr("fill","white")
.attr("fill-opacity","0")
.attr("stroke","white")
.attr("stroke-width",10);


return svg.node();
}
Insert cell
viewof grandezza = Inputs.range([0, 40], {label: "Grandezza", step: 1, value: 20})
Insert cell
{
let svg = d3.create("svg")
.attr("width", 600)
.attr("height", 400)

let gruppo = svg.append("g")

gruppo.append("circle")
.attr("cx", 300)
.attr("cy", 200)
.attr("r", 7 * grandezza)
.attr("fill", "green")

gruppo.append("rect")
.attr("y", 160)
.attr("x", 230)
.attr("width", 7 * grandezza)
.attr("height", 4 * grandezza)
.attr("fill", "yellow")

return svg.node()
}
Insert cell
viewof quantità = Inputs.range([1, 1000], {label: "Numerosità", step: 3})
Insert cell
{
let svg = d3.create("svg")
.attr("width", 500)
.attr("height", 500);
let colorScale = d3.scaleSequential(d3.interpolateRgb("lightgreen", "darkgreen")).domain([0, quantità]);

for(let i = 0; i < quantità; i++) {
let x = Math.random() * width;
let y = Math.random() * 500;
let r = Math.random() * 9 + 1;
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", r)
.attr("fill", colorScale(i));
}
return svg.node();
}
Insert cell
{
let svg = d3.create("svg").attr("width", 600).attr("height", 300);

// Definisci i dati per i 5 gruppi
let data = [
{ x: 50, y: 50, rotation: 0 },
{ x: 120, y: 100, rotation: 25 },
{ x: 200, y: 130, rotation: 100 },
{ x: 45, y: 150, rotation: 90 },
{ x: 430, y: 240, rotation: 280 }
];

// Crea un gruppo per ogni set di dati
let groups = svg.selectAll("g")
.data(data)
.enter()
.append("g")
.attr("transform", d => `translate(${d.x},${d.y}) rotate(${d.rotation})`);


groups.append("rect")
.attr("x", -15)
.attr("y", -10)
.attr("width", 40)
.attr("height", 40)
.attr("fill", "pink")
.attr("stroke", "blue");

return svg.node();
}
Insert cell
viewof raggio = Inputs.range([10,40], { label: "raggio", step: 1 })
Insert cell
{
const svg = d3.create ("svg").attr("height", 300).attr("width",400)
// Aggiungi un rettangolo di sfondo
svg
.append("rect")
.attr("width", 400)
.attr("height", 300)
.attr("fill", "black");

// Definisci i dati per il gruppo di primitive grafiche
const data = [
{ x: 100, y: 100, radius: raggio, color: "red" },
{ x: 150, y: 150, radius: raggio, color: "orange" },
{ x: 250, y: 100, radius: raggio, color: "yellow"},
{ x: 200, y: 200, radius: raggio, color: "green" },
{ x: 100, y: 200, radius: raggio, color: "blue" },
{ x: 300, y: 200, radius: raggio, color: "purple" }
];

// Aggiungi cerchi al gruppo di primitive grafiche
svg
.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", d => d.radius)
.attr("fill", d => d.color);
return svg.node();
}

Insert cell
{
let svg = d3.create("svg")
.attr("width", width)
.attr("height", 500);

for(let i = 0; i < quantità; i++) {
let x = Math.random() * width;
let y = Math.random() * 500;
let r = Math.random() * 9 + 1;
svg.append("circle")
.attr("cx", x)
.attr("cy", y)
.attr("r", r)
.attr("fill", d3.hsl(Math.random() * 256, 0.5, 0.5))
}
return svg.node();
}
Insert cell
car-models - data.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
{
let altezza = 500
let marginTop = 20
let marginBottom = 50
let marginLeft = 80
let marginRight = 20
let svg = d3.create("svg")
.attr("height", altezza)
.attr("width", width);

let gruppi = svg.selectAll("g")
.data(cars)
.join("g");

let xScale = d3.scaleLinear()
.range([marginLeft, width - marginRight])
.domain(d3.extent(cars, d=>d["weight (lb)"]));

let yScale = d3.scaleLinear()
.range([altezza - marginBottom, marginTop])
.domain(d3.extent(cars, d=>d["displacement (cc)"]));
gruppi
.attr("transform", (d)=> "translate(" + xScale(d["weight (lb)"]) + ", " + yScale(d["displacement (cc)"]) + ")");

gruppi.append("circle")
.attr("cx", 0)
.attr("cy", 0)
.attr("r", 6)
.attr("fill", "red")

svg.append("g")
.attr("transform", "translate(0, " + (altezza - marginBottom) + ")")
.call(d3.axisBottom(xScale));

svg.append("g")
.attr("transform", "translate(" + marginLeft + ", 0)")
.call(d3.axisLeft(yScale));

svg.append("text")
.attr("x", width/2)
.attr("y", altezza + 20 - marginBottom/2)
.attr("text-anchor", "middle")
.text("Weight (lb)");

svg.append("text")
.attr("x", marginLeft/2 - 10)
.attr("y", - marginTop + altezza/2)
.attr("text-anchor", "middle")
.attr("transform", "rotate(90, " + (marginLeft / 2 - 10) + ", " + (-marginTop + altezza / 2) + ")") //Per far sì che ruoti su se stesso
.text("Displacement (cc)");
return svg.node()
}
Insert cell
carmodels@1.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
{
let height = 600;
let svg = d3.create("svg").attr("width", width).attr("height", height);

let gruppi = svg.selectAll("g").data(carmodels1).join("g");

let larghezza = 30;

let xScale = d3
.scaleLinear()
.range([0, width])
.domain(d3.extent(carmodels1, (d) => d["TMEDIA °C"]));

let yScale = d3
.scaleLinear()
.range([0, height])
.domain(d3.extent(carmodels1, (d) => d["UMIDITA %"]));

gruppi.attr(
"transform",
(d) =>
"translate(" +
xScale(d["TMEDIA °C"]) +
"," +
yScale(d["UMIDITA %"]) +
")"
);

gruppi
.append("circle")
.attr("r", 10) // raggio del sole
.attr("cx", 100) // posizione x del sole
.attr("cy", 50) // posizione y del sole
.attr("fill", "#ffd100"); // colore del sole

// Aggiungi raggi al sole
for (let i = 0; i < 12; i++) {
const angle = (i * 30 * Math.PI) / 180; // angolo in radianti
const rayLength = 25; // lunghezza dei raggi
const startX = 100 + rayLength * Math.cos(angle);
const startY = 50 + rayLength * Math.sin(angle);
const endX = 100;
const endY = 50;

gruppi
.append("line")
.attr("x1", startX)
.attr("y1", startY)
.attr("x2", endX)
.attr("y2", endY)
.attr("stroke", "#ffd100")
.attr("stroke-width", 2);
}

return svg.node();
}
Insert cell
{
const svg = d3.create("svg").attr("height", 100).attr("width", 100); //crea una immagine svg 100px per 100px

let points = [];

for (let i = 0; i < 30; i++) {
points.push([Math.random() * 100, Math.random() * 100]);
}

svg
.append("path")
.attr("d", d3.line()(points))
.attr("fill", "none")
.attr("stroke", "red");

return svg.node(); // chiedi a Observable di renderizzare il risultato del codice
}
Insert cell
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