-
Notifications
You must be signed in to change notification settings - Fork 0
gchart
[size=6]Introduction[/size]
The purpose of the gchart helper is to provide functions to help with the use of [url=http://code.google.com/apis/chart/]Google's chart api[/url].
Currently, the only function in the helper is extendedencode(). To use the gChart api, one generates a series of HTTP GET parameters attached to a google url, which returns an image. The most complicated part of generating this url is encoded the data that represents the chart. This helper makes that easy.
[size=6]Source[/size] [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
- CodeIgniter
- An open source application development framework for PHP 4.3.2 or newer
- @package CodeIgniter
- @author Rick Ellis
- @copyright Copyright (c) 2006, EllisLab, Inc.
- @license http://www.codeigniter.com/user_guide/license.html
- @link http://www.codeigniter.com
- @since Version 1.0
- @filesource */
/**
- CodeIgniter gChart Helper
- @package CodeIgniter
- @subpackage Helpers
- @category Helpers
- @author Isaac Vetter */
/**
-
ExtendedEncode
-
Encodes an array of values using the extended encoding:
-
http://code.google.com/apis/chart/#extended_values
-
@access public
-
@param array
-
@param referencedstring
-
@return string */ function extendedencode($data, &$maxvalue='notspecified') { $grid = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '.');
// try to find reasonable maximum value if(is_numeric($maxvalue)){ // assume below manipulations are better than what caller is doing $max = ceil($maxvalue); } else { $max = ceil(max($data)); } $precision = strlen($max) - 1; $maxvalue = ceil($max/pow(10,$precision)); $maxvalue = $maxvalue * pow(10, $precision);
$multiplier = (float)(count($grid) * count($grid)) / $maxvalue;
$ret = ''; for($i=0;$i<count($data);$i++){ if(!is_numeric($data[$i])){ $ret .= '__'; } else { $datum = $data[$i] * $multiplier; $x = (int)($datum / count($grid)); $y = $datum % 64; $ret .= $grid[$x].$grid[$y]; } } return $ret; } ?> [/code]
[size=4]Usage[/size]
Here's an example of how to use this helper to generate a chart. To begin using it, add the following code to your controller:
[code]$this->load->helper( 'gchart' );[/code]
Next, add the following bit of code to your view where you would like to display the chart:
[code]<?php
$encoded_data = extendedencode(array(0, 1, 2, 3, 4, 5, 6) &maxvalue;);
echo <<< EOS EOS;
?>[/code]
The above code generate this url as the img src: [url=http://chart.apis.google.com/chart?cht=lc&chs=250x250&chd=e:AAKqVVgAqq1VA]http://chart.apis.google.com/chart?cht=lc&chs=250x250&chd=e:AAKqVVgAqq1VA[/url]
Which looks like this:
The Google Chart API is pretty awesome. The above really doesn't do it justice. You should read the [url=http://code.google.com/apis/chart/]Google documentation[/url].
Here's a more complicated example:
[code]<?php
$encoded = extendedencode(array(3.6, 1, 2, 1.5, 4.0, 5.6, 2.3), &$maxvalue); $halfmaxvalue = $maxvalue / 2; $chart = <<< EOS EOS;
print $chart;
?>[/code]
The above code generate this url as the img src: [url=http://chart.apis.google.com/chart?cht=lc&chs=250x250&chd=e:AAKqVVgAqq1VA]http://chart.apis.google.com/chart?cht=lc&chs=250x250&chd=e:AAKqVVgAqq1VA[/url] [url=http://chart.apis.google.com/chart?cht=bvg&chs=300x400&chd=e:mZKqVVQAqq7uYi&chts=b1946c,20&chxt=x,x,y,y&chxl=0:|Jan|Feb|Mar|Apr|May|June|July|1:||Months||2:|0|3|6|3:||||(in bazillions)|Traffic||&chf=c,ls,0,FFFFFF,.5,EEEEEE,.5]http://chart.apis.google.com/chart?cht=bvg&chs=300x400&chd=e:mZKqVVQAqq7uYi&chts=b1946c,20&chxt=x,x,y,y&chxl=0:|Jan|Feb|Mar|Apr|May|June|July|1:||Months||2:|0|3|6|3:||||(in bazillions)|Traffic||&chf=c,ls,0,FFFFFF,.5,EEEEEE,.5[/url] Which looks like this: