Google Chart API in PHP

Google have released the Google Chart API which enables you to produce swizzy graphs like the above, which is the total 'hours-listened' to BBC Radio 1 from March 1999 to September 2007.

In their API documentation, they publish some JavaScript code to help with encoding the URL.

Here's the same thing in PHP - which, by default, will also sort out the maximum size as the maximum value in the array you pass it.


// Here's an array containing some data to plot

// Here's where we call the chart, and return the encoded chart data
echo "<img src=".urlencode("It's an example!")."&cht=lc&chs=450x125&chd=".chart_data($test_data).">";

// And here's the function

function chart_data($values) {

// Port of JavaScript from

// First, find the maximum value from the values given

$maxValue max($values);

// A list of encoding characters to help later, as per Google's example
$simpleEncoding 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$chartData "s:";
  for (
$i 0$i count($values); $i++) {
$currentValue $values[$i];

    if (
$currentValue > -1) {
      else {

// Return the chart data - and let the Y axis to show the maximum value
return $chartData."&chxt=y&chxl=0:|0|".$maxValue;


Download this code