简体   繁体   English

dc.js 条形图 - 通过多个数据条目的总和计算条形大小

[英]dc.js bar chart - calculate bar size by sum of multiple data entries

Sorry for the title, it is hard to sumarize what I am trying to achieve in one sentence.对不起,标题很难用一句话概括我想要实现的目标。

I have a bar chart that uses a crossfilter that is also used by 6 other charts.我有一个使用交叉过滤器的条形图,其他 6 个图表也使用该交叉过滤器。 My data looks as follows (note: this is only a small sample, there are more keys in each object)我的数据如下(注意:这只是一个小样本,每个对象中有更多的键)

var data = [
    { clientime : '20210603000000', calendarweek : '22', x :  9, y :  4 },
    { clientime : '20210603000007', calendarweek : '22', x :  5, y :  5 },
    { clientime : '20210607000000', calendarweek : '23', x :  1, y :  2 },
    { clientime : '20210607000007', calendarweek : '23', x :  5, y :  5 },
    { clientime : '20210615000000', calendarweek : '24', x : 10, y : 20 },
    { clientime : '20210615000011', calendarweek : '24', x :  5, y :  5 },
];

The category for each bar is the calendarweek and I wan the the value to be the sum of all x devided by the sum of all y.每个条形的类别是日历周,我希望该值是所有 x 的总和除以所有 y 的总和。

According to the above sample I would like to see 3 bars.根据上面的示例,我想看到 3 个条形图。

Bar '22' should have the value `sum(9,5)/sum(4,5)` = 1.556
Bar '23' should have the value `sum(1,5)/sum(2,5)` = 0.857
Bar '24' should have the value `sum(10,5)/sum(20,5)` = 0.6

My first intention wasto use the reduce function where I would add or remove the sums in a custom dictionary.我的第一个意图是使用reduce函数,在其中添加或删除自定义字典中的总和。

var x_key = "calendarweek";
var dim = crossfilter.dimension( d => d[x_key] );
var grp = dim.group().reduce(
    ( p, v ) => {
        p.sum_x += v.x;
        p.sum_y += v.y;
        return p;
    },
    ( p, v ) => {
        p.sum_x -= v.x;
        p.sum_y -= v.y;
        return p;
    },
    () => ( { sum_x : 0, sum_y : 0 } )
);

var chart = dc.barChart( "#chart" );
chart
    .width( 490 )
    .height( 280 )
    .dimension( dim )
    .group( grp )
    .x( d3.scaleBand() )
    .xUnits( dc.units.ordinal )
    .elasticX( true )
    .elasticY( true )
    .controlsUseVisibility( true )
    .margins( {
        top    : 10, right  : 50, bottom : 20, left   : 40,
    } );

grp.all() does seem to look fine but from here on out I am not sure how to set the data correctly to chart. grp.all()看起来确实不错,但从现在开始我不确定如何将数据正确设置为图表。 Using the created group no bars are shown at all because I creted an object in the reduce function that dc.js does not understand.使用创建的组根本没有显示任何条,因为我在 reduce 函数中创建了一个 dc.js 不理解的对象。

团体

Additionally I would like to still be able to limit the bars to N entries.此外,我希望仍然能够将条形限制为 N 个条目。

Thanks to Gordon in the comments pointing me into the right direction I was able to solve my problem using the valueAccesor .感谢 Gordon 在评论中指出我正确的方向,我能够使用valueAccesor解决我的问题。

chart_mttrhist.valueAccessor( function( d ) {
    return ( d.value.sum_x / d.value.sum_y );
} );

Please note that the code in the question changed a little.请注意,问题中的代码略有变化。

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

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