使用数据计数小部件显示不同项目的数量 [英] display the number of distinct items with data count widget

查看:77
本文介绍了使用数据计数小部件显示不同项目的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目列表,其中一些具有同一项目的多个行(同一项目的不同变体)。

I have a list of items, some of them have several rows for the same item (different variants of the same item).

我要计算总共有多少个项目,以及选择了多少个项目。但是,当我使用数据计数小部件时,默认情况下它仅适用于行数,但无法弄清楚如何更改该行为。

I want to count how many items exists in total, and how many are selected. however, when I'm using the data count widget, it only works with the number of rows by default, but can't figure out how to change that behaviour.

var data=[{"item":"Category A","color":"blue"},
   {"item":"Category B","color":"blue"},
   {"item":"Category A","color":"pink"}];


var ndx           = crossfilter(data);

var dimension  = ndx.dimension(function(d) {return d.item;});
var all = ndx.groupAll();


 dc.dataCount('.dc-data-count')
    .dimension(ndx)
    .group(all)
    .html ({some: "%filter-count selected out of %total-count items <a class='btn btn-default btn-xs' href='javascript:dc.filterAll(); dc.renderAll();'  role='button'>Reset filters</a>", all: "%total-count items. Click on the graphs to filter them"})

根据dc doc,唯一可能的维是整个数据集,而唯一的组是维返回的那个。groupAll()

According to dc doc, the only dimension possible is the entire data set and the only group is the one returned by dimension.groupAll()

是否有一种变通方法来计算除记录数以外的其他内容(例如ndx.dimension(function(d){return d.item;});?

Is there a workaround to count something else than the number of records (eg. ndx.dimension(function(d) {return d.item;});?

推荐答案

嗯,这很丑陋,但是您可以轻松地对其进行破解以产生其他行为。

Well, it's pretty ugly but you could easily hack it to produce other behavior.

如果您看一下代码, ,它仅在维度上调用 .size(),而在组上仅调用 .value()。这就是为什么文件说只有groupAll被批准专心常规组没有 .value()

If you look at the code, it's only calling .size() on the dimension, and only .value() on the group. That's why the documents say that only a groupAll is appropriate; regular groups don't have .value().

https://github.com/dc-js/dc.js/blob/ developer / src / data-count.js#L93-94

但是它在维或组上没有调用其他方法,因此您可以伪造这个漂亮的轻松地:

But it calls no other methods on the dimension or group, so you can fake this pretty easily:

chart.dimension({size: function() { 
    return 42; // calculate something here
} });

使用两个数字显示小部件可能更合适,但这应该可行。

It might be somewhat more appropriate to use two number display widgets instead, but this should work.

这篇关于使用数据计数小部件显示不同项目的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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