Created
November 15, 2012 17:39
-
-
Save bspingarn/4080007 to your computer and use it in GitHub Desktop.
Google Charts API - chart loader module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a chart loading module to simply initializing charts that use a custom API to populate the data tables | |
google.load('visualization', '1', {'packages': ['corechart','geochart']}); | |
google.setOnLoadCallback(function(){ | |
coCharts.init(); | |
}); | |
var coCharts = (function(){ | |
var colors = { | |
orange: '#feb938', | |
teal: '#4ac0c0', | |
lightTeal: '#9ad9e2', | |
darkTeal: '#298280', | |
blue: '#558dca', | |
purple: '#765aa6', | |
fuscia: '#f1396f', | |
lightGreen: '#d3e27d' | |
}; | |
var chartData = {}; | |
var Chart = function(googleChartType, targetID, apiMethod, options){ | |
this.googleChartType = googleChartType; | |
this.targetID = targetID; | |
this.dataName = targetID; | |
this.apiMethod = apiMethod; | |
this.options = options; | |
this.showChart = function(){ | |
$('#' + this.targetID).addClass('loaded'); | |
}; | |
this.init = function(){ | |
var chart = new google.visualization[this.googleChartType](document.getElementById(this.targetID)); | |
var that = this; | |
if (that.dataName in chartData){ //redraw from page resize | |
chart.draw(chartData[that.dataName], that.options); | |
} | |
else { | |
coAPI[that.apiMethod](function(response){ // new page load | |
chartData[that.dataName] = google.visualization.arrayToDataTable(response.data); | |
chart.draw(chartData[that.dataName], that.options); | |
that.showChart(); | |
}); | |
} | |
}; | |
}; | |
var incomeDistribution = new Chart('ColumnChart', 'chart-income-target', 'get_income_distribution_for_chart', { | |
colors: [colors.lightGreen], | |
height: 300, | |
backgroundColor: {fill: "none"}, | |
vAxis: {title: "Income"}, | |
hAxis: {title: "Panelists"}, | |
legend: {position: 'none'} | |
}); | |
var ageDistribution = new Chart('BarChart', 'chart-age-target', 'get_age_distribution_for_chart', { | |
height: 300, | |
backgroundColor: {fill: "none"}, | |
vAxis: {title: "Age"}, | |
hAxis: {title: "Panelists"}, | |
colors: [colors.fuscia], | |
legend: {position: 'none'} | |
}); | |
var genderDistribution = new Chart('PieChart', 'chart-gender-target', 'get_gender_distribution_for_chart', { | |
colors: [colors.blue, colors.teal, colors.lightGreen], | |
height: 300, | |
backgroundColor: {fill: "none"}, | |
legend: {alignment: 'center'} | |
}); | |
var usaRegionDistribution = new Chart('GeoChart', 'chart-geolocation-target', 'get_state_distribution_for_chart', { | |
region: "US", | |
resolution: "provinces", | |
colorAxis: { | |
colors: [colors.orange] | |
}, | |
backgroundColor: {fill: "none"}, | |
height: 530 | |
}); | |
return { | |
init: function() { | |
incomeDistribution.init(); | |
ageDistribution.init(); | |
genderDistribution.init(); | |
usaRegionDistribution.init(); | |
} | |
}; | |
})(); | |
$(function() { | |
$(window).smartresize(function(){ | |
coCharts.init(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment