<?php
/**
 * @version $Id: bfGooglechartsapi.php 117 2009-07-14 20:29:10Z  $
 * @package Blue Flame Framework (bfFramework)
 * @copyright Copyright (C) 2003,2004,2005,2006,2007,2008,2009 Blue Flame IT Ltd. All rights reserved.
 * @license GNU General Public License
 * @link http://www.blueflameit.ltd.uk
 * @author Phil Taylor / Blue Flame IT Ltd.
 *
 * bfFramework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * bfFramework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this package.  If not, see http://www.gnu.org/licenses/
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );

/**
 * Port of JavaScript from http://code.google.com/apis/chart/
 * http://james.cridland.net/code
 */
final class bfGoogleChartsAPI {

	/**
	 *  Here's an array containing some data to plot
	 */
	private $_test_data = array (0.5, 6, 12, 17, 2, 0.3, 9 );

	/**
	 * A list of encoding characters to help later, as per Google's example
	 */
	const simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

	/**
	 * Here's where we call the chart, and return the encoded chart data
	 *
	 * @param Array $data
	 * @return string The img tag
	 */
	static public function getPieChart($txt1, $txt2, $one, $two, $color1 = 'ff0000', $color2 = '00ff00') {

		if ($one > 100 || $two > 100){
			$total = $one + $two;
			$one = ($one / $total) * 100;
			$two = ($two / $total) * 100;
		}

		$url = 'http://chart.apis.google.com/chart?cht=p3&chd=t:%s,%s&chs=250x100&chl=%s|%s&chco=%s,%s';
		return sprintf ( $url, $one, $two, urlencode($txt1), urlencode($txt2), $color1, $color2 );
	}

	private function chart_data($values) {

		// First, find the maximum value from the values given
		$maxValue = max ( $values );

		$chartData = "s:";
		for($i = 0; $i < count ( $values ); $i ++) {
			$currentValue = $values [$i];

			if ($currentValue > - 1) {
				$chartData .= substr ( bfGoogleChartsAPI::simpleEncoding, 61 * ($currentValue / $maxValue), 1 );
			} else {
				$chartData .= '_';
			}
		}

		// Return the chart data - and let the Y axis to show the maximum value
		return $chartData . "&chxt=y&chxl=0:|0|" . $maxValue;
	}
}