Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 22 revisions

Category: Helpers

[size=6]Introduction[/size]

The purpose of the gchart helper is to provide functions to help with the use of Google's chart api: http://code.google.com/apis/chart/

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 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(1, 2, 3, 4, 5, 6));

echo <<< EOS line graph of some example data EOS;

?>[/code]

Clone this wiki locally