Published
Edited
Sep 13, 2020
3 forks
3 stars
Insert cell
Insert cell
svg = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

const g = svg
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

g.append("g")
.call(d3.axisBottom(x))
.attr("transform", `translate(0, ${height - margin.top - margin.bottom})`)
.call(axis =>
axis
.append("text")
.text(xAttr)
.style("fill", "black")
.attr("transform", `translate( ${iwidth}, -10)`)
.style("text-anchor", "end")
);
g.append("g")
.call(d3.axisLeft(y))
.call(axis =>
axis
.append("text")
.text(yAttr)
.style("fill", "black")
.style("text-anchor", "middle")
.attr("y", -15)
);

// Your marks here
g.selectAll(".point")
.data(data)
.join("circle")
.attr("class", "point")
.attr("cx", d => x(d[xAttr]))
.attr("cy", d => y(d[yAttr]))
.attr("r", 2);

return svg.node();
}
Insert cell
iwidth = width - margin.left - margin.right
Insert cell
iheight = height - margin.top - margin.bottom
Insert cell
xAttr = "Beak Length (mm)"
Insert cell
yAttr = "Beak Depth (mm)"
Insert cell
x = d3
.scaleLinear()
.domain(d3.extent(data, d => d[xAttr]))
.range([0, iwidth])
.nice()
Insert cell
y = d3
.scaleLinear()
.domain(d3.extent(data, d => d[yAttr]))
.range([iheight, 0])
.nice()
Insert cell
height = 400
Insert cell
margin = ({ left: 50, top: 30, right: 20, bottom: 20 })
Insert cell
md`## Data`
Insert cell
data = vegaDatasets["penguins.json"]()
Insert cell
vegaDatasets = require("vega-datasets@2")
Insert cell
d3 = require("d3@6")
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