var width; var container; var jsonValues; function getJSONValues(type, id) { $.ajaxSetup({ cache: false}); $.getJSON("json/"+type+'.json', function(data){ jsonValues = extractChampionshipObj(data, id); drawGenderChart(); drawAgeChart(); loadDisciplineNames(); drawResultsChart(); }); } function getJSONChampionship(type) { $.ajaxSetup({ cache: false}); $.getJSON("json/"+type+'.json', function(data){ jsonValues = data.championships.sort(GetSortOrder('year')); drawChampionshipsGenderChart(); drawChampionshipsAgeChart(); drawChampionshipsResultsChart(); /*drawChampionshipsParticipationChart();*/ $.LoadingOverlay("hide"); }); } function extractChampionshipObj(data, id) { for(var key in data.championships) { if(data.championships.hasOwnProperty(key)) { //console.log(data.championships[key].id); if(data.championships[key].id == id) return data.championships[key]; } } return null; } function drawChampionshipCharts() { container = jQuery('.championships-container').find('.r-tabs-panel.r-tabs-state-active').eq(0); championshipType = container.attr('id'); getJSONChampionship(championshipType); } function drawCharts() { championshipType = container.parent().attr('id'); championshipId = container.data('id'); width = container.find('.container-chart').eq(0).width(); getJSONValues(championshipType, championshipId); } function drawGenderChart() { if(jQuery('.donut_gender', container).length) { var data = google.visualization.arrayToDataTable([ ['Gender', 'Proportion'], ['Men', jsonValues.participation.men], ['Women', jsonValues.participation.women] ]); var options = { pieHole: 0.4, pieSliceTextStyle: { color: 'white', }, legend: 'none', pieSliceText: 'label', title: 'Participation by gender', titleTextStyle: { color: '#333333', fontName: 'Verdana', fontSize: 14, fontWeight: 'normal' }, width: width, height: 350, }; chartDiv = jQuery('.donut_gender', container).eq(0)[0]; var chart = new google.visualization.PieChart(chartDiv); chart.draw(data, options); } } function drawDisciplineChart() { jsonValues.disciplines.sprint[0] = 'Sprints'; jsonValues.disciplines.hurdle[0] = 'Hurdles'; jsonValues.disciplines.jump[0] = 'Jumps'; jsonValues.disciplines.throw[0] = 'Throws'; jsonValues.disciplines.distance[0] = 'Distance'; jsonValues.disciplines.combined[0] = 'Combined'; var data = google.visualization.arrayToDataTable([ jsonValues.disciplines.names, jsonValues.disciplines.sprint, jsonValues.disciplines.hurdle, jsonValues.disciplines.jump, jsonValues.disciplines.throw, jsonValues.disciplines.distance, jsonValues.disciplines.combined ]); var options = { width: width, height: 300, legend: { position: "none" }, isStacked: true, title: 'Participation by discipline', }; var chart = new google.charts.Bar(jQuery('.chart_disciplines', container).eq(0)[0]); chart.draw(data, google.charts.Bar.convertOptions(options)); } function drawResultsChart() { if(jQuery('.donut_results', container).length) { var data = google.visualization.arrayToDataTable([ ['Round', 'Amount'], ['R1/Q', jsonValues.placing.r1q], ['R2', jsonValues.placing.r2], ['Semifinal', jsonValues.placing.sf], ['Final', jsonValues.placing.f], ['Medals', jsonValues.placing.medals] ]); var options = { pieHole: 0.4, pieSliceTextStyle: { color: 'white', }, legend: 'none', pieSliceText: 'label', title: 'Achieved results', titleTextStyle: { color: '#333333', fontName: 'Verdana', fontSize: 14, fontWeight: 'normal' }, width: width, height: 350 }; var chart = new google.visualization.PieChart(jQuery('.donut_results', container).eq(0)[0]); chart.draw(data, options); } } function drawAgeChart() { if(jQuery('.chart_age', container).length) { var data = google.visualization.arrayToDataTable([ ['', 'Youngest', 'Average', 'Oldest'], [jsonValues.year, jsonValues.ages.youngest, jsonValues.ages.average, jsonValues.ages.oldest] ]); var options = { chart: { title: 'Ages of participants', }, bars: 'vertical', vAxis: {format: 'decimal'}, legend: { position: "none" }, width: width, height: 350, }; var chart = new google.charts.Bar(jQuery('.chart_age', container).eq(0)[0]); chart.draw(data, google.charts.Bar.convertOptions(options)); } } function loadDisciplineNames() { if(jQuery('.chart_disciplines', container).length) { jQuery.getJSON( "../ajax/championships/disciplineNames.php", { ids: JSON.stringify(jsonValues.disciplines.discipline_ids) }, function( data ) { jsonValues.disciplines.names = data; drawDisciplineChart(); }); } } function drawChampionshipsGenderChart() { if(jQuery('.championships-gender-chart', container).length) { var data = new google.visualization.DataTable(); data.addColumn('string', 'Years'); data.addColumn('number', 'Men'); data.addColumn('number', 'Women'); data.addColumn('number', 'Total'); rows = new Array(); jQuery.each(jsonValues, function(index, championship) { rows[index] = new Array(championship.year, championship.participation.men, championship.participation.women, championship.participation.men+championship.participation.women); }); data.addRows(rows); var options = { title : 'Participation by gender', vAxis: {title: 'Nombre'}, hAxis: {title: 'Years'}, seriesType: 'bars', series: {3: {type: 'line'}} }; chartDiv = jQuery('.championships-gender-chart', container).eq(0)[0]; var chart = new google.visualization.ComboChart(chartDiv); chart.draw(data, options); } } function drawChampionshipsAgeChart() { if(jQuery('.championships-age-chart', container).length) { var data = new google.visualization.DataTable(); data.addColumn('string', 'Years'); data.addColumn('number', 'Youngest'); data.addColumn('number', 'Average'); data.addColumn('number', 'Oldest'); rows = new Array(); jQuery.each(jsonValues, function(index, championship) { rows[index] = new Array(championship.year, championship.ages.youngest, championship.ages.average, championship.ages.oldest); }); data.addRows(rows); var options = { title: 'Ages of participants', hAxis: { title: 'Years' }, vAxis: { title: 'Age' } }; chartDiv = jQuery('.championships-age-chart', container).eq(0)[0]; var chart = new google.visualization.LineChart(chartDiv); chart.draw(data, options); } } function drawChampionshipsResultsChart() { if(jQuery('.championships-results-chart', container).length) { var data = new google.visualization.DataTable(); data.addColumn('string', 'Years'); data.addColumn('number', 'R1/Q'); data.addColumn('number', 'R2'); data.addColumn('number', 'Semifinal'); data.addColumn('number', 'Final'); data.addColumn('number', 'Medals'); rows = new Array(); jQuery.each(jsonValues, function(index, championship) { rows[index] = new Array(championship.year, championship.placing.r1q, championship.placing.r2, championship.placing.sf, championship.placing.f, championship.placing.medals); }); data.addRows(rows); var options = { title : 'Achieved results', vAxis: {title: 'Result'}, hAxis: {title: 'Years'}, seriesType: 'bars', series: {5: {type: 'line'}} }; chartDiv = jQuery('.championships-results-chart', container).eq(0)[0]; var chart = new google.visualization.ComboChart(chartDiv); chart.draw(data, options); } } //Comparer Function function GetSortOrder(prop) { return function(a, b) { if (a[prop] > b[prop]) { return 1; } else if (a[prop] < b[prop]) { return -1; } return 0; } }