简体   繁体   中英

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)

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)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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