PHP Advent Calendar Day 19
19 Dec 2007Today's entry is provided by 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:
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:
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
) throughZ
(25
)Lowercase
a
(26
) throughz
(51
)Digits
0
(52
) through9
(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:
<?php
function simple_encoding(array $data, $min = 0, $max = 61) {
$codes = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
'abcdefghijklmnopqrstuvwxyz' .
'0123456789';
$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.