<commit_before>function hxlProxyToJSON(input){
    var output = [];
    var keys=[]
    input.forEach(function(e,i){
        if(i==0){
            e.forEach(function(e2,i2){
                var parts = e2.split('+');
                var key = parts[0]
                if(parts.length>1){
                    var atts = parts.splice(1,parts.length);
                    atts.sort();                    
                    atts.forEach(function(att){
                        key +='+'+att
                    });
                }
                keys.push(key);
            });
        } else {
            var row = {};
            e.forEach(function(e2,i2){
                row[keys[i2]] = e2;
            });
            output.push(row);
        }
    });
    return output;
}

function parseDates(tags,data){
    var parseDateFormat = d3.time.format("%Y-%m-%d").parse;
    data.forEach(function(d){
        tags.forEach(function(t){
            d[t] = parseDateFormat(d[t]);
        });
    });
    return data;
}

var date_sort = function (d1, d2) {
    if (d1.key > d2.key) return 1;
    if (d1.key < d2.key) return -1;
    return 0;
};

function generateMap(incidents,refugees,accessible,adm1,adm2,countries,countrieslabel){
    map.init(adm1,adm2,countries,incidents,refugees, accessible, countrieslabel);
}

function generateKeyStats(data){
    var cf = crossfilter(data);
    var datesDimension = cf.dimension(function(d){ return d['#date']; });

    var affectedGroup = datesDimension.group().reduceSum(function(d){ return (d['#affected']); }).top(Infinity).sort(date_sort);

    var maxDate = d3.max(data,function(d){return d['#date'];});

    var countryDimension = datesDimension.filter(maxDate);
    var countryArr = [];
    countryDimension.top(Infinity).forEach(function(key,i){
        countryArr.push({id:key['#country+code'],
                         affected:key['#affected'],
                         inneed:key['#inneed'],
                         foodinsecure:key['#affected+foodinsecure'],
                         displaced:key['#affected+displaced'],
                         sam:key['#affected+sam']});
    });

    var inneedGroup = datesDimension.group().reduceSum(function(d){ return (d['#inneed']); }).top(Infinity).sort(date_sort);

    var foodinsecureGroup = datesDimension.group().reduceSum(function(d){ return (d['#affected+foodinsecure']); }).top(Infinity).sort(date_sort);

    var displacedGroup = datesDimension.group().reduceSum(function(d){ return (d['#affected+displaced']); }).top(Infinity).sort(date_sort);

    var samGroup = datesDimension.group().reduceSum(function(d){ return (d['#affected+sam']); }).top(Infinity).sort(date_sort);
    
    var datesArr = ['x'];
    var affectedArr = ['Affected'];
    var inneedArr = ['In Need'];
    var foodinsecureArr = ['Food Insecure'];
    var displacedArr = ['Displaced'];
    var samArr = ['Sam'];

    for (var i=0;i<affectedGroup.length;i++){
        datesArr.push(affectedGroup[i].key);
        affectedArr.push(affectedGroup[i].value);
        inneedArr.push(inneedGroup[i].value);
        foodinsecureArr.push(foodinsecureGroup[i].value);
        displacedArr.push(displacedGroup[i].value);
        samArr.push(samGroup[i].value);
    }

    var sparklineW = 65;
    var sparklineH = 40;
    var keyFiguresArr = [
            { dimension: 'affected', dimensionArr: affectedArr, total: affectedGroup[affectedGroup.length-1].value },
            { dimension: 'inneed', dimensionArr: inneedArr, total: inneedGroup[inneedGroup.length-1].value },
            { dimension: 'foodinsecure', dimensionArr: foodinsecureArr, total: foodinsecureGroup[foodinsecureGroup.length-1].value },
            { dimension: 'displaced', dimensionArr: displacedArr, total: displacedGroup[displacedGroup.length-1].value },
            { dimension: 'sam', dimensionArr: samArr, total: samGroup[samGroup.length-1].value }
        ];
    for (var i=0;i<keyFiguresArr.length;i++) {
        $('#'+keyFiguresArr[i].dimension+'Total').html(numFormat(keyFiguresArr[i].total));
        var chart = c3.generate({
            bindto: '#'+keyFiguresArr[i].dimension+'Line',
            size: { 
                height: sparklineH,
                width: sparklineW
            },
            color: {
              pattern: ['#0066b9']
            },
            data: {
                x: 'x',
                columns: [ datesArr, keyFiguresArr[i].dimensionArr ]
            },
            axis: {
                x: { show: false },
                y: { show: false }
            },
            point: { show: false },
            tooltip: { show: false },
            legend: { hide: true }
        });
    }

    //map tooltips
    var keytip = d3.select('.keyfigures').append('div').attr('class', 'd3-tip hidden');
    $('.keyfigure').each(function(i,e){
        var figure = $(this).attr('data-figure');
        var str = '<h4>Country Breakdown</h4>';
        for (var j=0;j<countryArr.length;j++){
            str += countryArr[j].id + ': ' + numFormat(countryArr[j][figure]) + '<br>';
        }

        var l = $(e)[0].offsetLeft;
        $(e).find('span, div').on('mouseover', function(e) {  
            keytip
                .classed('hidden', false)
                .attr('style', 'left:'+l+'px;top:'+e.pageY+'px')
                .html(str)
        })
        $(e).find('span, div').on('mouseout',  function() {
            keytip.classed('hidden', true)
        }); 
    });
}

function generateFundingGraph(data){
    var cf = crossfilter(data);
    var fundsDimension = cf.dimension(function(d){return d['#date'];});
    
    var maxDate = d3.max(data,function(d){return d['#date'];});

    var fundingData = fundsDimension.filter(maxDate).top(Infinity).sort(function(a, b) {
        return b['#meta+requirement'] - a['#meta+requirement'];
    });

    var locationArr = ['x'];
    var fundedArr = ['Funded'];<commit_msg>Mapping country codes to name for keyfigure tooltips<commit_after>function hxlProxyToJSON(input){
    var output = [];
    var keys=[]
    input.forEach(function(e,i){
        if(i==0){
            e.forEach(function(e2,i2){
                var parts = e2.split('+');
                var key = parts[0]
                if(parts.length>1){
                    var atts = parts.splice(1,parts.length);
                    atts.sort();                    
                    atts.forEach(function(att){
                        key +='+'+att
                    });
                }
                keys.push(key);
            });
        } else {
            var row = {};
            e.forEach(function(e2,i2){
                row[keys[i2]] = e2;
            });
            output.push(row);
        }
    });
    return output;
}

function parseDates(tags,data){
    var parseDateFormat = d3.time.format("%Y-%m-%d").parse;
    data.forEach(function(d){
        tags.forEach(function(t){
            d[t] = parseDateFormat(d[t]);
        });
    });
    return data;
}

var date_sort = function (d1, d2) {
    if (d1.key > d2.key) return 1;
    if (d1.key < d2.key) return -1;
    return 0;
};

function generateMap(incidents,refugees,accessible,adm1,adm2,countries,countrieslabel){
    map.init(adm1,adm2,countries,incidents,refugees, accessible, countrieslabel);
}

function generateKeyStats(data){
    var cf = crossfilter(data);
    var datesDimension = cf.dimension(function(d){ return d['#date']; });

    var affectedGroup = datesDimension.group().reduceSum(function(d){ return (d['#affected']); }).top(Infinity).sort(date_sort);

    var maxDate = d3.max(data,function(d){return d['#date'];});

    var countryDimension = datesDimension.filter(maxDate);
    var countryArr = [];
    countryDimension.top(Infinity).forEach(function(key,i){
        countryArr.push({id:key['#country+code'],
                         affected:key['#affected'],
                         inneed:key['#inneed'],
                         foodinsecure:key['#affected+foodinsecure'],
                         displaced:key['#affected+displaced'],
                         sam:key['#affected+sam']});
    });
    var countryNameMap = {
        CHD: 'Chad',
        CMR: 'Cameroon',
        NGR: 'Nigeria',
        NER: 'Niger'
    };

    var inneedGroup = datesDimension.group().reduceSum(function(d){ return (d['#inneed']); }).top(Infinity).sort(date_sort);

    var foodinsecureGroup = datesDimension.group().reduceSum(function(d){ return (d['#affected+foodinsecure']); }).top(Infinity).sort(date_sort);

    var displacedGroup = datesDimension.group().reduceSum(function(d){ return (d['#affected+displaced']); }).top(Infinity).sort(date_sort);

    var samGroup = datesDimension.group().reduceSum(function(d){ return (d['#affected+sam']); }).top(Infinity).sort(date_sort);
    
    var datesArr = ['x'];
    var affectedArr = ['Affected'];
    var inneedArr = ['In Need'];
    var foodinsecureArr = ['Food Insecure'];
    var displacedArr = ['Displaced'];
    var samArr = ['Sam'];

    for (var i=0;i<affectedGroup.length;i++){
        datesArr.push(affectedGroup[i].key);
        affectedArr.push(affectedGroup[i].value);
        inneedArr.push(inneedGroup[i].value);
        foodinsecureArr.push(foodinsecureGroup[i].value);
        displacedArr.push(displacedGroup[i].value);
        samArr.push(samGroup[i].value);
    }

    var sparklineW = 65;
    var sparklineH = 40;
    var keyFiguresArr = [
            { dimension: 'affected', dimensionArr: affectedArr, total: affectedGroup[affectedGroup.length-1].value },
            { dimension: 'inneed', dimensionArr: inneedArr, total: inneedGroup[inneedGroup.length-1].value },
            { dimension: 'foodinsecure', dimensionArr: foodinsecureArr, total: foodinsecureGroup[foodinsecureGroup.length-1].value },
            { dimension: 'displaced', dimensionArr: displacedArr, total: displacedGroup[displacedGroup.length-1].value },
            { dimension: 'sam', dimensionArr: samArr, total: samGroup[samGroup.length-1].value }
        ];
    for (var i=0;i<keyFiguresArr.length;i++) {
        $('#'+keyFiguresArr[i].dimension+'Total').html(numFormat(keyFiguresArr[i].total));
        var chart = c3.generate({
            bindto: '#'+keyFiguresArr[i].dimension+'Line',
            size: { 
                height: sparklineH,
                width: sparklineW
            },
            color: {
              pattern: ['#0066b9']
            },
            data: {
                x: 'x',
                columns: [ datesArr, keyFiguresArr[i].dimensionArr ]
            },
            axis: {
                x: { show: false },
                y: { show: false }
            },
            point: { show: false },
            tooltip: { show: false },
            legend: { hide: true }
        });
    }

    //map tooltips
    var keytip = d3.select('.keyfigures').append('div').attr('class', 'd3-tip hidden');
    $('.keyfigure').each(function(i,e){
        var figure = $(this).attr('data-figure');
        var str = '<h4>Country Breakdown</h4>';
        for (var j=0;j<countryArr.length;j++){
            str += countryNameMap[countryArr[j].id] + ': ' + numFormat(countryArr[j][figure]) + '<br>';
        }

        var l = $(e)[0].offsetLeft;
        $(e).find('span, div').on('mouseover', function(e) {  
            keytip
                .classed('hidden', false)
                .attr('style', 'left:'+l+'px;top:'+e.pageY+'px')
                .html(str)
        })
        $(e).find('span, div').on('mouseout',  function() {
            keytip.classed('hidden', true)
        }); 
    });
}

function generateFundingGraph(data){
    var cf = crossfilter(data);
    var fundsDimension = cf.dimension(function(d){return d['#date'];});
    
    var maxDate = d3.max(data,function(d){return d['#date'];});

    var fundingData = fundsDimension.filter(maxDate).top(Infinity).sort(function(a, b) {
        return b['#meta+requirement'] - a['#meta+requirement'];
    });

    var locationArr = ['x'];
    var fundedArr = ['Funded'];
{'fraction_non_alphanumeric': 0.1644967451176765, 'fraction_numerical': 0.008345852111500584, 'mean_word_length': 2.302011573436208, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '41744', 'n_tokens_mistral': 3781, 'n_tokens_neox': 3595, 'n_words': 602}