The full code is visible and modifiable on observablehq
The relevant parts:
const columns = svg.selectAll("g")
.data(nested_city)
.enter()
.append("g")
.attr("city", function(d) { return d.key ;})
.attr("x", function(d) { return x(d.key) ;})
.attr("y", "0")
.attr("transform", function(d) { return "translate(" + x(d.key) + ",0)" ;})
.attr("class", "column")
.on("mouseover", function(d) {
d3.select(this)
.style("cursor","pointer");
})
.on("mouseout", function(d){
d3.select(this)
.style("cursor","default");
})
.call(d3.drag()
.subject(function() {
var t = d3.select(this);
var tr = getTranslation(t.attr("transform"));
return {x: t.attr("x") + tr[0],
y: t.attr("y") + tr[1]};
})
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
and the drag handlers:
function dragstarted(d) {
d3.select(this).raise().classed("active", true);
}
function dragged(d) {
d3.select(this).attr("transform", function(d,i){
var coordinates = d3.mouse(this);
var mx = coordinates[0]
console.log( coordinates[0])
return "translate(" + [mx,0] + ")"})
}
function dragended(d) {
d3.select(this).classed("active", false);
}
I'm trying to adapt the new drag and drop with translation (found here on SO) for d3 v4 version to my example on observableHQ, without success.
I have a very strange flickering behavior when i drag any of the two column (alternate between jump with old x values and new x values).
If you have any idea why ?