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