Part of the EllisLab Network
   
2 of 2
2
OpenFlashChart 2 and Codeigniter
Posted: 28 October 2009 07:35 PM   [ Ignore ]   [ # 16 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  03-07-2007

EDIT:

This is old information

See this post for an update.

I’ll leave the contents of this post, as it is still valid (but a rather complicated approach).

br,
Thomas

***

OLD

I created a small library that allows to use Open Flash Chart 2 in CodeIgniter.

This is the old file.


Library

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Provides a factory for Open Flash Chart2 objects
 *
 * @package CodeIgniter
 * @subpackage Open Flash Chart 2
 * @category Library
 * @author thomas(at)kbox.ch
 */
class Ofc2Factory
{
    
/**
     * Constructor
     * 
     * Loads OFC2 class definition files. Need to change working directory
     * temporarily, so that the links within the original class files work
     * for loading
     */
    
public function __construct()
    
{
        $old_cwd 
getcwd();
        
chdir(dirname(__FILE__)); //change cwd to the dir of this file
        
require_once('OFC/OFC_Chart.php');
        
chdir($old_cwd);
    
}

    
/**
     * Creates OFC2 objects from a passed classname and optional
     * array of arguments
     *
     * @param string $classname
     * @param array $arguments
     * @return mixed
     */
    
public function create($classname$arguments = array())
    
{
        
// check if class is defined
        
if (class_exists($classname))
        
{
            
return call_user_func_array(
                    array(new 
ReflectionClass($classname), 'newInstance'),
                    
$arguments
                   
);
        
}
        
else
        
{
            
die("Sorry can't create the object, class [$classname] not defined");
        
}
    }

It works as an object factory for OFC2 objects.
The library expects to find the open flash chart classes folder ‘OFC’ inside the libraries folder.

Usage:

Controller

<?php

/**
 * OFC2 Chart Controller
 * 
 * @package CodeIgniter
 * @author thomas(at)kbox.ch
 */
class Ofc2_chart extends Controller {

    
/**
     * Constructor
     */
    
function __construct()
    
{
        parent
::__construct();
    
}

    
/**
     * Gets called automatically if no controller function given
     */
    
public function index()
    
{
        $this
->run_test();
    
}

    
/**
     * Loads the OFC2 test view
     */
    
public function run_test()
    
{
        $data 
= array(
                    
'data_url' => $this->config->item('base_url').'ofc2_chart/get_data',
                    
'page_title' => 'OFC2 Test'
                
);

        
$this->load->view('ofc2_chart_view'$data);
    
}

    
/**
     * Generates data for OFC2 in json format (example)
     */
    
public function get_data()
    
{
        
// load the factory and give it a shorthand alias
        
$this->load->library('Ofc2factory'NULL'ofc2');

        
// start creating objects
        
$title $this->ofc2->create('OFC_Elements_Title', array("Our new House Schedule"));

        
$hbar $this->ofc2->create('OFC_Charts_Bar_Horizontal');
        
$hbar->append_value($this->ofc2->create('OFC_Charts_Bar_Horizontal_Value', array(0,4)));
        
$hbar->append_value($this->ofc2->create('OFC_Charts_Bar_Horizontal_Value', array(4,8)));
        
$hbar->append_value($this->ofc2->create('OFC_Charts_Bar_Horizontal_Value', array(8,11)));

        
$chart $this->ofc2->create('OFC_Chart');
        
$chart->set_title($title);
        
$chart->add_element($hbar);
        
$chart->add_y_axis($this->ofc2->create('OFC_Elements_Axis_Y'));

        
$x $this->ofc2->create('OFC_Elements_Axis_X');
        
$x->set_offset(false);
        
$x->set_labels_from_array(array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'));
        
$chart->set_x_axis($x);

        
$y $this->ofc2->create('OFC_Elements_Axis_Y');
        
$y->set_offset(true);
        
$y->set_labels(array("Make garden look sexy","Paint house","Move into house"));
        
$chart->add_y_axis($y);

        
// send response (json)
        
echo $chart->toString();
    
}


View (mangled by forum SW, see linked file in Wiki)

<html>
    <
head>
        <
title><?$page_title ?></title>
        <
base href="<?= $this->config->item('base_url') ?>" />
        
[removed][removed]
    
</head>

    <
body>

        <
h1><?$page_title ?></h1>
        
[removed]
            swfobject
.embedSWF(
              
"assets/swf/open-flash-chart.swf""test_chart""950""150",
              
"9.0.0""expressInstall.swf",
              
{"data-file":"<?= urlencode($data_url) ?>"}
              
);
        
[removed]

        
<div id="test_chart"></div>

    </
body>
</
html

br,
Thomas

Profile
 
 
Posted: 16 November 2009 06:52 AM   [ Ignore ]   [ # 17 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  743
Joined  09-11-2008

really nice library and working fine with my poll module i really love this library and work done by @Mesozoic and @pickupman great job.

 Signature 

CI,JQuery,Google Maps | widget with CI loader | Thumbnail, Image Resize, Image Crop Helper | CI shortcode

Profile
 
 
Posted: 18 January 2010 09:06 AM   [ Ignore ]   [ # 18 ]  
Summer Student
Total Posts:  1
Joined  01-18-2010

Hello everybody,

I’m trying to implement this in my website. But i have trouble using the library. Does somebody have some examples for me how to make a line graph?

The example above works, but how do i change the values for a line chart. I tried this:

public function get_start_data()
    
{
        $data_1 
= array();
        
$data_2 = array();
        
$data_3 = array();
        
        for( 
$i=0$i<6.2$i+=0.2 )
        
{
          $data_1[] 
= (sin($i) * 1.9) + 10;
          
$data_2[] = (sin($i) * 1.9) + 7;
          
$data_3[] = (sin($i) * 1.9) + 4;
        
}
        
// load the factory and give it a shorthand alias
        
$this->load->library('Ofc2factory'NULL'ofc2');

        
// start creating objects
        
$title $this->ofc2->create('OFC_Elements_Title', array("Test"));

        
$line_l $this->ofc2->create('OFC_Charts_Line');
        
$line_1->set_values($data_1);
       
        
$chart $this->ofc2->create('OFC_Chart');
        
$chart->set_title($title);
        
$chart->add_element($line_1);
        
$chart->add_y_axis($this->ofc2->create('OFC_Elements_Axis_Y'));

        
$x $this->ofc2->create('OFC_Elements_Axis_X');
        
$x->set_offset(false);
        
$x->set_labels_from_array(array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'));
        
$chart->set_x_axis($x);

        
$y $this->ofc2->create('OFC_Elements_Axis_Y');
        
$y->set_offset(true);
        
//$y->set_labels(array("Make garden look sexy","Paint house","Move into house"));
        
$chart->add_y_axis($y);

        
// send response (json)
        
echo $chart->toString();
    

But that gives a error.

Hope that you can help!

Profile
 
 
Posted: 27 January 2010 10:43 AM   [ Ignore ]   [ # 19 ]  
Summer Student
Total Posts:  8
Joined  01-27-2010

For me too.
If it should work out from the box - it isn’t;)

Profile
 
 
Posted: 27 January 2010 06:50 PM   [ Ignore ]   [ # 20 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  03-07-2007

Hello,

sorry for the late reply, have been very busy lately…


I have changed the OFC2 library to a simple plugin.

See the updated wiki article Open Flash Chart 2 Plugin

This is much easier, and now really should work as advertised wink

Here’s the new package: download

Instructions
1) Drop the contents of the ‘application’ folder into your application folder
2) Drop the ‘assets’ folder into your webroot (next to your index.php file)
3) Open your.hostname.com/index.php/ofc2


br,
Thomas

Profile
 
 
Posted: 05 May 2010 08:24 AM   [ Ignore ]   [ # 21 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  538
Joined  04-29-2007

Just now beginning to delve into the OFC2 plugin examples.

I need to display a OFC2 charts within an existing view.  The view contains usage statistics and the chart will be another representation of the stats.

Any ideas on how to do this?

TIA

 Signature 

@carvingCode

Profile
 
 
Posted: 05 May 2010 09:12 AM   [ Ignore ]   [ # 22 ]  
Lab Technician
RankRankRankRank
Total Posts:  1192
Joined  03-30-2009
carvingCode - 05 May 2010 12:24 PM

Just now beginning to delve into the OFC2 plugin examples.

I need to display a OFC2 charts within an existing view.  The view contains usage statistics and the chart will be another representation of the stats.

Any ideas on how to do this?

TIA

You need to create a method that will parse your statistics into JSON for the format of chart you need. Refer to OFC’s documentation for that. Then you will call OFC’s chart point the data url to your JSON method.

 Signature 

Useful CI Links
Base Controllers by Phil Sturgeon
Cache Library by Phil Sturgeon
Carabiner css/js management by tonydewan

Profile
 
 
Posted: 05 May 2010 10:29 AM   [ Ignore ]   [ # 23 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  538
Joined  04-29-2007

What I’ve done so far:

1) Copied the ‘get_bar_data()’ function into my controller.  (Using default function and dataset for test).
2) Added the Javascript found in the example view file into my view.  Made necessary updates to align the Flash file to a DIV on my page and hard coded in the URL to the controller function.

I can get the flash file inserted into the location in my view.  But, it doesn’t import the data.  It’s basically an empty Flash file.

I can run the controller/function and it returns the default dataset:

"elements"[ { "type""bar""values"[ 123456789 ] } ]"title""test" 

I’m getting no CI and Javascript errors.  But I have, intermittently, received a JSON error.

Things seem to be connecting up OK, but not getting the results.

Any ideas?

 Signature 

@carvingCode

Profile
 
 
Posted: 05 May 2010 10:54 AM   [ Ignore ]   [ # 24 ]  
Lab Technician
RankRankRankRank
Total Posts:  1192
Joined  03-30-2009

Can you post your view file showing how you are loading the chart?

 Signature 

Useful CI Links
Base Controllers by Phil Sturgeon
Cache Library by Phil Sturgeon
Carabiner css/js management by tonydewan

Profile
 
 
Posted: 05 May 2010 11:07 AM   [ Ignore ]   [ # 25 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  538
Joined  04-29-2007

Here it is.

<?php
    $this
->load->view('parts/head');
    
$output "<div id=\"sub-heading\">\n";
    
$output .= "<h2>Admin Menu</h2>\n";
    
$output .= "</div>\n";
    
$output .= "<div id=\"content\">\n";
    
$output .= "<div id=\"stats\">\n";
    
$output .= "<div id=\"stats-left\">\n";
    
$output .= "<h3>Totals</h3>\n";
    
$output .= "<table>\n";
    
$line_num=0;
    foreach (
$query->result_array() as $record)
    
{
        
if ($line_num++ % 2{
            $output 
.= "<tr class=\"odd\">";
        
}
        
else
        
{
            $output 
.= "<tr class=\"even\">";
        
}
        $output 
.= "<td class=\"cat-name\">".ucwords($record['category'])."</td>";
        
$output .= "<td class=\"cat-totals\">".$record['cat_totals']."</td>";
        
$output .= "</tr>\n";
    
}

    $output 
.= "</table>\n";
    
$output .= "</div>\n";
    
$output .= "<div id=\"stats-right\">\n";
    echo 
$output;
    
?>

[removed]
[removed]
[removed]
            swfobject
.embedSWF(
              
"http://server/assets/swf/open-flash-chart.swf""stats-right",
              
"260""490",
              
"9.0.0""expressInstall.swf",
              
{"data-file":"<?= urlencode('http://server/controller/get_data_bar') ?>"}
             
);
[removed]

    <?php
    $output 
"</div>\n";
    
$output .= "</div>\n";

    echo 
$output;

    
//Load page footer
    
$this->load->view('parts/foot');

    
/* End of file admin.php */
    /* Location: ./system/application/views/admin */ 

Not sure why EE removed the script tags…  That’s what is behind the ‘[removed]’ lines.  I’m placing the Javascript call to the ‘swfobject.js’ in the DIV that I need it to display to.

What I now getting is the ‘loading’ graphics for the Flash file.  It’s just not getting the data…

 Signature 

@carvingCode

Profile
 
 
Posted: 05 May 2010 11:29 AM   [ Ignore ]   [ # 26 ]  
Lab Technician
RankRankRankRank
Total Posts:  1192
Joined  03-30-2009

It looks like your view is correct, and if OFC is trying to load the data, it seems you’ve got that part right. Take a look at all of your json. Make sure have defined each of the required elements (ie x-axis, y-axis, color). You might try copying the json from the tutorials on the OFC website, and have your controller output that string. That could help narrow down where the problem is.

 Signature 

Useful CI Links
Base Controllers by Phil Sturgeon
Cache Library by Phil Sturgeon
Carabiner css/js management by tonydewan

Profile
 
 
Posted: 05 May 2010 11:42 AM   [ Ignore ]   [ # 27 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  538
Joined  04-29-2007
pickupman - 05 May 2010 03:29 PM

It looks like your view is correct, and if OFC is trying to load the data, it seems you’ve got that part right. Take a look at all of your json. Make sure have defined each of the required elements (ie x-axis, y-axis, color). You might try copying the json from the tutorials on the OFC website, and have your controller output that string. That could help narrow down where the problem is.

Will do.  Right now, I’m relying only on the ‘get_data_bar()’ function of the CI plugin to generate the data in the proper format.  And I’ve not made any changes to it.  I assumed that as it was working in the demos, it should be all that was needed for testing.

 Signature 

@carvingCode

Profile
 
 
Posted: 05 May 2010 11:47 AM   [ Ignore ]   [ # 28 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  538
Joined  04-29-2007

OK.  Copying the dataset from the bar example gets things working.  Will work on this more after lunch and report back.  But, looks like success is looming.

Thanks for the suggestion.

EDIT:  OK… My fault.  I forgot that I had made a change to the get_data_bar() function.  This caused the format of the dataset to be wrong.  Fixed, and all is well with the test data.  Now on to the real deal.

Again, thanks for the suggestion to compare datasets.  That was the solver.

 Signature 

@carvingCode

Profile
 
 
Posted: 07 May 2010 06:01 PM   [ Ignore ]   [ # 29 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  538
Joined  04-29-2007

OFC2 is running fine on my hosted server but just displays the ‘loading’ message and rotating thingy on my localhost install.  The sites are exact duplicates.

Do I need to make a path change or make sure a specific PHP library is running?

TIA

 Signature 

@carvingCode

Profile
 
 
Posted: 24 September 2010 06:14 AM   [ Ignore ]   [ # 30 ]  
Summer Student
Total Posts:  9
Joined  09-04-2010

I can’t get it to work. I also tried to load the chart in an external view and it’s not displaying on the page. I can see in the code that I have the call to the SWF object… but nothing on the page. Can someone help?

I included this in my view:

<h1>New Clients <?php echo date('M-Y');?></h1>
        
[removed]
            swfobject
.embedSWF(
              
"<?= base_url()?>assets/swf/open-flash-chart.swf""test_chart",
              
"40%""200",
              
"9.0.0""expressInstall.swf",
              
{"data-file":"<?= urlencode(''.base_url().'ofc2/get_data_pie') ?>"}
            
);
        
[removed] 

This is in the source code.

<h1>New Clients Sep-2010</h1>
        
[removed]
            swfobject
.embedSWF(
              
"http://localhost/assets/swf/open-flash-chart.swf""test_chart",
              
"40%""200",
              
"9.0.0""expressInstall.swf",
              
{"data-file":"http://localhost/ofc2/get_data_pie"}
            
);
        
[removed] 

I also have this in the header:

[removed][removed] 

If I load http://localhost/ofc2/show_chart/pie it’s working great and I can see that http://localhost/pos/ofc2/get_data_pie json is right…

CI Forum automatically removed [removed][removed] (“removed”)

Thanks!

Profile
 
 
   
2 of 2
2
 
‹‹ uri problem      mkdir problems ››