简体   繁体   English

d3.js 在重新缩放后获得新的域值

[英]d3.js get new domain values after rescale

I try to get the new axes values after rescaling my axes.在重新缩放我的轴后,我尝试获取新的轴值。

I tried to get them from y.domain() as answered here but the values returned are the initial values.我试图从y.domain()获取它们作为回答here但返回的值是初始值。

So what am i doing wrong here?那么我在这里做错了什么?

Relevant code:相关代码:

function zoomed() {

  for(let i = 0; i < ys.length; i++){
    yAxes[i].call(
      d3.axisLeft(ys[i])
        .scale(d3.event.transform.rescaleY(ys[i]))
    )
  }

  //Now I need the new values

  console.log(ys[0].domain()) //returns [0, 10] which are my initial domain values for this axis.

}

After zooming, the values i have on screen are bigger than 0 and smaller than 10. So how do i get my domains new min and max values?缩放后,我在屏幕上的值大于 0 且小于 10。那么如何让我的域获得新的最小值和最大值?

Here is a fiddle with an older version of my code, which has the exact same behavior (see zoomed() function).是我的旧版本代码的小提琴,它具有完全相同的行为(请参阅zoomed()函数)。

Maybe I misunderstand what the domain is and how it works, what I need are the new max and min values of my axis.也许我误解了域是什么以及它是如何工作的,我需要的是轴的新最大值和最小值。

basing my answer on this code from the fiddle:我的答案基于小提琴中的这段代码:

  function zoomed() {
    let center = getBoundingBoxCenterX(rect);
    let chartsWidth = (2 * center) * d3.event.transform.k;
    d3.event.transform.x = center - chartsWidth / 2;
    xAxis.call(d3.axisBottom(x).scale(d3.event.transform.rescaleX(x)));
    yAxis.call(d3.axisLeft(y).scale(d3.event.transform.rescaleY(y)));
    zAxis.call(d3.axisRight(z).scale(d3.event.transform.rescaleY(z)));

    //here i need the new values
    console.log(y.domain()) //returns [0,6]

Edit编辑

You can get the axis scale this way:您可以通过这种方式获得轴比例:

let leftAxis = d3.axisLeft(y).scale(d3.event.transform.rescaleY(y));
console.log('new domain values', leftAxis.scale().domain())

I've got a version of the fiddle with this working: https://jsfiddle.net/aj5sLz5u/我有这个工作的小提琴版本: https : //jsfiddle.net/aj5sLz5u/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM