dc/crossfilter 为图表提供数据的行图表排序数据
dc/crossfilter row chart sorting data that feeds chart
我要做的是从上到下对spenderRowChart
(右图)的顺序进行排序。
为此,我创建了一个排序数组
var topSpender =spendPerName.top(Infinity);
如果我理解正确 topSpender
si 与 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
)的输出不是一个组,而是一个对象数组形式的查询结果。
这是我的回答,见fiddle
代码:(选项 A 或 B 有效)
spenderRowChart
.width(250).height(200)
.dimension(nameDim)
.group(spendPerName)
.ordering(function(d) { return -d.value; }) //OPTION A
.elasticX(true);
//spenderRowChart.ordering(function(d) { return -d.value; }) //OPTION B
这里的类似问题很有帮助:
Sorting (ordering) the bars in a bar chart by the bar values with dc.js
dc.js/crossfilter -> How to sort data in dc.js chart (like row) - Ascending x Descending
我要做的是从上到下对spenderRowChart
(右图)的顺序进行排序。
为此,我创建了一个排序数组
var topSpender =spendPerName.top(Infinity);
如果我理解正确 topSpender
si 与 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
)的输出不是一个组,而是一个对象数组形式的查询结果。
这是我的回答,见fiddle
代码:(选项 A 或 B 有效)
spenderRowChart
.width(250).height(200)
.dimension(nameDim)
.group(spendPerName)
.ordering(function(d) { return -d.value; }) //OPTION A
.elasticX(true);
//spenderRowChart.ordering(function(d) { return -d.value; }) //OPTION B
这里的类似问题很有帮助:
Sorting (ordering) the bars in a bar chart by the bar values with dc.js
dc.js/crossfilter -> How to sort data in dc.js chart (like row) - Ascending x Descending