Public
Edited
Apr 2
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
<svg width="500" height="100">
<rect x="10" y="10" width="300" height="20" fill="purple"/>
<rect x="10" y="40" width="250" height="20" fill="brown"/>
<rect x="10" y="70" width="200" height="20" fill="orange"/>
</svg>
Insert cell

{const data = [
{ width: 300, color: "purple", y: 10 },
{ width: 250, color: "brown", y: 40 },
{ width: 200, color: "orange", y: 70 }
];

const svg = d3.create("svg")
.attr("width", 500)
.attr("height", 100);
svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", 10)
.attr("y", d => d.y)
.attr("width", d => d.width)
.attr("height", 20)
.attr("fill", d => d.color);
return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
rectInfo = [
{ xPos: 10, yPos: 10, width: 300, height: 20, color: 'purple' },
{ xPos: 10, yPos: 40, width: 250, height: 20, color: 'brown' },
{ xPos: 10, yPos: 70, width: 200, height: 20, color: 'orange' },
]
Insert cell
//tu código aquí
{
const rectInfo = [
{ xPos: 10, yPos: 10, width: 300, height: 20, color: 'purple' },
{ xPos: 10, yPos: 40, width: 250, height: 20, color: 'brown' },
{ xPos: 10, yPos: 70, width: 200, height: 20, color: 'orange' },
];
const svg = d3.create("svg")
.attr("width", 500)
.attr("height", 100);
svg.selectAll('rect')
.data(rectInfo)
.join('rect')
.attr('x', d => d.xPos)
.attr('y', d => d.yPos)
.attr('width', d => d.width)
.attr('height', d => d.height)
.attr('fill', d => d.color);
return svg.node();
}
Insert cell
Insert cell
circleGrid = [
{
y: 50,
color: 'purple',
label: 'Row A',
circles: [
{ x: 25, radius: 5 },
{ x: 75, radius: 10 },
{ x: 125, radius: 15 },
{ x: 175, radius: 20 },
{ x: 225, radius: 25 }
]
},
{
y: 150,
color: 'brown',
label: 'Row B',
circles: [
{ x: 25, radius: 25 },
{ x: 75, radius: 20 },
{ x: 125, radius: 15 },
{ x: 175, radius: 10 },
{ x: 225, radius: 5 }
]
},
{
y: 250,
color: 'orange',
label: 'Row C',
circles: [
{ x: 25, radius: 20 },
{ x: 75, radius: 10 },
{ x: 125, radius: 5 },
{ x: 175, radius: 10 },
{ x: 225, radius: 20 }
]
}
]
Insert cell
Insert cell
{
//Crear SVG
const svg = d3.create('svg')
.attr('width', 500)
.attr('height', 300);

return svg.node();
}
Insert cell
Insert cell
Insert cell
rectGrid = [
{
y: 50,
color: 'purple',
rects: [
{ x: 25, width: 10, height: 30 },
{ x: 75, width: 30, height: 10 },
{ x: 125, width: 20, height: 20 },
{ x: 175, width: 30, height: 10 },
{ x: 225, width: 10, height: 30 }
]
},
{
y: 150,
color: 'brown',
rects: [
{ x: 25, width: 5, height: 5 },
{ x: 75, width: 10, height: 10 },
{ x: 125, width: 15, height: 15 },
{ x: 175, width: 20, height: 20 },
{ x: 225, width: 25, height: 25 }
]
},
{
y: 250,
color: 'orange',
rects: [
{ x: 25, width: 10, height: 10 },
{ x: 75, width: 10, height: 15 },
{ x: 125, width: 10, height: 20 },
{ x: 175, width: 10, height: 25 },
{ x: 225, width: 10, height: 30 }
]
}
]
Insert cell
Insert cell
// Tu código aquí
{
const rectGrid = [
{
y: 50,
color: 'purple',
rects: [
{ x: 25, width: 10, height: 30 },
{ x: 75, width: 30, height: 10 },
{ x: 125, width: 20, height: 20 },
{ x: 175, width: 30, height: 10 },
{ x: 225, width: 10, height: 30 }
]
},
{
y: 150,
color: 'brown',
rects: [
{ x: 25, width: 5, height: 5 },
{ x: 75, width: 10, height: 10 },
{ x: 125, width: 15, height: 15 },
{ x: 175, width: 20, height: 20 },
{ x: 225, width: 25, height: 25 }
]
},
{
y: 250,
color: 'orange',
rects: [
{ x: 25, width: 10, height: 10 },
{ x: 75, width: 10, height: 15 },
{ x: 125, width: 10, height: 20 },
{ x: 175, width: 10, height: 25 },
{ x: 225, width: 10, height: 30 }
]
}
];

const svg = d3.create("svg")
.attr("width", 300)
.attr("height", 350);

const rows = svg.selectAll("g")
.data(rectGrid)
.join("g")
.attr("transform", d => `translate(0, ${d.y})`)
.attr("fill", d => d.color);


return svg.node();
}
Insert cell
Insert cell
Insert cell
scaleDiagram
Insert cell
Insert cell
Insert cell
linearScale =
Insert cell
Insert cell
fahrenheitToCelsius =
Insert cell
Insert cell
scores = [
{name: 'Brian', score: 90},
{name: 'Taylor', score: 80},
{name: 'Kelly', score: 70},
{name: 'Jane', score: 45},
{name: 'Joe', score: 30},
]
Insert cell
Insert cell
width = 300
Insert cell
Insert cell
maxScore = d3.max(scores, d=>d.score
)
Insert cell
Insert cell
x = d3.scaleLinear()
Insert cell
x(0)
Insert cell
x(maxScore/2)
Insert cell
x(maxScore)
Insert cell
Insert cell
Insert cell
Insert cell
names = scores.map(d => d.name)
Insert cell
height = 100
Insert cell
y = d3.scaleBand()
Insert cell
Insert cell
Insert cell
y.bandwidth()
Insert cell
Insert cell
scores
Insert cell
{
// creamos nuestro elemento SVG externo con un tamaño de 500x100
const svg = d3.create('svg')
.attr('width', 500)
.attr('height', 100);
// barra por cada elemento en scores
const bars = svg.selectAll('rect')
.data(scores)
.join('rect')
// establecemos los atributos para los rectángulos
.attr('fill', 'steelblue')
.attr('x', 0)
.attr('y', d => yscale(d.name))
.attr('width', d => x(d.value)) // EDITAR aquí
.attr('height', yscale.bandwidth()); // EDITAR aquí
// devolvemos el elemento SVG externo para que se pueda renderizar
return svg.node();

}
Insert cell
Insert cell
Insert cell
Insert cell
ordinalScale = d3.scaleOrdinal()
.domain(['one', 'two','three'])
.range([1,2,3])
Insert cell
ordinalScale('one')
Insert cell
Insert cell
names
Insert cell
d3.schemeCategory10
Insert cell
ordinalColor = d3.scaleOrdinal()
.domain(names)
.range(d3.schemeCategory10)
Insert cell
Insert cell
import {swatches} from "@d3/color-legend"
Insert cell
swatches({color: ordinalColor})
Insert cell
Insert cell
{

const svg = d3.create('svg')
.attr('width', 500)
.attr('height', 100);


const circles = svg.selectAll('rect')
.data(scores)
.join('rect')
.attr('fill', d => ordinalColor(d)) // Aquí
.attr('x', 0)
.attr('y', d => y(d.name))
.attr('width', d => x(d.score))
.attr('height', y.bandwidth())

return svg.node();
}
Insert cell
Insert cell
d3 = require('d3@7')
Insert cell
Insert cell
import {chart as scaleDiagram} from '@uw-info474/animated-scale-diagram'
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