From 79354a0ebfa04f78dc0096462d52f3344c5b8e45 Mon Sep 17 00:00:00 2001
From: daniel <daniel.barbero@artica.es>
Date: Fri, 18 May 2018 14:22:39 +0200
Subject: [PATCH] fixed graphs

---
 pandora_console/include/functions.php         |  46 +-
 .../include/functions_custom_graphs.php       |   6 +-
 pandora_console/include/functions_graph.php   | 632 ++++++++----------
 .../include/functions_reporting.php           |  71 +-
 pandora_console/include/graphs/fgraph.php     |   1 -
 .../include/graphs/flot/pandora.flot.js       |  44 +-
 .../operation/agentes/stat_win.php            |   2 +-
 7 files changed, 377 insertions(+), 425 deletions(-)

diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php
index edcb7c9034..479d15f132 100644
--- a/pandora_console/include/functions.php
+++ b/pandora_console/include/functions.php
@@ -3004,7 +3004,7 @@ function legend_graph_array(
 	global $legend;
 	$unit = $format_graph['unit'];
 
-	if (isset($show_elements_graph['labels']) && is_array($show_elements_graph['labels'])){
+	if (isset($show_elements_graph['labels']) && is_array($show_elements_graph['labels']) && (count($show_elements_graph['labels']) > 0)){
 		$legend['sum'.$series_suffix] = $show_elements_graph['labels'][$data_module_graph['module_id']] . ' ' ;
 	}
 	else{
@@ -3066,22 +3066,36 @@ function legend_graph_array(
 	return $legend;
 }
 
-function series_type_graph_array( $series_suffix, $data_module_graph){
+function series_type_graph_array($data){
 	global $config;
-	global $series_type;
-
-	$series_type['event'.$series_suffix] = 'points';
-	$series_type['alert'.$series_suffix] = 'points';
-	$series_type['unknown'.$series_suffix] = 'unknown';
-	switch ($data_module_graph['id_module_type']) {
-		case 21: case 2: case 6:
-		case 18: case 9: case 31:
-			$series_type['sum'.$series_suffix] = 'boolean';
-			break;
-		default:
-			$series_type['sum'.$series_suffix] = 'area';
-			break;
+	foreach ($data as $key => $value) {
+		if(strpos($key, 'sum') !== false){
+			switch ($value['id_module_type']) {
+				case 21: case 2: case 6:
+				case 18: case 9: case 31:
+					$series_type[$key] = 'boolean';
+					break;
+				default:
+					$series_type[$key] = 'area';
+					break;
+			}
+		}
+		elseif(strpos($key, 'event') !== false){
+			$series_type[$key] = 'points';
+		}
+		elseif(strpos($key, 'alert') !== false){
+			$series_type[$key] = 'points';
+		}
+		elseif(strpos($key, 'unknown') !== false){
+			$series_type[$key] = 'unknown';
+		}
+		elseif(strpos($key, 'percentil') !== false){
+			$series_type[$key] = 'percentil';
+		}
+		else{
+			$series_type[$key] = 'area';
+		}
 	}
-	$series_type['percentil' . $series_suffix] = 'percentil';
+	return $series_type;
 }
 ?>
diff --git a/pandora_console/include/functions_custom_graphs.php b/pandora_console/include/functions_custom_graphs.php
index 83ffb6e290..9ca67f5ca2 100644
--- a/pandora_console/include/functions_custom_graphs.php
+++ b/pandora_console/include/functions_custom_graphs.php
@@ -238,7 +238,8 @@ function custom_graphs_print($id_graph, $height, $width, $period,
 		$homeurl = ui_get_full_url(false, false, false, false);
 	}
 
-	$output = graphic_combined_module($modules,
+	$output = graphic_combined_module(
+		$modules,
 		$weights,
 		$period,
 		$width,
@@ -271,7 +272,8 @@ function custom_graphs_print($id_graph, $height, $width, $period,
 		$fullscale,
 		$summatory,
 		$average,
-		$modules_series);
+		$modules_series
+	);
 
 	if ($return)
 		return $output;
diff --git a/pandora_console/include/functions_graph.php b/pandora_console/include/functions_graph.php
index fdafb3afab..5333a1801f 100644
--- a/pandora_console/include/functions_graph.php
+++ b/pandora_console/include/functions_graph.php
@@ -331,6 +331,8 @@ function grafico_modulo_sparse_data_chart (
 	$array_data["sum" . $series_suffix]['max'] = $max_value;
 	$array_data["sum" . $series_suffix]['avg'] = $sum_data/$count_data;
 
+	$array_data["sum" . $series_suffix]['id_module_type'] = $data_module_graph['id_module_type'];
+
 	if (!is_null($show_elements_graph['percentil']) &&
 		$show_elements_graph['percentil'] &&
 		!$show_elements_graph['flag_overlapped']) {
@@ -354,11 +356,10 @@ function grafico_modulo_sparse_data(
 	$series_suffix, $str_series_suffix) {
 
 	global $config;
-	global $array_data;
+	//global $array_data;
 	global $caption;
 	global $color;
 	global $legend;
-	global $series_type;
 	global $array_events_alerts;
 
 	if($show_elements_graph['fullscale']){
@@ -465,23 +466,6 @@ function grafico_modulo_sparse_data(
 			}
 		}
 
-//XXXX que hacer con baseline
-		/*
-		// Get baseline data
-		$baseline_data = array();
-		if ($baseline) {
-			$baseline_data = array ();
-			if ($baseline == 1) {
-				$baseline_data = enterprise_hook(
-					'reporting_enterprise_get_baseline',
-					array ($agent_module_id, $period, $width, $height , $title, $unit_name, $date));
-				if ($baseline_data === ENTERPRISE_NOT_HOOK) {
-					$baseline_data = array ();
-				}
-			}
-		}
-		*/
-
 		if ($show_elements_graph['show_events']  ||
 			$show_elements_graph['show_alerts'] ) {
 
@@ -603,13 +587,9 @@ function grafico_modulo_sparse_data(
 		$data_module_graph
 	);
 
-	series_type_graph_array(
-		$series_suffix,
-		$data_module_graph
-	);
-
 	$array_events_alerts[$series_suffix] = $events;
 
+	return $array_data;
 }
 
 function grafico_modulo_sparse ($agent_module_id, $period, $show_events,
@@ -621,22 +601,23 @@ function grafico_modulo_sparse ($agent_module_id, $period, $show_events,
 	$menu = true, $backgroundColor = 'white', $percentil = null,
 	$dashboard = false, $vconsole = false, $type_graph = 'area', $fullscale = false,
 	$id_widget_dashboard = false,$force_interval = 0,$time_interval = 300,
-	$max_only = 0, $min_only = 0) {
+	$max_only = 0, $min_only = 0, $array_data_create = 0) {
 
 	global $config;
+
+
 	global $graphic_type;
-	global $array_data;
+	//global $array_data;
 	global $caption;
 	global $color;
 	global $legend;
-	global $series_type;
 	global $array_events_alerts;
 
+
 	$array_data   = array();
 	$caption      = array();
 	$color        = array();
 	$legend       = array();
-	$series_type  = array();
 
 	$array_events_alerts = array();
 
@@ -735,58 +716,66 @@ function grafico_modulo_sparse ($agent_module_id, $period, $show_events,
 
 	$type_graph = $config['type_module_charts'];
 
-	if ($show_elements_graph['compare'] !== false) {
-		$series_suffix = 2;
-		$series_suffix_str = ' (' . __('Previous') . ')';
+	if(!$array_data_create){
+		if ($show_elements_graph['compare'] !== false) {
+			$series_suffix = 2;
+			$series_suffix_str = ' (' . __('Previous') . ')';
 
-		$date_array_prev['final_date'] = $date_array['start_date'];
-		$date_array_prev['start_date'] = $date_array['start_date'] - $date_array['period'];
-		$date_array_prev['period']     = $date_array['period'];
+			$date_array_prev['final_date'] = $date_array['start_date'];
+			$date_array_prev['start_date'] = $date_array['start_date'] - $date_array['period'];
+			$date_array_prev['period']     = $date_array['period'];
 
-		if ($show_elements_graph['compare'] === 'overlapped') {
-			$show_elements_graph['flag_overlapped'] = 1;
-		}
-		else{
-			$show_elements_graph['flag_overlapped'] = 0;
+			if ($show_elements_graph['compare'] === 'overlapped') {
+				$show_elements_graph['flag_overlapped'] = 1;
+			}
+			else{
+				$show_elements_graph['flag_overlapped'] = 0;
+			}
+
+			$array_data = grafico_modulo_sparse_data(
+				$agent_module_id, $date_array_prev,
+				$data_module_graph, $show_elements_graph,
+				$format_graph, $exception_interval_graph,
+				$series_suffix, $series_suffix_str
+			);
+
+			switch ($show_elements_graph['compare']) {
+				case 'separated':
+				case 'overlapped':
+					// Store the chart calculated
+					$array_data_prev  = $array_data;
+					$legend_prev      = $legend;
+					$color_prev       = $color;
+					$caption_prev     = $caption;
+					break;
+			}
 		}
 
-		grafico_modulo_sparse_data(
-			$agent_module_id, $date_array_prev,
+		$series_suffix = 1;
+		$series_suffix_str = '';
+		$show_elements_graph['flag_overlapped'] = 0;
+
+		$array_data = grafico_modulo_sparse_data(
+			$agent_module_id, $date_array,
 			$data_module_graph, $show_elements_graph,
 			$format_graph, $exception_interval_graph,
-			$series_suffix, $series_suffix_str
+			$series_suffix, $str_series_suffix
 		);
 
-		switch ($show_elements_graph['compare']) {
-			case 'separated':
-			case 'overlapped':
-				// Store the chart calculated
-				$array_data_prev  = $array_data;
-				$legend_prev      = $legend;
-				$series_type_prev = $series_type;
-				$color_prev       = $color;
-				$caption_prev     = $caption;
-				break;
+		if($show_elements_graph['compare']){
+			if ($show_elements_graph['compare'] === 'overlapped') {
+				$array_data = array_merge($array_data, $array_data_prev);
+				$legend     = array_merge($legend, $legend_prev);
+				$color      = array_merge($color, $color_prev);
+			}
 		}
 	}
+	else{
+		$array_data = $array_data_create;
+	}
 
-	$series_suffix = 1;
-	$series_suffix_str = '';
-	$show_elements_graph['flag_overlapped'] = 0;
-
-	grafico_modulo_sparse_data(
-		$agent_module_id, $date_array,
-		$data_module_graph, $show_elements_graph,
-		$format_graph, $exception_interval_graph,
-		$series_suffix, $str_series_suffix
-	);
-
-	if($show_elements_graph['compare']){
-		if ($show_elements_graph['compare'] === 'overlapped') {
-			$array_data = array_merge($array_data, $array_data_prev);
-			$legend     = array_merge($legend, $legend_prev);
-			$color      = array_merge($color, $color_prev);
-		}
+	if($show_elements_graph['return_data']){
+		return $array_data;
 	}
 
 	//XXX
@@ -801,6 +790,10 @@ function grafico_modulo_sparse ($agent_module_id, $period, $show_events,
 		)
 	);
 
+	$series_type = series_type_graph_array(
+		$array_data
+	);
+
 	//esto la sparse
 	//setup_watermark($water_mark, $water_mark_file, $water_mark_url);
 
@@ -829,6 +822,11 @@ function grafico_modulo_sparse ($agent_module_id, $period, $show_events,
 		}
 		$return .= '<br>';
 		if (!empty($array_data_prev)) {
+
+			$series_type = series_type_graph_array(
+				$array_data_prev
+			);
+
 			$return .= area_graph(
 				$agent_module_id,
 				$array_data_prev,
@@ -871,176 +869,6 @@ function grafico_modulo_sparse ($agent_module_id, $period, $show_events,
 	}
 
 return $return;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-//de aki al final eliminar;
-
-	enterprise_include_once("include/functions_reporting.php");
-
-	global $chart;
-	global $color;
-	global $color_prev;
-	global $legend;
-	global $long_index;
-	global $series_type;
-	global $chart_extra_data;
-	global $warning_min;
-	global $critical_min;
-	
-	$series_suffix_str = '';
-	if ($compare !== false) {
-		$series_suffix = '2';
-		$series_suffix_str = ' (' . __('Previous') . ')';
-		// Build the data of the previous period
-
-		grafico_modulo_sparse_data ($agent_module_id, $period,
-			$show_events, $width, $height, $title, $unit_name,
-			$show_alerts, $avg_only, $date-$period, $unit, $baseline,
-			$return_data, $show_title, $projection, $adapt_key,
-			$compare, $series_suffix, $series_suffix_str,
-			$show_unknown, $percentil, $dashboard, $vconsole,$type_graph, 
-			$fullscale, $flash_chart,$force_interval,$time_interval,$max_only,$min_only);
-		
-		switch ($compare) {
-			case 'separated':
-				// Store the chart calculated
-				$chart_prev = $chart;
-				$legend_prev = $legend;
-				$long_index_prev = $long_index;
-				$series_type_prev = $series_type;
-				$color_prev = $color;
-				break;
-			case 'overlapped':
-				// Store the chart calculated deleting index,
-				// because will be over the current period
-				$chart_prev = $chart;
-				$legend_prev = $legend;
-				$series_type_prev = $series_type;
-				$color_prev = $color;
-				foreach($color_prev as $k => $col) {
-					$color_prev[$k]['color'] = '#' .
-						get_complementary_rgb($color_prev[$k]['color']);
-				}
-				break;
-		}
-	}
-	
-	// Build the data of the current period
-	$data_returned = grafico_modulo_sparse_data ($agent_module_id,
-		$period, $show_events,
-		$width, $height , $title, $unit_name,
-		$show_alerts, $avg_only,
-		$date, $unit, $baseline, $return_data, $show_title,
-		$projection, $adapt_key, $compare, '', '', $show_unknown,
-		$percentil, $dashboard, $vconsole, $type_graph, $fullscale,$flash_chart,
-		$force_interval,$time_interval,$max_only,$min_only);
-
-	if ($return_data) {
-		return $data_returned;
-	}
-	if ($compare === 'overlapped') {
-		/*
-		$i = 0;
-		foreach ($chart as $k=>$v) {
-			if (!isset($chart_prev[$i])) {
-				continue;
-			}
-			$chart[$k] = array_merge($v,$chart_prev[$i]);
-			$i++;
-		}
-		*/
-		foreach ($chart_prev as $k => $v) {
-			$chart[$k+$period]['sum2'] = $v['sum2'];
-			//unset($chart[$k]);
-		}
-		
-		/*
-		$chart["1517834070"]["sum2"] = "5";
-		$chart["1517834075"]["sum2"] = "6";
-		$chart["1517834080"]["sum2"] = "7";
-		$chart["1517834085"]["sum2"] = "8";
-		
-		$chart["1517834088"]["sum2"] = "3";
-		*/
-		ksort($chart);
-
-		$legend = array_merge($legend, $legend_prev);
-		$color = array_merge($color, $color_prev);
-	}
-
-	if ($only_image) {
-		$flash_chart = false;
-	}
-	if($config["fixed_graph"] == false){
-		$water_mark = array('file' =>
-			$config['homedir'] . "/images/logo_vertical_water.png",
-			'url' => ui_get_full_url("images/logo_vertical_water.png", false, false, false));
-	}
-
-	if ($type_graph === 'area') {
-		if ($compare === 'separated') {
-			return
-				area_graph($flash_chart, $chart, $width, $height/2, $color,
-					$legend, $long_index,
-					ui_get_full_url("images/image_problem_area_small.png", false, false, false),
-					$title, $unit, $homeurl, $water_mark, $config['fontpath'],
-					$config['font_size'], $unit, $ttl, $series_type,
-					$chart_extra_data, $warning_min, $critical_min,
-					$adapt_key, false, $series_suffix_str, $menu,
-					$backgroundColor).
-				'<br>'.
-				area_graph($flash_chart, $chart_prev, $width, $height/2,
-					$color_prev, $legend_prev, $long_index_prev,
-					ui_get_full_url("images/image_problem_area_small.png", false, false, false),
-					$title, $unit, $homeurl, $water_mark, $config['fontpath'],
-					$config['font_size'], $unit, $ttl, $series_type_prev,
-					$chart_extra_data, $warning_min, $critical_min,
-					$adapt_key, false, $series_suffix_str, $menu,
-					$backgroundColor);
-		}
-		else {
-			// Color commented not to restrict serie colors
-			if($id_widget_dashboard){
-				$opcion = unserialize(db_get_value_filter('options','twidget_dashboard',array('id' => $id_widget_dashboard)));
-				$color['min']['color'] = $opcion['min'];
-				$color['sum']['color'] = $opcion['avg'];
-				$color['max']['color'] = $opcion['max'];
-			}
-
-			return
-				area_graph($flash_chart, $chart, $width, $height, $color,
-					$legend, $long_index,
-					ui_get_full_url("images/image_problem_area_small.png", false, false, false),
-					$title, $unit, $homeurl, $water_mark, $config['fontpath'],
-					$config['font_size'], $unit, $ttl, $series_type,
-					$chart_extra_data, $warning_min, $critical_min,
-					$adapt_key, false, $series_suffix_str, $menu,
-					$backgroundColor, $dashboard, $vconsole, $agent_module_id);
-		}
-	}
-	elseif ($type_graph === 'line') {
-
-	}
 }
 
 function graph_get_formatted_date($timestamp, $format1, $format2) {
@@ -1124,7 +952,6 @@ function graphic_combined_module (
 	global $config;
 	global $graphic_type;
 	global $legend;
-	global $series_type;
 
 	$legend        = array();
 	$caption       = array();
@@ -1149,11 +976,11 @@ function graphic_combined_module (
 	$average
 	$modules_series
 */
-
+/*
 html_debug_print($name_list);
 html_debug_print($unit_list);
 html_debug_print($from_interface);
-
+*/
 	$date_array = array();
 	$date_array["period"]     = $period;
 	$date_array["final_date"] = $date;
@@ -1182,14 +1009,14 @@ html_debug_print($from_interface);
 	$show_elements_graph['pure']         = $pure;
 	$show_elements_graph['baseline']     = false; //dont use
 	$show_elements_graph['only_image']   = $only_image;
-	$show_elements_graph['return_data']  = true; //dont use
+	$show_elements_graph['return_data']  = false; //dont use
 	$show_elements_graph['id_widget']    = $id_widget_dashboard;
 	$show_elements_graph['labels']       = $labels;
 	$show_elements_graph['stacked']      = $stacked;
 
 	$format_graph = array();
-	$format_graph['width']      = $width . "px";
-	$format_graph['height']     = $height;
+	$format_graph['width']      = "90%";
+	$format_graph['height']     = "450";
 	$format_graph['type_graph'] = ''; //dont use
 	$format_graph['unit_name']  = $unit_name;
 	$format_graph['unit']       = ''; //dont use
@@ -1292,11 +1119,6 @@ html_debug_print($from_interface);
 			$data_module_graph
 		);
 
-		series_type_graph_array(
-			$series_suffix,
-			$data_module_graph
-		);
-
 		if($config["fixed_graph"] == false){
 			$water_mark = array(
 				'file' => $config['homedir'] .  "/images/logo_vertical_water.png",
@@ -1310,6 +1132,10 @@ html_debug_print($from_interface);
 		$i++;
 	}
 
+	$series_type = series_type_graph_array(
+		$array_data
+	);
+
 	if ($flash_charts === false && $stacked == CUSTOM_GRAPH_GAUGE)
 		$stacked = CUSTOM_GRAPH_BULLET_CHART;
 
@@ -1644,10 +1470,6 @@ html_debug_print($from_interface);
 		$module_name_list  = array();
 		$collector         = 0;
 
-
-
-
-
 				// Added to support projection graphs
 				if ($projection != false and $i != 0) {
 					$projection_data = array();
@@ -1677,6 +1499,9 @@ html_debug_print($from_interface);
 	//	$temp = array();
 
 
+	$graph_values = $temp;
+
+
 
 	//Set graph color
 	$threshold_data = array();
@@ -1791,129 +1616,100 @@ html_debug_print($from_interface);
 
 	//summatory and average series
 	if($stacked == CUSTOM_GRAPH_AREA  || $stacked == CUSTOM_GRAPH_LINE) {
-		//Fix pdf label
-		$static_pdf = strpos($module_name_list[0], '<span style');
+		if($summatory || $average) {
+			if(isset($array_data) && is_array($array_data)){
+				foreach ($array_data as $key => $value) {
+					if(strpos($key, 'sum') !== false){
+						$data_array_reverse[$key] = array_reverse($value['data']);
+						if(!$modules_series) {
+							unset($array_data[$key]);
+							unset($legend[$key]);
+							unset($series_type[$key]);
+						}
+					}
+				}
 
-		if($summatory && $average) {
-			foreach ($graph_values as $key => $value) {
-				$cont = count($value);
-				$summ = array_sum($value);
-				array_push($value,$summ);
-				array_push($value,$summ/$cont);
-				$graph_values[$key] = $value;
-				if(!$modules_series) {
-					array_splice($graph_values[$key],0,count($graph_values[$key])-2);
+				if(isset($data_array_reverse) && is_array($data_array_reverse)){
+					$array_sum_reverse = array();
+					$data_array_prev = false;
+					$data_array_pop = array();
+					$count = 0;
+
+					while(count($data_array_reverse['sum0']) > 0){
+						foreach ($data_array_reverse as $key_reverse => $value_reverse) {
+							if(is_array($value_reverse) && count($value_reverse) > 0){
+								$data_array_pop[$key_reverse] = array_pop($data_array_reverse[$key_reverse]);
+							}
+						}
+
+						if(isset($data_array_pop) && is_array($data_array_pop)){
+							$acum_data = 0;
+							$acum_array = array();
+							$sum_data = 0;
+							$count_pop = 0;
+							foreach ($data_array_pop as $key_pop => $value_pop) {
+								if( $value_pop[0] > $acum_data ){
+									if($acum_data != 0){
+										$sum_data = $sum_data + $data_array_prev[$key_pop][1];
+										$data_array_reverse[$key_pop][] = $value_pop;
+										$data_array_prev[$acum_key] = $acum_array;
+									}
+									else{
+										if($data_array_prev[$key_pop] == false){
+											$data_array_prev[$key_pop] = $value_pop;
+										}
+										$acum_key   = $key_pop;
+										$acum_data  = $value_pop[0];
+										$acum_array = $value_pop;
+										$sum_data   = $value_pop[1];
+									}
+								}
+								elseif($value_pop[0] < $acum_data){
+									$sum_data = $sum_data + $data_array_prev[$key_pop][1];
+									$data_array_reverse[$acum_key][] = $acum_array;
+									$data_array_prev[$key_pop] = $value_pop;
+									$acum_key   = $key_pop;
+									$acum_data  = $value_pop[0];
+									$acum_array = $value_pop;
+								}
+								elseif($value_pop[0] == $acum_data){
+									$data_array_prev[$key_pop] = $value_pop;
+									$sum_data += $value_pop[1];
+								}
+								$count_pop++;
+							}
+							if($summatory){
+								$array_sum_reverse[$count][0] = $acum_data;
+								$array_sum_reverse[$count][1] = $sum_data;
+							}
+							if($average){
+								$array_avg_reverse[$count][0] = $acum_data;
+								$array_avg_reverse[$count][1] = $sum_data / $count_pop;
+							}
+						}
+						$count++;
+					}
 				}
 			}
 
-			if(!$modules_series) {
-				if(empty($percentil)) {
-					array_splice($module_name_list,0,count($module_name_list));
-				} else {
-					array_splice($module_name_list,0,count($module_name_list)-(count($module_name_list)/2));
-				}
-				if($static_pdf === 0) {
-					array_unshift($module_name_list,'<span style=\"font-size:' . ($config['font_size']) . 'pt;font-family: smallfontFont;\" >' . __('summatory'). '</span>');
-					array_unshift($module_name_list,'<span style=\"font-size:' . ($config['font_size']) . 'pt;font-family: smallfontFont;\" >' . __('average'). '</span>');
-				} else {
-					array_unshift($module_name_list, __('summatory'));
-					array_unshift($module_name_list, __('average'));
-				}
-
-			} else {
-				if(empty($percentil)) {
-					if($static_pdf === 0) {
-						array_push($module_name_list,'<span style=\"font-size:' . ($config['font_size']) . 'pt;font-family: smallfontFont;\" >' . __('summatory'). '</span>');
-						array_push($module_name_list,'<span style=\"font-size:' . ($config['font_size']) . 'pt;font-family: smallfontFont;\" >' . __('average'). '</span>');
-					} else {
-						array_push($module_name_list, __('summatory'));
-						array_push($module_name_list, __('average'));
-					}
-				} else {
-					if($static_pdf === 0) {
-						array_splice($module_name_list,(count($module_name_list)/2),0,'<span style=\"font-size:' . ($config['font_size']) . 'pt;font-family: smallfontFont;\" >' . __('average'). '</span>');
-						array_splice($module_name_list,(count($module_name_list)/2),0,'<span style=\"font-size:' . ($config['font_size']) . 'pt;font-family: smallfontFont;\" >' . __('summatory'). '</span>');
-					} else {
-						array_splice($module_name_list,(count($module_name_list)/2),0, __('average'));
-						array_splice($module_name_list,(count($module_name_list)/2),0,__('summatory'));
-					}
-				}
-			}
-		} elseif($summatory) {
-			foreach ($graph_values as $key => $value) {
-				array_push($value,array_sum($value));
-				$graph_values[$key] = $value;
-				if(!$modules_series){
-					array_splice($graph_values[$key],0,count($graph_values[$key])-1);
-				}
+//XXXXX color,type,title, opacity
+			if(isset($array_sum_reverse) && is_array($array_sum_reverse)){
+				$array_data['sumatory']['data']  = $array_sum_reverse;
+				$array_data['sumatory']['color'] = 'purple';
+				$legend['sumatory']              = __('Summatory');
+				$series_type['sumatory']         = 'area';
 			}
 
-			if(!$modules_series) {
-				if(empty($percentil)) {
-					array_splice($module_name_list,0,count($module_name_list));
-				} else {
-					array_splice($module_name_list,0,count($module_name_list)-(count($module_name_list)/2));
-				}
-				if($static_pdf === 0) {
-					array_unshift($module_name_list,'<span style=\"font-size:' . ($config['font_size']) . 'pt;font-family: smallfontFont;\" >' . __('summatory'). '</span>');
-				} else {
-					array_unshift($module_name_list, __('summatory'));
-				}
-			} else {
-				if(empty($percentil)) {
-					if($static_pdf === 0) {
-						array_push($module_name_list,'<span style=\"font-size:' . ($config['font_size']) . 'pt;font-family: smallfontFont;\" >' . __('summatory'). '</span>');
-					} else {
-						array_push($module_name_list,__('summatory'));
-					}
-				} else {
-					if($static_pdf === 0) {
-						array_splice($module_name_list,(count($module_name_list)/2),0,'<span style=\"font-size:' . ($config['font_size']) . 'pt;font-family: smallfontFont;\" >' . __('summatory'). '</span>');
-					} else {
-						array_splice($module_name_list,(count($module_name_list)/2),0,__('summatory'));
-					}
-				}
-			}
-		} elseif($average) {
-			foreach ($graph_values as $key => $value) {
-				$summ = array_sum($value) / count($value);
-				array_push($value,$summ);
-				$graph_values[$key] = $value;
-				if(!$modules_series){
-					array_splice($graph_values[$key],0,count($graph_values[$key])-1);
-				}
-			}
-			if(!$modules_series) {
-				if(empty($percentil)) {
-					array_splice($module_name_list,0,count($module_name_list));
-				} else {
-					array_splice($module_name_list,0,count($module_name_list)-(count($module_name_list)/2));
-				}
-				if($static_pdf === 0) {
-					array_unshift($module_name_list,'<span style=\"font-size:' . ($config['font_size']) . 'pt;font-family: smallfontFont;\" >' . __('average'). '</span>');
-				} else {
-					array_unshift($module_name_list,__('average'));
-				}
-			} else {
-				if(empty($percentil)) {
-					if($static_pdf === 0) {
-						array_push($module_name_list,'<span style=\"font-size:' . ($config['font_size']) . 'pt;font-family: smallfontFont;\" >' . __('average'). '</span>');
-					} else {
-						array_push($module_name_list,__('average'));
-					}
-				} else {
-					if($static_pdf === 0) {
-						array_splice($module_name_list,(count($module_name_list)/2),0,'<span style=\"font-size:' . ($config['font_size']) . 'pt;font-family: smallfontFont;\" >' . __('average'). '</span>');
-					} else {
-						array_splice($module_name_list,(count($module_name_list)/2),0,__('average'));
-					}
-				}
+			if(isset($array_avg_reverse) && is_array($array_avg_reverse)){
+				$array_data['sum_avg']['data']  = $array_avg_reverse;
+				$array_data['sum_avg']['color'] = 'orange';
+				$legend['sum_avg']              = __('AVG');
+				$series_type['sum_avg']         = 'area';
 			}
+
 		}
 	}
 
-	$graph_values = $temp;
-
 	switch ($stacked) {
 		default:
 		case CUSTOM_GRAPH_STACKED_LINE:
@@ -1963,6 +1759,108 @@ html_debug_print($from_interface);
 	}
 }
 
+function combined_graph_summatory_average ($array_data){
+	if(isset($array_data) && is_array($array_data)){
+		foreach ($array_data as $key => $value) {
+			if(strpos($key, 'sum') !== false){
+				$data_array_reverse[$key] = array_reverse($value['data']);
+				/*
+				if(!$modules_series) {
+					unset($array_data[$key]);
+					unset($legend[$key]);
+					unset($series_type[$key]);
+				}
+				*/
+			}
+		}
+
+		if(isset($data_array_reverse) && is_array($data_array_reverse)){
+			$array_sum_reverse = array();
+			$array_avg_reverse = array();
+			$data_array_prev = false;
+			$data_array_pop = array();
+			$count = 0;
+
+			while(count($data_array_reverse['sum0']) > 0){
+				foreach ($data_array_reverse as $key_reverse => $value_reverse) {
+					if(is_array($value_reverse) && count($value_reverse) > 0){
+						$data_array_pop[$key_reverse] = array_pop($data_array_reverse[$key_reverse]);
+					}
+				}
+
+				if(isset($data_array_pop) && is_array($data_array_pop)){
+					$acum_data  = 0;
+					$acum_array = array();
+					$sum_data   = 0;
+					$count_pop  = 0;
+					foreach ($data_array_pop as $key_pop => $value_pop) {
+						if( $value_pop[0] > $acum_data ){
+							if($acum_data != 0){
+								$sum_data = $sum_data + $data_array_prev[$key_pop][1];
+								$data_array_reverse[$key_pop][] = $value_pop;
+								$data_array_prev[$acum_key] = $acum_array;
+							}
+							else{
+								if($data_array_prev[$key_pop] == false){
+									$data_array_prev[$key_pop] = $value_pop;
+								}
+								$acum_key   = $key_pop;
+								$acum_data  = $value_pop[0];
+								$acum_array = $value_pop;
+								$sum_data   = $value_pop[1];
+							}
+						}
+						elseif($value_pop[0] < $acum_data){
+							$sum_data = $sum_data + $data_array_prev[$key_pop][1];
+							$data_array_reverse[$acum_key][] = $acum_array;
+							$data_array_prev[$key_pop] = $value_pop;
+							$acum_key   = $key_pop;
+							$acum_data  = $value_pop[0];
+							$acum_array = $value_pop;
+						}
+						elseif($value_pop[0] == $acum_data){
+							$data_array_prev[$key_pop] = $value_pop;
+							$sum_data += $value_pop[1];
+						}
+						$count_pop++;
+					}
+				//	if($summatory){
+					//	$array_sum_reverse[$count][0] = $acum_data;
+					//	$array_sum_reverse[$count][1] = $sum_data;
+				//	}
+				//	if($average){
+						$array_avg_reverse[$count][0] = $acum_data;
+						$array_avg_reverse[$count][1] = $sum_data / $count_pop;
+				//	}
+				}
+				$count++;
+			}
+		}
+		return $array_avg_reverse;
+	}
+	else{
+		return false;
+	}
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 /**
  * Print a graph with access data of agents
  *
diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php
index 2a3fc52101..f3a6766835 100755
--- a/pandora_console/include/functions_reporting.php
+++ b/pandora_console/include/functions_reporting.php
@@ -3479,75 +3479,108 @@ function reporting_simple_baseline_graph($report, $content,
 	$force_height_chart = null) {
 
 	global $config;
-	
+
 	if ($config['metaconsole']) {
 		$id_meta = metaconsole_get_id_server($content["server_name"]);
-		
-		
 		$server = metaconsole_get_connection_by_id ($id_meta);
 		metaconsole_connect($server);
 	}
-	
+
 	$return['type'] = 'simple_baseline_graph';
-	
+
 	if (empty($content['name'])) {
 		$content['name'] = __('Simple baseline graph');
 	}
-	
+
 	$module_name = io_safe_output(
 		modules_get_agentmodule_name($content['id_agent_module']));
 	$agent_name = io_safe_output(
 		modules_get_agentmodule_agent_alias ($content['id_agent_module']));
-	
+
 	$return['title'] = $content['name'];
 	$return['subtitle'] = $agent_name . " - " . $module_name;
 	$return["description"] = $content["description"];
 	$return["date"] = reporting_get_date_text($report, $content);
 	$return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : '';
-	
+
 	// Get chart
 	reporting_set_conf_charts($width, $height, $only_image, $type,
 		$content, $ttl);
-	
+
 	if (!empty($force_width_chart)) {
 		$width = $force_width_chart;
 	}
-	
+
 	if (!empty($force_height_chart)) {
 		$height = $force_height_chart;
 	}
-	
+
+//XXXXXXXX
+	$width  = '90%';
+	$height = 300;
+
+	$baseline_data = enterprise_hook(
+		'reporting_enterprise_get_baseline',
+		array (
+			$content['id_agent_module'],
+			$content['period'],
+			$report["datetime"]
+		)
+	);
+
+	if ($baseline_data === ENTERPRISE_NOT_HOOK) {
+		$baseline_data = array ();
+	}
+
 	switch ($type) {
 		case 'dinamic':
 		case 'static':
-			$return['chart'] = grafico_modulo_sparse(
+			$return['chart'] = grafico_modulo_sparse (
 				$content['id_agent_module'],
 				$content['period'],
 				false,
 				$width,
 				$height,
 				'',
-				'',
+				null,
+				false,
+				0,
 				false,
-				true,
-				true,
 				$report["datetime"],
 				'',
-				true,
+				0,
 				0,
 				true,
 				$only_image,
 				ui_get_full_url(false, false, false, false),
-				$ttl);
+				$ttl,
+				false,
+				'',
+				false,
+				false,
+				true,
+				'white',
+				null,
+				false,
+				false,
+				'area',
+				false,
+				false,
+				0,
+				300,
+				0,
+				0,
+				$baseline_data
+			);
 			break;
 		case 'data':
 			break;
 	}
-	
+
 	if ($config['metaconsole']) {
 		metaconsole_restore_db();
 	}
-	
+
 	return reporting_check_structure_content($return);
 }
 
diff --git a/pandora_console/include/graphs/fgraph.php b/pandora_console/include/graphs/fgraph.php
index a708a29263..425dda50c1 100644
--- a/pandora_console/include/graphs/fgraph.php
+++ b/pandora_console/include/graphs/fgraph.php
@@ -230,7 +230,6 @@ function area_graph(
 	global $config;
 
 	include_once('functions_flot.php');
-
 	if ($config['flash_charts']) {
 		return flot_area_graph(
 			$agent_module_id,
diff --git a/pandora_console/include/graphs/flot/pandora.flot.js b/pandora_console/include/graphs/flot/pandora.flot.js
index 88df07cd3b..af44d4907a 100644
--- a/pandora_console/include/graphs/flot/pandora.flot.js
+++ b/pandora_console/include/graphs/flot/pandora.flot.js
@@ -1558,28 +1558,33 @@ function pandoraFlotArea(
 					break;
 			}
 
-			data_base.push({
-				id: 'serie_' + i,
-				data: value.data,
-				label: index + series_suffix_str,
-				color: value.color,
-				lines: {
-					show: line_show,
-					fill: filled,
-					lineWidth: lineWidth,
-					steps: steps_chart
-				},
-				points: {
-					show: points_show,
-					radius: 3,
-					fillColor: fill_points
-				}
-			});
-
-			i++;
+			//in graph stacked unset percentil
+			if(	! ( (type == 1) && ( /percentil/.test(index) ) == true ) &&
+				! ( (type == 3) && ( /percentil/.test(index) ) == true )   ){
+				data_base.push({
+					id: 'serie_' + i,
+					data: value.data,
+					label: index + series_suffix_str,
+					color: value.color,
+					lines: {
+						show: line_show,
+						fill: filled,
+						lineWidth: lineWidth,
+						steps: steps_chart
+					},
+					points: {
+						show: points_show,
+						radius: 3,
+						fillColor: fill_points
+					}
+				});
+			}
 		}
+		i++;
 	});
 
+	console.log(data_base);
+
 	// The first execution, the graph data is the base data
 	datas = data_base;
 	font_size = 8;
@@ -2120,6 +2125,7 @@ function pandoraFlotArea(
 		}
 
 		// Get only two decimals
+		//XXXXXXXXXX
 		formatted = round_with_decimals(formatted, 100)
 		return '<div class='+font+' style="font-size:'+font_size+'pt;">'+formatted+'</div>';
 	}
diff --git a/pandora_console/operation/agentes/stat_win.php b/pandora_console/operation/agentes/stat_win.php
index a2c92376ed..6d4527aedf 100644
--- a/pandora_console/operation/agentes/stat_win.php
+++ b/pandora_console/operation/agentes/stat_win.php
@@ -505,4 +505,4 @@ ui_include_time_picker(true);
 	$(window).resize(function() {
 		$("#field_list").css('height', ($(window).height() - 160) + 'px');
 	});
-</script>
\ No newline at end of file
+</script>