js/script.js
/* Loads and processes all the data for this project into the appropriate structures. */
Loads and processes all the data for this project into the appropriate structures.
Loads and processes all the data for this project into the appropriate structures .
async function loadData() {
    let africa = await loadFile('data/agriculture-crop-production/data/production_crops_e_africa.csv');
    let americas = await loadFile('data/agriculture-crop-production/data/production_crops_e_americas.csv');
    let asia = await loadFile('data/agriculture-crop-production/data/production_crops_e_asia.csv');
    let europe = await loadFile('data/agriculture-crop-production/data/production_crops_e_europe.csv');
    let oceania = await loadFile('data/agriculture-crop-production/data/production_crops_e_oceania.csv');
    let cmu5 = await loadFile('data/cmu5.csv'); 
    let iso_countryName_map = {};
    cmu5.map(row => iso_countryName_map[row.geo.toUpperCase()] = row.country);
    let countryName_iso_map = {};
    cmu5.map(row => countryName_iso_map[row.country] = row.geo.toUpperCase());
    let continents = {
        'africa': africa,
        'americas': americas,
        'asia': asia,
        'europe': europe,
        'oceania': oceania,
    };
    let cropSet = new Set();
    let elementSet = new Set();
    let countries = new Set();
    for (let continent in continents) {
        for (let row of continents[continent]) {
            let thisCrop = row.item;
            let thisCountry = row.area;
            let thisElement = row.element;
            cropSet.add(thisCrop);
            countries.add(thisCountry);
            elementSet.add(thisElement);
        }
    }
    let max_element_vals_for_each_crop = {}; 
    for (let crop of cropSet) {
        max_element_vals_for_each_crop[crop] = {};
        for (let element of elementSet) {
            max_element_vals_for_each_crop[crop][element] = {};
            for (let year = 1961; year <= 2014; year++) {
                max_element_vals_for_each_crop[crop][element]["y" + String(year)] = 0;
            }
        }
    }
    let countriesDict = {};
    for (let country of countries) {
        countriesDict[country] = {};
        for (let crop of cropSet) {
            countriesDict[country][crop] = {};
            for (let element of elementSet) {
                countriesDict[country][crop][element] = {};
            }
        }
    }
    for (let continent in continents) {
        for (let row of continents[continent]) {
            let thisCountry = row.area;
            let thisCrop = row.item;
            let thisElement = row.element;
            for (let attribute in row) {
                let thisEntry = row[attribute];
                countriesDict[thisCountry][thisCrop][thisElement][attribute] = thisEntry;
                if (attribute.charAt(0) == "y" && attribute.charAt(attribute.length - 1) != "f") {
                    if (+thisEntry > +max_element_vals_for_each_crop[thisCrop][thisElement][attribute]) {
                        max_element_vals_for_each_crop[thisCrop][thisElement][attribute] = +thisEntry;
                    }
                }
            }
        }
    }
    function sortObjectByKey(unorderedObject) {
        let orderedObject = {};
        Object.keys(unorderedObject).sort().forEach(function (key) {
            orderedObject[key] = unorderedObject[key];
        });
        return orderedObject;
    }
    let orderedCrops = [...cropSet].sort();
    let orderedCountries = sortObjectByKey(countriesDict);
    return {
        'crops': orderedCrops, 'countries': orderedCountries, 'iso_countryName_map': iso_countryName_map,
        'max_element_vals_for_each_crop': max_element_vals_for_each_crop, 'countryName_iso_map': countryName_iso_map
    };
}
async function loadData ( ) { let africa = await loadFile ( 'data/agriculture-crop-production/data/production_crops_e_africa.csv' ) ; let americas = await loadFile ( 'data/agriculture-crop-production/data/production_crops_e_americas.csv' ) ; let asia = await loadFile ( 'data/agriculture-crop-production/data/production_crops_e_asia.csv' ) ; let europe = await loadFile ( 'data/agriculture-crop-production/data/production_crops_e_europe.csv' ) ; let oceania = await loadFile ( 'data/agriculture-crop-production/data/production_crops_e_oceania.csv' ) ; let cmu5 = await loadFile ( 'data/cmu5.csv' ) ; let iso_countryName_map = { } ; cmu5 . map ( row => iso_countryName_map [ row . geo . toUpperCase ( ) ] = row . country ) ; let countryName_iso_map = { } ; cmu5 . map ( row => countryName_iso_map [ row . country ] = row . geo . toUpperCase ( ) ) ; let continents = { 'africa' : africa , 'americas' : americas , 'asia' : asia , 'europe' : europe , 'oceania' : oceania , } ; let cropSet = new Set ( ) ; let elementSet = new Set ( ) ; let countries = new Set ( ) ; for ( let continent in continents ) { for ( let row of continents [ continent ] ) { let thisCrop = row . item ; let thisCountry = row . area ; let thisElement = row . element ; cropSet . add ( thisCrop ) ; countries . add ( thisCountry ) ; elementSet . add ( thisElement ) ; } } let max_element_vals_for_each_crop = { } ; for ( let crop of cropSet ) { max_element_vals_for_each_crop [ crop ] = { } ; for ( let element of elementSet ) { max_element_vals_for_each_crop [ crop ] [ element ] = { } ; for ( let year = 1961 ; year <= 2014 ; year ++ ) { max_element_vals_for_each_crop [ crop ] [ element ] [ "y" + String ( year ) ] = 0 ; } } } let countriesDict = { } ; for ( let country of countries ) { countriesDict [ country ] = { } ; for ( let crop of cropSet ) { countriesDict [ country ] [ crop ] = { } ; for ( let element of elementSet ) { countriesDict [ country ] [ crop ] [ element ] = { } ; } } } for ( let continent in continents ) { for ( let row of continents [ continent ] ) { let thisCountry = row . area ; let thisCrop = row . item ; let thisElement = row . element ; for ( let attribute in row ) { let thisEntry = row [ attribute ] ; countriesDict [ thisCountry ] [ thisCrop ] [ thisElement ] [ attribute ] = thisEntry ; if ( attribute . charAt ( 0 ) == "y" && attribute . charAt ( attribute . length - 1 ) != "f" ) { if ( + thisEntry > + max_element_vals_for_each_crop [ thisCrop ] [ thisElement ] [ attribute ] ) { max_element_vals_for_each_crop [ thisCrop ] [ thisElement ] [ attribute ] = + thisEntry ; } } } } } function sortObjectByKey ( unorderedObject ) { let orderedObject = { } ; Object . keys ( unorderedObject ) . sort ( ) . forEach ( function ( key ) { orderedObject [ key ] = unorderedObject [ key ] ; } ) ; return orderedObject ; } let orderedCrops = [ ... cropSet ] . sort ( ) ; let orderedCountries = sortObjectByKey ( countriesDict ) ; return { 'crops' : orderedCrops , 'countries' : orderedCountries , 'iso_countryName_map' : iso_countryName_map , 'max_element_vals_for_each_crop' : max_element_vals_for_each_crop , 'countryName_iso_map' : countryName_iso_map } ; }
Loads and processes all the data for this project into the appropriate structures.
Loads and processes all the data for this project into the appropriate structures .
// Used only for mapping ISO geo codes to country names // Create sets of all the different crops, countries, and elements // Used for setting choropleth color scales // Set up the empty dictionaries we'll have to access and fill later // Now fill them // The attributes representing amount of an element are of the form e.g. y1996 but not y1996f /* Sorts an object by its keys */