Published
Edited
Dec 25, 2020
1 fork
Insert cell
Insert cell
chart = {
const width = 200;
const height = 140;

const svg = d3.select(DOM.svg(width, height))
.style("width", "100%")
.style("height", "auto");
const marker = svg.append('defs')
.append("marker")
.attr("id", "arrowhead")
.attr("markerWidth", 8)
.attr("markerHeight", 8)
.attr("refX", 6)
.attr("refY", 3)
.attr("orient", "auto")
.append('path')
.attr("d", "M 0 0 L 6 3 L 0 6")
.attr("fill", "none")
.attr("stroke", "grey")
.attr("stroke-linejoin", "miter-clip");
const dr = rayon / 2;
const decalage = rayon + 4;
const formes = svg.append("svg:g")
.attr("id", "forme");
formes
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", height)
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 1);
const ptA = formes
.append("circle")
.attr("cx", pos[0].x)
.attr("cy", pos[0].y)
.attr("r", rayon)
.attr("fill", "black")
.attr("stroke-width", 1)
.attr("stroke", "black")
.transition()
.on("start", function repeat() {
d3.active(this)
.transition().duration(vitesse)
.attr("fill", "white")
.attr("stroke", "grey")
.transition().duration(0)
.attr("fill", "black")
.attr("stroke", "black")
.on("end", repeat);
});
const ptB = formes
.append("circle")
.attr("cx", pos[1].x)
.attr("cy", pos[1].y)
.attr("r", rayon)
.attr("fill", "white")
.attr("stroke-width", 1)
.attr("stroke", "black")
.transition()
.on("start", function repeat() {
d3.active(this)
.transition().duration(vitesse)
.attr("fill", "black")
.attr("stroke", "black")
.transition().duration(0)
.attr("fill", "white")
.attr("stroke", "grey")
.on("end", repeat);
});
const LFleche = offset(pos[0], pos[1], decalage);
const posAl = LFleche[0];
const posBl = LFleche[1];
const fleche = formes
.append("line")
.attr("x1", posAl.x)
.attr("y1", posAl.y)
.attr("x2", posBl.x)
.attr("y2", posBl.y)
.attr("stroke-width", 1)
.attr("stroke", "grey")
.attr("stroke-dasharray", "4, 2")
.attr("marker-end", "url(#arrowhead)");

//Animation
const vitesse = 2000;
formes
.append("circle")
.attr("cx", pos[0].x)
.attr("cy", pos[0].y)
.attr("r", rayon)
.attr("fill", "black")
.transition()
.on("start", function repeat() {
d3.active(this)
.transition().duration(vitesse)
.attr("cx", pos[1].x)
.attr("cy", pos[1].y)
.transition().duration(0)
.attr("cx", pos[0].x)
.attr("cy", pos[0].y)
.on("end", repeat);
});
//Dessin par rendu
return svg.node();
}
Insert cell
pos = [{"x": 60, "y": 100}, {"x": 140, "y": 40}];
Insert cell
rayon = 4;
Insert cell
offset = function(start,destination,decalage) {
// find angle of line
var dx = destination.x - start.x;
var dy = destination.y - start.y;
var angle = Math.atan2(dy,dx);
// offset them:
var newStart = {"x": start.x - Math.cos(angle-Math.PI)*decalage, "y": start.y - Math.sin(angle-Math.PI)*decalage};
var newDestination = {"x": destination.x + Math.cos(angle-Math.PI)*decalage, "y": destination.y + Math.sin(angle-Math.PI)*decalage};
;
// return the new start/end points
return [newStart,newDestination]
}
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more