dc.js - 如何按唯一ID分组 [英] dc.js - how to group by unique id

查看:284
本文介绍了dc.js - 如何按唯一ID分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用dc.js和crossfilter从csv创建3个rowchart。前两个是straighforward,但第三个需要有一个维度设置为我的输入csv的多个列,所以我重新构建了数据,所以对于每一个必需的列(有值的存在),一个新的重复行是在新列下输入的列值创建的。

I'm using dc.js and crossfilter to create 3 rowcharts from a csv. The first two are straighforward but the third one needs to have a dimension set to multiple columns of my input csv, so i've restructured the data so that for every required column (with a value present) for that row, a new duplicate row is created with that columns value inputted under a new column. See below;

旧结构:

ID --- rowchart1 --- rowchart2 ---- rowchart3 ----- rowchart3 - --- rowchart3 -----

1

2

3

old structure:
ID --- rowchart1 --- rowchart2 ---- rowchart3 ----- rowchart3 ----- rowchart3 -----
1
2
3

新结构:

ID --- rowchart1 --- rowchart2 ---- newRowchart3

1

2

2

2

3

3

new structure:
ID --- rowchart1 --- rowchart2 ---- newRowchart3
1
2
2
2
3
3

这一切都很好,因为我可以将行图设置为新建立的列,但我有麻烦获得原始计数显示在其他两个。有效地,我在其他行图表中获得重复的计数。

是否有一种方法按唯一的ID分组,以便它只计算那些唯一的ID而不是每一行计数?

很感谢

It all works fine as i'm able to set the rowchart to this new constructed column but i'm having trouble getting the original counts to be displayed in the other two. Effectively i'm getting duplicate counts in those other row charts.
Is there a way to group by a unique id so it only counts those unique ids instead of every row count?
many thanks

推荐答案

请参阅:有没有办法让crossfilter将数组元素作为单独的记录而不是处理整个数组作为单键?

您的示例是一个稍微简单的示例,因为您没有rowchart3值的数组,但解决方案仍然可以为你工作。

Your example is a slightly simpler example because you don't have an array for "rowchart3" values but the solution could still work for you.

答案包括一个jsfiddle,演示如何手动管理图表中的类别。您需要更改

The answer includes a jsfiddle that demonstrates how to manually manage the categories in the chart. You would need to change

function reduceAdd(p, v) {
   if (v.topics[0] === "") return p;    // skip empty values
   v.topics.forEach (function(val, idx) {
      p[val] = (p[val] || 0) + 1; //increment counts
   });
   return p;
}

function reduceAdd(p, v) {
   if(v.rowhart3a != "")
      p[v.rowchart3a] = (p[v.rowchart3a] || 0) + 1; //increment counts

   if(v.rowhart3b != "")
      p[v.rowchart3b] = (p[v.rowchart3b] || 0) + 1; //increment counts

   if(v.rowhart3c != "")
      p[v.rowchart3c] = (p[v.rowchart3c] || 0) + 1; //increment counts

   return p;
}

我希望这有助于。
-DJ

I hope this helps. -DJ

这篇关于dc.js - 如何按唯一ID分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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