In https://observablehq.com/d/4e5d48b692f468e6,
I created a cell which contains a function drawChessboard() which draws a SVG with a chess board.
I would like to add pieces or paths to this SVG in a different cell. How do I correctly reference that SVG in order to continue drawing upon it.
What I did was this cell:
{
const svg = drawChessboard();
const dataArray = […piecePositions.values()];
d3.select(“svg”)
// d3.selectAll(".chess-translate")
.selectAll(“path.piece-move”)
.data(dataArray)
.join(“path”)
.attr(“class”, “piece-move”)
.attr(“d”, d => d)
.style(“stroke”, (d, i) => (i < 16) ? “orange” : “steelblue”)
.style(“stroke-width”, 5)
.style(“stroke-linejoin”, “round”)
.style(“fill”, “none”);
/*
const pos = squareToPosition(“e4”);
d3.select(“svg”)
.append(“circle”)
.style(“fill”, “orange”)
.attr(“cx”, pos.x)
.attr(“cy”, pos.y)
.attr(“r”, 30);
*/
return svg;
}
However, the command d3.select(“svg”) does not seem to be the right selection to continue drawing since it doesn’t know which SVG is right one.
The experience was surprising, in one cell which this code it worked in a different one it doesn’t. Then I added something, later deleted, now it does not show anything more than the chessboard.
Initially it showed the desired result: