简体   繁体   English

D3时间刻度,日期对象返回NaN

[英]D3 timescale with date object returning NaN

I have a Date object that I am passing to a D3 time scale: 我有一个Date对象,我传递给D3时间刻度:

var x = d3.time.scale()
    .range([0, main_width-axis_offset]);

x.domain(d3.extent(data, function(d) { return d.date; }));

Then later on... 然后......

var area = d3.svg.area()
    .x(function(d) { return x(d.date); })

d.date is in the following format: d.date采用以下格式:

d.date = new Date(d.year, d.month-1, d.day);

Mon Jan 20 2014 00:00:00 GMT-0500 (Eastern Standard Time) 

Here is a fiddle link showing the full code: http://jsfiddle.net/goodspeedj/Ds9E6/ 这是一个显示完整代码的小提琴链接: http//jsfiddle.net/goodspeedj/Ds9E6/

The docs state "Given a date x in the input domain, returns the corresponding value in the output range." 文档声明“给定输入域中的日期x,返回输出范围中的相应值”。

Is this expecting date in a different format? 这个期待日期是否有不同的格式? Thank you. 谢谢。

The problem is that you are getting the domains for x and y for data , but the real data is under data.results . 问题是您获取data x和y域,但实际数据位于data.results下。 If you change your code accordingly, everything works fine: 如果您相应地更改了代码,一切正常:

x.domain(d3.extent(data.result, function(d) { return d.date; }));
y.domain([0, d3.max(data.result, function(d) { return d.y0 + d.y; })]);

Complete jsfiddle here . 在这里完成jsfiddle。

You haven't specified the domain for the scale, only the range. 您尚未指定比例的域,仅指定范围。 Eg: 例如:

var x = d3.time.scale().range([0, main_width-axis_offset]).domain([minDate, maxDate]);

The scale requires range and domain, because it maps one to the other. 该比例需要范围和域,因为它将一个映射到另一个。 So you should have the date you want to map to 0 in the x axis in place of "minDate", and the date you want to map to whatever value main_width-axis_offset is in place of "maxDate". 因此,您应该将要在x轴上映射到0的日期替换为“minDate”,并且要映射到任何值的日期main_width-axis_offset代替“maxDate”。

In the API doc: https://github.com/mbostock/d3/wiki/Time-Scales#wiki-domain 在API文档中: https//github.com/mbostock/d3/wiki/Time-Scales#wiki-domain

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

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