Open Flash Chart
Support This Project :: Home - Do more with less - advanced
Info: Open Flash Chart 2 is out. Version 1.x and these pages will never disappear and the charts will continue working forever, but further development on 1.x has stopped. Take a look at version 2 here Open Flash Chart 2.

Do more with less - advanced

If you are constantly setting up your own style of graph over and over, then using PHP to do the grunt work is a nice option.

PHP will let you do some simple class inheritance. We create a new chart class called my_chart and inherit from graph. Then in the constuctor we call the base class (graph) constructor to set up all the default values. After that we overwrite some of the defaults:

Create the flash object
<?php

include_once( 'ofc-library/open-flash-chart.php' );

class 
my_chart extends graph
{
        function 
my_chart()
        {
                
// this calls the base object constructor
                
parent::graph();
                
                
// now override some of the default settings:
                
                // the new default title style will be:
                // #ff00CC = bright pink
                
$this->title_style '{font-size: 45px; color: #FF00CC}';
        }
}

?>

In the above example we have set the default title size to 45 pixels and the default title colour to bright pink.

Now save this file as 'my_chart.php' on your server. We will include this file in our data page (see below).

This is what it looks like:


Create the flash object
<?php
include_once 'ofc-library/open_flash_chart_object.php';
open_flash_chart_object400250'http://'$_SERVER['SERVER_NAME'] .'/open-flash-chart/gallery-data-11.php' );
?>

In the data page, we:

  • include_once the 'my_chart.php' file we wrote.
  • create a new my_chart object, and then use it like normal.
  • Note how we call $g->title( 'Portreath beach' ); without setting the optional parameters. We don't want to over write the new defaults we set in my_chart

gallery-data-11.php
<?php

// generate some random data
srand((double)microtime()*1000000);

$tmp = array();
for( 
$i=0$i<8$i+=0.2 )
{
  
$tmp[] = sin($i) + 1.5;
}

//
// our new chart object
//
include_once( 'my_chart.php' );
$g = new my_chart();
//
//
//

$g->set_data$tmp );
$g->set_y_max);
$g->y_label_steps);
$g->set_y_legend'Wave height (cm)'15'#736AFF' );
$g->set_x_legend'Waves in Feburary'20'#736AFF' );

//
// NOTE: how the title picks up our new default settings.
//
$g->title'Portreath beach' );
echo 
$g->render();
?>
To see the data produced : gallery-data-11.php, then 'view source'.

This means you don't have to set your title size and colour on each data page. You can easily add more defaults into the my_chart object so both the X and Y axis are set to your liking.

Take a look at the PHP files to see all the default values.

Help support the project: Support This Project

Adverts: