user3614894
user3614894

Reputation: 65

dc.js Access value after grouping for line chart

My code is like this.What I am trying to do is that I want to average the swn_pospol and use it as a Y-axis.What I get in dateDimGroup is key and values.Now how to get avg_pospol and use it as Y-axis

var dateDim = ndx.dimension(function(d) {return d.fields.date;});

var dateDimGroup = dateDim.group().reduce(
    //add
    function(p,v){
        ++p.count;
        p.sum_pospol += v.fields.swn_pospol;
        p.avg_pospol = p.sum_pospol / p.count;
        return p;
    },
    //remove
    function(p,v){
         --p.count;
        p.sum_pospol -= v.fields.swn_pospol;
        p.avg_pospol = p.sum_pospol / p.count;
        return p;
    },
    //init
    function(p,v){
        return {count:0, sum_pospol: 0,  avg_pospol: 0};
    }
);

var lineChart  = dc.lineChart("#chart-line"); 
lineChart 
.width(1000).height(200)
.dimension(dateDim)
.group(cityDimensionGroup,"Positive")
.x(d3.time.scale().domain([minDate,maxDate])); 

dc.renderAll(); 

What I want is date on X-axis and avg_pospol on Y-axis of line chart.How to achieve that?

Upvotes: 0

Views: 498

Answers (1)

Ethan Jewett
Ethan Jewett

Reputation: 6010

Seems to me that adding on a valueAccessor function should get you what you want.

lineChart 
.width(1000).height(200)
.dimension(dateDim)
.group(cityDimensionGroup,"Positive")
.valueAccessor(function(p) { return p.value.avg_pospol; })
.x(d3.time.scale().domain([minDate,maxDate]));

Untested, but at least that hopefully sets you on the right path.

Upvotes: 1

Related Questions