generateChart() for creating a Google Chart API pie chart from JavaScript

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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('&');
}

More like this

  1. Last.fm Charts by sedden 5 years, 9 months ago
  2. Generate QR Code image for a string by johnnoone 4 years, 11 months ago
  3. Publishing service endpoint uri to javascript by dberansky 3 years, 10 months ago
  4. Google Charts Templatetags (Python) by justquick 5 years, 9 months ago
  5. Google Charts Templatetags (HTML) by justquick 5 years, 11 months ago

Comments

(Forgotten your password?)