1
我需要關於如何獲得我的數據結構的幫助。我試圖使用D3做一個數據圖和散點圖(使用鏈接的視圖)。該數據集我使用的是這樣的: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
謝謝您的回答!儘管它在你的例子中起作用,但我無法讓它在我自己的代碼中工作。我改變了我的數據的名稱以匹配你的,但然後我得到以下錯誤:無法讀取未定義的屬性'1995'。我得到這個錯誤在線:let lifeexpectancy = lifeExpectancyData.filter(matchesCountry)[0] [year]; – JappaB
這可能是因爲國家代碼列表包含其他數據集中未找到的條目 – Brian
實際上,其他方式實際上,預期壽命和健康數據都包含國家/地區代碼列表中不存在的國家/地區。但是我不完全理解你的代碼。有一個簡單的解決方法嗎? – JappaB