简体   繁体   English

d3数据缩放问题

[英]d3 data scaling issue

After a while I finally figured how to scale data in d3 linear. 一段时间后,我终于想出了如何在d3 linear中缩放数据。 But if you look at the following screenshot you might notice that numbers do not seem to scale appropriately: 但是,如果您看下面的屏幕截图,您可能会注意到数字似乎无法适当缩放: 屏幕截图 So: eg 4.55 and 16.2 are almost same length, which they shouldn't be (also 4 should be much closer to the left as I scale between 0 and 565) 所以:例如4.55和16.2几乎是相同的长度,它们不应该是相同的(当我在0到565之间缩放时,4应该更靠近左侧)

so this is how I create the linear scale: 所以这就是我创建线性比例的方法:

var scaledData = d3.scale.linear()
                      .domain(d3.extent(data, function(d){return d.Betrag;})) 
                      .range([0, width-35]);

and this is how I draw the bars: 这就是我画条的方式:

var bar = chart.selectAll("g")
      .data(data)
    .enter().append("g")
      .attr("transform", function(d, i) { return "translate(0," + i * barHeight + ")"; });

  bar.append("rect")
    .attr("width", function(d) { return scaledData(Math.abs(Math.round(d.Betrag))); })
    .attr("height", barHeight - 1);

  bar.append("text")
    .attr("x",function(d) { return scaledData(Math.abs(Math.round(d.Betrag)))+3; })
    .attr("y", barHeight / 2)
    .attr("dy", ".35em")
    .text(function(d) { return Math.abs(d.Betrag); });
  });

I have tried around different things with this part scaledData(Math.abs(Math.round(d.Betrag))) but no matter what I do it doesn't do what I think it should... Perhaps as a newbie I still don't understand the scales thing completely... What I figure is that the problem comes from how I set the domain. 我已经使用scaledData(Math.abs(Math.round(d.Betrag)))尝试了不同的事情,但是无论我做什么,都没有做我认为应该做的事情……也许作为新手,我仍然不能完全理解秤的东西...我想的是问题出在我如何设置域。 if I replace the d3.extend function by [0,2000] it's all good... 如果我用[0,2000]替换d3.extend函数,那一切都很好...

You'll have to show your data to know for sure, but I think you have negative values in your data, so your domain looks like it goes from about [-600, 1800]. 您必须先确定要知道的数据,但是我认为您的数据中存在负值,因此您的域看起来像是从[-600,1800]开始。 But when you calculate your width you first take the absolute value of your data, so your lowest possible value is 0. The solution is in your d3.extent accessor function, to evaluate the absolute value of your data (if that's actually what you want). 但是,当您计算宽度时,首先要获取数据的绝对值,因此最低的可能值为0。解决方案是在d3.extent访问器函数中,用于评估数据的绝对值(如果这实际上是您想要的) )。

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

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