我正在使用以下数据集:
Landen,"Perioden","SITC","Onderwerpen_1","Waarde eenheid","Waarde"
Afghanistan,"2012","0 Voeding en levende dieren","Invoerwaarde","mln euro","1"
Afghanistan,"2012","1 Dranken en tabak","Invoerwaarde","mln euro","0"
Afghanistan,"2012","2 Grondstoffen niet eetbaar behalve...","Invoerwaarde","mln euro","."
Afghanistan,"2012","3 Minerale brandstoffen smeermiddele...","Invoerwaarde","mln euro","."
Afghanistan,"2012","4 Dierlijke en plantaardige oliën en ...","Invoerwaarde","mln euro","."
Afghanistan,"2012","5 Chemische producten","Invoerwaarde","mln euro","0"
Afghanistan,"2012","6 Fabricaten hoofdzakelijk gerangschi...","Invoerwaarde","mln euro","0"
Afghanistan,"2012","7 Machines en vervoermaterieel","Invoerwaarde","mln euro","1"
Afghanistan,"2012","8 Diverse gefabriceerde goederen","Invoerwaarde","mln euro","1"
Afghanistan,"2012","9 Niet afzonderlijk genoemde goederen","Invoerwaarde","mln euro","0"
我要做的第一件事是更改所有非“ Waarde”值。变成这样的整数:
for (let i = 0; i < dataTotal.length; i++) {
if (dataTotal[i].Waarde != ".") {
dataTotal[i].Waarde = parseInt(dataTotal[i].Waarde);
}
}
然后我使用d3.nest制作一个新的更可用的数组,如下所示:
nested_data = d3.nest()
.key(function (d) {
return d.SITC;
})
.rollup(function (d) {
return d3.sum(d, function (e) {
return e.Waarde
});
});
.entries(dataTotal);
结果如下:
您可以看到d3.sum更改了“。”值设为0,这很有意义。但是,我希望汇总汇总在dataTotal.Waarde是数字时返回数字,并返回带有“。”的字符串。当不是。我尝试了以下方法:
nested_data = d3.nest()
.key(function (d) {
return d.SITC;
})
.rollup(function (d) {
if (typeof dataTotal.Waarde == "number") {
return d3.sum(d, function (e) {
return e.Waarde
});
} else if (typeof dataTotal.Waarde == "string"){
}
})
.entries(dataTotal);
这是结果:
现在,它为所有值返回未定义。在希望汇总返回dataTotal.Waarde是数字并返回字符串“。”的位置时,如何解决此问题。在哪里?
答案 0 :(得分:1)
根据您的comment,您根本不需要d3.nest()
!您可以使用简单的map
创建所需的数组,使用unary plus将字符串转换为数字,并在NaN
的情况下测试"."
:>
const nested = data.map(function(d) {
return {
key: d.SITC,
value: +d.Waarde === +d.Waarde ? +d.Waarde : d.Waarde
};
});
这是一个演示:
const csv = `Landen,"Perioden","SITC","Onderwerpen_1","Waarde eenheid","Waarde"
Afghanistan,"2012","0 Voeding en levende dieren","Invoerwaarde","mln euro","1"
Afghanistan,"2012","1 Dranken en tabak","Invoerwaarde","mln euro","0"
Afghanistan,"2012","2 Grondstoffen niet eetbaar behalve...","Invoerwaarde","mln euro","."
Afghanistan,"2012","3 Minerale brandstoffen smeermiddele...","Invoerwaarde","mln euro","."
Afghanistan,"2012","4 Dierlijke en plantaardige oliën en ...","Invoerwaarde","mln euro","."
Afghanistan,"2012","5 Chemische producten","Invoerwaarde","mln euro","0"
Afghanistan,"2012","6 Fabricaten hoofdzakelijk gerangschi...","Invoerwaarde","mln euro","0"
Afghanistan,"2012","7 Machines en vervoermaterieel","Invoerwaarde","mln euro","1"
Afghanistan,"2012","8 Diverse gefabriceerde goederen","Invoerwaarde","mln euro","1"
Afghanistan,"2012","9 Niet afzonderlijk genoemde goederen","Invoerwaarde","mln euro","0"`;
const data = d3.csvParse(csv);
const nested = data.map(function(d) {
return {
key: d.SITC,
value: +d.Waarde === +d.Waarde ? +d.Waarde : d.Waarde
};
});
console.log(nested)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>