我正在尝试为数据集属性组创建一个自定义的reduce函数,该函数将求和另一个属性的许多唯一值。
I am trying to create a custom reduce function for a dataset attribute group that would sum a number of unique values for another attribute.
例如,我的数据集看起来就像团队成员对项目执行的操作的列表一样:
For example, my dataset looks like a list of actions on projects by team members:
{ project:"Website Hosting", teamMember:"Sam", action:"email" }, { project:"Website Hosting", teamMember:"Sam", action:"phoneCall" }, { project:"Budjet", teamMember:"Joe", action:"email" }, { project:"Website Design", teamMember:"Joe", action:"design" }, { project:"Budget", teamMember:"Sam", action:"email" }因此,团队成员通过每行执行一项操作来处理可变数量的项目。我按团队成员确定维度,并希望通过项目(独特)的数量来减少维度。
So, team members work on a variable number of projects by performing one action per line. I have a dimension by team member, and would like to reduce it by the number of projects (uniques).
我尝试了以下操作(将项目存储在唯一数组中)而没有成功(对不起,这可能会伤害您的眼睛):
I tried the below (storing project in a uniques array) without success (sorry, this might hurt your eyes):
var teamMemberDimension = dataset.dimension(function(d) { return d.teamMember; }); var teamMemberDimensionGroup = teamMemberDimension.group().reduce( // add function(p,v) { if( p.projects.indexOf(v.project) == -1 ) { p.projects.push(v.project); p.projectsCount += 1; } return p; }, // remove function(p,v) { if( p.projects.indexOf(v.projects) != -1 ) { p.projects.splice(p.projects.indexOf(v.projects), 1); p.projectsCount -= 1; } return p; }, // init function(p,v) { return { projects:[], projectsCount:0 } } );非常感谢!
编辑后DJ Martin的答案::
Edit after DJ Martin's answer ::
因此,更清楚地说,我想得到的数字是:
So, to be clearer, I would like to get the numbers I am after here would be:
----------- Sam : 2 (projects he is workin on, no matter the number of actions) Joe : 2 (projects he is workin on, no matter the number of actions) -----------DJ Martin提供的答案将我带到了那里。但是,我希望找到一种在DC.JS条形图中使用这些数字的方法,而不是对表进行硬编码。当我只使用动作数(仅是reduceCount())时,我的操作如下:
The answer provided by DJ Martin gets me there. But rather than hard coding a table, I would like to find a way to use these numbers for my DC.JS bar chart. When I was only using the number of actions (so just a reduceCount() ), I did it like below:
teamMemberChart.width(270) .height(220) .margins({top: 5, left: 10, right: 10, bottom: 20}) .dimension(teamMemberDimension) .group(teamMemberDimensionGroup) .colors(d3.scale.category20()) .elasticX(true) .xAxis().ticks(4);我想group()中可能会有一些更改。
I guess there might be something to change in the group().
推荐答案更新后的答案
对不起,我误解了这个问题……您实际上是正确的。您只需要维护每个项目的计数,以便您的减法函数知道何时删除该值。
Sorry I misunderstood the question... you are actually on the right track. You'll just need to maintain a count of each project so that your subtract function can know when to remove the value.
teamMemberGroup = teamMemberDimension.group().reduce( function (p, d) { if( d.project in p.projects) p.projects[d.project]++; else p.projects[d.project] = 1; return p; }, function (p, d) { p.projects[d.project]--; if(p.projects[d.project] === 0) delete p.projects[d.project]; return p; }, function () { return {projects: {}}; });以下是更新的小提琴: jsfiddle/djmartin_umich/3LyhL/
Here is an updated fiddle: jsfiddle/djmartin_umich/3LyhL/