dc.js: Reducing rows in data table

一个人想着一个人 提交于 2019-12-11 02:07:02

问题


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!


回答1:


Use the following code;

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



来源:https://stackoverflow.com/questions/31275308/dc-js-reducing-rows-in-data-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!