Public
Edited
Apr 6
Insert cell
Insert cell
Insert cell
Insert cell
html`<svg width="100" height="100">
<circle cx="40" cy="60" r="20" style="fill:red;">
</circle>`
Insert cell
html`<svg width="110" height="110">
<polygon points="50,5 20,99 95,39 5,39 80,99" style="fill:blue;stroke:red;stroke-width:5;fill-rule:evenodd;" />
</circle>`
Insert cell
Insert cell
Insert cell
{
const svg = d3.create("svg")
.attr("width", 256)
.attr("height", 256);

const rows = 20;
const cols = 20;
const spacing = 12;
const radius = 5;

const xScale = d3.scaleLinear().domain([0, cols - 1]).range([20, 236]);
const yScale = d3.scaleLinear().domain([0, rows - 1]).range([20, 236]);

for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
const rx = i / (cols - 1); // rojo
const gy = j / (rows - 1); // verde

const color = d3.rgb(
255 * rx, // rojo
255 * gy, // verde
0 // azul
).toString();

svg.append("circle")
.attr("cx", xScale(i))
.attr("cy", yScale(j))
.attr("r", radius)
.attr("fill", color);
}
}

return svg.node();
}

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

const cols = 32; // 32 triángulos por fila
const rows = 16; // 16 filas
const dx = 8; // ancho del triángulo
const dy = 12
; // alto del triángulo

for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
const x = i * dx;
const y = j * dy;

const isEvenCol = i % 2 === 0;

const p1 = isEvenCol ? [x + dx / 2, y] : [x + dx / 2, y + dy];
const p2 = isEvenCol ? [x, y + dy] : [x, y];
const p3 = isEvenCol ? [x + dx, y + dy]: [x + dx, y];

svg.append("polygon")
.attr("points", `${p1} ${p2} ${p3}`)
.attr("fill", isEvenCol ? "yellow" : "white")
.attr("stroke", isEvenCol ? "blue" : "deeppink")
.attr("stroke-width", 1.5);
}
}

return svg.node();
}

Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
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