Public
Edited
Sep 28, 2022
Insert cell
# Brushed Scatter
Insert cell
viewof scatterplot = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);

const circle = svg.append("g")
.attr("fill-opacity", 0.2)
.selectAll("circle")
.data(data)
.join("circle")
.attr("transform", d => `translate(${xscale(d.x)},${yscale(d.y)})`)
.attr("r", 3.5);

// svg.append("g")
// .call(xAxis);

// svg.append("g")
// .call(yAxis);

svg.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(xAxis)
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").clone()
.attr("y2", margin.top + margin.bottom - height)
.attr("stroke-opacity", 0.1))
.call(g => g.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(xLabel));

svg.append("g")
.attr("transform", `translate(${margin.left},0)`)
.call(yAxis)
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").clone()
.attr("x2", width - margin.left - margin.right)
.attr("stroke-opacity", 0.1))
.call(g => g.append("text")
.attr("x", - margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(yLabel));
return svg.node();
}
Insert cell
height = 200
Insert cell
xscale = d3.scaleLinear(xdomain, [margin.left, width - margin.right]);
Insert cell
yscale = d3.scaleLinear(ydomain, [height - margin.bottom, margin.top]);
Insert cell
data = [{x: 10, y:20}, {x: 1, y:10}, {x: 100, y:19}]
Insert cell
Insert cell
xdomain = calcMinMax(data.map(d => d.x));
Insert cell
ydomain = calcMinMax(data.map(d => d.y));
Insert cell
margin = ({top: 10, right: 20, bottom: 20, left: 20})
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xscale))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left}, 0)`)
.call(d3.axisLeft(yscale))
Insert cell
yLabel = "Y Axis"
Insert cell
xLabel = "X Axis"
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