2016-04-27 66 views
0

我有以下CSV:單列Crossfilter尺寸和兩列上的組?

ccgcode,ccgname,metric,male,female 
06M,Great Yarmouth And Waveney,3,24,76 
05G,North Staffordshire,3,46,54 
... etc. 

如何獲得有關創建Sex尺寸驅動dc.pieChart()?我的問題具體是關於Crossfilter的DimensionGroup方面,而不是如何呈現餅圖。

+0

你真的應該改變你的數據,以便它更適合Crossfilter格式。例如,您應該將其更改爲具有「M」或「F」值的「性別」維度,然後您應該具有該值的「計數」維度。 –

+0

所以有更多的粒度數據。這是唯一的方法嗎? – Ciwan

+0

粒度與數據中信息的數量不變更相同。這是不同的格式。這不是唯一的方法,但它是最好的方法。將它們保存在不同的列中會給你帶來各種各樣的問題,而通過使數據變得更長而不是更廣泛而導致的相當小的問題已經由諸如Reductio和Universe之類的幫助程序庫解決。 –

回答

2

Crossfilter不能簡單地總結您需要的兩列,因此重新格式化數據是您最好的選擇(根據評論中的對話)。您可以使用melt.js,如果您熟悉R,其工作原理與reshape相似,因此您沒有觸摸原始數據。

此解決方案假定您的數據是鍵 - 值對(漂亮的標準)的對象的列表:

data = [ 
    { ccgcode: '06M', ccgname: 'Great Yarmouth And Waveney', metric: 3, male: 24, female: 76 }, 
    { ccgcode: '05G', ccgname: 'North Staffordshire', metric: 3, male: 46, female: 54 }, 
...]; 

您首先要確定你不想「融化」靜態列(分列):

var staticColumns = ['ccgcode', 'ccgname', 'metric']; 

然後使用melt取消組合男性和女性列。 Melt發生在你的數據,你不希望融化的列,而新的熔體列的名稱(在你的情況下,它sex):

var ndx = crossfilter(melt(data, staticColumns, 'sex', 'count')); 
var sexDim = ndx.dimension(function (d) { return d.sex; }); 

您使用以下尺寸落得

sexDim = [ 
    { sex: 'male', count: 24, _id: 0, ccgcode: '06M', ccgname: 'Great Yarmouth And Waveney', metric: 3 }, 
    { sex: 'male', count: 46, _id: 1, ccgcode: '05G', ccgname: 'North Staffordshire', metric: 3}, 
    { sex: 'female', count: 76, _id: 0, ccgcode: '06M', ccgname: 'Great Yarmouth And Waveney', metric: 3 }, 
    { sex: 'female', count: 54, _id: 1, ccgcode: '05G', ccgname: 'North Staffordshire', metric: 3}, 
...]; 

請注意melt爲您創建的有用的_id,以防萬一需要將所有內容連接在一起(儘管您的靜態列也可以幫助完成此操作)。您可以將false作爲第五個參數傳遞給melt以排除_id屬性。

而且你仍然可以通過性別組:

var males = sexDim.group().reduceSum(function (d) { 
    if (d.sex === 'male') { 
    return d.count; 
    } else { 
    return 0; 
    } 
}); 

希望幫助!