问题
I am converting my existing chart to support new data format.
Old data format for X-axis: using dates in epoch format like 1356912000000
(Number)
New data format for X-axis: using dates in regular format like "2012-12-31"
(String)
Format for Y-axis in the same (Number).
Chart as per old data format: http://jsfiddle.net/rdesai/vfe2B/85/
Chart as per new data format: http://jsfiddle.net/rdesai/vfe2B/84/
For old format, I was converting the epoch date to regular date and feeding it to the ticks. For new format, not sure what I need to do.
How do I fix this?
回答1:
Please read the D3 page on time formatting
In your case you just need to convert those strings in the format "2012-12-31" to dates:
> var dateFormat = d3.time.format("%Y-%m-%d");
> dateFormat.parse("2012-12-31");
Mon Dec 31 2012 00:00:00 GMT+0100 (CET)
Edit: find here a working fiddle http://jsfiddle.net/vfe2B/94/
The error was that you need to use these date parser to convert the strings to dates when you are creating the chart. The function .tickformat just controls how you represent the data on the axis, but you need to "feed" the chart with dates when you create it like this:
.x(function(d) { return dateFormat.parse(d[0]) })
I changed the tickformat in my jsfiddle so you can understand better the difference.
来源:https://stackoverflow.com/questions/24673333/unable-to-plot-chart-with-new-data-format