0
我想按鍵和值使用D3.nest(),根據自定義數組順序排序數據集。關鍵排序工作沒有問題,但但我無法得到排序的值。D3巢 - 排序值
我的自定義排序列被定義爲:
var priority_source_order = ["Biological","Water","Sediment","Eco-Fish"];
var priority_indicator_order = ["Biological Status","Water Quality Index","Phosphorus","Nitrogen","Dissolved Oxygen","Water Clarity","Chlorophyll a","Sediment Quality Index","Sediment Contaminants","Sediment Toxicity","Fish Quality Index"];
的.sortKeys方法工作得很好,但我沒能值進行排序。這可能是因爲嵌套在D3.nest()創建的「值」鍵內的多個鍵/值對,並且該函數不知道需要對哪個鍵進行排序。這裏是我的代碼:
var final_data = d3.nest()
.key(function(d) {
return d[ncca_ce_source];
})
.sortKeys(function(a,b) {
return priority_source_order.indexOf(a) - priority_source_order.indexOf(b);
})
.sortValues(function(a,b) {
return priority_indicator_order.indexOf(a) - priority_indicator_order.indexOf(b);
})
.entries(cond_est_data);
我的數據目前看起來像:
[
{
"key": "Biological",
"values": [
{
"Type": "National",
"Indicator.Plain.Language": "Benthic Index"
}
]
},
{
"key": "Water",
"values": [
{
"Type": "National",
"Indicator.Plain.Language": "Chlorophyll a"
},
{
"Type": "National",
"Indicator.Plain.Language": "Dissolved Oxygen"
}
]
}
]
有沒有人對如何根據的訂單上的「Indicator.Plain.Language」鍵排序任何意見priority_indicator_order數組?正如你在上面的輸出中看到的,生物和水的「關鍵」被正確排序。但是,「Indicator.Plain.Language」的值不是由priority_indicator_order數組定義的順序。任何幫助表示讚賞!
我認爲你需要重新審視三元 '回報(priority_indicator_order.indexOf(一) - priority_indicator_order.indexOf(B)1? -1);'我很確定這將始終返回第一個表達式(1)。 –
我想我需要澄清一下我的問題。現在去更新。 – thefreeline
明白了,必須修改sortValue爲:return priority_indicator_order.indexOf(a [ncca_ce_indic_plain]) - priority_indicator_order.indexOf(b [ncca_ce_indic_plain]); – thefreeline