本文介绍了dc.js:减少数据表中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 data.table 对象,它的输出如下:

So I have a data.table object that is being outputed like this:

gender  hair-color  pets  group1.totals   group2.totals   group3.totals
  F       black      Y       10               0                 0
  F       black      Y        0               7                 0
  F       black      Y        0               0                 8

如何折叠它,使其像这样?

How do I collapse it so that it will be like this?

  gender  hair-color  pets  group1.totals   group2.totals   group3.totals
    F       black      Y         10               7                 8

我尝试缩小尺寸,但似乎不起作用。我的代码如下:

I have tried reducing the dimensions but it doesn't seem to work. My code is below:

ndx = crossfilter(data);
dataTable = dc.dataTable('#data-table');
var tableDim = ndx.dimension(function(d) {
    return d.gender + "/" + d.hair-color + "/" + d.pets;
      });

dataTable
   .width(400)
   .height(800)
   .dimension(tableDim)
   .group(function(d){
     return "Data Counts";
    }),
   .columns([
      function(d) {
        return d.gender;
       },
      function(d) {
        return d.hair-color;
       },
      function(d) {
        return d.pets;
      }
      function(d) {
        if (d.group == 1) return d.totals;
          else return 0;
      },
      function(d) {
        if (d.group == 2) return d.totals;
          else return 0;
      },
      function(d) {
        if (d.group == 3) return d.totals;
          else return 0;

基本上我知道我必须减少并分组我的数据,但我无法具体找到要实现的目标。谢谢,任何帮助都会很棒!

Essentially I know that I have to reduce and group my data but I can't find specifically what I have to do in order to achieve. Any help would be great, thanks!

推荐答案

使用以下代码;

var ndx=crossfilter(data);
var dimension=ndx.dimension(function(d){return d.hair-color});
var dataByHairColor=dimension.group().reduceCount();

我希望它能解决问题。如果您想要其他过滤选项,请使用该选项。我用染发剂。让我知道您是否仍然遇到问题

I hope it'll solve the problem. If you want other filtering option use that. I used hair color. Let me know if you are still facing issues

这篇关于dc.js:减少数据表中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 15:40