<?php

// setup environment
// script url is based on jumi's stored fileid of application (testApp1.php)
defined('_JEXEC') or die('Restricted access');
$script = "/index.php?option=com_jumi&view=application&fileid=25&format=raw";
$basepath = $_SERVER ['DOCUMENT_ROOT'];

$flashchart_root = "/plugins/content/flashchart";
$lib_path = $basepath . $flashchart_root . "/lib/";
$lib = $lib_path . "flashChartUtil.php";
require_once ($lib);

// create new chart object (use default properties)
$chart = new flashChart("chart01", null, "90%", "300");
$chart->setChartProperty("flashchart_root", $flashchart_root);
$javascript_html = $chart->getJavascripts();

// setup html
$urlpath = $chart->getChartProperty("url_path");
$html = "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en-gb' lang='en-gb' >
<head>$javascript_html
 <link href='$urlpath/samples/sample_style.css' rel='stylesheet' type='text/css' />
</head>
<body style='min-width: 650px;'>";

// process user input and setup charts
if (array_key_exists('chart', $_POST))
{
     $selection = $_POST ['chart'];
     $chart->setChartProperty("label_fontsize", "11");
     if ($selection == "menu")
          $chart->setChartProperty("menu", "Data via Database,sql_chart|Data via URL,url_chart|Data via File,file_chart");
     else
          $html .= createHTMLform($script);
     
     if ($selection == "sql" || $selection == "menu")
     {
          // use Joomla's Configfile and define sql statement
          $dbconfig_file = $basepath . "/configuration.php";
          $chart->setChartProperty("db_config_file", $dbconfig_file);
          $sql = "Select title, hits From jos_content Order By hits Desc Limit 10";
          $chart->setChartProperty("x_label_rotate", "40");
          $chart->setChartProperty("data", "database");
          $rc = $chart->setChartProperty("sql", $sql);
          $chart->setChartProperty("type", "bar_dome");
          $chart->setChartProperty("tooltip", "#val# hits");
          if ($selection != "menu")
               $html .= "<br/ >Chart data via <b>sql</b>=&quot;$sql&quot;";
          $chart->setChartProperty("title", "Chart Data from Database (most read articles)");
          if ($selection == "menu")
               $html .= $chart->createChartasScript("sql_chart");
     }
     
     if ($selection == "file" || $selection == "menu")
     {
          $file = $basepath . "php-apps/flashchart/samples/square.data";
          if ($selection != "menu")
               $html .= "<br/ >Chart data via <b>file</b>=&quot;$file&quot;";
          $chart->setChartProperty("x_label_rotate", "0");
          $chart->setChartProperty("data", "file");
          $rc = $chart->setChartProperty("file", $file);
          $chart->setChartProperty("x_step", "10");
          $chart->setChartProperty("y_step", "8");
          $chart->setChartProperty("axis_3d", "0");
          $chart->setChartProperty("type", "line");
          $chart->setChartProperty("tooltip", "x=#x_label# -> y=#y#");
          $chart->setChartProperty("title", "Chart Data from File (square values)");
          if ($selection == "menu")
               $html .= $chart->createChartasScript("file_chart");
     }
     
     if ($selection == "url" || $selection == "menu")
     {
          $url = "http://www.jschmidt-systemberatung.de/php-apps/flashchart/samples/sinus.php";
          if ($selection != "menu")
               $html .= "<br/ >Chart data via <b>url</b>=&quot;$url&quot;";
          $chart->setChartProperty("data", "url");
          $rc = $chart->setChartProperty("url", "$url");
          $chart->setChartProperty("x_label_rotate", "0");
          $chart->setChartProperty("x_step", "9");
          $chart->setChartProperty("y_step", "0.2");
          $chart->setChartProperty("y_min", "-1");
          $chart->setChartProperty("axis_3d", "0");
          $chart->setChartProperty("type", "line_area");
          $chart->setChartProperty("tooltip", "sinus #x_label# = #y#");
          $chart->setChartProperty("title", "Chart Data from Remote WebServer (sinus from 0 to 360 degrees)");
          if ($selection == "menu")
               $html .= $chart->createChartasScript("url_chart");
     }
     
	 if ($selection == "formula" || $selection == "menu")
	 {
		  $formula = "y = 2 * e^-(0.2 * x) * sin(x * 2 * pi)";
		  $chart->setChartProperty("title", "Chart Data created via Formula");
		  $chart->setChartProperty("type", "line");
		  $chart->setChartProperty("axis_3d", "0");
		  $chart->setChartProperty("y_step", "0.5");
		  $chart->setChartProperty("x_step", "10");
		  $chart->setChartProperty("x_interval", "0.1");
	      $chart->setChartProperty("x_max", "20");
		  $chart->setChartProperty("y_min", "-2");
		  $chart->setChartProperty("data", "formula");
		  $rc = $chart->setChartProperty("formula", $formula);
		  if ($selection == "menu")
			$html .= $chart->createChartasScript("formula_chart");
		  else
			$html .= "<br/ >Chart data via <b>formula</b>=&quot;$formula&quot;:";
	 }
     
     // create empty chart if "menu" requested
     if ($selection == "menu")
     {
          $chart->setChartProperty("data", "null");
          $chart->setChartProperty("y_min", "null");
          $chart->setChartProperty("grid_color", "f9f9f9");
          $chart->setChartProperty("tooltip", "");
          $chart->setChartProperty("title", "Charts via Chart Menu");
     }
     
     if ($rc)
     {
          $html .= $chart->createChart();
          if ($selection == "menu")
               $html .= "<br /><a class='readmore-link' href='javascript:window.history.go(-1);' title='go back'>...back...</a>";
     }
     else
          echo "<br><b>Error</b> - setting chart property for $selection";
}
else
     $html .= createHTMLform($script);
     
// close and send html 
$html .= "\n</body>\n</html>";
echo $html;

// create html-form
function createHTMLform($script)
{
     $html = "\n<form action='$script'  method='post'>
   <b>Select Chart:</b>  
   <select name='chart'>
   <option value='sql'>data from database</option>
   <option value='url'>data via URL</option>
   <option value='file'>data via file</option>
   <option value='formula'>data via formula</option>
   <option value='menu'>all charts via chart menu</option>
   </select>
   <input  type='submit' value='show chart' class='button' />
   </form>";
     
     return $html;
}

?>