无法以新数据格式绘制图表 [英] Unable to plot chart with new data format

查看:83
本文介绍了无法以新数据格式绘制图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在转换我现有的图表以支持新的数据格式.

I am converting my existing chart to support new data format.

X轴的旧数据格式:使用纪元格式的日期,例如1356912000000(数字)
X轴的新数据格式:使用常规格式的日期,例如"2012-12-31"(字符串)

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)

Y轴的格式相同(数字).

Format for Y-axis in the same (Number).

按旧数据格式绘制的图表: http://jsfiddle.net/rdesai/vfe2B/85/
根据新数据格式的图表: http://jsfiddle.net/rdesai/vfe2B/84/

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.

我该如何解决?

推荐答案

请阅读 D3关于时间格式的页面

在您的情况下,您只需要将格式为"2012-12-31"的字符串转换为日期即可:

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)

编辑:在此处找到有效的小提琴 http://jsfiddle.net/vfe2B/94/

find here a working fiddle http://jsfiddle.net/vfe2B/94/

错误是在创建图表时,您需要使用这些日期解析器将字符串转换为日期.函数.tickformat仅控制如何在轴上表示数据,但是像这样创建图表时,需要使用日期填充"该图表:

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]) })

我在jsfiddle中更改了tickformat,以便您可以更好地理解它们之间的区别.

I changed the tickformat in my jsfiddle so you can understand better the difference.

这篇关于无法以新数据格式绘制图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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