{
const width = 600;
const height = 400;
const margin = { top: 20, bottom: 20, left: 50, right: 50 };
const div = htl.html`<div></div>`;
const categories = d3.groups(iris, (d) => d.species);
const buttons = d3
.select(div)
.selectAll("button")
.data(categories)
.join("button");
buttons.text((d) => d[0]);
const svg = d3
.select(div)
.append("div")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
const g = svg
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
const x = d3
.scaleLinear()
.domain(d3.extent(iris, (d) => d["sepalLength"]))
.range([0, width]);
const y = d3
.scaleLinear()
.domain(d3.extent(iris, (d) => d["sepalWidth"]))
.range([height, 0]);
const color = d3
.scaleOrdinal()
.domain(categories.map((d) => d[0]))
.range(d3.schemeDark2);
g.append("g").call(d3.axisLeft(y));
g.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(x));
const circles = g
.selectAll("circle")
.data(iris.filter((d) => d.species == "setosa"))
.join("circle")
.attr("r", 4)
.style("stroke", "#000")
.attr("cx", (d) => x(d.sepalLength))
.attr("cy", (d) => y(d.sepalWidth))
.style("fill", (d) => color(d.species));
buttons.on("click", (event, d) => {
g.selectAll("circle")
.data(d[1], (d) => d)
.join(
(enter) =>
enter
.append("circle")
.attr("r", 4)
.style("stroke", "#000")
.attr("cx", (d) => x(d.sepalLength))
.attr("cy", (d) => y(d.sepalWidth))
.style("fill", (d) => color(d.species)),
(update) =>
update
.attr("cx", (d) => x(d.sepalLength))
.attr("cy", (d) => y(d.sepalWidth))
.style("fill", (d) => color(d.species)),
(exit) =>
exit
.attr("fill", "red")
.call((exit) =>
exit.transition().duration(2000).attr("r", 0).remove()
)
);
});
return div;
}