Skip to content

TeeChart and CodeIgniter

Derek Jones edited this page Jul 5, 2012 · 10 revisions

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

Introduciton

TeeChart for PHP can be used as a library for CodeIgniter. It allows you to setup a Chart with with a few lines of

code the easy way. This library includes many Chart styles, Functions, Tools and lot of features which makes it very

customisable.

Don't forget to check the [url=http://www.steema.com/products/teechart/php/demos/Features/]Online Demo[/url] from

our web site.

You can also download the [url=http://www.steema.com/downloads/dwn_tch_php.html]Eval version[/url].

Requirements

GD Library. PHP5 or above.

Features

Please check all the [url=http://www.steema.com/products/teechart/php/specifications.html]Features[/url]

Installation

1) Download TeeChart for PHP and copy the contents of the “source” folder to the “system/libraries/teechart” directory. If you're using the Eval version you will need to download the PHPExpress loaders, copy them into the webroot folder and set as an extension. You can find detailed instructions in the Install.txt file (included with the zip installer). 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.

Example of use

Controller

Create a new Controller called "teechart" which will be used to load the TeeChart library, to setup the Chart and load the view.

<?php

class TeeChart extends Controller {

    function TeeChart()
    {
        parent::Controller();

        $this->load->helper('url'); 
        $this->load->library('teechart/TChart');    
    }
    
    function index()
    {
        
        $data['title'] = "Using TeeChart from CodeIgniter";        
           
        // Setup Chart
        // The Data could com from a function defined into the model, which could be adquired 
        // from a database.
        $ydata = array(20,20,20,20,20);   
        $labels = array('Cars','Phones','Bikes','Computers','Motorbikes');        
        
        $chart = new TChart(500,350);
        $chart->getChart()->getHeader()->setText("Pie Style");
        $chart->getChart()->getAspect()-> setChart3DPercent(35);

        // Here we use the Pie Style, but TeeChart includes many Series types (like Bar, 
        // HorizBar, Line, HorizLine, Area, HorizArea, Candle, Volume, etc.. ). Check them !
        
        $pie=new Pie($chart->getChart());
        $pie->getMarks()->setVisible(true);
        $pie->getMarks()->setTransparent(true);
        $pie->getMarks()->setArrowLength(-65);
        $pie->getMarks()->getArrow()->setVisible(false);
        $pie->setCircled(true);
        ThemesList::applyTheme($chart->getChart(),1);  // BlackIsBack 
        $pie->getMarks()->getFont()->setColor(Color::Black());
                
        // Setup the Pie
        $pie->setBevelPercent(20);
        $pie->addArray($ydata);
        $pie->setLabels($labels);

        // File locations
        // Could possibly add to config file if necessary
        // In the webroot (add directory to .htaccess exclude)
        $chart_temp_directory = 'temp';  
        
        // Create the Chart and write to file 
        $chart->render($chart_temp_directory . '/' . 'chart.png');    
        $data['chart'] = $chart_temp_directory . '/' . 'chart.png';
        
        $this->load->view('chart_view', $data);        
    }
}
?> 

View

Now we just have to create the view called "chart_view" which will output the generated file.

<html>
<head>
<style type="text/css">

body {
 background-color: #fff;
 margin: 40px;
 font-family: Lucida Grande, Verdana, Sans-serif;
 font-size: 14px;
 color: #4F5155;
}

h2 {
 color: #444;
 background-color: transparent;
 border-bottom: 1px solid #D0D0D0;
 font-size: 16px;
 font-weight: bold;
 margin: 24px 0 2px 0;
 padding: 5px 0 6px 0;
}


<title><?php echo $title?></title>
</style>
</head> 
<body>
<h2>&lt;?php echo $title;?&gt;</h2>
<br>
<img src="&lt;?php echo  base_url(). $chart?&gt;"/>
&lt;/body&gt;
&lt;/html&gt; 

Output

That’s it! On my configuration, I run the file as http://mysite/chart.

Image:teechart.png

Download

File:TeeChartAndCodeIgniter.zip contains the example files.

Enjoy with TeeChart for PHP ! [url=http://www.steema.com/]Steema Software[/url]

Clone this wiki locally