chart = {
let sl;
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
const margin = { left: 40, bottom: 40, top: 10, right: 10 };
const x = (d) => d["economy (mpg)"];
const y = (d) => d["displacement (cc)"];
const data = cars.filter(
(d) =>
x(d) !== undefined && x(d) !== null && y(d) !== undefined && y(d) !== null
);
const xScale = d3
.scaleLinear()
.domain([0, d3.max(data, x)])
.range([0, width - margin.left - margin.right])
.nice();
const yScale = d3
.scaleLinear()
.domain([0, d3.max(data, y)])
.range([height - margin.top - margin.bottom, 0]);
const g = svg
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
g.append("g").call(d3.axisLeft(yScale));
g.append("g")
.attr("transform", `translate(0, ${yScale.range()[0]})`)
.call(d3.axisBottom(xScale));
g.selectAll("circle")
.data(data)
.attr("stroke", "black")
.join("circle")
.attr("cx", (d) => xScale(x(d)))
.attr("cy", (d) => yScale(y(d)))
.attr("r", 3)
.append("title")
.text((d) => d.name);
const gLabels = g.append("g").attr("id", "labels");
if (!useSmartLabels) {
g.selectAll("text.label")
.data(data)
.attr("stroke", "black")
.join("text")
.attr("class", "label")
.style("font", "10px sans-serif")
.attr("x", (d) => xScale(x(d)))
.attr("y", (d) => yScale(y(d)))
.text((d) => d.name);
} else {
sl = smartLabels(data, {
target: gLabels,
label: (d) => d.name,
x: (d) => xScale(x(d)),
y: (d) => yScale(y(d)),
width: width - margin.left - margin.right,
height: height - margin.top - margin.bottom,
labelsInCentroids,
threshold,
showVoronoi: showVoronoi,
showAnchors: showAnchors,
font: (
d
) =>
labelsToHighlight.includes(d.name)
? "18pt sans-serif"
: "10pt sans-serif",
onHoverFont: (d) =>
labelsToHighlight.includes(d.name)
? "bold 18pt sans-serif"
: "bold 10pt sans-serif",
alwaysShow: (d) => labelsToHighlight.includes(d.name)
});
}
return Object.assign(svg.node(), { sl });
}