Code Example

Last modified: August 20, 2009 - 06:39

Code Example

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


$data = array();
for(
$i=0; $i<6; $i++ )
{
 
$data[] = rand(5,9);
}

$g = new open_flash_chart_api();
$g->set_title( 'Bar Chart', '{font-size: 20px;}' );
$g->set_width(200);

//
// BAR CHART:
//
$g->set_data( $data );
$g->bar( 50, '#9933CC', 'Page views', 10 );
//
// ------------------------
//

//
// X axis tweeks:
//
$g->set_x_labels( array( 'January' ,'February', 'March', 'April', 'May', 'June' ) );
//
// set the X axis to show every 2nd label:
//
$g->set_x_label_style( 10, '#9933CC', 0, 2 );
//
// and tick every second value:
//
$g->set_x_axis_steps( 2 );
//

$g->set_y_max( 10 );
$g->y_label_steps( 4 );
$g->set_y_legend( 'Open Flash Chart', 12, '#736AFF' );
$g->set_bg_colour('0xDFFFDF');
echo
$g->render();
?>

MULTI Bar chart

mattez - June 30, 2008 - 10:09

How to implement multi bar open chart:
http://drupal.org/node/200860#comment-660780

Pie chart function

jkantarek - July 23, 2008 - 21:12

Here's some code to generate a pie chart with random colors for each pie piece. It works for multiple charts on the same page as well

<?php
function generate_pie_chart($chart_data, $chart_labels, $chart_links,$title,$width,$height)
{
srand((double)microtime()*1004000);
$colors = array();
for(
$i=0; $i< count($chart_data); $i++)
   { 
      
$colors[$i]=sprintf("#%u%u%u%u%u%u",
                                     
dechex(mt_rand(0,15)),
                                     
dechex(mt_rand(0,15)),
                                     
dechex(mt_rand(0,15)),
                                     
dechex(mt_rand(0,15)),
                                     
dechex(mt_rand(0,15)),
                                     
dechex(mt_rand(0,15))
                                     );
   }
   
$chart = new open_flash_chart_api();
   
$chart->pie(60,'#505050','{font-size: 12px; color: #404040;');
   
$chart->pie_values( $chart_data, $chart_labels,$chart_links );
   
$chart->pie_slice_colours( $colors );
   
$chart->set_tool_tip( 'Value: #val#%&' );
   
$chart->title( $title, '{font-size:18px; color: #d01f3c}' );
   
$chart->set_width($width);
   
$chart->set_height($height);
    echo
$chart->render();
}
?>

The original code

dr.fang - January 17, 2009 - 09:33

The original code is:
$g->set_x_labels( array( 'January,February,March,April,May,June' ) );

This makes these six month as one key. Hence it will display not properly in the chart. It should be:

$g->set_x_labels( array( 'January' ,'February', 'March', 'April', 'May', 'June' ) );

Scatter Plot

mrwendell - June 18, 2009 - 05:50

So any ideas how to create a scatter plot, or better yet an X by Y line with smoothing? I have been trying for hours with no luck even getting a scatter plot.

Not sure. A lot of info can

redndahead - June 22, 2009 - 20:41

Not sure. A lot of info can be found on the openflashchart website. Scatter can be found here http://teethgrinder.co.uk/open-flash-chart/gallery-scatter.php Alot of the info there can be correlated into this module. This will be even more the case when OFC v2 is completed and V3 of this module.

How does it works?

SilentAssasin - June 21, 2009 - 23:01

Please, tell me, where I can use this code (in Code Example post)

Thanks in advance

Apparently this

brandon.dixon - September 23, 2009 - 04:31

Apparently this (http://drupal.org/project/charts) module can use it, but the documentation is sparse. I am attempting to get this working now, so if I succeed I will post some examples.

dueyesterday.net - Documentation for the masses

So if you are interested you

brandon.dixon - September 23, 2009 - 05:39

So if you are interested you can use the module I mentioned above to get the charts to display within your Drupal site. Its actually pretty easy to make it happen.

Follow these instructions to initially install the Open Flash Charts API and Open Flash Charts package:
http://drupal.org/node/193583

Once you have set this up download and install the Charts module:
http://drupal.org/project/charts

Once you have this installed be sure to enable the Open Flash Charts part of the module. After this is enabled you can begin using the charts in your code with the following sample code:

function name()
{
        $employee_chart = array(
        '#title'        => 'Weekly Employee'
        '#plugin'       => 'openflashchart',
        '#type'         => 'pie2D',
        '#height'       => 200,         // in pixels
        '#width'        => 450,         // in pixels

        array(
          array('#value' => 75),
          array('#value' => 25),
          ),
        );

        return charts_chart($employee_chart);
}

The actual documentation for the Charts module is decent, but they neglect to mention a few small things that one would need to know to construct an actual chart (at least in my opinion). Once you have that code in place the chart should load with no problem. One of the current bugs as of today is that the Titles for the charts show up extremely small. Take a look at this (http://drupal.org/node/555232) bug for a solution to that issue. Hope all this helps.

dueyesterday.net - Documentation for the masses

 
 

Drupal is a registered trademark of Dries Buytaert.