<?php
/**
 * @version		$Id: bar.php 135 2009-06-25 04:32:04Z eddieajau $
 * @copyright	Copyright (C) 2009 New Life in IT Pty Ltd. All rights reserved.
 * @license		GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
 * @link		http://www.theartofjoomla.com
 */

// no direct access
defined('_JEXEC') or die;

JLoader::import('chart', JPATH_QUANTA.DS.'libraries');

/**
 * Scatter chart helper
 *
 * @package		TAOJ.Quanta
 * @subpackage	com_quanta
 */
class JChartBar extends JChart
{
	/**
	 * @var	array	The available sub-types for this chart type.
	 */
	protected $_subtypes = array(
			'3d',
			'cylinder_outline',
			'cylinder',
			'dome',
			'filled',
			'glass',
			'horizontal',
			'round',
			'round3d',
			'round_glass',
			'sketch',
			'stack'
			);

	/**
	 * Initialise the chart
	 */
	function _initialise()
	{
		// Create an instance of the chart type.
		$bar		= OFC_Chart::getInstance('Bar', $this->_subtype);

		// Initialise variables.
		$data		= array();
		$labels		= array();
		$yMin		= 0;
		$yMax		= 0;
		$rangeMode	= $this->_params->get('chart-range_mode', 1);
		$tipFormat	= $this->_params->get('chart-tip_format', '#val# of #total#<br>#percent# of 100%');

		// Loop through the items and find extrema.
		foreach ($this->_points as &$point)
		{
			// Avoid division by zero errors.
			if ($point->y_max == 0) {
				$relative = 0;
			}
			else {
				$relative = $point->y/$point->y_max*100.0;
			}
			$yMin = min($yMin, $point->y);

			if ($rangeMode == 2)
			{
				// Find the extrema of the item maxima.
				$yMax	= 100;
				$value	= new OFC_Charts_Bar_Value($relative);
			}
			else if ($rangeMode == 1)
			{
				// Find the extrema of the item maxima.
				$yMax	= max($yMax, $point->y_max);
				$value	= new OFC_Charts_Bar_Value($point->y);
			}
			else
			{
				// Find the extrema of the plotted values.
				$yMax	= max($yMax, $point->y);
				$value	= new OFC_Charts_Bar_Value($point->y);
			}

			// Set the bar colour.
			$value->set_colour($point->params->get('chart-set_colour'));

			if ($tipFormat)
			{
				$tip = str_replace(
					array('#title#', '#val#', '#total#', '#percent#'),
					array(htmlspecialchars($point->title, ENT_QUOTES, 'utf-8'), number_format($point->y), number_format($point->y_max), sprintf('%.1f', $relative)),
					$tipFormat
				);

				$value->set_tooltip($tip);
			}

			array_push($data,	$value);
			array_push($labels,	$point->label ? $point->label : $point->title);
		}

		$bar->values	= $data;

		//
		// Configure the chart.
		//

		// Chart title.
		if ($v = $this->_params->get('chart-title')) {
			$this->_chart->set_title($v);
		}

		// Chart background colour.
		if ($v = $this->_params->get('chart-bgcolour')) {
			$this->_chart->set_bg_colour($v);
		}

		// Chart data.
		$this->_chart->add_element($bar);

		// Setup the X-Axis.
		$this->_setXAxis($labels);

		// Setup the Y-Axis.
		$this->_setYAxis($yMin, $yMax);
	}

	/**
	 * Configures the X-Axis of the chart
	 *
	 * @param	object	The chart object
	 * @param	double	The minimum range of the x-axis
	 * @param	double	The maximum range of the x-axis
	 */
	protected function _setXAxis($labels)
	{
		parent::_setXAxis();

		$x = new OFC_Elements_Axis_X();
		$x->set_labels_from_array($labels);
		$this->_chart->set_x_axis($x);
	}

	/**
	 * Configures the Y-Axis of the chart
	 *
	 * @param	double	The minimum range of the y-axis
	 * @param	double	The maximum range of the y-axis
	 */
	protected function _setYAxis($yMin, $yMax)
	{
		parent::_setXAxis();

		// Adjust the Y-Axis Range
		$rangeMode	= $this->_params->get('chart-range_mode');
		$round		= $this->_params->get('chart-y_round');
		if ($round > 0 && $rangeMode < 2)
		{
			if ($round == 2)
			{
				foreach ($this->_sets as &$set) {
					$yMax = max($yMax, $set->y_max);
				}
			}

			// Round up to the nearset lower order of magitude
			// eg, round 2535 up to 3000.
			$mag	= floor(log10($yMax));
			$inc	= pow(10, $mag);
			$yMax	= round($yMax + $inc/2, -$mag);
		}

		$y		= new OFC_Elements_Axis_Y;
		$ySteps	= (int) $this->_params->get('chart-y_steps', 5);
		$y->set_range($yMin, $yMax, ($yMax - $yMin)/$ySteps);

		// Add the Y Axis object to the chart:
		$this->_chart->set_y_axis($y);
	}
}
