D3解析日期“2013-07-11T00:00:00”用于折线图

时间:2014-07-14 20:59:56

标签: javascript parsing d3.js

如何解析这样的日期数组:" 2013-07-11T00:00:00",可用于折线图?该图表将延续一整年。

我正在尝试创建一个动态图形,就像在"绘制图表"关于this page的部分。

如果我能有这样的刻度标记(4月和13号),那将是很棒的。

我目前在JavaScript中使用以下功能:

var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%SZ").parse;

但它没有用。

1 个答案:

答案 0 :(得分:1)

你有一个尾随" Z"以您的格式说明符,它使您的解析器返回null表示您提供给它的日期。这是一个有效的例子:

var parse = d3.time.format("%Y-%m-%dT%H:%M:%S").parse;
parse('2013-07-11T00:00:00'); // Thu Jul 11 2013 00:00:00 GMT+0200 (CEST)

您可以使用d3 time scales显示带有适当刻度线的基于时间的轴。

修改

我想我误解了你的问题......这是另一个(希望)解释时间尺度和日期格式的用法:

var scale = d3.time.scale(),
    axis = d3.svg.axis(),
    // Assuming you have your data as strings:
    data = ['2013-07-11T00:00:00', '2013-08-11T00:00:00', ...],
    range = [0, 500], // size of chart
    domain = d3.extent(data, function (d) { // generate the data extent (min,max)
        return new Date(d); // use Date instances for values
    });

// Configure the scale with domain and range:
scale.domain(domain).range(range);

// Use the above scale for the axis:    
axis.scale(scale)
    // Print the dates in "Mmm dd" format:
    .tickFormat(d3.time.format('%b %d'));

// Render the axis in your svg node:
svg.call(axis);