Skip to content

TeeChart and CodeIgniter

World Wide Web Server edited this page Jul 4, 2012 · 10 revisions

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

[h3]Introduciton[/h3] 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].

[h3]Requirements[/h3]

GD Library. PHP5 or above.

[h3]Features[/h3]

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

[h3]Installation[/h3]

[b]1)[/b] 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). [b]2)[/b] 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. [b]3)[/b] If you use mod_rewrite, add the “temp” directory to your .htaccess exclusion list.

[h3]Example of use[/h3]

[h3]Controller[/h3]

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

load the view.

[code] <?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);        
}

} ?> [/code]

[h3]View[/h3]

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

[code] <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>

<?php echo $title;?>


</body> </html> [/code]

[h3]Output[/h3]

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

Image:chart.png

[h3]Download[/h3]

File:TeeChartAndCodeIgniter.zip contains the example files.

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

Clone this wiki locally