From 5a226d2e662a4002b8a33eaa90f475da6b78b56f Mon Sep 17 00:00:00 2001
From: zarzuelo <noreply@pandorafms.org>
Date: Mon, 18 Apr 2011 10:30:45 +0000
Subject: [PATCH] 2011-04-18  Sergio Martin <sergio.martin@artica.es>

	* include/graphs/functions_pchart.php
	include/graphs/functions_fsgraph.php
	include/graphs/fgraph.php: Fixed bar graphs margins
	Fixed horizontal flash bar



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@4234 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_console/ChangeLog                     |  7 ++
 pandora_console/include/graphs/fgraph.php     |  2 +-
 .../include/graphs/functions_fsgraph.php      | 96 +++++++++++++++----
 .../include/graphs/functions_pchart.php       | 51 +++++-----
 4 files changed, 106 insertions(+), 50 deletions(-)

diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog
index f5d232ee81..a836df3ade 100644
--- a/pandora_console/ChangeLog
+++ b/pandora_console/ChangeLog
@@ -1,3 +1,10 @@
+2011-04-18  Sergio Martin <sergio.martin@artica.es>
+
+	* include/graphs/functions_pchart.php
+	include/graphs/functions_fsgraph.php
+	include/graphs/fgraph.php: Fixed bar graphs margins
+	Fixed horizontal flash bar
+
 2011-04-18  Sergio Martin <sergio.martin@artica.es>
 
 	* include/graphs/functions_pchart.php
diff --git a/pandora_console/include/graphs/fgraph.php b/pandora_console/include/graphs/fgraph.php
index 64a1c16a3f..6ee657dddc 100755
--- a/pandora_console/include/graphs/fgraph.php
+++ b/pandora_console/include/graphs/fgraph.php
@@ -265,7 +265,7 @@ function polar_graph($flash_chart, $chart_data, $width, $height, $no_data_image)
 function hbar_graph($flash_chart, $chart_data, $width, $height, $color = array(),
 	$legend = array(), $xaxisname = "", $yaxisname = "", $force_height = true, $homedir="") {
 	if($flash_chart) {
-		echo fs_hbar_chart (array_values($chart_data), array_keys($chart_data), $width, $height);
+		echo fs_2d_hcolumn_chart ($chart_data, $width, $height);
 	}
 	else {
 		$graph = array();
diff --git a/pandora_console/include/graphs/functions_fsgraph.php b/pandora_console/include/graphs/functions_fsgraph.php
index 5b4530889d..afddb39922 100755
--- a/pandora_console/include/graphs/functions_fsgraph.php
+++ b/pandora_console/include/graphs/functions_fsgraph.php
@@ -518,26 +518,6 @@ function fs_2d_pie_chart ($data, $names, $width, $height, $background = "EEEEEE"
 	return get_chart_code2 ($chart, $width, $height, 'include/graphs/FusionCharts/FCF_Pie2D.swf');
 }
 
-// Prints a BAR Horizontalchart
-function fs_hbar_chart ($data, $names, $width, $height) {
-	if (sizeof ($data) != sizeof ($names)) {
-		return;
-	}
-
-	// Generate the XML
-	$chart = new FusionCharts("Bar2D", $width, $height);
-	$chart->setSWFPath("include/graphs/FusionCharts/");
-  	$params="showNames=1;showValues=0;showPercentageValues=0;baseFontSize=9;rotateNames=1;chartLeftMargin=0;chartRightMargin=0;chartBottomMargin=0;chartTopMargin=0;showBarShadow=1;showLimits=1";
-  	$chart->setChartParams($params);
-
-	for ($i = 0; $i < sizeof ($data); $i++) {
-		$chart->addChartData($data[$i], 'name=' . clean_flash_string($names[$i]));
-	}
-
-	// Return the code
-	return get_chart_code2 ($chart, $width, $height, 'include/graphs/FusionCharts/FCF_Bar2D.swf');
-}
-
 // Returns a 2D column chart
 function fs_2d_column_chart ($data, $width, $height) {
 	if (sizeof ($data) == 0) {
@@ -613,6 +593,82 @@ function fs_2d_column_chart ($data, $width, $height) {
 	return get_chart_code ($chart, $width, $height, 'include/graphs/FusionCharts/FCF_Column2D.swf');
 }
 
+// Returns a BAR Horizontalchart
+function fs_2d_hcolumn_chart ($data, $width, $height) {
+	if (sizeof ($data) == 0) {
+		return;
+	}
+
+	// Generate the XML
+	$chart = new FusionCharts('Bar2D', $width, $height);
+
+	$pixels_between_xdata = 25;
+	$max_xdata_display = round($width / $pixels_between_xdata);
+	$ndata = count($data);
+	if($max_xdata_display > $ndata) {
+		$xdata_display = $ndata;
+	}
+	else {
+		$xdata_display = $max_xdata_display;
+	}
+	
+	$step = round($ndata/$xdata_display);
+	
+	
+	if(is_array(reset($data))) {
+	 	$data2 = array();
+	 	$count = 0;
+		foreach($data as $i =>$values) {
+			$count++;
+			$show_name = '0';
+			if (($count % $step) == 0) {
+				$show_name = '1';
+			}
+
+			$chart->addCategory($i, //'');
+					'hoverText=' . $i .  
+					';showName=' . $show_name);
+			
+			$c = 0;
+			foreach($values as $i2 => $value) {
+				$data2[$i2][$i] = $value;
+				$c++;
+			}
+		}
+		$data = $data2;
+	 }
+	 else {
+		$data = array($data);
+	 }
+	 
+    $empty = 0;
+    $num_vlines = 0;
+    $count = 0;
+
+	foreach ($data as $legend_value => $values) {
+
+		foreach($values as $name => $value) {
+			if (($count++ % $step) == 0) {
+				$show_name = '1';
+				$num_vlines++;
+			} else {
+				$show_name = '0';
+			}
+			if ($value > 0) {
+				$empty = 0;
+			}
+			$chart->addChartData($value, 'name=' . clean_flash_string($name) . ';showName=' . $show_name/* . ';color=95BB04'*/);
+		}
+	}
+
+  	$params='showNames=1;showValues=0;showPercentageValues=0;baseFontSize=9;rotateNames=1;chartLeftMargin=0;chartRightMargin=0;chartBottomMargin=0;chartTopMargin=0;showBarShadow=1;showLimits=1';
+
+    $chart->setChartParams($params.';numVDivLines='.$num_vlines.($empty == 1 ? ';yAxisMinValue=0;yAxisMaxValue=1' : ''));
+
+	// Return the code
+	return get_chart_code ($chart, $width, $height, 'include/graphs/FusionCharts/FCF_Bar2D.swf');
+}
+
 // Returns a 3D column chart
 function fs_3d_column_chart ($data, $width, $height) {
 	if (sizeof ($data) == 0) {
diff --git a/pandora_console/include/graphs/functions_pchart.php b/pandora_console/include/graphs/functions_pchart.php
index 13bf1d4fe1..1436eb8af7 100755
--- a/pandora_console/include/graphs/functions_pchart.php
+++ b/pandora_console/include/graphs/functions_pchart.php
@@ -484,43 +484,36 @@ function pch_bar_graph ($graph_type, $index, $data, $width, $height, $font, $ant
 	 /* Draw the scale */
 	 // TODO: AvoidTickWhenEmpty = FALSE When the distance between two ticks will be less than 50 px
 	 // TODO: AvoidTickWhenEmpty = TRUE When the distance between two ticks will be greater than 50 px
-	 switch($graph_type) {
-		case "vbar":
-				$scaleSettings = array("AvoidTickWhenEmpty" => FALSE, "AvoidGridWhenEmpty" => FALSE, "GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE, "Mode"=>SCALE_MODE_START0, "LabelRotation" => 60);
-				$leftmargin = 40;
-				break;
-		case "hbar":
-				$scaleSettings = array("GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE, "Mode"=>SCALE_MODE_START0, "Pos"=>SCALE_POS_TOPBOTTOM, "LabelValuesRotation" => 60);
-				
-				//Calculate the bottom margin from the size of string in each index
-				 $max_chars = 0;
-				 foreach ($index as $string_index) {
-				 	if (empty($string_index)) continue;
-				 	
-				 	$len = strlen($string_index);
-				 	if ($len > $max_chars) {
-				 		$max_chars = $len; 
-				 	}
-				 }
-				 $leftmargin = 5 * $max_chars;
-				
-				break;
-	 }
 	 
 	 //Calculate the top margin from the size of string in each index
 	 $max_chars = 0;
 	 foreach ($index as $string_index) {
-	 	if (empty($string_index)) continue;
-	 	
-	 	$len = strlen($string_index);
-	 	if ($len > $max_chars) {
-	 		$max_chars = $len; 
-	 	}
+		if (empty($string_index)) continue;
+		
+		$len = strlen($string_index);
+		if ($len > $max_chars) {
+			$max_chars = $len; 
+		}
 	 }
 	 $margin_top = 10 * $max_chars;
+				 
+	 switch($graph_type) {
+		case "vbar":
+				$scaleSettings = array("AvoidTickWhenEmpty" => FALSE, "AvoidGridWhenEmpty" => FALSE, "GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE, "Mode"=>SCALE_MODE_START0, "LabelRotation" => 60);
+				$margin_left = 40;
+				$margin_top = 10;
+				$margin_bottom = 8 * $max_chars;
+				break;
+		case "hbar":
+				$scaleSettings = array("GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE, "Mode"=>SCALE_MODE_START0, "Pos"=>SCALE_POS_TOPBOTTOM, "LabelValuesRotation" => 60);
+				$margin_left = 5 * $max_chars;
+				$margin_top = 40;
+				$margin_bottom = 10;
+				break;
+	 }
 	 
 	 /* Define the chart area */
-	 $myPicture->setGraphArea($leftmargin,$margin_top,$width,$height-80);
+	 $myPicture->setGraphArea($margin_left,$margin_top,$width,$height-$margin_bottom);
 
 	 $myPicture->drawScale($scaleSettings);