Skip to content

Instantly share code, notes, and snippets.

@battlehorse
Created September 25, 2011 14:22
Show Gist options
  • Save battlehorse/1240641 to your computer and use it in GitHub Desktop.
Save battlehorse/1240641 to your computer and use it in GitHub Desktop.
Attempt to draw Google Charts in Chrome extension popups, while loading the API in the background page
<!DOCTYPE html>
<html>
<head>
// Autoload the 'corechart' visualization library, as described in:
// http://code.google.com/apis/chart/interactive/docs/library_loading_enhancements.html#enhancedloading
//
// the 'autoload' parameter is URI encoded.
<script type="text/javascript" src="https://www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22visualization%22%2C%22version%22%3A%221%22%2C%22packages%22%3A%5B%22corechart%22%5D%7D%5D%7D"></script>
</head>
<body>
</body>
</html>
{
"name": "Chart Extension",
"version": "1.0",
"description": "Chart Extension.",
"browser_action": {
"default_popup": "popup.html"
},
"background_page": "background.html"
}
<!DOCTYPE html>
<html>
<body>
<div id="chart_div">
</div>
<script>
var background = chrome.extension.getBackgroundPage();
if (background.google && background.google.visualization) {
var data = new background.google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Sales');
data.addColumn('number', 'Expenses');
data.addRows(4);
data.setValue(0, 0, '2004');
data.setValue(0, 1, 1000);
data.setValue(0, 2, 400);
data.setValue(1, 0, '2005');
data.setValue(1, 1, 1170);
data.setValue(1, 2, 460);
data.setValue(2, 0, '2006');
data.setValue(2, 1, 860);
data.setValue(2, 2, 580);
data.setValue(3, 0, '2007');
data.setValue(3, 1, 1030);
data.setValue(3, 2, 540);
var chart = new background.google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240, title: 'Company Performance'});
} else {
// Google Visualization API have not been loaded yet by the background page.
// Deal with it.
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment