简体   繁体   English

D3时间和日期直方图

[英]D3 time and date histogram

I'm attempting to make a histogram using primarily time and date data, provided in a json file (along with other info) in this format: 2014-03-01 00:18:00. 我正在尝试主要使用json文件(以及其他信息)中提供的以下格式的时间和日期数据制作直方图:2014-03-01 00:18:00。 I've looked at http://bl.ocks.org/mbostock/3048450 as an example, but I haven't managed to crack it. 我以http://bl.ocks.org/mbostock/3048450为例,但是我还没有破解它。 The key part seems to be this: 关键部分似乎是这样的:

    var data = d3.layout.histogram()
        .bins(x.ticks(20))
        (dataset.timestamp);

When I view my code in the browser it gives "TypeError: data is undefined", and refers to d3.v3.js line 5878. 当我在浏览器中查看代码时,它显示“ TypeError:数据未定义”,并指向d3.v3.js第5878行。

Assuming I fix that error, the next place it may stumble is the axis formatting: 假设我已修复该错误,则可能遇到的下一个问题是轴格式设置:

    var formatDate = d3.time.format("%y-%m-%d %h:%m:%s");

    var xAxis = d3.svg.axis()
        .scale(x)
        .orient("bottom")
        .tickFormat(formatDate);

Will the format syntax correspond correctly to my time and date format? 格式语法是否与我的时间和日期格式正确对应?

I don't think that the histogram layout accepts non-numeric input (I could be wrong). 我不认为直方图布局接受非数字输入(我可能错了)。 One option would be to convert the dates into the number of milliseconds since 1970 by parsing and using getTime() : 一种选择是通过解析和使用getTime()将日期转换为自1970年以来的毫秒数:

var formatDate = d3.time.format("%Y-%m-%d %H:%M:%S");

var data = d3.layout.histogram()
    .bins(x.ticks(20))
    .value(function(d) {return formatDate.parse(d.timestamp).getTime()})
    (dataset);

You'll need to make sure that the x scale (as in x.ticks(20) ) has a domain based on the millisecond data. 您需要确保x缩放比例(如x.ticks(20) )具有基于毫秒数据的域。

So I figured it out, thanks to the helpful answers here. 所以我想通了,这要归功于这里的有用答案。 First off, the json data is acquired: 首先,获取json数据:

d3.json("inkmarch.json", function(error, json) {
        if (error) return console.warn(error);
        dataset = json; 

Then I specify a formatting variable specific to my date formatting: 然后,指定一个特定于日期格式的格式变量:

var formatDate = d3.time.format("%Y-%m-%d %H:%M:%S");

I map the re-formatted dates to another var: 我将重新格式化的日期映射到另一个变量:

mappedSet = (dataset.map(function(d) {return formatDate.parse(d.timestamp).getTime()}))

And finally I can make a proper histogram dataset. 最后,我可以制作一个合适的直方图数据集。

var data = d3.layout.histogram()
            .bins(x.ticks(31))
            .value(function(d) {return formatDate.parse(d.timestamp).getTime()})
            (dataset);

(31 ticks since this is monthly data from March). (因为这是3月份的月度数据,所以有31个滴答声)。 I did away with the d3.time.scale since they were integers anyway, so my scales look like such: 我删除了d3.time.scale,因为无论如何它们都是整数,所以我的比例看起来像这样:

var x = d3.scale.linear()
        .domain([d3.min(mappedSet), d3.max(mappedSet)])
        .range([0, width]);
var y = d3.scale.linear()
        .domain([0, d3.max(data, function(d) { return d.y; })])
        .range([height, 0]);

(Still not sure what the function in the y domain does). (仍然不确定y域中的功能是什么)。 The svg and bar vars look exactly like the example I linked in my question post, as do the 'rect' and 'text' bars (except I changed width of rect and x position of the text to fixed values since the example formulas were giving hideous negative values). svg和bar var看起来与我在问题中链接的示例完全相同,“ rect”和“ text”条也是如此(除非我将rect的宽度和文本的x位置更改为固定值,因为示例公式给出了可怕的负值)。 Thanks again for the help, I'm sure I'll have more d3 questions in the near future. 再次感谢您的帮助,我相信不久的将来我还会有更多的d3问题。

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

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