Public
Edited
Jan 17
1 star
Insert cell
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
Insert cell
Plot.plot({
marks: [
Plot.dot(penguins, {fill: "species", r: 3, x: "flipper_length_mm", y: "body_mass_g"})
]
})
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