我需要有关如何正确设置数据结构的帮助。我正在尝试使用 D3 来制作数据图和散点图(使用链接视图)。我使用的数据集如下所示:
预期寿命(出生时):
[{"country":"Abkhazia","1995":null,"1996":null,"1997":null,"1998":null,"1999":null,"2000":null,"2001":null,"2002":null,"2003":null,"2004":null,"2005":null,"2006":null,"2007":null,"2008":null,"2009":null,"2010":null,"2011":null,"2012":null,"2013":null,"2014":null,"2015":null,"2016":null},
{"country":"Afghanistan","1995":49.4,"1996":49.7,"1997":49.5,"1998":48.6,"1999":50,"2000":50.1,"2001":50.4,"2002":51,"2003":51.4,"2004":51.8,"2005":52,"2006":52.1,"2007":52.4,"2008":52.8,"2009":53.3,"2010":53.6,"2011":54,"2012":54.4,"2013":54.8,"2014":54.9,"2015":53.8,"2016":52.72},
{"country":"Akrotiri and Dhekelia","1995":null,"1996":null,"1997":null,"1998":null,"1999":null,"2000":null,"2001":null,"2002":null,"2003":null,"2004":null,"2005":null,"2006":null,"2007":null,"2008":null,"2009":null,"2010":null,"2011":null,"2012":null,"2013":null,"2014":null,"2015":null,"2016":null},
etc.
国家代码:
var countryCodes =[
["af", "AFG", "Afghanistan"],
["ax", "ALA", "Åland Islands"],
["al", "ALB", "Albania"],
["dz", "DZA", "Algeria"],
etc.
用于医疗保健的 GDP 百分比:
[{"country":"Abkhazia","1995":null,"1996":null,"1997":null,"1998":null,"1999":null,"2000":null,"2001":null,"2002":null,"2003":null,"2004":null,"2005":null,"2006":null,"2007":null,"2008":null,"2009":null,"2010":null},
{"country":"Afghanistan","1995":null,"1996":null,"1997":null,"1998":null,"1999":null,"2000":null,"2001":null,"2002":5.7,"2003":6.8,"2004":6.4,"2005":6.6,"2006":6.8,"2007":7.3,"2008":7.0,"2009":7.6,"2010":7.6},
{"country":"Akrotiri and Dhekelia","1995":null,"1996":null,"1997":null,"1998":null,"1999":null,"2000":null,"2001":null,"2002":null,"2003":null,"2004":null,"2005":null,"2006":null,"2007":null,"2008":null,"2009":null,"2010":null},
{"country":"Albania","1995":2.6,"1996":4.0,"1997":4.8,"1998":5.3,"1999":5.8,"2000":6.4,"2001":6.0,"2002":6.3,"2003":6.2,"2004":6.9,"2005":6.8,"2006":6.7,"2007":6.9,"2008":6.7,"2009":6.9,"2010":6.5},
etc.
我尝试了什么 (1/2) 按照老师的建议,我最终编写了以下代码:
// If error, show in console
if (error) return console.warn(error);
// Default year and country when first rendering map
var year = 2011;
var country = "Netherlands";
data = {}
// Using colorblind safe colors from colorbrewer2.org
var colorArray = ["lowest", "low", "middle", "high", "highest"]
// number of years (= number of objects per country minus 1 for the name of the country)
number = Object.keys(lifeExpectancy[1]).length - 1
// getting the minimum and the maximum life expectancy of the entire dataset
var min = Number.MAX_VALUE,
max = -Number.MAX_VALUE;
lifeExpectancy.forEach(function (o) {
Object.keys(o).forEach(function (k) {
if (k !== 'country' && o[k] !== null) {
min = Math.min(min, o[k]);
max = Math.max(max, o[k]);
}
});
});
// calculating the denumerator
var denumerator = max /5;
// // Make the datastructure
for (var i = 0; i < lifeExpectancy.length; i++){
for (var j = 0; j < countryCodes.length; j++){
if(lifeExpectancy[i]["country"] == countryCodes[j][2]){
data[countryCodes[j][1]] = {}
for(var k = 0; k < number; k++){
var year = 1995 + k;
data[countryCodes[j][1]][Object.keys(lifeExpectancy[i])[k]] =
{fillKey: (Math.floor((lifeExpectancy[i][year] - min)/denumerator)),
country: lifeExpectancy[i]["country"],
lifeExpectancy: lifeExpectancy[i][Object.keys(lifeExpectancy[i])[k]],
healthPercGDP: healthPercGDP[i][Object.keys(healthPercGDP[i])[k]]}
}
}
}
}
使用它,数据结构如下所示:
data = {ABW {1995 { country: "Aruba", fillKey:2, healthPercGDP:null, lifeExpectancy:73,62},
1996{.........},
1997{..........},.........}
,AFG{1995{ country:"Afghanistan", fillkey: 1, healthPercGDP: null, lifeExpectancy:49,62} etc.}}
但是,我发现 D3.datamaps 可以工作,
我需要以下结构:
data = {1995{ABW{.....},AFG{.....},....}
1996{ABW{....},AFG{....},....}etc.}
我尝试了什么(2/2)
var data2 = {};
// Make the datastructure
for (var i = 0; i < lifeExpectancy.length; i++){
for(var k = 0; k < number; k++){
var year = 1995 + k;
data2[Object.keys(lifeExpectancy[i])[k]] = {}
for (var j = 0; j < countryCodes.length; j++){
if(lifeExpectancy[i]["country"] == countryCodes[j][2]){
data2[Object.keys(lifeExpectancy[i])[k]][countryCodes[j][1]] = {
fillKey: (Math.floor((lifeExpectancy[i][year] - min)/denumerator)),
country: lifeExpectancy[i]["country"],
lifeExpectancy: lifeExpectancy[i][Object.keys(lifeExpectancy[i])[k]],
healthPercGDP: healthPercGDP[i][Object.keys(healthPercGDP[i])[k]]}
}
}
}
}
但是最后一段代码只是给了我:
data2={1995{SSD{country: "South Sudan", fillKey:1, healthPercGDP:null, lifeExpectancy: 52.7}},1996{SSD{.....}},1997{SSD{....}}, etc.}
我只得到 1995-2016 年的字典,全都是南苏丹的值。更奇怪的是,南苏丹不是国家代码列表中的最后一个变量。
完整数据集:
https://github.com/JappaB/DataProcessing/tree/master/Homework/week-6