Public
Edited
Jan 19, 2023
Insert cell
Insert cell
Insert cell
Inputs.table(data)
Insert cell
Insert cell
scatter(data)
Insert cell
function scatter(data, {
width = 640,
height = 400,
margin = 50,
x = (d) => d.bill_length_mm,
y = (d) => d.flipper_length_mm,
} = {}) {
const xScale = d3.scaleLinear()
.domain(d3.extent(data, x))
.range([margin, width - margin])

const yScale = d3.scaleLinear()
.domain(d3.extent(data, y))
.range([height - margin, margin])
var svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.on("click", addDatapoint);

let xg = svg.append("g").attr("transform", `translate(0,${height-margin})`)
let xAxis = d3.axisBottom(xScale) // this sets up the axis component
xAxis(xg) // this renders SVG elements into the "g" container

let yg = svg.append("g").attr("transform", `translate(${margin},0)`)
let yAxis = d3.axisLeft(yScale) // this sets up the axis component
yAxis(yg) // this renders SVG elements into the "g" container

svg.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", d => xScale(x(d)))
.attr("cy", d => yScale(y(d)))
.attr("r", 5)


// Click interaction for plot.
function addDatapoint(event) {
let [x, y] = d3.pointer(event);
x = xScale(x);
y = yScale(y);

// how to update the data?
data = [... data, {
species: "New",
island: "Atlantis",
bill_length_mm: x,
bill_depth_mm: 33.3,
flipper_length_mm: y,
body_mass_g: 333,
sex: "male",
year: 2023,
}];
}
let domElement = svg.node()
return domElement
}
Insert cell
Insert cell
Insert cell
Insert cell
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