1

データ構造を正しくする方法について助けが必要です。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

4

1 に答える 1