简体   繁体   English

使用序数刻度创建d3 v4直方图

[英]Creating a d3 v4 histogram with an ordinal scale

This is my piece of code, I am unable to fetch the data in bins. 这是我的一段代码,我无法获取bin中的数据。 I am new to d3 and I think there is some problem with the ordinal or scaleband scale. 我是d3的新手,我认为序数或规模带有一些问题。 Can anyone help. 谁能帮忙。

    dom = (data.map(function(d) { return d[attri]; }));

    x = d3.scaleBand()      
   .range([0,width])
   .paddingInner([0.1])
   .paddingOuter([0.3])
   .align([0.5]);

   x.domain(dom);

   bins = d3.histogram()
  .domain(x.domain)
  (mydata);

There are two problems here. 这里有两个问题。

First problem, domain as a getter (and as a setter as well) has parentheses: 第一个问题, domain作为getter(以及作为setter)有括号:

x.domain()

But, unfortunately, that doesn't fix your code, you cannot do: 但是,遗憾的是,这不能修复您的代码,您无法做到:

bins.domain(x.domain())

... for your histogram domain with the scale you have. ...对于你的直方图域,你有你的规模。 The reason is the second problem: 原因是第二个问题:

In a d3 histogram, domain is an array with two values, the minimum and the maximum value. 在d3直方图中, domain是具有两个值(最小值和最大值)的数组。 According to the documentation: 根据文件:

If domain is specified, sets the domain accessor to the specified function or array and returns this histogram generator [...] The histogram domain is defined as an array [min, max], where min is the minimum observable value and max is the maximum observable value; 如果指定了domain,则将域访问器设置为指定的函数或数组并返回此直方图生成器[...]直方图域定义为数组[min,max],其中min是最小可观察值,max是最大可观察值; both values are inclusive. 两个值都是包容性的。 Any value outside of this domain will be ignored when the histogram is generated. 生成直方图时,将忽略此域之外的任何值。 ( source ) 来源

However, right now, your x.domain() is an array with several values (from data.map ). 但是,现在, x.domain()是一个包含多个值的数组(来自data.map )。 You could do x.domain() to set your histogram domain if your scale were a linear one. 如果您的比例是线性的,您可以使用x.domain()来设置直方图域。

So, set your histogram domain properly. 因此,请正确设置直方图域。

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

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