Public
Edited
May 7, 2023
Fork of Simple D3
Insert cell
Insert cell
{
const margin = ({top: 20, right: 30, bottom: 30, left: 40});
const innerWidth = width - (margin.left + margin.right);
const height = innerWidth + (margin.top + margin.bottom);
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height); // make a square at center

svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`)
.append("rect")
.attr("width", innerWidth)
.attr("height", innerWidth)
.attr("fill", "none")
.attr("stroke", "red");
svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`)
.append("circle")
.attr("cx", (innerWidth)/2)
.attr("cy", (innerWidth)/2)
.attr("r", innerWidth/2)
.attr("fill", "none")
.attr("stroke", "red");
svg.append("g")
.attr("transform", `translate(${margin.left+innerWidth}, ${margin.top+innerWidth})`)
.append("text")
.attr('x', 0)
.attr('y', 0)
.attr('stroke', 'green')
.style("font-size", 19)
.attr("text-anchor", "end")
.attr("dy", "16")
.text("I'm another piece of text");

const x = d3.scaleLinear()
.domain(d3.extent(data, d => d.x)).nice()
.range([margin.left, width - margin.right]);
const y = d3.scaleLinear()
.domain(d3.extent(data, d => d.y)).nice()
.range([height - margin.bottom, margin.top]);

svg.append("g")
.attr("transform", `translate(${0}, ${margin.top+(height-margin.top-margin.bottom)/2})`)
.call(d3.axisBottom(x))
// .call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", width - margin.right)
.attr("y", -4)
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.text(data.x));
svg.append("g")
.attr("transform", `translate(${margin.left+(width-margin.left-margin.right)/2}, 0)`)
.call(d3.axisLeft(y))
// .call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 4)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y));

return svg.node();
}
Insert cell
data = ([
{x:-100,y:-100},
{x:100, y:100},
])
Insert cell
// chart = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);

svg
.selectAll("circle")
.data(d3.range(100))
.join("circle")
.attr("cx", d => d * 50)
.attr("cy", height / 2)
.attr("r", d => Math.random() * 20)
.attr("fill", "hsl(216deg 100% 13%)");
return svg.node();
}
Insert cell
// height = width
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