简体   繁体   English

d3js图形的时间解析和缩放

[英]Time parsing and scaling for d3js graphs

I have this code that draws a d3js multichart object, the value time:Years values are given in this format Year=2011... with this time format the code works fine but once I wanted to change the data time in this format Year=(YMD H:M:S) 我有这段代码绘制了一个d3js多图表对象,值time:Years值以Year = 2011的格式给出...使用这种时间格式,代码可以正常工作,但是一旦我想以Year = 2011的格式更改数据时间(YMD H:M:S)

var data = [{"Year":"2011-10-01 20:46:04","Happy":"63.4","Sad":"42.7","Angry":"12.2","Surprised":"44.2"},
{"Year":"2012-10-01 17:02:04","Happy":"75.4","Sad":"32.7","Angry":"78.2","Surprised":"82.2"},
{"Year":"2013-10-01 19:55:44","Happy":"73.4","Sad":"20.7","Angry":"92.2","Surprised":"75.4"}];

I parsed the data Year: 我解析了数据年份:

 var parseDate=d3.time.format("%Y-%m-%d %H:%M:%S").parse; //line 13
 d.Year=parseDate(d.Year);  //and then line 53

But it is not working, how can I read data Year in this format:(YMD H:M:S) 但是它不起作用,我该如何读取以下格式的数据Year:(YMD H:M:S)

The problem in your example is that you're converting all properties of the objects to numbers. 您的示例中的问题在于,您要将对象的所有属性都转换为数字。 This works if the date is just the year, but not if it is a string that needs to be parsed. 如果日期只是年份,则此方法有效,但如果它是需要解析的字符串,则无效。 In your case, you don't need to do this conversion anyway because you can give the numbers as numbers rather than strings directly. 在您的情况下,您无需进行任何转换,因为您可以直接将数字作为数字而不是字符串。 That is, you don't need 也就是说,您不需要

for(var prop in d) {
    if(d.hasOwnProperty(prop)) {
        d[prop] = parseFloat(d[prop]);
    }
}

Working jsfiddle with the conversion code removed here . 使用jsfiddle并在此处删除了转换代码。

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

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