Skip to content

Instantly share code, notes, and snippets.

@tshaddix
Last active December 21, 2015 09:59
Show Gist options
  • Save tshaddix/6288549 to your computer and use it in GitHub Desktop.
Save tshaddix/6288549 to your computer and use it in GitHub Desktop.
Using a geochart with OOcharts
oo.load(function(){
var regionQuery = new oo.Query('PROFILE ID', "30d");
//Add metrics, dimensions, and filter for region
regionQuery.addMetric('ga:visits');
regionQuery.addDimension('ga:metro');
regionQuery.setFilter('ga:region==California');
regionQuery.execute(function(data){
var _rows = data.rows;
//remove first row (not set row)
_rows.shift();
// Add column headers
_rows.unshift(['State', 'Visits']);
var dt = google.visualization.arrayToDataTable(_rows);
var _ele = document.getElementById('regionGeo');
var regionGeo = new google.visualization.GeoChart(_ele);
regionGeo.draw(dt, {
backgroundColor : 'transparent',
region : 'US-CA',
resolution: 'provinces',
displayMode : 'markers',
colorAxis: {colors: ['#1abc9c','#f1c40f', '#e67e22', '#e74c3c']},
datalessRegionColor : 'transparent'
});
});
});
/*
* Loads OOcharts dependencies
* @param {Function} callback
*/
var _load = function(callback){
if(!_apiKey) throw "Set APIKey with oo.setAPIKey before calling load";
// load JSAPI
var load_jsapi = function (callback) {
if (typeof google === 'undefined') {
_loadScript("https://www.google.com/jsapi", callback);
}
else { callback(); }
};
// load Google Visualization
var load_visualization = function (callback) {
if (typeof google.visualization === 'undefined') {
google.load("visualization", "1", { packages: ['corechart', 'table', 'geochart'], 'callback': callback });
}
else {
var necpac = [];
if (typeof google.visualization.corechart === 'undefined') {
necpac.push('corechart');
}
if (typeof google.visualization.table === 'undefined') {
necpac.push('table');
}
if(typeof google.visualization.geochart === 'undefined') {
necpac.push('geochart');
}
if (necpac.length > 0) {
google.load("visualization", "1", { packages: necpac, 'callback': callback });
}
}
};
var cb = callback;
load_jsapi(function () {
load_visualization(function () {
if(cb) cb();
// run autobuild
_autoBuild();
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment