chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("stroke-width", 2);
+
const drag = d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
const z0 = math.complex(1,1);
const circles = computePoints(z0);
var lineGenerator = d3.line();
var points = [
[width/2, height],
[width/2, 0],
[width/2, height/2],
[width / 2 + height/2, height/2],
[width/2 - height/2, height/2]
];
var pathData = lineGenerator(points);
svg.append("path")
.attr('d', pathData)
.attr("fill","none")
.attr("stroke","black")
.attr("stroke-width",2)
svg.selectAll("circle.z")
.data([z2p(z0)])
.join("circle")
.attr("circle", "z")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", radius)
.attr("fill", (d, i) => d3.schemeCategory10[i % 10])
.call(drag)
.on("click", clicked);
svg.selectAll("circle.poly")
.data(circles)
.join("circle")
.attr("class", "poly")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", radius)
.attr("fill", (d, i) => d3.schemeCategory10[(i + 1) % 10])
//.call(drag)
//.on("click", clicked);
+
function dragstarted(d) {
d3.select(this).attr("stroke", "black");
}
function dragged(d) {
d3.select(this).raise().attr("cx", d.x = d3.event.x).attr("cy", d.y = d3.event.y);
const zz = p2z(d3.event.x, d3.event.y)
updatePoints(zz);
}
function dragended(d) {
//d3.select(this).attr("stroke", null);
const zz = p2z(d3.event.x, d3.event.y)
updatePoints(zz)
/*console.log(zz)
d3.select("svg")
.selectAll("circle.poly")
.data(computePoints(zz))
.join()
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", radius)
.attr("fill", (d, i) => d3.schemeCategory10[i % 10])*/
}
function updatePoints(zz) {
svg
.selectAll("circle.poly")
.data(computePoints(zz))
.attr("cx", d => d.x)
.attr("cy", d => d.y)
//.attr("r", radius)
//.attr("fill", (d, i) => d3.schemeCategory10[(i+i) % 10])
}
updatePoints(z0);
function clicked(d, i) {
if (d3.event.defaultPrevented) return; // dragged
d3.select(this).transition()
.attr("fill", "black")
.attr("r", radius * 2)
.transition()
.attr("r", radius)
.attr("fill", d3.schemeCategory10[i % 10]);
}
return svg.node();
}