Skip to content

Instantly share code, notes, and snippets.

@Kalimaha
Last active August 29, 2015 14:10
Show Gist options
  • Save Kalimaha/6908d83eb2860e78cef1 to your computer and use it in GitHub Desktop.
Save Kalimaha/6908d83eb2860e78cef1 to your computer and use it in GitHub Desktop.
GHG_QA_QC.prototype.populate_agsoils_tables_faostat = function(country_code) {
var query_config = [
{item: '1709', element: '7231'},
{item: '5056', element: '7235'},
{item: '3102', element: '72353'},
{item: '1755', element: '72351'},
{item: '1712', element: '72352'},
{item: '6729', element: '72318'},
{item: '1755', element: '72350'},
{item: '5057', element: '7237'},
{item: '5061', element: '7235'}
];
for (var z = 0 ; z < query_config.length ; z++) {
var sql = {
'query': "SELECT A.AreaNameS, E.ElementListNameS, I.ItemNameS, I.ItemCode, D.Year, D.value, D.ElementCode " +
"FROM Data AS D, Area AS A, Element AS E, Item I " +
"WHERE D.AreaCode = '" + country_code + "' " +
"AND D.ElementCode IN (" + query_config[z].element + ") " +
"AND D.ItemCode IN (" + query_config[z].item + ") " +
"AND D.Year IN (1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, " +
"2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, " +
"2010, 2011, 2012) " +
"AND D.AreaCode = A.AreaCode " +
"AND D.ElementListCode = E.ElementListCode " +
"AND D.ItemCode = I.ItemCode " +
"GROUP BY A.AreaNameS, E.ElementListNameS, I.ItemNameS, I.ItemCode, D.Year, D.value, D.ElementCode"
};
var data = {};
data.datasource = 'faostat';
data.thousandSeparator = ',';
data.decimalSeparator = '.';
data.decimalNumbers = 2;
data.json = JSON.stringify(sql);
data.cssFilename = '';
data.nowrap = false;
data.valuesIndex = 0;
var url_data = this.CONFIG.url_data;
$.ajax({
type: 'POST',
url: url_data,
data: data,
success: function (response) {
var json = response;
if (typeof json == 'string')
json = $.parseJSON(response);
for (var i = 0; i < json.length; i++) {
var item = json[i][3];
var element = json[i][6];
var y = json[i][4];
var v = json[i][5];
var crf = null;
switch (element) {
case '7231':
crf = '4d';
break;
case '7235':
crf = '4d1';
break;
case '72353':
crf = '4d11';
break;
case '72351':
crf = '4d12';
break;
case '72352':
crf = '4d14';
break;
case '72318':
crf = '4d15';
break;
case '72350':
crf = '4d2';
break;
case '7237':
crf = '4d3';
break;
}
if (item == '5061' && element == '7235')
crf = '4d11';
var value = parseFloat(v).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
$('#faostat_' + y + '_' + crf).html(value);
}
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment