{
const svg = d3.create("svg")
.attr("width", dimensions.width)
.attr("height", dimensions.height)
.attr("viewBox", [0, 0, dimensions.width, dimensions.height])
.style("max-width", "100%")
.style("height", "auto")
const bounds = svg.append("g")
.style("transform", `translate(${dimensions.margin.left}px, ${dimensions.margin.top}px)`)
bounds.selectAll("circle")
.data(plotData)
.join("circle")
.attr("cx", d => xScale(xAccessor(d)))
.attr("cy", d => yScale(yAccessor(d)))
.attr("fill", d => colorScale(colorAccessor(d)))
.attr("r", radius)
bounds.append("g")
.call(xAxis)
.style("transform", `translateY(${boundedHeight}px)`)
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").clone()
.attr("y2", -(boundedHeight + dimensions.margin.top * 0.5))
.style("opacity", 0.1))
bounds.append("g")
.call(yAxis)
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").clone()
.attr("x2", boundedWidth + dimensions.margin.left * 0.5)
.style("opacity", 0.1))
const axisLabels = [
{x: 366, y: 435, text: "Bill length (in mm)"},
{x: 5, y: -4, text: "Bill depth (in mm)"}
]
bounds.selectAll("rect")
.data(labels)
.join("rect")
.attr("x", d => d.x)
.attr("y", d => d.y)
.attr("width", d => d.bbox.width + 2 * 4)
.attr("height", d => d.bbox.height + 2 * 2)
.attr("fill", "white")
.attr('transform', function(d) {
return `translate(-4, -${d.bbox.height * 0.8 + 2})`
})
bounds.selectAll("labels")
.data(labels)
.join("text")
.attr("class", "labels")
.attr("x", d => d.x)
.attr("y", d => d.y)
.attr("fill", d => colorScale(d.text))
.text(d => d.text)
bounds.selectAll("axis-labels")
.data(axisLabels)
.join("text")
.attr("class", "axis-labels")
.attr("x", d => d.x)
.attr("y", d => d.y)
.text(d => d.text)
.call(d3.drag().on("drag", positionLabel))
function positionLabel(e, d) {
d3.select(this)
.attr("x", d.x = e.x)
.attr("y", d.y = e.y)
}
return svg.node()
}