Skip to content
Derek Jones edited this page Jul 5, 2012 · 12 revisions

Category:Library | Category:Library::External | Category:Library::Charts

Introduction

This is a brief summary of how to integrate the JpGraph charting library with CodeIgniter. JpGraph is a comprehensive charting package with support for over 15 chart types. The source code is available for download (there are separate versions for PHP4 and PHP5). Keep in mind that JpGraph has a very large codebase, so be sure to include only the specific libraries you need for each chart.

Note: This example was tested using Windows, Apache 2 w/mod_rewrite, PHP 5.1, CodeIgniter 1.5.2

Installation

Setup

1) Download JpGraph (I used version 2.14 for PHP5) and copy the contents of the "src" folder to the "system/plugins/jpgraph" directory. 2) Create a directory "temp" in your document root (same directory as your index.php) to store the generated charts. Make sure this directory is writable by Apache. 3) If you use mod_rewrite, add the "temp" directory to your .htaccess exclusion list. My .htaccess file now looks like the following:

RewriteEngine on
RewriteRule ^$ /index.php [L]
RewriteCond $1 !^(index\.php|img|css|temp|js|video_files|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]

Create the CI Files

1) Create the Plugin “jpgraph_pi.php” in “system/plugins” with a function for each of your required chart types (only use the libraries you need for any given chart to reduce overhead). The charting code is from the example0.php file included with the JpGraph download.

<?php
function linechart($ydata, $title='Line Chart')
{
    require_once("jpgraph/jpgraph.php");
    require_once("jpgraph/jpgraph_line.php");    
    
    // Create the graph. These two calls are always required
    $graph = new Graph(350,250,"auto",60);
    $graph->SetScale("textlin");
    
    // Setup title
    $graph->title->Set($title);
    
    // Create the linear plot
    $lineplot=new LinePlot($ydata);
    $lineplot->SetColor("blue");
    
    // Add the plot to the graph
    $graph->Add($lineplot);
    
    return $graph; // does PHP5 return a reference automatically?
}
?>

2) Create the Controller (system/application/controllers/chart.php):

<?php

class Chart extends Controller {

    function Chart()
    {
        parent::Controller();
        $this->load->helper('url');
        $this->load->plugin('jpgraph');    
    }
    
    function index()
    {
        $data['title'] = "Using JpGraph from CodeIgniter 1.5";        
        $data['heading'] = "Example 0 in JpGraph 2.1.4";        
            
        // Setup Chart
        $ydata = array(11,3,8,12,5,1,9,13,5,7); // this should come from the model        
        $graph = linechart($ydata, 'This is a Line Chart');  // add more parameters to plugin function as required
        
        // File locations
        // Could possibly add to config file if necessary
        $graph_temp_directory = 'temp';  // in the webroot (add directory to .htaccess exclude)
        $graph_file_name = 'test.png';    
        
        $graph_file_location = $graph_temp_directory . '/' . $graph_file_name;
                
        $graph->Stroke('./'.$graph_file_location);  // create the graph and write to file
        
        $data['graph'] = $graph_file_location;
        
        $this->load->view('chart_view', $data);
    }
}
?>

3) Create the View (system/application/views/chart_view.php):

<html>
<body>
<head>
<title><?=$title?></title>
</head>
<h1>&lt;?=$heading?&gt;</h1>
<img src="&lt;?= base_url(). "/>
&lt;/body&gt;
&lt;/html&gt;

That’s it! On my configuration, I run the file as http://charting/chart (using an apache virtual host, mod_rewrite, and $config[’base_url’] = “http://charting”; and $config[’index_page’] = “”;

There are clearly many improvements that can be made to the code (e.g. models, more dynamic plugin structure, etc.).

Update for Codeigniter 2+

This tutorial was useful but some updates had to be made for the changes in codeigniter 2+ with the removal of the plugins folder. • copied contents of the src folder to "application/libraries/jpgraph" • created a file "application/libraries/Jpgraph.php"

&lt;?php
class Jpgraph {
    function linechart($ydata, $title='Line Chart')
    {
        require_once("jpgraph/jpgraph.php");
        require_once("jpgraph/jpgraph_line.php");    
        
        // Create the graph. These two calls are always required
        $graph = new Graph(350,250,"auto",60);
        $graph->SetScale("textlin");
        
        // Setup title
        $graph->title->Set($title);
        
        // Create the linear plot
        $lineplot=new LinePlot($ydata);
        $lineplot->SetColor("blue");
        
        // Add the plot to the graph
        $graph->Add($lineplot);
        
        return $graph; // does PHP5 return a reference automatically?
    }
}

• in my controller construct

$this->load->library('jpgraph');

• and instead of

$graph = linechart($ydata, 'This is a Line Chart');  // add more parameters to plugin function as required

call the function as so:

$graph = $this->jpgraph->linechart($ydata, 'This is a Line Chart');  // add more parameters to plugin function as required

Categories

@todo - make ToC from/for directories, and reorganize

Repository

Applications [Controller] [Core] [DMZ_Extensions] [Helper] [Internationalization] [Libraries] [Libraries:Permissions] [Library] [Library:Views] [Models] [Module] [Plugin] [Views]

Training

[Analytics] [Approaches] [Books] [Config] [Getting Started] [Help] [Help:TipsAndTricks] [IIS]

Communities

[Contributions] [Contributions - Modifications] [Groups] [Links] [Wiki]

Clone this wiki locally