时间格式在dc.js LineChart中不起作用

时间:2015-04-14 20:57:55

标签: d3.js dc.js

我有以下h:m:s格式的时间解析代码

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

我有一个json文件,可以在不同的时间从不同的传感器读取。 样本数据是=

[
{"id":1,"time":"2015-03-29T20:32:24Z"},
{"id":2,"time":"2015-03-29T20:32:24Z"},
{"id":3,"time":"2015-03-29T20:32:24Z"},
{"id":1,"time":"2015-03-29T20:33:24Z"},
{"id":2,"time":"2015-03-29T20:33:24Z"},
{"id":3,"time":"2015-03-29T20:33:24Z"},
]

我将绘制一个dc.js行图,其中x轴的分钟时间和y轴的频率。我使用以下代码来执行此操作。但它回归的NaNs。

var freqchart= dc.lineChart("#chart1");
var countByTime=ndx.dimension(function (d) {return d.time; });
var freqbyTimeGroup = countByTime.group().reduceCount();
freqchart.width(400).height(200).transitionDuration(500)
    .dimension(countByTime).group(freqbyTimeGroup).elasticY(true).x(
                d3.time.scale().domain([d3.min(data,function(d){return d.time;}),
                                        d3.max(data,function(d){return d.time;})])).xUnits(d3.time.minutes).yAxisLabel("Frequency").xAxisLabel('Time').elasticX(true)

我该如何解决这个问题?这是jsfiddle无法正常工作

1 个答案:

答案 0 :(得分:1)

Hurray我得到了解决方案。这是代码

   var data=[
    {"id":20,"time":"2015-03-29T20:32:24Z","speed":20},
    {"id":21,"time":"2015-03-29T20:32:24Z","speed":15},
    {"id":22,"time":"2015-03-29T20:32:24Z","speed":16},
    {"id":23,"time":"2015-03-29T20:33:25Z","speed":14},
    {"id":20,"time":"2015-03-29T20:33:26Z","speed":20},
    {"id":21,"time":"2015-03-29T20:34:24Z","speed":10},
    {"id":22,"time":"2015-03-29T20:34:24Z","speed":15},
    {"id":23,"time":"2015-03-29T20:35:24Z","speed":15},
]
    // The datset is much larger with many detector. This is sample
        var dateformat=d3.time.format("%H:%M:%S").parse;
        var ISO8601format=d3.time.format("%Y-%m-%dT%H:%M:%SZ");
        var hoursandminsformat=d3.time.format("%H:%M:%S"); 
    data.forEach(function(d) {
        d.time=d.time.substring(11,19);
        d.time=dateformat(d.time);
    });

这是工作jsfiddle