dc.js - how to group by unique id

99封情书 提交于 2019-12-11 00:39:57

问题


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;

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

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

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


回答1:


Please see: Is there a way to tell crossfilter to treat elements of array as separate records instead of treating whole array as single key?

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.

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;
}

to something like

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;
}

I hope this helps. -DJ



来源:https://stackoverflow.com/questions/24791496/dc-js-how-to-group-by-unique-id

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