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
