简体   繁体   English

dc.js折线图分组后的访问值

[英]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 我的代码是这样的,我想做的是我想对swn_pospol求平均值并将其用作Y轴.dateDimGroup中得到的是键和值。现在如何获取avg_pospol并将其用作Y-轴

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? 我想要的是折线图的X轴上的日期和Y轴上的avg_pospol。如何实现?

Seems to me that adding on a valueAccessor function should get you what you want. 在我看来,添加valueAccessor函数应该可以为您提供所需的东西。

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. 未经测试,但至少可以使您走上正确的道路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM