如何将直流叠加实例卷成单条? [英] how to roll up dc stacked-bar example into single bar?

查看:124
本文介绍了如何将直流叠加实例卷成单条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://dc-js.github.io /dc.js/examples/stacked-bar.html 我想将这种数据集转换为单条柱形图。我改变了

"http://dc-js.github.io/dc.js/examples/stacked-bar.html" I want to roll this kind of dataset into a single bar stacked-bar chart. I changed the

 speedSumGroup       = runDimension.group().reduce(function(p, v) {
                    p[v.Expt] = (p[v.Expt] || 0) + v.Speed;
                    return p;
                }, function(p, v) {
                    p[v.Expt] = (p[v.Expt] || 0) - v.Speed;
                    return p;
                }, function() {
                    return {};
                });

speedSumGroup2 = runDimension.groupAll v){

,并检查该组显示的数据累积到单个预期。

and inspecting the group shows the data rolled up to the single expected.

Object {1: 18180, 2: 17120, 3: 16900, 4: 16410, 5: 16630}

但是,我使用.value()检查数据,而原始数据需要一个all(),这个组不能使用图表

However I inspect the data with .value() whereas the original needs an all(), and this group does not work with the chart

chart
                .width(768)
                .height(480)
                .x(d3.scale.linear().domain([1,21]))
                .margins({left: 50, top: 20, right: 10, bottom: 20})
                .brushOn(false)
                .clipPadding(10)
                .title(function(d) {
                    return d.key + '[' + this.layer + ']: ' + d.value[this.layer];
                })
                .yAxisLabel("This is the Y Axis!")
                .dimension(runDimension)
                .group(speedSumGroup2, "1", sel_stack('1'))
                .renderLabel(true);

给出a.group.all不是函数错误。

giving "a.group.all is not a function" error. Is the result of a dimension.groupAll() a standard group that can be plugged into the chart?

推荐答案

好的问题 - 它是一个可以插入到图表中的标准组从来没有清楚groupAll是什么样的对象。

Good question - it's never been clear what kind of object a groupAll is.

正如您所推测的,dc.js图表​​需要一个常规组,而不是一组。这里有一个转换函数可以使用:

As you've surmised, dc.js charts expect a "regular" group, not a groupAll. Here's a conversion function you could use:

function regularize_groupAll(groupAll) {
    return {
        all: function() {
            return [{key: 'all', value: groupAll.value()}];
        }
    };
}

使用方法如下:

var speedSumReg2 = regularize_groupAll(speedSumGroup2);
...
chart
    .group(speedSumReg2, "1", sel_stack('1'))
    .stack(speedSumReg2, "2", sel_stack('2'))
...

这篇关于如何将直流叠加实例卷成单条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆