简体   繁体   English

如何显示前n个项目的图形?

[英]How to display a graph of top n items?

I have a list of events and want to show who are the top participants (the ones that have come more often) 我有一个活动列表,并想显示谁是最重要的参与者(经常参加的人)

What I've done is a rowChart 我所做的是一个行图

var dim = ndx.dimension (function(d) {
  if (!d.guest) {
      return "Not mentioned";
  }
  return d.guest;
});

var group = dim.group().reduceSum(function(d) { return 1; });
var graph = dc.rowChart (".topvisitor")
  .margins({top: 0, right: 10, bottom: 20, left: 10})
  .height(300)
  .width(200)
  .cap(10)
  .x(d3.scale.ordinal())
  .elasticX(true)
.ordering(function(d){return -d.value})
  .dimension(dim)
  .group(group);

That kind of works, but there is a big "other" that I'd want to remove. 这种工作,但是我想删除一个很大的“其他”。 Am I abusing the rowChart to create a topN graph? 我是否在滥用rowChart创建topN图?

So the path I followed is to filter the data first (by creating a fake group that has a new all() function that returns a top(n) of the real group): 因此,我遵循的路径是首先过滤数据(通过创建一个伪造的组,该组具有一个新的all()函数,该函数返回真实组的top(n)):

var group = dim.group().reduceSum(function(d) { return 1; }); var group = dim.group()。reduceSum(function(d){return 1;});

var filteredGroup = (function (source_group) {return {
  all:function () {
    return source_group.top(10).filter(function(d) {
      return d.key != "Not mentioned";
     });
  }
};})(group);

and for the graph, use this group 对于图表,请使用该组

.group(filteredGroup); 。集团(filteredGroup);

I think it does the trick, despite Gordon's approval, still feels a bit hackish, but it does the job (cap+filter some data) 我认为,尽管获得了戈登的认可,它还是可以解决问题的,但确实可以完成工作(使用Cap +过滤某些数据)

you have to remove the cap, or implement the top function (same as all in this case) 您必须卸下盖子或实现顶部功能(与本例中的所有功能相同)

As a side note: rowChart seems to be one of the chart where you can override the data function, so: 附带说明:rowChart似乎是您可以覆盖数据功能的图表之一,因此:

.data(function (group) {
      return group.top(10);
})

would Work too (but wouldn't filter the "Not mentioned" items 也可以使用(但不会过滤“未提及”的项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM