D3直方图 - 基于日期 [英] D3 Histogram - Date Based

查看:995
本文介绍了D3直方图 - 基于日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期数组

["01/01/2001", "01/01/2001", "03/01/2001", "01/04/2001", "01/05/2001", "02/05/2001", "01/07/2001", "01/07/2001", "01/07/2001", "01/10/2001"]

有些是重复的,

我想要做的是产生一个直方图(或条形图),显示计数

What I want to do is produce a histogram (or bar chart) that shows counts of "dates" in arbitrary # of buckets (like 20 buckets).

如果存储桶中没有日期,则显示为零,并且包含日期的存储桶总数为0

Where there are no dates in a bucket it shows zero and a total for those buckets that contain dates.

基本上就像这个例子:
http://bl.ocks.org/mbostock/3048450

Basically like this example: http://bl.ocks.org/mbostock/3048450

但我不需要显示随机的秒,我需要dates。

But instead of showing random "seconds" I need "dates".

喜欢:

我想我正在将X缩放域更改为数据中的日期范围。

I guess I'm looking to change X scale domain to the range of dates in my data.

推荐答案

因此,解决方案是在注释中建议的@LarsKotthoff。

So the solution for this is as @LarsKotthoff suggested in the comments.

var x = d3.time.scale().domain([firstDate, lastDate]).range([0, width]);

var data = d3.layout.histogram()
        .bins(x.ticks(bins))  
        (values);

var formatDate = d3.time.format("%d/%m/%y");
var xAxis = d3.svg.axis()
            .scale(x)
            .orient("bottom")
            .tickFormat(formatDate);

这篇关于D3直方图 - 基于日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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