Public
Edited
Jul 5, 2023
Paused
797 forks
9 stars
Insert cell
Insert cell
chart = {

// Specify the chart’s dimensions.
const width = 640;
const height = 400;
const marginTop = 25;
const marginRight = 20;
const marginBottom = 35;
const marginLeft = 40;

// Define the horizontal scale.
const x = d3.scaleLinear()
.domain(d3.extent(penguins, d => d.flipper_length_mm)).nice()
.range([marginLeft, width - marginRight]);

// Define the vertical scale.
const y = d3.scaleLinear()
.domain(d3.extent(penguins, d => d.body_mass_g)).nice()
.range([height - marginBottom, marginTop]);

// Create the container SVG.
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;");

// Add the axes.
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(x));

svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y));

// Append a circle for each data point.
svg.append("g")
.selectAll("circle")
.data(penguins)
.join("circle")
.filter(d => d.body_mass_g)
.attr("cx", d => x(d.flipper_length_mm))
.attr("cy", d => y(d.body_mass_g))
.attr("r", 3);

return svg.node();
}
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