我目前正在尝试弄清楚如何使用DJ.js和D3.js显示要显示的唯一记录的数量
数据集看起来像这样:
id,name,artists,genre,danceability,energy,key,loudness,mode,speechiness,acousticness,instrumentalness,liveness,valence,tempo,duration_ms,time_signature
6DCZcSspjsKoFjzjrWoCd,God's Plan,Drake,Hip-Hop/Rap,0.754,0.449,7,-9.211,1,0.109,0.0332,8.29E-05,0.552,0.357,77.169,198973,4
3ee8Jmje8o58CHK66QrVC,SAD!,XXXTENTACION,Hip-Hop/Rap,0.74,0.613,8,-4.88,1,0.145,0.258,0.00372,0.123,0.473,75.023,166606,4
数据集中有100条记录,我希望该数目显示70个唯一艺术家的数目。
var ndx = crossfilter(spotifyData);
totalArtists(ndx);
....
function totalArtists(ndx) {
// Select the artists
var totalArtistsND = dc.numberDisplay("#unique-artists");
// Count them
var dim = ndx.dimension(dc.pluck("artists"));
var uniqueArtist = dim.groupAll();
totalArtistsND.group(uniqueArtist).valueAccessor(x => x);
totalArtistsND.render();
}
当我应该达到70时,我只能得到100。
感谢您一百万,我们将不胜感激
您走在正确的轨道上-groupAll对象通常是与dc.numberDisplay
一起使用的正确对象。
但是,dimension.groupAll不使用尺寸的键功能。像任何groupAll一样,它查看所有记录并返回一个值; dimension.groupAll()
和crossfilter.groupAll()
之间的唯一区别是,前者未观察到尺寸的滤镜,而后者则观察了所有滤镜。
如果要使用dimension.groupAll
,则必须编写reduce函数来监视行的添加和删除,并记录已看到的唯一艺术家数。听起来有点乏味,甚至可能是越野车。
相反,我们可以编写一个“ fake groupAll”,该对象的.value()
方法将返回根据当前过滤器动态计算的值。
普通组对象已经具有唯一的计数:箱数。因此,我们可以创建一个伪造的groupAll,它包装一个普通的组并返回group.all()
返回的数组的长度:
function unique_count_groupall(group) {
return {
value: function() {
return group.all().length;
}
};
}
使用伪造的groupAll像这样:
var uniqueArtist = unique_count_groupall(dim.group());