<?php
/**
 * PHP Integration of Open Flash Chart
 *
 * @version		$Id: OFC_Chart.php 123 2009-06-19 08:57:39Z eddieajau $
 * @copyright	Copyright (C) 2008 John Glazebrook <open-flash-chart@teethgrinder.co.uk>
 * @license		GNU Lesser General Public License version 2.1 or later.
 * @link		http://teethgrinder.co.uk/open-flash-chart-2/
 */

define('PATH_OFC', dirname(__FILE__));

require_once PATH_OFC.'/OFC_Elements.php';

/**
 * @package		OpenFlashCharts
 */
class OFC_Chart
{
	/**
	 * @var	array	The array of data elements
	 */
	public $elements = array();

	/**
	 * @var	string	The chart title
	 */
	public $title = null;

	public $colour = null;

	public $bg_colour = null;

	/**
	 * Constructor
	 *
	 * @param	string	The chart title
	 */
	public function __construct($t = '')
	{
		$this->set_title($t);
	}

	/**
	 * Magic method to convert the object to a string.
	 */
	public function __toString()
	{
		return json_encode($this);
	}

	/**
	 * Get an instance of a chart type and lazy load the appropriate files
	 *
	 * @param	string	The Chart type
	 * @param	string	The Chart subtype
	 *
	 * @return	mixed	A chart object
	 */
	public function getInstance($type, $subType = '')
	{
		// Filter the type and subtype.
		$type		= preg_replace('#[^A-Z0-9_]#i', '', $type);
		$subType	= preg_replace('#[^A-Z0-9_]#i', '', $subType);

		// Determine the PHP class name.
		if ($subType) {
			$class = 'OFC_Charts_'.$type.'_'.$subType;
		}
		else {
			$class = 'OFC_Charts_'.$type;
		}

		// Attempt to load the class if it hasn't been yet.
		if (!class_exists($class))
		{
			// Compute the file path.
			if ($subType) {
				$file = $type.'/OFC_Charts_'.$type.'_'.$subType.'.php';
			}
			else {
				$file = 'OFC_Charts_'.$type.'.php';
			}
			$path = PATH_OFC.'/Charts/'.$file;
			if (file_exists($path))
			{
				require $path;

				if (!class_exists($class)) {
					throw new Exception('OFC Class '.$class.' not found.');
				}
			}
			else {
				throw new Exception('OFC Class file'.$file.' not found.');
			}
		}

		return new $class;
	}

	public function set_title($t)
	{
		$this->title = ($t Instanceof OFC_Elements_Title) ? $t : new OFC_Elements_Title($t);
	}

	public function set_x_axis($value)
	{
		$this->x_axis = $value;
	}

	public function set_y_axis($value)
	{
		$this->y_axis = $value;
	}

	public function add_y_axis($value)
	{
		$this->y_axis = $value;
	}

	public function set_y_axis_right($value)
	{
		$this->y_axis_right = $value;
	}

	public function add_element($value)
	{
		$this->elements[] = $value;
	}

	/**
	 * Sets the X Legend for the chart.
	 *
	 * @param	OFC_Elements_Legend_X	An X-Legend object.
	 */
	public function set_x_legend($value)
	{
		$this->x_legend = $value;
	}

	public function set_y_legend($value)
	{
		$this->y_legend = $value;
	}

	public function set_bg_colour($value)
	{
		$this->bg_colour = $value;
	}
}
