Skip to content

Instantly share code, notes, and snippets.

@simonw
Created February 21, 2009 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonw/68098 to your computer and use it in GitHub Desktop.
Save simonw/68098 to your computer and use it in GitHub Desktop.
function generateChart(figures) {
// figures is an object mapping labels to numbers
var cht = 'p'; // Chart type: pie
var chs = '460x200'; // Image dimensions
var chd = []; // Chart data
var chl = []; // Corresponding labels
var min = 0;
var max = 0;
$.each(figures, function(label, value) {
chl[chl.length] = label;
chd[chd.length] = value;
max = Math.max(max, value);
});
if (max == 0) {
return ''; // Don't attempt to render blank graphs
}
var chds = '' + min + ',' + max; // Chart data scale
chd = 't:' + chd.join(',');
chl = chl.join('|');
return 'http://chart.apis.google.com/chart?' + [
'cht=' + cht,
'chs=' + chs,
'chd=' + chd,
'chl=' + chl,
'chds=' + chds
].join('&');
}
/* Usage:
var src = generateChart({
"foo": 5,
"bar": 3,
"baz": 6
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment