通过本教程here工作
这是我的工作fiddle
我想要做的是从上到下对spenderRowChart
(右图)的顺序进行排序。
为此,我创建了一个排序数组var topSpender =spendPerName.top(Infinity);
如果我正确理解topSpender
与spendPerName
相同,但是topSpender
将被排序
这是spendPerName
供参考:spendPerName = nameDim.group().reduceSum(function(d) {return +d.Spent;});
然后将topSpender
传递到spenderRowChart
.group(topSpender)
但这不起作用,并且出现以下错误。 fiddle here
Uncaught TypeError: group.all is not a function
谁能纠正我的方式的错误?
更多代码在这里
var yearRingChart = dc.pieChart("#chart-ring-year"),
spenderRowChart = dc.rowChart("#chart-row-spenders");
//var connection = new WebSocket('ws://localhost:8001/websocket');
var data1 = [
{Name: 'Ben', Spent: 330, Year: 2014, 'total':1},
{Name: 'Aziz', Spent: 1350, Year: 2012, 'total':2},
{Name: 'Vijay', Spent: 440, Year: 2014, 'total':2},
{Name: 'Jarrod', Spent: 555, Year: 2015, 'total':1},
];
// set crossfilter with first dataset
var xfilter = crossfilter(data1),
yearDim = xfilter.dimension(function(d) {return +d.Year;}),
spendDim = xfilter.dimension(function(d) {return Math.floor(d.Spent/10);}),
nameDim = xfilter.dimension(function(d) {return d.Name;}),
spendPerYear = yearDim.group().reduceSum(function(d) {return +d.Spent;}),
spendPerName = nameDim.group().reduceSum(function(d) {return +d.Spent;});
var topSpender =spendPerName.top(Infinity); //sort top spenders
function render_plots(){
yearRingChart
.width(200).height(200)
.dimension(yearDim)
.group(spendPerYear)
.innerRadius(50);
spenderRowChart
.width(250).height(200)
.dimension(nameDim)
.group(topSpender)
.elasticX(true);
dc.renderAll();
}
render_plots();
最佳答案
您需要改为.group(spendPerName)
。 DC.js直接在Crossfilter组上工作。 group.top
(即topSpender
)的输出不是一个组,而是一个对象数组形式的查询结果。