chart = {
let setIndex="set1";
const bouton = html`<button id="monBouton">Change data</button>`
const svgDoc = d3.select(DOM.svg(width, height));
svgDoc.append("text")
.attr("x", baseLine.xMiddle)
.attr("y",50)
.text("Dataset "+setIndex)
.style("text-anchor","middle");
svgDoc.append("line")
.attr("x1", baseLine.x)
.attr("y1", baseLine.y)
.attr("x2", baseLine.x + baseLine.length)
.attr("y2", baseLine.y)
.style("stroke","#ddd")
.style("shape-rendering","crispEdges");
svgDoc.append("g").selectAll("circle")
.data(data[setIndex])
.enter()
.append("circle")
.attr("cx", place_circle)
.attr("cy", baseLine.y)
.attr("r", (d,i) => d)
.style("fill","orange")
.style("stroke", "orange")
.style("fill-opacity",'0.5');
d3.select(bouton)
.text("Change data")
.on("click", on_click_func);
function on_click_func(){
setIndex = (setIndex === "set1") ? "set2" : "set1";
const circle = svgDoc.select("g").selectAll("circle")
.data(data[setIndex]);
circle.exit().remove();
circle.enter().append("circle")
.attr("cx", place_circle)
.attr("cy", baseLine.y)
.attr("r", (d,i) => d)
.style("fill","orange")
.style("stroke", "orange")
.style("fill-opacity",'0.5');
circle.transition()
.duration(500)
.attr("cx", place_circle)
.attr("cy", baseLine.y)
.attr("r", (d,i) => d);
svgDoc.select("text").text("Dataset "+setIndex);
};
function place_circle(d,i){
const spacing = baseLine.length/(data[setIndex].length);
return baseLine.x+(i*spacing)
}
const div = html`<div></div>`;
div.appendChild(bouton);
div.appendChild(svgDoc.node());
return div
}