scatter = {
let svg, xg, yg, cg
if(this) {
svg = d3.select(this)
xg = svg.select("g.x-axis")
yg = svg.select("g.y-axis")
cg = svg.select("g.circles")
} else {
svg = d3.create("svg")
xg = svg.append("g").classed("x-axis", true)
yg = svg.append("g").classed("y-axis", true)
cg = svg.append("g").classed("circles", true)
}
svg
.attr("width", width)
.attr("height", height)
xg.attr("transform", `translate(0, ${height - 30})`)
xAxis(xg)
yg.attr("transform", `translate(30, 0)`)
yAxis(yg)
function enter(selection) {
return selection.append("circle")
.attr("cx", d => xScale(d[xdim]))
.attr("cy", d => yScale(d[ydim]))
.attr("fill", d => colorScale(d.species))
.attr("r", 1)
}
function update(selection) {
return selection
}
let circles = cg.selectAll("circle")
.data(data)
.join(enter, update)
.transition()
.duration(3000)
.attr("cx", d => xScale(d[xdim]))
.attr("cy", d => yScale(d[ydim]))
.attr("fill", d => colorScale(d.species))
.attr("r", 5)
return svg.node()
}