PHP Advent Calendar Day 19

19 Dec 2007

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

Marcus Börger

Name
Marcus Börger
Blog
marcus-boerger.de
Biography
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.
Location
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:

http://chart.apis.google.com/chart?...mp;chd=s:foobar

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

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:

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:

  1. <?php
  2.  
  3. function simple_encoding(array $data, $min = 0, $max = 61) {
  4.     $codes = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
  5.              'abcdefghijklmnopqrstuvwxyz' .
  6.              '0123456789';
  7.     $min = (float)$min;
  8.     $max = (float)$max;
  9.     $diff = ($max - $min) / 61;
  10.     $result = '';
  11.  
  12.     foreach ($data as $value) {
  13.         if (is_null($data)) {
  14.             $result .= '_';
  15.         } else {
  16.             $value = (int)(((float)$value - $min) / $diff);
  17.  
  18.             if ($value < 0 || $value > 61) {
  19.                   $result .= '_';
  20.             } else {
  21.                 $result .= $codes[$value];
  22.             }
  23.         }
  24.     }
  25.  
  26.     return $result;
  27. }
  28.  
  29. var_dump(simple_encoding(array(-10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100), 20));
  30. var_dump(simple_encoding(array(-10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100), 20, 100));
  31.  
  32. ?>

This produces the following:

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

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