[英]Using d3, how can I create a histogram or bar plot where the last bar is the count of all values greater than some number?
Code link: https://plnkr.co/edit/jLkoMxdzArBBULHF80nb?p=preview 代码链接: https : //plnkr.co/edit/jLkoMxdzArBBULHF80nb?p = preview
I have a data with some disperse values. 我有一些带有一些分散值的数据。 It ranges from 61 to 1.2m.
它的范围从61到1.2米。
How can I represent it in a Histogram in a way that makes sense? 我怎样才能以有意义的方式在直方图中表示它? Can I have the last bucket on d3 that is
> 2000
for instance? 我可以在d3上使用
> 2000
的最后一个存储桶吗?
Something like this (greater than 5 minutes): 这样的事情(超过5分钟):
First of all you will need to arrange your data (if you haven't yet), where you just need to create a variable with the > 2000
values. 首先 ,您需要安排您的数据(如果您还没有),您只需要创建一个
> 2000
值的变量。
This is the way I did it (I started d3
last week and I don't have any previous knowledge on JavaScript
, so there's probably a better way to do it): 这就是我这样做的方式(我上周开始使用
d3
并且我之前没有任何关于JavaScript
知识,所以可能有更好的方法):
var data = [];
for (var i = 0; i < oldData.length; ++i) {
if (oldData[i] >= 2000) {
data[i] = 2000;
}
else data[i] = oldData[i];
}
Next thing, is o set manually the ticks you want and the tickLabels that correspond to it: 接下来 ,手动设置您想要的刻度和与之对应的刻度线:
var ticks = [0,200,400,600,800,1000,1200,1400,1600,1800,2000];
var tickLabels = [0,200,400,600,800,1000,1200,1400,1600,1800,"> 2000"];
(notice that you can change to "1,200" and so on if you want the separator) (请注意,如果需要分隔符,可以更改为“1,200”等等)
And instead of calling the d3.axisBottom(x)
directly to your chart, I like to create a separate xAxis
variable, and set the ticks and tickLabels to it: 而不是直接将
d3.axisBottom(x)
调用到您的图表,我想创建一个单独的xAxis
变量,并将ticks和tickLabels设置为:
var xAxis = d3.axisBottom(x)
.tickValues(ticks)
.tickFormat(function(d,i){ return tickLabels[i] });
Finally you call the xAxis
on your chart: 最后 ,在图表上调用
xAxis
:
chart.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.