About the Author

Chris Shiflett

Hi, I’m Chris: entrepreneur, community leader, husband, and father. I live and work in Boulder, CO.

PHP Advent Calendar Day 19

Today's entry is provided by Marcus Börger.

Marcus Börger

Marcus Börger
Marcus Börger is a specialist in C, C++, databases, UML, XML, and of course PHP. To the PHP community, he is also known as helly. As a core developer, he contributes a lot to PHP and focuses on the new OO features of PHP 5 and Zend Engine 2. Marcus has been hacking around on all sorts of things for over 15 years, and being an avid snowboarder, he happily accepted an offer from Google to work in their Zürich office. He can often be found snatching gummy bears from his favorite Italian pizzeria and taking photographs.
Zürich, Switzerland

Before I joined Google, a Swiss engineer had an idea to create an internal service at Google that generates graphs from simple URLs. Shortly thereafter, the service became quite popular internally. During this time, use of the service was restricted; a key was required. This past summer, we decided to open the service to the general public, hoping a few people would like the idea as much as we did. There is now an easy, keyless service that generates various types of charts such as line charts, bar charts, pie charts, and more.

Consider the following example chart:

Hello World

This chart is generated from the following URL:


This URL consists of the base URL (http://chart.apis.google.com/chart?) followed by the query string:

  • The chart type (cht=p3) is a 3D pie chart.

  • The chart size (chs=200x100) is 200 pixels wide and 100 pixels high.

  • The chart title (chtt=Hello+World) is Hello World.

  • The chart data (chd=s:foobar) is the simple string foobar. Choices for encoding are simple (s), extended (e), and text (t). The encoding is separated from the data by a colon.

Incoming data must be scaled to the range of the encoding, so you need to provide a way to do this. When using simple encoding, the API accepts 62 values:

  • Uppercase A (0) through Z (25)

  • Lowercase a (26) through z (51)

  • Digits 0 (52) through 9 (61)

You can indicate a missing value with an underscore (_), and you can separate data sets with a comma (,).

The following function (simple_encoding()) takes care of the encoding for you:

function simple_encoding(array $data, $min = 0, $max = 61) {
             'abcdefghijklmnopqrstuvwxyz' .
    $min = (float)$min;
    $max = (float)$max;
    $diff = ($max - $min) / 61;
    $result = '';
    foreach ($data as $value) {
        if (is_null($data)) {
            $result .= '_';
        } else {
            $value = (int)(((float)$value - $min) / $diff);
            if ($value < 0 || $value > 61) {
                  $result .= '_';
            } else {
                $result .= $codes[$value];
    return $result;
var_dump(simple_encoding(array(-10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100), 20));
var_dump(simple_encoding(array(-10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100), 20, 100));

This produces the following:

string(12) "___AOds7____"
string(12) "___AHPWemt19"

More about the API can be found at chart.apis.google.com.

About this post

PHP Advent Calendar Day 19 was posted on Wed, 19 Dec 2007. If you liked it, follow me on Twitter or share:


1.Jakob Buis said:

Another great (and recent) article on the Google Graphs API can be found @ 24ways 2007

Thu, 20 Dec 2007 at 06:47:11 GMT Link

2.Asanka Dewage said:

Thank You!

Sun, 23 Dec 2007 at 23:41:11 GMT Link

Hello! What’s your name?

Want to comment? Please connect with Twitter to join the discussion.