用setInterval设置x.domain [英] Setting x.domain with setInterval

查看:253
本文介绍了用setInterval设置x.domain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次修改这个问题,因为解决方案不能在新版本的库d3(3.0.3)中工作,所以我再来请求一些帮助会非常感激。

modify this question again, because the solution does not work in the new version of the library d3 (3.0.3), so I come to ask for some help again would be very grateful.

下面是库d3(3.0.3)的新代码:

Here is the new code with the library d3 (3.0.3):

https://gist.github.com/4495104
http://bl.ocks.org/4495104/e7a7589098140dff36df7ab2a824d71072bc3be4

https://gist.github.com/4495104 http://bl.ocks.org/4495104/e7a7589098140dff36df7ab2a824d71072bc3be4

根据我的工作,错误应该在行491。

According to what I've worked, the error should be in the line "491".

// Reset the domain relative to the current zoom offsets.
    x.domain(x0.range().map(function(x) {
      return (x - translate[0]) / scale; 
    }).map(x0.invert));

我们正在改变 x.domain 第二个是 setInterval ,因为我们想要生成背景移动的外观,但每次运行一个事件 d3.behavior.zoom() (缩放或平移) x.domain 自动切换到初始值。在下一个链接中,您可以查看问题。

We are changing the x.domain every second with setInterval, because we want to generate the appearance that the background is moving, but every time you run an event d3.behavior.zoom() (zoom or panning) the x.domain automatically switches to the initial values​​. In the next link you can review the problem.

http://jsfiddle.net/cristian540/Uy6bW/11/

推荐答案

如果您更改秤的域通过编程方式,您需要将缩放重新分配给缩放行为。这是因为缩放行为在内部存储了一个克隆的缩放比例,用于为新比例计算新域并转换偏移量。因此,如果您不重新分配比例,它会继续使用(旧的)克隆量表。

If you change the scale's domain programmatically, you need to reassign the scale to the zoom behaviour. This is because the zoom behaviour stores a clone of the scale internally for computing the new domain for new scale and translate offsets. So if you don't reassign the scale, it continues to use the (old) cloned scale.

我更新了文档以包含此注释。

I've updated the documentation to include a note about this.

还有一个额外的复杂性,即缩放的x和y刻度应该代表缩放比例= 1的域。因此,您实际上需要在缩放比例= 1时保留您自己的当前比例的克隆,并将偏移应用于克隆和当前比例。

There is an additional complication, which is that the zoom's x- and y-scales should represent the domains at zoom scale=1. So you actually need to keep your own clone of the current scale(s) at zoom scale=1 and apply the offset to both the clones and the current scales.

然后,您可能还需要反转当前的缩放平移和缩放,具体取决于您需要的确切行为。

Then, you may also need to invert the current zoom translate and scale, depending on the exact behaviour that you need.

所以它应该看起来像:

var x0 = d3.scale.linear().domain(…).range(…), // initial scale
    x = x0.copy(); // copy to use for axes and zoom behaviour

setInterval(function() {
  var translate = zoom.translate(),
      scale = zoom.scale(),
      xd = x0.domain(),
      dx = 1;

  // Set a new x-domain: offset by dx.
  x0.domain([xd[0] += dx, xd[1] += dx]);

  // Set the zoom x-domain (this resets the domain at zoom scale=1).
  zoom.x(x.domain(xd));

  // Reset the domain relative to the current zoom offsets.
  x.domain(x0.range().map(function(x) {
    return (x - translate[0]) / scale;
  }).map(x0.invert));
}, 1e3);

示例

这篇关于用setInterval设置x.domain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆