Search code examples
dc.jstimeserieschart

dc.js time series line chart


I'm plotting a time series line chart which takes time on x-axis, other resp values on y-axis. I need help doing that. The graph is not plotted with the following code. It shows only the lines(axes) and labels but no graph nor the values on axes.

data = [{"date": "31-03-2015 11:05:36", "snr": 3.2}, {"date": "31-03-2015 11:05:36", "snr": 4.9},...{"date": "31-03-2015 11:05:36", snr: 5.0}];

var parseDate = d3.time.format("%d-%m-%Y %H:%M:%S").parse;
        data.forEach(function(d) {
                     d.date = parseDate(d.date);
                     });

var dimByTime = cf.dimension(function(d) { return d.date;});
        var groupForSNR = dimByTime.groupAll();
        groupForSNR.all = function() { return 1; };

var snrAP = dc.lineChart("#snrAP");
        snrAP
        .dimension(dimByTime)
        .group(groupForSNR).valueAccessor(function(d) { return d.value.snr; })
        .x(d3.time.scale())
        .xUnits(d3.time.day);
dc.renderAll();

I want to show snr on y-axis and the corresponding time on x-axis.


Solution

  • Well, I've fixed it myself. May not be the optimum solution, but it still serves my purpose. Here is the code with modification...

    var dimByTime = cf.dimension(function(d) { return d3.time.minute(d.date);});
            var groupForSNR = dimByTime.group().reduce(
                                                       function reduceAdd(p, v) { return v.snr; },
                                                       function reduceRemove(p, v) { return v.snr; },
                                                       function reduceInitial() { return 0; });
            groupForSNR.dispose();
    
    var snrAP = dc.lineChart("#snrAP");
            snrAP
            .dimension(dimByTime)
            .group(groupForSNR)
            .x(d3.time.scale().domain([new Date(2015, 02, 21), new Date()]))
            .xUnits(d3.time.minute)
            .title(function(d){ return "Time: " + d.data.key.toDateString() + "\nS/N Ratio: " + numFormat(d.data.value); })
            .xAxis().ticks(5);