drag = {
return function (g, svg) {
function dragstarted(event, d) {
d3.select(this).attr("opacity", 1).attr("pointer-events", "none");
const me = d3.select(this).raise();
me.attr(
"transform",
`translate(${event.x - x.bandwidth() / 2},${
event.y - y.bandwidth() / 2
})`
);
}
function dragged(event, d, i) {
const me = d3.select(this);
me.attr(
"transform",
`translate(${event.x - x.bandwidth() / 2},${
event.y - y.bandwidth() / 2
})`
);
let xx = scaleBandInvert(x)(event.x + x.bandwidth() / 2);
let yy = scaleBandInvert(y)(event.y);
let overs = data.filter((t, i) => {
return (
(i % 3 === xx || (i % 3) + 1 === xx) &&
Math.floor(i / 3) === yy &&
t.name != me.data()[0].name
);
});
d3.select(svg)
.selectAll(".sub")
.data(overs, (d) => d.name)
.each(function (d, i) {
d3.select(this)
.select("rect")
.attr("x", overs.length === 1 ? 15 : i === 0 ? -15 : 15);
})
.exit()
.each(function (d) {
d3.select(this).select("rect").attr("x", 0);
});
}
function dragended(event, d) {
const me = d3.select(this).attr("pointer-events", "auto");
let xx = scaleBandInvert(x)(event.x + x.bandwidth() / 2);
let yy = scaleBandInvert(y)(event.y);
let overs = data.filter((t, i) => {
return (
(i % 3 === xx || (i % 3) + 1 === xx) &&
Math.floor(i / 3) === yy &&
t.name != me.data()[0].name
);
});
switchPos(me.data()[0], overs);
mutable rero = rero + 1;
}
const drag = d3
.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
return drag(g);
};
}