{
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
const path = svg
.append("path")
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 8)
.attr("d", line(data));
const pathB = svg
.append("path")
.attr("fill", "none")
.attr("stroke", "lime")
.attr("stroke-width", 2),
pathC = svg
.append("path")
.attr("fill", "none")
.attr("stroke", "orange")
.attr("stroke-width", 2);
const points = svg
.append("g")
.selectAll("circle")
.data(data.slice(0, Math.ceil(n + 1)))
.join("circle")
.attr("r", 4)
.style("stroke", "white")
.style("fill", "black")
.attr("cx", d => d[0])
.attr("cy", d => d[1]);
pathB.attr(
"d",
lineM(
[data[0]]
.concat(data)
.concat([data[data.length - 1]])
.slice(0, 2 + Math.floor(n + 1))
)
);
pathC.attr(
"d",
lineM(
[data[0]]
.concat(data)
.concat([data[data.length - 1]])
.slice(0, 2 + Math.ceil(n + 1))
)
);
const lB = pathB.node().getTotalLength();
const lC = pathC.node().getTotalLength();
const l = lB + (lC - lB) * (n - Math.floor(n));
pathC.attr("stroke-dasharray", [0, lB, lC - lB]);
path.attr("stroke-dasharray", [l, path.node().getTotalLength() - l]);
return svg.node();
}