d3js图的时间解析和缩放 [英] Time parsing and scaling for d3js graphs

查看:124
本文介绍了d3js图的时间解析和缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码,它绘制了一个d3js多图表对象,其值是时间:年份值以Year = 2011的格式给出... 使用这种时间格式,代码可以正常工作,但是一旦我想更改这种格式的数据时间Year =(Y-M-D H:M:S)

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=(Y-M-D 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"}];

我解析了数据年份:

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

但是它不起作用,我该如何读取格式为Year(Y-M-D H:M:S)的数据Year

But it is not working, how can I read data Year in this format:(Y-M-D 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]);
    }
}

在工作jsfiddle的情况下删除了转换代码此处.

Working jsfiddle with the conversion code removed here.

这篇关于d3js图的时间解析和缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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