gpt4 book ai didi

javascript - 带有 d3 v4 拖动功能的闪烁翻译

转载 作者:行者123 更新时间:2023-12-01 01:48:57 25 4
gpt4 key购买 nike

完整代码在 observablehq 上可见且可修改

相关部分:

  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));

和拖动处理程序:

      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);
}

我正在尝试将 d3 v4 版本的新拖放与翻译 ( found here on SO ) 适应我在 observableHQ 上的示例,但没有成功。

当我拖动两列中的任何一个时,我有一个非常奇怪的闪烁行为(在旧的 x 值和新的 x 值之间交替跳转)。

如果你知道为什么吗?

最佳答案

有一个d3.mouse(this) dragged() 中的声明功能。这将返回当前事件相对于指定容器 ( ref ) 的 x 和 y 坐标。您的容器mouse(<container>)this并表示拖动<g>元素。但应该是<svg> 。因此您应该使用d3.mouse(svg.node())相反。

但要小心,因为您还没有在 <g> 内获取偏移量考虑到(很难描述)。也许你必须在<g>内添加光标位置的坐标.

恕我直言,无论如何我更喜欢使用 d3.event超过d3.mouse example .

编辑:具有正确坐标的新拖动功能

// New: Coordinates inside <g>
var coordG = 0;

function dragstarted(d) {

// New: Read out coordinates inside <g>
coordG = d3.mouse(this)[0];
d3.select(this).raise().classed("active", true);
}

function dragged(d) {
d3.select(this).attr("transform", function(d,i){
var coordinates = d3.mouse(svg.node());
var mx = coordinates[0] - coordG;
console.log( coordG);
return "translate(" + [mx,0] + ")";
});
}

function dragended(d) {
d3.select(this).classed("active", false);
}

关于javascript - 带有 d3 v4 拖动功能的闪烁翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51743802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com