diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index 0125496b50..7a0faced18 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,18 @@ +2009-03-12 Esteban Sanchez + + * reporting/pandora_graph.php: Added to repository. New interface to + abstract pandora from the chart engines. This would make easier a + engine changing in the future. + + * reporting/pchart_graph.php: Added to repository. Specific class to + use pChart engine for Pandora. First working version. + + * reporting/pChart/pCache.class, reporting/pChart/pDaita.class: Added + to repository. pChart engine + + * reporting/pChart/pChart.class: Added to repository. pChart engine + slightly modified and adopted to Pandora needs. + 2009-03-12 Esteban Sanchez * include/auth/mysql.php: Added a cache to is_user_admin(). diff --git a/pandora_console/reporting/fgraph.php b/pandora_console/reporting/fgraph.php index 2c063de1d4..ef01b062e2 100644 --- a/pandora_console/reporting/fgraph.php +++ b/pandora_console/reporting/fgraph.php @@ -9,7 +9,7 @@ // modify it under the terms of the GNU Lesser General Public License (LGPL) // as published by the Free Software Foundation for version 2. // -// This program is distributed in the hope that it will be useful, +// This program is distributed in the hop4e 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. @@ -18,38 +18,32 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. require_once ('../include/config.php'); -if (!isset ($config["auth"])) { - require_once ($config["homedir"]."/include/auth/mysql.php"); -} else { - require_once ($config["homedir"]."/include/auth/".$config["auth"]["scheme"].".php"); -} - -require_once ($config["homedir"].'/include/functions.php'); -require_once ($config["homedir"].'/include/functions_db.php'); -require_once ('Image/Graph.php'); +require_once ($config['homedir'].'/reporting/pandora_graph.php'); -ini_set ('display_errors', 0); //This is to prevent notices from making the thing not graph +set_time_limit (0); +error_reporting (0); -if (!isset($_SESSION["id_user"])){ +if (! isset ($config["id_user"])) { session_start (); session_write_close (); + $config["id_user"] = $_SESSION["id_usuario"]; } -$config["id_user"] = $_SESSION["id_usuario"]; - // Session check check_login (); /** * Show a brief error message in a PNG graph */ - -function graphic_error () { - Header('Content-type: image/png'); - $imgPng = imageCreateFromPng('../images/image_problem.png'); - imageAlphaBlending($imgPng, true); - imageSaveAlpha($imgPng, true); - imagePng($imgPng); +function graphic_error ($image = 'image_problem.png') { + global $config; + + Header ('Content-type: image/png'); + $img = imagecreatefromPng ($config['homedir'].'/images/'.'image_problem.png'); + imagealphablending ($img, true); + imagesavealpha ($img, true); + imagepng ($img); + exit; } /** @@ -73,7 +67,7 @@ function dame_fecha ($mh) { */ function dame_fecha_grafico_timestamp ($timestamp) { - return date('d/m H:i', $timestamp); + return date ('d/m H:i', $timestamp); } /** @@ -91,32 +85,31 @@ function dame_fecha_grafico_timestamp ($timestamp) { * @param int Pure mode (without titles) (set to 1) * @param int Date to start of getting info. */ -function graphic_combined_module ($module_list, $weight_list, $periodo, $width, $height, +function graphic_combined_module ($module_list, $weight_list, $period, $width, $height, $title, $unit_name, $show_event = 0, $show_alert = 0, $pure = 0, $stacked = 0, $date = 0) { global $config; - - require_once 'Image/Graph.php'; + $resolution = $config['graph_res'] * 50; // Number of "slices" we want in graph if (! $date) $date = get_system_time (); - //$unix_timestamp = strtotime($mysql_timestamp) // Convert MYSQL format tio utime - $fechatope = $date - $periodo; // limit date - $horasint = $periodo / $resolution; // Each intervalo is $horasint seconds length - $module_number = count($module_list); + + $datelimit = $date - $period; // limit date + $interval = $period / $resolution; // Each interval is $interval seconds length + $module_number = count ($module_list); - // intervalo - This is the number of "rows" we are divided the time to fill data. - // more interval, more resolution, and slower. + // interval - This is the number of "rows" we are divided the time to fill data. + // more interval, more resolution, and slower. // periodo - Gap of time, in seconds. This is now to (now-periodo) secs // Init tables - for ($i = 0; $i < $module_number; $i++){ + for ($i = 0; $i < $module_number; $i++) { $real_data[$i] = array(); $mod_data[$i] = 1; // Data multiplier to get the same scale on all modules if ($show_event == 1) - $real_event[$i] = array(); - if (isset($weight_list[$i])){ + $real_event[$i] = array (); + if (isset ($weight_list[$i])) { if ($weight_list[$i] == 0) $weight_list[$i] = 1; } else @@ -126,1132 +119,553 @@ function graphic_combined_module ($module_list, $weight_list, $periodo, $width, $max_value = 0; $min_value = 0; // FOR EACH MODULE IN module_list.... - for ($i = 0; $i < $module_number; $i++){ + for ($i = 0; $i < $module_number; $i++) { $id_agente_modulo = $module_list[$i]; - $nombre_agente = get_agentmodule_agent_name($id_agente_modulo); - $id_agente = dame_agente_id($nombre_agente); + $nombre_agente = get_agentmodule_agent_name ($id_agente_modulo); + $id_agente = dame_agente_id ($nombre_agente); $nombre_modulo = get_agentmodule_name ($id_agente_modulo); - $module_list_name[$i] = substr($nombre_agente,0,9)." / ".substr($nombre_modulo,0,20); + $module_list_name[$i] = substr ($nombre_agente, 0, 9)." / ".substr ($nombre_modulo, 0, 20); for ($j = 0; $j <= $resolution; $j++) { - $valores[$j][0] = 0; // SUM of all values for this interval - $valores[$j][1] = 0; // counter - $valores[$j][2] = $fechatope + ($horasint * $j); // [2] Top limit for this range - $valores[$j][3] = $fechatope + ($horasint*($j+1)); // [3] Botom limit - $valores[$j][4] = 0; // MIN - $valores[$j][5] = 0; // MAX - $valores[$j][6] = 0; // Event + $data[$j]['sum'] = 0; // SUM of all values for this interval + $data[$j]['count'] = 0; // counter + $data[$j]['timestamp_bottom'] = $datelimit + ($interval * $j); // [2] Top limit for this range + $data[$j]['timestamp_top'] = $datelimit + ($interval*($j+1)); // [3] Botom limit + $data[$j]['min'] = 0; // MIN + $data[$j]['max'] = 0; // MAX + $data[$j]['events'] = 0; // Event } // Init other general variables - - - if ($show_event == 1){ + if ($show_event == 1) { // If we want to show events in graphs - $sql1="SELECT utimestamp FROM tevento WHERE id_agentmodule = $id_agente_modulo AND utimestamp > $fechatope"; - $result=mysql_query($sql1); - while ($row=mysql_fetch_array($result)){ - $utimestamp = $row[0]; - for ($i=0; $i <= $resolution; $i++) { - if ( ($utimestamp <= $valores[$i][3]) && ($utimestamp >= $valores[$i][2]) ){ - $real_event[$i]=1; + $sql = "SELECT utimestamp FROM tevento WHERE id_agentmodule = $id_agente_modulo AND utimestamp > $datelimit"; + $result = get_db_all_rows_sql ($sql); + if ($result === false) + $result = array (); + foreach ($result as $row) { + $utimestamp = $row['utimestamp']; + for ($i = 0; $i <= $resolution; $i++) { + if ($utimestamp <= $data[$i]['timestamp_top'] && $utimestamp >= $data[$i]['timestamp_bottom']) { + $real_event[$i] = 1; } } } } $alert_high = 0; $alert_low = 10000000; - if ($show_alert == 1){ + if ($show_alert == 1) { // If we want to show alerts limits - $sql1="SELECT * FROM talert_template_modules WHERE id_agent_module = ".$id_agente_modulo; - $result=mysql_query($sql1); - while ($row=mysql_fetch_array($result)){ - if ($row["max_value"] > $alert_high) + $sql = "SELECT * FROM talerta_agente_modulo where id_agente_modulo = ".$id_agente_modulo; + $result = get_db_all_rows_sql ($sql); + if ($result === false) + $result = array (); + foreach ($result as $row) { + if ($row["dis_max"] > $alert_high) $alert_high = $row["dis_max"]; - if ($row["max_value"] < $alert_low) + if ($row["dis_min"] < $alert_low) $alert_low = $row["dis_min"]; } } - $previous=0; - // Get the first data outsite (to the left---more old) of the interval given - $sql = "SELECT datos FROM tagente_datos WHERE id_agente_modulo = $id_agente_modulo AND utimestamp < $fechatope AND utimestamp >= $date ORDER BY utimestamp DESC"; - $previous = (float) get_db_sql ($sql); - $sql1="SELECT datos,utimestamp FROM tagente_datos WHERE id_agente_modulo = $id_agente_modulo AND utimestamp >= $fechatope AND utimestamp < $date"; - - $result = (array) get_db_all_rows_sql ($sql1); + // Get the first data outsite (to the left---more old) of the interval given + $previous = (float) get_previous_data ($id_agente_modulo, $datelimit); + + $result = get_db_all_rows_filter ('tagente_datos', + array ('id_agente' => $id_agente, + 'id_agente_modulo' => $id_agente_modulo, + "utimestamp > $datelimit", + "utimestamp < $date", + 'order' => 'utimestamp ASC'), + array ('datos', 'utimestamp')); + + if ($result === false) + graphic_error (); + foreach ($result as $row) { $datos = $row["datos"]; $utimestamp = $row["utimestamp"]; for ($j = 0; $j <= $resolution; $j++) { - if ($utimestamp <= $valores[$j][3] && $utimestamp > $valores[$j][2]) { - $valores[$j][0]=$valores[$j][0]+$datos; - $valores[$j][1]++; + if ($utimestamp <= $data[$j]['timestamp_top'] && $utimestamp > $data[$j]['timestamp_bottom']) { + $data[$j]['sum']=$data[$j]['sum']+$datos; + $data[$j]['count']++; // Init min value - if ($valores[$j][4] == 0) - $valores[$j][4] = $datos; + if ($data[$j]['min'] == 0) + $data[$j]['min'] = $datos; else { // Check min value - if ($datos < $valores[$j][4]) - $valores[$j][4] = $datos; + if ($datos < $data[$j]['min']) + $data[$j]['min'] = $datos; } // Check max value - if ($datos > $valores[$j][5]) - $valores[$j][5] = $datos; + if ($datos > $data[$j]['max']) + $data[$j]['max'] = $datos; break; } } } - - // Calculate Average value for $valores[][0] + // Calculate Average value for $data[][0] for ($j = 0; $j <= $resolution; $j++) { - if ($valores[$j][1] > 0){ - $real_data[$i][$j] = $weight_list[$i] * ($valores[$j][0]/$valores[$j][1]); - $valores[$j][0] = $valores[$j][0]/$valores[$j][1]; + if ($data[$j]['count'] > 0){ + $real_data[$i][$j] = $weight_list[$i] * ($data[$j]['sum']/$data[$j]['count']); + $data[$j]['sum'] = $data[$j]['sum']/$data[$j]['count']; } else { - $valores[$j][0] = $previous; + $data[$j]['sum'] = $previous; $real_data[$i][$j] = $previous * $weight_list[$i]; - $valores[$j][4] = $previous; - $valores[$j][5] = $previous; + $data[$j]['min'] = $previous; + $data[$j]['max'] = $previous; } // Get max value for all graph - if ($valores[$j][5] > $max_value ){ - $max_value = $valores[$j][5]; + if ($data[$j]['max'] > $max_value ){ + $max_value = $data[$j]['max']; } // This stores in mod_data max values for each module - if ($mod_data[$i] < $valores[$j][5]){ - $mod_data[$i] = $valores[$j][5]; + if ($mod_data[$i] < $data[$j]['max']) { + $mod_data[$i] = $data[$j]['max']; } // Take prev. value // TODO: CHeck if there are more than 24hours between // data, if there are > 24h, module down. - $previous = $valores[$j][0]; + $previous = $data[$j]['sum']; } } - for ($i = 0; $i < $module_number; $i++){ - // Disabled autoadjusment, is not working fine :( - // $weight_list[$i] = ($max_value / $mod_data[$i]) + ($weight_list[$i]-1); + for ($i = 0; $i < $module_number; $i++) { if ($weight_list[$i] != 1) - $module_list_name[$i] .= " (x". format_numeric($weight_list[$i],1).")"; - $module_list_name[$i] = $module_list_name[$i]." (MAX: ".format_numeric($mod_data[$i]).")"; + $module_list_name[$i] .= " (x". format_numeric ($weight_list[$i], 1).")"; } - - // Create graph - // ************* - $Graph =& Image_Graph::factory('graph', array($width, $height)); - // add a TrueType font - - if ($periodo == 86400) - $title_period = "Last day"; - elseif ($periodo == 604800) - $title_period = "Last week"; - elseif ($periodo == 3600) - $title_period = "Last hour"; - elseif ($periodo == 2419200) - $title_period = "Last month"; + + if ($period <= 86400) + $title_period = __('Last day'); + elseif ($period <= 604800) + $title_period = __('Last week'); + elseif ($period <= 3600) + $title_period = __('Last hour'); + elseif ($period <= 2419200) + $title_period = __('Last month'); else - $title_period = "Last ".format_numeric (($periodo / (3600 * 24)), 2)." days"; - - if ($pure == 0){ - $Font =& $Graph->addNew('font', $config['fontpath']); - $Font->setSize(7); - $Graph->setFont($Font); - $Graph->add( - Image_Graph::vertical( - Image_Graph::vertical( - $Title = Image_Graph::factory('title', array(' Pandora FMS Graph - '. $title_period, 10)), - $Subtitle = Image_Graph::factory('title', array(' '.$title, 7)), - 90 - ), - Image_Graph::vertical( - $Plotarea = Image_Graph::factory('plotarea'), - $Legend = Image_Graph::factory('legend'), - 80 - ), - 20) - ); - $Legend->setPlotarea($Plotarea); - $Title->setAlignment(IMAGE_GRAPH_ALIGN_LEFT); - $Subtitle->setAlignment(IMAGE_GRAPH_ALIGN_LEFT); - } else { - $Font =& $Graph->addNew('font', $config['fontpath']); - $Font->setSize(7); - $Graph->setFont($Font); - $Graph->add( - Image_Graph::vertical( - $Plotarea = Image_Graph::factory('plotarea'), - $Legend = Image_Graph::factory('legend'), - 85 - ) - ); - $Legend->setPlotarea($Plotarea); - } - - // Create the dataset - // Merge data into a dataset object (sancho) - // $Dataset =& Image_Graph::factory('dataset'); - - for ($i = 0; $i < $module_number; $i++){ - $dataset[$i] = Image_Graph::factory('dataset'); - $dataset[$i] -> setName($module_list_name[$i]); - } - if ($show_event == 1) { - $dataset_event = Image_Graph::factory('dataset'); - $dataset_event -> setName("Event Fired"); - } - - // ... and populated with data ... - for ($i = 0; $i < $resolution; $i++) { - $tdate = date('d/m', $valores[$i][2])."\n".date('H:i', $valores[$i][2]); - for ($j = 0; $j < $module_number; $j++) { - $dataset[$j]->addPoint($tdate, $real_data[$j][$i]); - if (($show_event == 1) AND (isset($real_event[$i]))) { - $dataset_event->addPoint($tdate, $max_value); - } - } - } + $title_period = sprintf (__('Last %s days'), format_numeric (($period / (3600 * 24)), 2)); if ($max_value <= 0) { graphic_error (); - return; - } - // Show events ! - if ($show_event == 1) { - $Plot =& $Plotarea->addNew('Plot_Impulse', array($dataset_event)); - $Plot->setLineColor( 'black' ); - $Marker_event =& Image_Graph::factory('Image_Graph_Marker_Cross'); - $Plot->setMarker($Marker_event); - $Marker_event->setFillColor( 'red' ); - $Marker_event->setLineColor( 'red' ); - $Marker_event->setSize ( 5 ); } - // Show limits (for alert or whathever you want... - if ($show_alert == 1){ - $Plot =& $Plotarea->addNew('Image_Graph_Axis_Marker_Area', IMAGE_GRAPH_AXIS_Y); - $Plot->setFillColor( 'blue@0.1' ); - $Plot->setLowerBound( $alert_low); - $Plot->setUpperBound( $alert_high ); - } + $engine = get_graph_engine ($period); - - // create the 1st plot as smoothed area chart using the 1st dataset - if ($stacked == 0) { - - // Non-stacked - $Plot =& $Plotarea->addNew('area', array(&$dataset)); - - } elseif ($stacked == 1) { - - // Stacked (> 2.0) - $Plot =& $Plotarea->addNew('Image_Graph_Plot_Area', array(&$dataset, 'stacked')); - - } else { - - $color_array[0] = "red"; - $color_array[1] = "blue"; - $color_array[2] = "green"; - $color_array[3] = 'yellow'; // yellow - $color_array[4] = '#FF5FDF'; // pink - $color_array[5] = 'orange'; // orange - $color_array[6] = '#FE00DA'; // magenta - $color_array[7] = '#00E2FF'; // cyan - $color_array[8] = '#000000'; // Black - - // Single lines, new in 2.0 (Jul08) - for ($i = 0; $i < $module_number; $i++){ - $Plot =& $Plotarea->addNew('line', array(&$dataset[$i])); - $Plot->setLineColor($color_array[$i]); - } - } - - // Color management - if ($stacked != 2){ - $Plot->setLineColor('gray@0.4'); - } - - $AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X); - // $AxisX->Hide(); - $AxisY =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y); - $AxisY->setLabelOption("showtext",true); - $AxisY->setLabelInterval(ceil($max_value / 5)); - $AxisY->showLabel(IMAGE_GRAPH_LABEL_ZERO); - if ($unit_name != "") - $AxisY->setTitle($unit_name, 'vertical'); - $AxisX->setLabelInterval($resolution / 10); - //$AxisY->forceMinimum($minvalue); - //$AxisY->forceMaximum($max_value+($max_value/12)) ; - $GridY2 =& $Plotarea->addNew('bar_grid', IMAGE_GRAPH_AXIS_Y_SECONDARY); - $GridY2->setLineColor('gray'); - $GridY2->setFillColor('lightgray@0.05'); - // set line colors - $FillArray =& Image_Graph::factory('Image_Graph_Fill_Array'); - $Plot->setFillStyle($FillArray); - $FillArray->addColor('#BFFF51@0.6'); // Green - $FillArray->addColor('yellow@0.6'); // yellow - $FillArray->addColor('#FF5FDF@0.6'); // pink - $FillArray->addColor('orange@0.6'); // orange - $FillArray->addColor('#7D8AFF@0.6'); // blue - $FillArray->addColor('#FF302A@0.6'); // red - $FillArray->addColor('brown@0.6'); // brown - $FillArray->addColor('green@0.6'); - $AxisY_Weather =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y); - $Graph->done(); + $engine->width = $width; + $engine->height = $height; + $engine->data = &$real_data; + $engine->legend = &$legend; + $engine->fontpath = $config['fontpath']; + $engine->title = ' '.strtoupper ($nombre_agente)." - ".__('Module').' '.$title; + $engine->subtitle = ' '.__('Period').': '.$title_period; + $engine->show_title = !$pure; + $engine->stacked = $stacked; + $engine->legend = $module_list_name; + $engine->xaxis_interval = $resolution; + $events = $show_event ? $real_event : false; + $alerts = $show_alert ? array ('low' => $alert_low, 'high' => $alert_high) : false; + $engine->combined_graph ($data, $events, $alerts, $unit_name, $max_value, $stacked); } -function grafico_modulo_sparse ($id_agente_modulo, $periodo, $show_event, - $width, $height , $title, $unit_name, $show_alert, $avg_only = 0, $pure = 0, $date = 0) { +function grafico_modulo_sparse ($id_agente_modulo, $period, $show_event, + $width, $height , $title, $unit_name, + $show_alert, $avg_only = 0, $pure = false, + $date = 0) { global $config; - require_once 'Image/Graph.php'; - + if (empty ($date)) $date = get_system_time (); $resolution = $config["graph_res"] * 50; // Number of "slices" we want in graph - $fechatope = $date - $periodo; - - $horasint = $periodo / $resolution; // Each intervalo is $horasint seconds length + $datelimit = $date - $period; + $real_event = array (); + + $interval = (int) ($period / $resolution); // Each interval is $interval seconds length $nombre_agente = get_agentmodule_agent_name ($id_agente_modulo); $id_agente = dame_agente_id ($nombre_agente); $nombre_modulo = get_agentmodule_name ($id_agente_modulo); - - if ($show_event) { - // If we want to show events in graphs - $sql1 = "SELECT utimestamp FROM tevento WHERE id_agentmodule = $id_agente_modulo AND utimestamp > $fechatope"; - $result = mysql_query($sql1); - while ($row=mysql_fetch_array($result)) { - $utimestamp = $row[0]; - for ($i=0; $i <= $resolution; $i++) { - if ($utimestamp <= $valores[$i][3] && $utimestamp >= $valores[$i][2]) { - $real_event[$i]=1; - } - } - } - } - - if ($show_alert) { - $alert_high = false; - $alert_low = false; - // If we want to show alerts limits - - $alert_high = (int) get_db_value ('MAX(max_value)', 'talert_template_modules', 'id_agent_module', (int) $id_agente_modulo); - $alert_low = (int) get_db_value ('MIN(max_value)', 'talert_template_modules', 'id_agent_module', (int) $id_agente_modulo); - - // if no valid alert defined to render limits, disable it - if (($alert_low == 0) && ($alert_high == 0)) { - $show_alert = 0; - } - } - - // intervalo - This is the number of "rows" we are divided the time - // to fill data. more interval, more resolution, and slower. - // periodo - Gap of time, in seconds. This is now to (now-periodo) secs // Init tables for ($i = 0; $i <= $resolution; $i++) { - $valores[$i][0] = 0; // SUM of all values for this interval - $valores[$i][1] = 0; // counter - $valores[$i][2] = $fechatope + ($horasint * $i); // [2] Top limit for this range - $valores[$i][3] = $fechatope + ($horasint * ($i + 1)); // [3] Botom limit - $valores[$i][4] = 0; // MIN - $valores[$i][5] = 0; // MAX - $valores[$i][6] = 0; // Event - + $data[$i]['sum'] = 0; + $data[$i]['count'] = 0; + $data[$i]['timestamp_bottom'] = $datelimit + ($interval * $i); + $data[$i]['timestamp_top'] = $datelimit + ($interval * ($i + 1)); + $data[$i]['min'] = 0; + $data[$i]['max'] = 0; + $data[$i]['last'] = 0; + $data[$i]['events'] = 0; } - // Init other general variables - if ($show_event){ - // If we want to show events in graphs - $sql = sprintf ('SELECT utimestamp FROM tevento WHERE id_agente = %d AND utimestamp > %d', $id_agente, $fechatope); - $eventos = get_db_all_rows_sql ($sql); - if ($eventos === false) { - $eventos = array (); - } - - foreach ($eventos as $row) { - $utimestamp = $row[0]; - for ($i = 0; $i <= $resolution; $i++) { - if ($utimestamp <= $valores[$i][3] && $utimestamp >= $valores[$i][2]) { - $real_event[$i] = 1; - } - } - } - } - // Init other general variables + + $all_data = get_db_all_rows_filter ('tagente_datos', + array ('id_agente' => $id_agente, + 'id_agente_modulo' => $id_agente_modulo, + "utimestamp > $datelimit", + "utimestamp < $date", + 'order' => 'utimestamp ASC'), + array ('datos', 'utimestamp')); + + if ($all_data === false) + graphic_error (); $max_value = 0; $min_value = 0; - - // Get the first data outsite (to the left---more old) of the interval given - $sql = sprintf ('SELECT datos, utimestamp FROM tagente_datos - WHERE id_agente_modulo = %d - AND utimestamp < %d ORDER BY utimestamp DESC LIMIT 1', $id_agente, $id_agente_modulo, $fechatope); - $previous = (float) get_db_sql ($sql); - - $sql = sprintf ('SELECT datos,utimestamp FROM tagente_datos - WHERE id_agente_modulo = %d AND utimestamp > %d', $id_agente_modulo, $fechatope); - $result = get_db_all_rows_sql ($sql); - if (empty ($result)) { - graphic_error (); - return; - } - foreach ($result as $row) { - $datos = $row["datos"]; - $utimestamp = $row["utimestamp"]; - for ($i = 0; $i <= $resolution; $i++) { - if ( ($utimestamp <= $valores[$i][3]) && ($utimestamp >= $valores[$i][2]) ){ - $valores[$i][0]=$valores[$i][0]+$datos; - $valores[$i][1]++; - // Init min value - if ($valores[$i][4] == 0) - $valores[$i][4] = $datos; - else { - // Check min value - if ($datos < $valores[$i][4]) - $valores[$i][4] = $datos; - } - // Check max value - if ($datos > $valores[$i][5]) - $valores[$i][5] = $datos; - break; - } - } + $start = 0; + foreach ($all_data as $module_data) { + $utimestamp = $module_data['utimestamp']; + $real_data = $module_data['datos']; + for ($i = $start; $i <= $resolution; $i++) { + if ($utimestamp <= $data[$i]['timestamp_top'] && $utimestamp >= $data[$i]['timestamp_bottom']) { + $start = $i; + $data[$i]['sum'] += $real_data; + $data[$i]['count']++; + $data[$i]['last'] = $real_data; - } - - // Calculate Average value for $valores[][0] - for ($i =0; $i <= $resolution; $i++) { - if ($valores[$i][1] > 0) - $valores[$i][0] = $valores[$i][0]/$valores[$i][1]; - else { - $valores[$i][0] = $previous; - $valores[$i][4] = $previous; - $valores[$i][5] = $previous; - } - // Get max value for all graph - if ($valores[$i][5] > $max_value) - $max_value = $valores[$i][5]; - // Get min value for all graph - if ($valores[$i][5] < $min_value) - $min_value = $valores[$i][5]; - - // Take prev. value - // TODO: CHeck if there are more than 24hours between - // data, if there are > 24h, module down. - $previous = $valores[$i][0]; - } - - // Create graph - // ************* - $Graph =& Image_Graph::factory('graph', array($width, $height)); - // add a TrueType font - $Font =& $Graph->addNew('font', $config['fontpath']); - $Font->setSize(6); - $Graph->setFont($Font); - - if ($periodo == 86400) - $title_period = "Last day"; - elseif ($periodo == 604800) - $title_period = "Last week"; - elseif ($periodo == 3600) - $title_period = "Last hour"; - elseif ($periodo == 2419200) - $title_period = "Last month"; - else - $title_period = "Last ".format_numeric(($periodo / (3600*24)),2)." days"; - if ($pure == 0){ - $Graph->add( - Image_Graph::vertical( - Image_Graph::vertical( - $Title = Image_Graph::factory('title', array(' Pandora FMS Graph - '.strtoupper($nombre_agente)." - ".$title_period, 10)), - $Subtitle = Image_Graph::factory('title', array(' '.$title, 7)), - 90 - ), - Image_Graph::horizontal( - $Plotarea = Image_Graph::factory('plotarea'), - $Legend = Image_Graph::factory('legend'), - 85 - ), - 15) - ); - $Legend->setPlotarea($Plotarea); - $Title->setAlignment(IMAGE_GRAPH_ALIGN_LEFT); - $Subtitle->setAlignment(IMAGE_GRAPH_ALIGN_LEFT); - } else { // Pure, without title and legends - $Graph->add($Plotarea = Image_Graph::factory('plotarea')); - } - // Create the dataset - // Merge data into a dataset object (sancho) - // $Dataset =& Image_Graph::factory('dataset'); - if ($avg_only == 1) { - $dataset[0] = Image_Graph::factory('dataset'); - $dataset[0]->setName("Avg."); - } else { - $dataset[0] = Image_Graph::factory('dataset'); - $dataset[0]->setName("Max."); - $dataset[1] = Image_Graph::factory('dataset'); - $dataset[1]->setName("Avg."); - $dataset[2] = Image_Graph::factory('dataset'); - $dataset[2]->setName("Min."); - } - // Event dataset creation - if ($show_event == 1){ - $dataset_event = Image_Graph::factory('dataset'); - $dataset_event -> setName("Event Fired"); - } - - // ... and populated with data ... - for ($i = 0; $i <= $resolution; $i++) { - $tdate = date('d/m', $valores[$i][2])."\n".date('H:i', $valores[$i][2]); - if ($avg_only == 0) { - $dataset[1]->addPoint($tdate, $valores[$i][0]); - $dataset[0]->addPoint($tdate, $valores[$i][5]); - $dataset[2]->addPoint($tdate, $valores[$i][4]); - } else { - $dataset[0]->addPoint($tdate, $valores[$i][0]); - } - if (($show_event == 1) AND (isset($real_event[$i]))) { - $dataset_event->addPoint($tdate, $valores[$i][5]); - } - } - - if ($max_value != $min_value){ - // Show alert limits - if ($show_alert == 1){ - $Plot =& $Plotarea->addNew('Image_Graph_Axis_Marker_Area', IMAGE_GRAPH_AXIS_Y); - $Plot->setFillColor( 'gray@0.1' ); - $Plot->setLowerBound( $alert_low); - $Plot->setUpperBound( $alert_high ); - } - - // create the 1st plot as smoothed area chart using the 1st dataset - $Plot =& $Plotarea->addNew('area', array(&$dataset)); - if ($avg_only == 1){ - $Plot->setLineColor('black@0.1'); - } else { - $Plot->setLineColor('yellow@0.2'); - } - - $AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X); - // $AxisX->Hide(); + // Init min value + if ($data[$i]['min'] == 0 || $real_data < $data[$i]['min']) + $data[$i]['min'] = $real_data; + + // Check max value + if ($real_data > $data[$i]['max']) + $data[$i]['max'] = $real_data; + + // Get max value for all graph + if ($data[$i]['max'] > $max_value) + $max_value = $data[$i]['max']; + + // Get min value for all graph + $max_value = max ($max_value, $data[$i]['max']); + $min_value = min ($min_value, $data[$i]['min']); + + if ($show_alert) { + $alert_high = false; + $alert_low = false; + // If we want to show alerts limits - $AxisY =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y); - $AxisY->setDataPreprocessor(Image_Graph::factory('Image_Graph_DataPreprocessor_Function', 'format_for_graph')); - $AxisY->setLabelOption("showtext",true); - $yinterval = $height / 30; - - - if (($min_value < 0) AND ($max_value > 0)) - $AxisY->setLabelInterval( -1 * ceil(($min_value - $max_value)/ $yinterval )); - elseif ($min_value < 0) - $AxisY->setLabelInterval( -1 * ceil($min_value / $yinterval)); - else - $AxisY->setLabelInterval(ceil($max_value / $yinterval)); - - $AxisY->showLabel(IMAGE_GRAPH_LABEL_ZERO); - if ($unit_name != ""){ - $AxisY->setTitle($unit_name, 'vertical'); if ($periodo < 10000) - $xinterval = 8; - } else - $xinterval = $resolution / 7 ; - $AxisX->setLabelInterval($xinterval) ; - - //$AxisY->forceMinimum($minvalue); - $AxisY->forceMaximum($max_value+($max_value/12)) ; - $GridY2 =& $Plotarea->addNew('bar_grid', IMAGE_GRAPH_AXIS_Y_SECONDARY); - $GridY2->setLineColor('gray'); - $GridY2->setFillColor('lightgray@0.05'); - // set line colors - $FillArray =& Image_Graph::factory('Image_Graph_Fill_Array'); - - $Plot->setFillStyle($FillArray); - if ($avg_only == 1){ - $FillArray->addColor($config["graph_color1"]); - } else { - $FillArray->addColor($config["graph_color3"]); - $FillArray->addColor($config["graph_color2"]); - $FillArray->addColor($config["graph_color1"]); - - - } - $AxisY_Weather =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y); - - // Show events ! - if ($show_event == 1){ - $Plot =& $Plotarea->addNew('Plot_Impulse', array($dataset_event)); - $Plot->setLineColor( 'red' ); - $Marker_event =& Image_Graph::factory('Image_Graph_Marker_Cross'); - $Plot->setMarker($Marker_event); - $Marker_event->setFillColor( 'red' ); - $Marker_event->setLineColor( 'red' ); - $Marker_event->setSize ( 5 ); - } - - $Graph->done(); - } else - graphic_error (); -} - -function generic_pie_graph ($width = 300, $height = 200, $data, $legend) { - global $config; - - if (sizeof($data) == 0) { - graphic_error (); - return; - } - // create the graph - $driver=& Image_Canvas::factory('png',array('width'=>$width,'height'=>$height,'antialias' => 'native')); - $Graph = & Image_Graph::factory('graph', $driver); - // add a TrueType font - $Font =& $Graph->addNew('font', $config['fontpath']); - // set the font size to 7 pixels - $Font->setSize(7); - $Graph->setFont($Font); - // create the plotarea - $Graph->add( - Image_Graph::horizontal( - $Plotarea = Image_Graph::factory('plotarea'), - $Legend = Image_Graph::factory('legend'), - 50 - ) - ); - $Legend->setPlotarea($Plotarea); - // Create the dataset - // Merge data into a dataset object (sancho) - $Dataset1 =& Image_Graph::factory('dataset'); - for ($i = 0; $i < sizeof($data); $i++) { - $Dataset1->addPoint(str_pad($legend[$i],15), $data[$i]); - } - $Plot =& $Plotarea->addNew('pie', $Dataset1); - $Plotarea->hideAxis(); - // create a Y data value marker - $Marker =& $Plot->addNew('Image_Graph_Marker_Value', IMAGE_GRAPH_PCT_Y_TOTAL); - // create a pin-point marker type - $PointingMarker =& $Plot->addNew('Image_Graph_Marker_Pointing_Angular', array(1, &$Marker)); - // and use the marker on the 1st plot - $Plot->setMarker($PointingMarker); - // format value marker labels as percentage values - $Marker->setDataPreprocessor(Image_Graph::factory('Image_Graph_DataPreprocessor_Formatted', '%0.1f%%')); - $Plot->Radius = 15; - $FillArray =& Image_Graph::factory('Image_Graph_Fill_Array'); - $Plot->setFillStyle($FillArray); - - $FillArray->addColor('green@0.7'); - $FillArray->addColor('yellow@0.7'); - $FillArray->addColor('red@0.7'); - $FillArray->addColor('orange@0.7'); - $FillArray->addColor('blue@0.7'); - $FillArray->addColor('purple@0.7'); - $FillArray->addColor('lightgreen@0.7'); - $FillArray->addColor('lightblue@0.7'); - $FillArray->addColor('lightred@0.7'); - $FillArray->addColor('grey@0.6', 'rest'); - $Plot->explode(6); - $Plot->setStartingAngle(0); - // output the Graph - $Graph->done(); -} - - -function graphic_agentmodules($id_agent, $width, $height) { - global $config; - - $sql1="SELECT * FROM ttipo_modulo"; - $result=mysql_query($sql1); - $ntipos = 0; - while ($row=mysql_fetch_array($result)){ - $data_label[$ntipos]=$row["nombre"]; - $data[$ntipos]=0; - $data_id[$ntipos] = $row["id_tipo"]; - $ntipos++; - } - $cx=0; - $sql1="SELECT * FROM tagente_modulo WHERE id_agente = ".$id_agent; - $result=mysql_query($sql1); - while ($row=mysql_fetch_array($result)){ - $cx++; - for ($i = 0; $i <= $ntipos; $i++){ - if (isset($data_id[$i])){ - if ($data_id[$i] == $row["id_tipo_modulo"]) { - $data[$i]++; + $alert_high = (int) get_db_value ('MAX(max_value)', + 'talert_template_modules', + 'id_agent_module', + (int) $id_agente_modulo); + $alert_low = (int) get_db_value ('MIN(max_value)', + 'talert_template_modules', + 'id_agent_module', + (int) $id_agente_modulo); + + // if no valid alert defined to render limits, disable it + if (($alert_low == 0) && ($alert_high == 0)) { + $show_alert = 0; + } } } - } - } - $data2 = ""; - $data_label2 = ""; - $mayor = 0; - $mayor_data =0; - for ($i = 0; $i < sizeof($data); $i++) - if ($data[$i] > $mayor_data){ - $mayor = $i; - $mayor_data = $data[$i]; - } - $bx=0; - for ($i=0;$i < sizeof($data_label); $i++){ - if ($data[$i] > 0){ - $data_label2[$bx] = $data_label[$i]; - $data2[$bx] = $data[$i]; - $bx++; + + if ($show_event) { + // If we want to show events in graphs + $events = get_db_value_filter ('COUNT(*)', 'tevento', + array ('id_agentmodule' => $id_agente_modulo, + 'utimestamp >= '.$data[$i]['timestamp_bottom'], + 'utimestamp < '.$data[$i]['timestamp_top'])); + + if ($events) + $data[$i]['events']++; + } } } - generic_pie_graph ($width, $height, $data2, $data_label2); - -} - -function graphic_agentaccess ($id_agent, $periodo, $width, $height) { - global $config; - $color ="#437722"; // Green pandora 1.1 octopus color - /* - $agent_interval = give_agentinterval($id_agent); - $intervalo = 30 * $config['graph_res']; // Desired interval / range between dates - $intervalo_real = (86400 / $agent_interval); // 60x60x24 secs - if ($intervalo_real < $intervalo ) { - $intervalo = $intervalo_real; - - }*/ - $intervalo = 24; - $UNIXdate = date('U'); - $fechatope = $UNIXdate - (60*24*60); - $horasint = 86400 / $intervalo; - - // $intervalo now stores "ideal" interval } - // interval is the number of rows that will store data. more rows, more resolution - - // Para crear las graficas vamos a crear un array de Ix4 elementos, donde - // I es el numero de posiciones diferentes en la grafica (30 para un mes, 7 para una semana, etc) - // y los 4 valores en el ejeY serian los detallados a continuacion: - // Rellenamos la tabla con un solo select, y los calculos se hacen todos sobre memoria - // esto acelera el tiempo de calculo al maximo, aunque complica el algoritmo :-) - - // Creamos la tabla (array) con los valores para el grafico. Inicializacion - for ($i = 0; $i <= $intervalo; $i++) { - $valores[$i][0] = 0; // [0] Valor (contador) - $valores[$i][1] = 0; // [0] Valor (contador) - $valores[$i][2] = $fechatope + ($horasint * $i); // [2] Rango superior de fecha para ese rango - $valores[$i][3] = $fechatope + ($horasint*($i+1)); // [3] Rango inferior de fecha para ese rango - } - $sql1="SELECT utimestamp FROM tagent_access WHERE id_agent = ".$id_agent." and utimestamp > '".$fechatope."'"; - - $result= get_db_all_rows_sql ($sql1); - foreach ($result as $row) { - for ($i = 0; $i < $intervalo; $i++){ - if (($row["utimestamp"] > $valores[$i][2]) and ($row["utimestamp"] <= $valores[$i][3]) ){ - // entra en esta fila - $valores[$i][0]++; - } - - } - } - $valor_maximo = 0; - - for ($i = 0; $i < $intervalo; $i++) { // 30 entries in graph, one by day - $grafica[]=$valores[$intervalo-$i-1][0]; - if ($valores[$i][0] > $valor_maximo) - $valor_maximo = $valores[$i][0]; - } - - // Create graph - // create the graph - $Graph =& Image_Graph::factory('graph', array($width, $height)); - // add a TrueType font - $Font =& $Graph->addNew('font', $config['fontpath']); - $Font->setSize(6); - $Graph->setFont($Font); - $Graph->add( - Image_Graph::vertical( - Image_Graph::factory('title', array("", 2)), - $Plotarea = Image_Graph::factory('plotarea'),0) - ); - // Create the dataset - // Merge data into a dataset object (sancho) - $Dataset =& Image_Graph::factory('dataset'); - for ($i = 0; $i < sizeof($grafica); $i++) { - $Dataset->addPoint ($i, $grafica[$i]); - } - // create the 1st plot as smoothed area chart using the 1st dataset - $Plot =& $Plotarea->addNew('area', array(&$Dataset)); - // set a line color - $Plot->setLineColor('green'); - // set a standard fill style - $Plot->setFillColor('green@0.5'); - // $Plotarea->hideAxis(); - $AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X); - // $AxisX->Hide(); - - $AxisY =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y); - $AxisY->setLabelOption("showtext",true); - $AxisY->setLabelInterval($valor_maximo / 2); - - $AxisX->setLabelInterval($intervalo / 5); - - $GridY2 =& $Plotarea->addNew('bar_grid', IMAGE_GRAPH_AXIS_Y_SECONDARY); - $GridY2->setLineColor('green'); - $GridY2->setFillColor('green@0.2'); - $AxisY2 =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y_SECONDARY); - $Graph->done(); -} - -function graphic_string_data ($id_agent_module, $periodo, $width, $height, $pure = 0, $date = 0) { - global $config; - - // $color = $config["color_graph1"]; //#437722"; // Green pandora 1.1 octopus color - $color = "#437722"; - - - if ($date == 0) - $date = get_system_time (); - $resolution = $config["graph_res"] * 5; // Number of "slices" we want in graph - $fechatope = $date - $periodo; - $horasint = $periodo / $resolution; // Each intervalo is $horasint seconds length - - - // Creamos la tabla (array) con los valores para el grafico. Inicializacion - for ($i = 0; $i <$resolution; $i++) { - $valores[$i][0] = 0; // [0] Valor (contador) - $valores[$i][1] = dame_fecha_grafico_timestamp ($fechatope + ($horasint * $i)); - $valores[$i][2] = $fechatope + ($horasint * $i); // [2] Top limit for this range - $valores[$i][3] = $fechatope + ($horasint * ($i + 1)); // [3] Botom limit - } - $sql1="SELECT utimestamp FROM tagente_datos_string WHERE id_agente_modulo = ".$id_agent_module." and utimestamp > '".$fechatope."'"; - - $result = get_db_all_rows_sql ($sql1); - - foreach ($result as $row) { - for ($i = 0; $i < $resolution; $i++){ - if (($row["utimestamp"] < $valores[$i][3]) and ($row["utimestamp"] >= $valores[$i][2]) ){ - // entra en esta fila - $valores[$i][0]++; - } - } - - } - $valor_maximo = 0; - for ($i = 0; $i < $resolution; $i++) { // 30 entries in graph, one by day -//echo $valores[$i][2]. " - ". $valores[$i][3] ." | ". $valores[$i][1]." - ".$valores[$i][0]; -//echo "
"; - $grafica[]=$valores[$i][0]; - if ($valores[$i][0] > $valor_maximo) - $valor_maximo = $valores[$i][0]; - } - - if ($valor_maximo <= 0) { + if ($max_value <= $min_value) { graphic_error (); return; } - - $nombre_agente = get_agentmodule_agent_name ($id_agent_module); - $id_agente = dame_agente_id ($nombre_agente); - $nombre_modulo = get_agentmodule_name ($id_agent_module); - - if ($pure == 0) { - $Graph =& Image_Graph::factory('graph', array($width, $height)); - // add a TrueType font - $Font =& $Graph->addNew('font', $config['fontpath']); - $Font->setSize(7); - $Graph->setFont($Font); - - $Graph->add( - Image_Graph::vertical( - Image_Graph::vertical( - $Title = Image_Graph::factory('title', array(' Pandora FMS Graph - '.strtoupper($nombre_agente)." - ".human_time_description_raw ($periodo), 10)), - $Subtitle = Image_Graph::factory('title', array(' '.__('Data occurrence for module ').$nombre_modulo, 7)), - 90 - ), - Image_Graph::horizontal( - $Plotarea = Image_Graph::factory('plotarea'), - $Legend = Image_Graph::factory('legend'), - 100 - ), - 15) - ); - $Legend->setPlotarea($Plotarea); - $Title->setAlignment(IMAGE_GRAPH_ALIGN_LEFT); - $Subtitle->setAlignment(IMAGE_GRAPH_ALIGN_LEFT); - - - } else { // Pure, without title and legends - $Graph->add($Plotarea = Image_Graph::factory('plotarea')); - } - - //$Legend->setPlotarea($Plotarea); - // Create the dataset - // Merge data into a dataset object (sancho) - $Dataset1 =& Image_Graph::factory('dataset'); - for ($i = 0; $i < $resolution; $i++) { - $Dataset1->addPoint($valores[$i][1], $valores[$i][0]); - } - $Plot =& $Plotarea->addNew('bar', $Dataset1); - $GridY2 =& $Plotarea->addNew('bar_grid', IMAGE_GRAPH_AXIS_Y_SECONDARY); - $GridY2->setLineColor('gray'); - $GridY2->setFillColor('lightgray@0.05'); - $Plot->setLineColor('gray'); - $Plot->setFillColor($color."@0.70"); - $AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X); - $AxisY =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y); - $AxisY->setLabelInterval($valor_maximo / 2); - $AxisX->setLabelInterval($resolution / 5); - $Graph->done(); -} - - -function grafico_incidente_estados() { - $data = array(0,0,0,0); - // 0 - Abierta / Sin notas - // 2 - Descartada - // 3 - Caducada - // 13 - Cerrada - $sql1="SELECT * FROM tincidencia"; - $result=mysql_query($sql1); - while ($row=mysql_fetch_array($result)){ - if ($row["estado"] == 0) - $data[0]=$data[0]+1; - if ($row["estado"] == 2) - $data[1]=$data[1]+1; - if ($row["estado"] == 3) - $data[2]=$data[2]+1; - if ($row["estado"] == 13) - $data[3]=$data[3]+1; - } - $mayor = 0; - $mayor_data =0; - for ($i = 0; $i < sizeof($data); $i++) { - if ($data[$i] > $mayor_data) { - $mayor = $i; - $mayor_data = $data[$i]; + + // Get the first data outsite (to the left---more old) of the interval given + $previous = (float) get_previous_data ($id_agente_modulo, $datelimit); + for ($i = 0; $i <= $resolution; $i++) { + if ($data[$i]['count']) { + $data[$i]['sum'] = $data[$i]['sum'] / $data[$i]['count']; + $previous = $data[$i]['last']; + } else { + $data[$i]['sum'] = $previous; + $data[$i]['min'] = $previous; + $data[$i]['max'] = $previous; + /* Previous does not change here*/ } } - $legend = array ("Open Incident", "Closed Incident", "Outdated", "Invalid"); - generic_pie_graph (370, 180,$data, $legend); + + if ($period <= 86400) + $title_period = __('Last day'); + elseif ($period <= 604800) + $title_period = __('Last week'); + elseif ($period <= 3600) + $title_period = __('Last hour'); + elseif ($period <= 2419200) + $title_period = __('Last month'); + else + $title_period = sprintf (__('Last %s days'), format_numeric (($period / (3600 * 24)), 2)); + + $engine = get_graph_engine ($period); + $engine->width = $width; + $engine->height = $height; + $engine->data = &$data; + $engine->xaxis_interval = $resolution; + if ($title == '') + $title = get_agentmodule_name ($id_agente_modulo); + $engine->title = ' '.strtoupper ($nombre_agente)." - ".__('Module').' '.$title; + $engine->subtitle = ' '.__('Period').': '.$title_period; + $engine->show_title = !$pure; + $engine->max_value = $max_value; + $engine->min_value = $min_value; + $engine->events = (bool) $show_event; + $engine->alert_top = $show_alert ? $alert_high : false; + $engine->alert_bottom = $show_alert ? $alert_low : false;; + if (! $pure) { + $engine->legend = &$legend; + } + $engine->fontpath = $config['fontpath']; + + $engine->sparse_graph ($period, $avg_only, $min_value, $max_value, $unit_name); +} + +function graphic_agentmodules ($id_agent, $width, $height) { + global $config; + + $data = array (); + $sql = sprintf ('SELECT ttipo_modulo.nombre,COUNT(id_agente_modulo) + FROM tagente_modulo,ttipo_modulo WHERE + id_tipo_modulo = id_tipo AND id_agente = %d + GROUP BY id_tipo_modulo', $id_agent); + $modules = get_db_all_rows_sql ($sql); + foreach ($modules as $module) { + $data[$module['nombre']] = $module[1]; + } + generic_pie_graph ($width, $height, $data); +} + +function graphic_agentaccess ($id_agent, $period, $width, $height) { + global $config; + + $interval = 24; + $datelimit = get_system_time () - $period; + $hours = $period / $interval; + + for ($i = 0; $i < $interval; $i++) { + $time[$i]['timestamp_bottom'] = $datelimit + ($interval * $i); + $time[$i]['timestamp_top'] = $datelimit + ($interval * ($i + 1)); + } + + $sql = sprintf ('SELECT UNIX_TIMESTAMP(timestamp) utimestamp + FROM tagent_access + WHERE id_agent = %d AND UNIX_TIMESTAMP(timestamp) > %d', + $id_agent, $datelimit); + $result = get_db_all_rows_sql ($sql); + if ($result === false) + $result = array (); + $start = 0; + $data = array_pad (array (), $interval, 0); + foreach ($result as $access) { + for ($i = $start; $i < $interval; $i++) { + if ($access['utimestamp'] < $time[$i]['timestamp_bottom'] && $access['utimestamp'] >= $time[$i]['timestamp_top']) { + $data[$access['utimestamp']] = 1; + $start = $i; + break; + } + } + } + + generic_single_graph ($width, $height, $data, (int) $interval / 7); +} + +function graph_incidents_status () { + $data = array (0, 0, 0, 0); + + $data = array (); + $data[__("Open Incident")] = 0; + $data[__("Closed Incident")] = 0; + $data[__("Outdated")] = 0; + $data[__("Invalid")] = 0; + + $incidents = get_db_all_rows_filter ('tincidencia', + array ('estado' => array (0, 2, 3, 13)), + array ('estado')); + if ($incidents === false) + $incidents = array (); + foreach ($incidents as $incident) { + if ($incident["estado"] == 0) + $data[__("Open Incident")]++; + if ($incident["estado"] == 2) + $data[__("Closed Incident")]++; + if ($incident["estado"] == 3) + $data[__("Outdated")]++; + if ($incident["estado"] == 13) + $data[__("Invalid")]++; + } + + generic_pie_graph (370, 180, $data); } function grafico_incidente_prioridad () { - $data = array (0, 0, 0, 0, 0, 0); - // 0 - Abierta / Sin notas - // 2 - Descartada - // 3 - Caducada - // 13 - Cerrada - $sql = "SELECT * FROM tincidencia"; - $result = mysql_query ($sql); - while ($row = mysql_fetch_array ($result)){ - if ($row["prioridad"] < 10) - $data[$row["prioridad"]] += 1; + $data_tmp = array (0, 0, 0, 0, 0, 0); + $sql = 'SELECT COUNT(id_incidencia), prioridad + FROM tincidencia GROUP BY prioridad + ORDER BY 2 DESC'; + $incidents = get_db_all_rows_sql ($sql); + foreach ($incidents as $incident) { + if ($incident['prioridad'] < 5) + $data_tmp[$incident[1]] = $incident[0]; else - $data[5] += 1; + $data_tmp[5] += $incident[0]; } - - $legend = array (__('Informative'), - __('Low'), - __('Medium'), - __('Serious'), - __('Very serious'), - __('Maintenance')); - generic_pie_graph (320, 200, $data, $legend); + $data = array (__('Informative') => $data_tmp[0], + __('Low') => $data_tmp[1], + __('Medium') => $data_tmp[2], + __('Serious') => $data_tmp[3], + __('Very serious') => $data_tmp[4], + __('Maintenance') => $data_tmp[5]); + + generic_pie_graph (320, 200, $data); } function graphic_incident_group () { - $data = array(); - $legend = array(); - $sql = "SELECT distinct id_grupo FROM tincidencia "; - $result = mysql_query ($sql); - while ($row = mysql_fetch_array($result)) { - $sql="SELECT COUNT(id_incidencia) FROM tincidencia WHERE id_grupo = ".$row[0]; - $result2=mysql_query ($sql); - $row2 = mysql_fetch_array($result2); - $data[] = $row2[0]; - $legend[] = get_group_name ($row[0])."(".$row2[0].")"; + $data = array (); + $max_items = 5; + $sql = sprintf ('SELECT COUNT(id_incidencia), nombre + FROM tincidencia,tgrupo + WHERE tgrupo.id_grupo = tincidencia.id_grupo + GROUP BY tgrupo.id_grupo ORDER BY 1 DESC LIMIT %d', + $max_items); + $incidents = get_db_all_rows_sql ($sql); + foreach ($incidents as $incident) { + $name = $incident[1].' ('.$incident[0].')'; + $data[$name] = $incident[0]; } - array_multisort ($data, $legend); - - generic_pie_graph (320, 200, $data, $legend); + generic_pie_graph (320, 200, $data); } -function graphic_incident_user() { - $data = array(); - $legend = array(); - $sql1="SELECT distinct id_usuario FROM tincidencia "; - $result=mysql_query($sql1); - while ($row=mysql_fetch_array($result)){ - $sql1="SELECT COUNT(id_incidencia) FROM tincidencia WHERE id_usuario = '".$row[0]."'"; - $result2=mysql_query($sql1); - $row2=mysql_fetch_array($result2); - $data[] = $row2[0]; - $legend[] = $row[0]."(".$row2[0].")"; +function graphic_incident_user () { + $data = array (); + $max_items = 5; + $sql = sprintf ('SELECT COUNT(id_incidencia), id_usuario + FROM tincidencia GROUP BY id_usuario + ORDER BY 1 DESC LIMIT %d', $max_items); + $incidents = get_db_all_rows_sql ($sql); + foreach ($incidents as $incident) { + $name = $incident[1].' ('.$incident[0].')'; + $data[$name] = $incident[0]; } - array_multisort ($data, $legend); - - generic_pie_graph (320, 200, $data, $legend); + generic_pie_graph (320, 200, $data); } function graphic_user_activity ($width = 350, $height = 230) { - $data = array(); - $legend = array(); - $loop = 0; - $sql = "SELECT COUNT(*) AS count, ID_usuario FROM tsesion GROUP BY ID_usuario ORDER BY count DESC"; - $result = get_db_all_rows_sql ($sql); - foreach ($result as $row) { - if ($loop > 5) { - $data[5] += $row["count"]; - $legend[5] = __('Other')." (".$data[5].")"; - } else { - if (empty ($row["ID_usuario"])) { - $row["ID_usuario"] = __('Unknown'); - } - $data[] = $row["count"]; - $legend[] = mb_substr ($row["ID_usuario"], 0, 14)." (".$row["count"].")"; - } - $loop++; + $data = array (); + $max_items = 5; + $sql = sprintf ('SELECT COUNT(id_usuario), id_usuario + FROM tsesion GROUP BY id_usuario + ORDER BY 1 DESC LIMIT %d', $max_items); + $logins = get_db_all_rows_sql ($sql); + foreach ($logins as $login) { + $data[$login[1]] = $login[0]; } - - array_multisort ($data, $legend); - - generic_pie_graph ($width, $height, $data, $legend); + generic_pie_graph ($width, $height, $data); } -function graphic_incident_source ($width=320, $height=200) { - $data = array(); - $legend = array(); - $loop = 0; - $sql = "SELECT COUNT(*) as count, origen FROM tincidencia GROUP BY origen"; - $result = get_db_all_rows_sql ($sql); - - foreach ($result as $row) { - if ($loop > 5) { - $data[5] += $row["count"]; - $legend[5] = __('Other')." (".$data[5].")"; - } else { - $data[] = $row["count"]; - $legend[] = mb_substr ($row["origen"], 0, 14)." (".$row["count"].")"; - } - $loop++; +function graphic_incident_source ($width = 320, $height = 200) { + $data = array (); + $max_items = 5; + $sql = sprintf ('SELECT COUNT(id_incidencia), origen + FROM tincidencia GROUP BY `origen` + ORDER BY 1 DESC LIMIT %d', $max_items); + $origins = get_db_all_rows_sql ($sql); + foreach ($origins as $origin) { + $data[$origin[1]] = $origin[0]; } - - array_multisort ($data, $legend); - - generic_pie_graph ($width, $height, $data, $legend); + generic_pie_graph ($width, $height, $data); } -function grafico_db_agentes_modulos($width, $height) { - $data = array(); - $legend = array(); +function graph_db_agentes_modulos ($width, $height) { + $data = array (); - $agents = get_group_agents (1); + $modules = get_db_all_rows_sql ('SELECT COUNT(id_agente_modulo),id_agente + FROM tagente_modulo GROUP BY id_agente + ORDER BY 1 DESC'); + if ($modules === false) + $modules = array (); - foreach ($agents as $agent_id => $agent_name) { - //This query is not the most efficient on itself. But other functions - //use this query so it will a) be fetched from cache and b) it will - //return the result in the same order as the other graph and data - $data[$agent_name] = get_agent_modules_count ($agent_id); + foreach ($modules as $module) { + $agent_name = get_agent_name ($module['id_agente'], "none"); + $data[$agent_name] = $module[0]; } - asort ($data, SORT_NUMERIC); - generic_bar_graph ($width, $height, $data, array_keys ($data)); + generic_horizontal_bar_graph ($width, $height, $data); } -function grafico_eventos_usuario ( $width=420, $height=200) { - $data = array(); - $legend = array(); - $loop = 0; - - $sql = "SELECT COUNT(*) as count, id_usuario FROM tevento GROUP BY id_usuario"; - $result = get_db_all_rows_sql ($sql); - - foreach ($result as $row) { - if ($loop > 5) { - $data[5] += $row["count"]; - $legend[5] = __('Other')." (".$data[5].")"; - } else { - if (empty ($row["id_usuario"])) { - $row["id_usuario"] = __('SYSTEM'); - } - $data[] = $row["count"]; - $legend[] = mb_substr ($row["id_usuario"], 0, 14)." (".$row["count"].")"; - } - $loop++; +function grafico_eventos_usuario ($width, $height) { + $data = array (); + $max_items = 5; + $sql = sprintf ('SELECT COUNT(id_evento),id_usuario + FROM tevento GROUP BY id_usuario + ORDER BY 1 DESC LIMIT %d', $max_items); + $events = get_db_all_rows_sql ($sql); + foreach ($events as $event) { + $data[$event[1]] = $event[0]; } - array_multisort ($data, $legend); - - generic_pie_graph ($width, $height, $data, $legend); + generic_pie_graph ($width, $height, $data); } function grafico_eventos_total ($filter = "") { $filter = str_replace ( "\\" , "", $filter); - $data = array(); - $legend = array(); + $data = array (); + $legend = array (); $total = 0; - $sql1="SELECT COUNT(id_evento) FROM tevento WHERE criticity = 0 $filter"; - $result=mysql_query($sql1); - $row=mysql_fetch_array($result); - $data[] = $row[0]; - $legend[] = __('Maintenance')." ( $row[0] )"; - $total = $row[0]; + $sql = "SELECT COUNT(id_evento) FROM tevento WHERE criticity = 0 $filter"; + $data[__('Maintenance')] = get_db_sql ($sql); - $sql1="SELECT COUNT(id_evento) FROM tevento WHERE criticity = 1 $filter"; - $result=mysql_query($sql1); - $row=mysql_fetch_array($result); - $data[] = $row[0]; - $total = $total + $row[0]; - $legend[] = __('Informational')."( $row[0] )"; + $sql = "SELECT COUNT(id_evento) FROM tevento WHERE criticity = 1 $filter"; + $data[__('Informational')] = get_db_sql ($sql); - $sql1="SELECT COUNT(id_evento) FROM tevento WHERE criticity = 2 $filter"; - $result=mysql_query($sql1); - $row=mysql_fetch_array($result); - $data[] = $row[0]; - $total = $total + $row[0]; - $legend[] = __('Normal')." ( $row[0] )"; + $sql = "SELECT COUNT(id_evento) FROM tevento WHERE criticity = 2 $filter"; + $data[__('Normal')] = get_db_sql ($sql); - $sql1="SELECT COUNT(id_evento) FROM tevento WHERE criticity = 3 $filter"; - $result=mysql_query($sql1); - $row=mysql_fetch_array($result); - $data[] = $row[0]; - $total = $total + $row[0]; - $legend[] = __('Warning')." ( $row[0] )"; + $sql = "SELECT COUNT(id_evento) FROM tevento WHERE criticity = 3 $filter"; + $data[__('Warning')] = get_db_sql ($sql); - $sql1="SELECT COUNT(id_evento) FROM tevento WHERE criticity = 4 $filter"; - $result=mysql_query($sql1); - $row=mysql_fetch_array($result); - $data[] = $row[0]; - $total = $total + $row[0]; - $legend[] = __('Critical')." ( $row[0] )"; - - array_multisort ($data, $legend); + $sql = "SELECT COUNT(id_evento) FROM tevento WHERE criticity = 4 $filter"; + $data[__('Critical')] = get_db_sql ($sql); - generic_pie_graph (320, 200, $data, $legend); + asort ($data); + + generic_pie_graph (320, 200, $data); } function graph_event_module ($width = 300, $height = 200, $id_agent) { - global $config; - - $data = array(); - $legend = array(); - - $sql = sprintf ("SELECT DISTINCT(id_agentmodule) AS id_agentmodule, id_grupo, COUNT(id_agentmodule) AS count FROM tevento WHERE id_agente = %d GROUP BY id_agentmodule ORDER BY count DESC",$id_agent); - $result = get_db_all_rows_sql ($sql); - if ($result === false) - $result = array(); - - foreach ($result as $row) { - if (give_acl ($config["id_user"], $row["id_grupo"], "AR") == 1) { - $data[] = $row["count"]; - if ($row["id_agentmodule"] == 0) { - //System event - $legend[] = "SYSTEM (".$row["count"].")"; - } else { - //Other events - $legend[] = substr (get_agentmodule_name ($row["id_agentmodule"]), 0, 15)." (".$row["count"].")"; - } - } - } - - $max_items = 6; //Maximum items on the piegraph - while (count($data) > $max_items) { - //Pops an element off the array until the array is small enough - array_pop ($data); - } - - generic_pie_graph ($width, $height, $data, $legend); - + $data = array (); + $max_items = 6; + $sql = sprintf ('SELECT COUNT(id_evento),nombre + FROM tevento, tagente_modulo + WHERE id_agentmodule = id_agente_modulo + AND disabled = 0 AND tevento.id_agente = %d + GROUP BY id_agentmodule LIMIT %d', $id_agent, $max_items); + $events = get_db_all_rows_sql ($sql); + if ($events === false) { + graphic_error (); + return; + } + foreach ($events as $event) { + $data[$event['nombre'].' ('.$event[0].')'] = $event[0]; + } + + /* System events */ + $sql = "SELECT COUNT(*) FROM tevento WHERE id_agentmodule = 0 AND id_agente = $id_agent"; + $value = get_db_sql ($sql); + if ($value > 0) { + $data[__('System').' ('.$value.')'] = $value; + } + asort ($data); + + // Take only the first $max_items values + if (sizeof ($data) >= $max_items) { + $data = array_slice ($data, 0, $max_items); + } + + generic_pie_graph ($width, $height, $data, array ('zoom' => 75)); } @@ -1259,105 +673,148 @@ function grafico_eventos_grupo ($width = 300, $height = 200, $url = "") { global $config; $url = html_entity_decode (rawurldecode ($url), ENT_QUOTES); //It was urlencoded, so we urldecode it - $data = array(); - $legend = array(); + $data = array (); $loop = 0; $badstrings = array (";", "SELECT ", "DELETE ", "UPDATE ", "INSERT ", "EXEC"); - $url = str_ireplace ($badstrings,"",$url); //remove bad strings from the query so queries like ; DELETE FROM don't pass + //remove bad strings from the query so queries like ; DELETE FROM don't pass + $url = str_ireplace ($badstrings, "", $url); //This will give the distinct id_agente, give the id_grupo that goes //with it and then the number of times it occured. GROUP BY statement //is required if both DISTINCT() and COUNT() are in the statement - $sql = "SELECT DISTINCT(id_agente) AS id_agente, id_grupo, COUNT(id_agente) AS count FROM tevento WHERE 1=1 ".$url." GROUP BY id_agente ORDER BY count DESC"; + $sql = sprintf ('SELECT DISTINCT(id_agente) AS id_agente, id_grupo, COUNT(id_agente) AS count + FROM tevento WHERE 1=1 %s + GROUP BY id_agente ORDER BY count DESC', $url); $result = get_db_all_rows_sql ($sql); if ($result === false) { $result = array(); } foreach ($result as $row) { - if (!give_acl ($config["id_user"], $row["id_grupo"], "AR") == 1) { + if (!give_acl ($config["id_user"], $row["id_grupo"], "AR") == 1) continue; - } + if ($loop > 5) { - $data[5] += $row["count"]; - $legend[5] = __('Other')." (".$data[5].")"; + if (!isset ($data[__('Other')])) + $data[__('Other')] = 0; + $data[__('Other')] += $row["count"]; } else { if ($row["id_agente"] == 0) { - $legend[0] = __('SYSTEM')." (".$row["count"].")"; + $name = __('SYSTEM')." (".$row["count"].")"; } else { - $legend[] = mb_substr (get_agent_name ($row["id_agente"], "lower"), 0, 14)." (".$row["count"].")"; + $name = mb_substr (get_agent_name ($row["id_agente"], "lower"), 0, 14)." (".$row["count"].")"; } - $data[] = $row["count"]; + $data[$name] = $row["count"]; } $loop++; } - - generic_pie_graph ($width, $height, $data, $legend); + error_reporting (0); + generic_pie_graph ($width, $height, $data, array ('show_legend' => false)); } - -function generic_bar_graph ( $width =380, $height = 200, $data, $legend) { +function generic_single_graph ($width = 380, $height = 200, &$data, $interval = 1) { global $config; - if (sizeof($data) > 10){ - $height = sizeof($legend) * 20; - } - - // create the graph - $Graph =& Image_Graph::factory('graph', array($width, $height)); - // add a TrueType font - $Font =& $Graph->addNew('font', $config['fontpath']); - $Font->setSize(9); - $Graph->setFont($Font); - $Graph->add( - Image_Graph::vertical ( - $Plotarea = Image_Graph::factory('plotarea',array('category', 'axis', 'horizontal')), - $Legend = Image_Graph::factory('legend'), - 100 - ) - ); + if (sizeof ($data) == 0) + graphic_error (); - $Legend->setPlotarea($Plotarea); - // Create the dataset - // Merge data into a dataset object (sancho) - $Dataset1 =& Image_Graph::factory('dataset'); - $i = 0; - foreach ($data as $datapoint) { - $Dataset1->addPoint(substr($legend[$i], 0, 22), $datapoint); - $i++; + $engine = get_graph_engine (); + $engine->width = $width; + $engine->height = $height; + $engine->data = &$data; + $engine->max_value = max ($data); + $engine->fontpath = $config['fontpath']; + $engine->xaxis_interval = $interval; + + $engine->single_graph (); +} + +function generic_vertical_bar_graph ($width = 380, $height = 200, &$data, &$legend) { + global $config; + + if (sizeof ($data) == 0) + graphic_error (); + + $engine = get_graph_engine (); + + $engine->width = $width; + $engine->height = $height; + $engine->data = &$data; + $engine->max_value = max ($data); + $engine->legend = &$legend; + $engine->fontpath = $config['fontpath']; + $engine->vertical_bar_graph (); +} + +function generic_horizontal_bar_graph ($width = 380, $height = 200, &$data, $legend = false) { + global $config; + + if (sizeof ($data) == 0) + graphic_error (); + + $engine = get_graph_engine (); + + $engine->width = $width; + $engine->height = $height; + $engine->data = &$data; + $engine->legend = &$legend; + $engine->fontpath = $config['fontpath']; + + $engine->horizontal_bar_graph (); +} + +function generic_pie_graph ($width = 300, $height = 200, &$data, $options = false) { + global $config; + + if (sizeof ($data) == 0) + graphic_error (); + + $show_title = false; + $show_legend = true; + $zoom = 85; + if (is_array ($options)) { + if (isset ($options['show_title'])) + $show_title = (bool) $options['show_title']; + if (isset ($options['show_legend'])) + $show_legend = (bool) $options['show_legend']; + if (isset ($options['zoom'])) + $zoom = (bool) $options['zoom']; } - $Plot =& $Plotarea->addNew('bar', $Dataset1); - $GridY2 =& $Plotarea->addNew('bar_grid', IMAGE_GRAPH_AXIS_Y_SECONDARY); - $GridY2->setLineColor('gray'); - $GridY2->setFillColor('lightgray@0.05'); - $Plot->setLineColor('gray'); - $Plot->setFillColor('blue@0.85'); - $Graph->done(); + + $engine = get_graph_engine (); + + $engine->width = $width; + $engine->height = $height; + $engine->data = &$data; + $engine->zoom = $zoom; + $engine->legend = array_keys ($data); + $engine->show_legend = $show_legend; + $engine->show_title = $show_title; + $engine->zoom = 50; + $engine->fontpath = $config['fontpath']; + $engine->pie_graph (); } function grafico_db_agentes_paquetes ($width = 380, $height = 300) { - $data = array(); - $legend = array(); + $data = array (); + $legend = array (); - $agents = get_group_agents (1); + $agents = get_group_agents (1, false, "none"); $count = get_agent_modules_data_count (array_keys ($agents)); - unset ($count["total"]); - asort ($count, SORT_NUMERIC); + arsort ($count, SORT_NUMERIC); + $count = array_slice ($count, 10, 10, true); foreach ($count as $agent_id => $value) { - $data[] = $value; - $legend[] = $agents[$agent_id]; - } + $data[$agents[$agent_id]] = $value; + } - generic_bar_graph ($width, $height, $data, $legend); + generic_horizontal_bar_graph ($width, $height, $data, $legend); } function grafico_db_agentes_purge ($id_agent, $width, $height) { - //If id_agent is -1 or 0 (depending on what passed it), it should be - //All. Positive id is a possible agent if ($id_agent < 1) { $id_agent = -1; $query = ""; @@ -1370,556 +827,230 @@ function grafico_db_agentes_purge ($id_agent, $width, $height) { $time["all"] = get_system_time (); // 1 day ago - $time["1day"] = $time["all"]-86400; + $time["1day"] = $time["all"] - 86400; // 1 week ago - $time["1week"] = $time["all"]-(86400*7); + $time["1week"] = $time["all"] - 604800; // 1 month ago - $time["1month"] = $time["all"]-(86400*30); + $time["1month"] = $time["all"] - 2592000; // Three months ago - $time["3month"] = $time["all"]-(86400*90); - - //Init legend - $legend[0] = __("Today"); - $legend[1] = "1 ".__("Week"); - $legend[2] = "1 ".__("Month"); - $legend[3] = "3 ".__("Months"); - $legend[4] = __("Older"); + $time["3month"] = $time["all"] - 7776000; - $data[0] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["1day"], $query)); - $data[1] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["1week"], $query)); - $data[2] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["1month"], $query)); - $data[3] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["3month"], $query)); - $data[4] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE 1=1 %s", $query)); - - - $data[0] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["1day"], $query)); - $data[1] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["1week"], $query)); - $data[2] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["1month"], $query)); - $data[3] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["3month"], $query)); - $data[4] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE 1=1 %s", $query)); - - $data[4] = $data[4] - $data[3]; + $data[__("Today")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["1day"], $query)); + $data["1 ".__("Week")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["1week"], $query)); + $data["1 ".__("Month")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["1month"], $query)); + $data["3 ".__("Months")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["3month"], $query)); + $data[__("Older")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE 1=1 %s", $query)); - generic_pie_graph ($width, $height, $data, $legend); + $data[__("Today")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["1day"], $query)); + $data["1 ".__("Week")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["1week"], $query)); + $data["1 ".__("Month")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["1month"], $query)); + $data["3 ".__("Months")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["3month"], $query)); + $data[__("Older")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE 1=1 %s", $query)); + + $data[__("Older")] = $data[__("Older")] - $data["3 ".__("Months")]; + + generic_pie_graph ($width, $height, $data); } -function drawWarning($width,$height) { - global $config; - - if ($width == 0) { - $width = 50; - } - if ($height == 0) { - $height = 30; - } - - - $image = imagecreate($width,$height); - //colors - $back = ImageColorAllocate($image,255,255,255); - $border = ImageColorAllocate($image,0,0,0); - $red = ImageColorAllocate($image,255,60,75); - $fill = ImageColorAllocate($image,44,81,150); - - ImageFilledRectangle($image,0,0,$width-1,$height-1,$back); - ImageRectangle($image,0,0,$width-1,$height-1,$border); - ImageTTFText($image, 8, 0, ($width/2)-($width/10), ($height/2)+($height/5), $border, $config['fontpath'], __('no data')); - imagePNG($image); - imagedestroy($image); -} - - // *************************************************************************** // Draw a dynamic progress bar using GDlib directly // *************************************************************************** function progress_bar ($progress, $width, $height, $mode = 1) { - // Copied from the PHP manual: - // http://us3.php.net/manual/en/function.imagefilledrectangle.php - // With some adds from sdonie at lgc dot com - // Get from official documentation PHP.net website. Thanks guys :-) - function drawRating($rating, $width, $height, $mode) { - global $config; - - $rating = format_numeric($rating,1); - if ($width == 0) { - $width = 150; - } - if ($height == 0) { - $height = 20; - } - - //$rating = $_GET['rating']; - $ratingbar = (($rating/100)*$width)-2; - - $image = imagecreate($width,$height); - //colors - $back = ImageColorAllocate($image,255,255,255); - $border = ImageColorAllocate($image,140,140,140); - $textcolor = ImageColorAllocate($image,60,60,60); - $red = ImageColorAllocate($image,255,60,75); - - if ($mode == 0){ - if ($rating > 70) - $fill = ImageColorAllocate($image,176,255,84); // Green - elseif ($rating > 50) - $fill = ImageColorAllocate($image,255,230,84); // Yellow - elseif ($rating > 30) - $fill = ImageColorAllocate($image,255,154,83); // Orange - else - $fill = ImageColorAllocate($image,255,0,0); // Red - } - else - $fill = ImageColorAllocate($image,44,81,150); - - - $grey = ImageColorAllocate($image,230,230,210); - - if ($mode == 1){ - ImageFilledRectangle($image,0,0,$width-1,$height-1,$back); - } else { - ImageFilledRectangle($image,0,0,$width-1,$height-1,$grey); - } - if ($rating > 100) - ImageFilledRectangle($image,1,1,$ratingbar,$height-1,$red); - else - ImageFilledRectangle($image,1,1,$ratingbar,$height-1,$fill); - if ($mode == 1){ - ImageRectangle($image,0,0,$width-1,$height-1,$border); - } - if ($mode == 1){ - if ($rating > 50) - if ($rating > 100) - ImageTTFText($image, 8, 0, ($width/4), ($height/2)+($height/5), $back, $config["fontpath"], __('Out of limits')); - else - ImageTTFText($image, 8, 0, ($width/2)-($width/10), ($height/2)+($height/5), $back, $config["fontpath"], $rating."%"); - else - ImageTTFText($image, 8, 0, ($width/2)-($width/10), ($height/2)+($height/5), $textcolor, $config["fontpath"], $rating."%"); - } - imagePNG($image); - imagedestroy($image); - } - Header("Content-type: image/png"); - if ($progress > 100 || $progress < 0){ - // HACK: This report a static image... will increase render in about 200% :-) useful for - // high number of realtime statusbar images creation (in main all agents view, for example - $imgPng = imageCreateFromPng("../images/outof.png"); - imageAlphaBlending($imgPng, true); - imageSaveAlpha($imgPng, true); - imagePng($imgPng); - } else - drawRating($progress,$width,$height,$mode); -} - -function odo_tactic ($value1, $value2, $value3) { global $config; - // create the graph - $driver=& Image_Canvas::factory('png',array('width'=>350,'height'=>260,'antialias' => 'driver')); - $Graph = & Image_Graph::factory('graph', $driver); - // add a TrueType font - $Font =& $Graph->addNew('font', $config['fontpath']); - // set the font size to 11 pixels - $Font->setSize(8); - $Graph->setFont($Font); - - // create the plotarea - $Graph->add( - Image_Graph::vertical( - $Plotarea = Image_Graph::factory('plotarea'), - $Legend = Image_Graph::factory('legend'), - 80 - ) - ); - - $Legend->setPlotarea($Plotarea); - $Legend->setAlignment(IMAGE_GRAPH_ALIGN_HORIZONTAL); - if ($value1 <0) - $value1=0; - if ($value2 <0) - $value2=0; - if ($value3 <0) - $value3=0; - /***************************Arrows************************/ - $Arrows = & Image_Graph::factory('dataset'); - $Arrows->addPoint('Global Health', $value1, 'GLOBAL'); - $Arrows->addPoint('Data Health', $value2, 'DATA'); - $Arrows->addPoint('Monitor Health', $value3, 'MONITOR'); - - /**************************PARAMATERS for PLOT*******************/ - - // create the plot as odo chart using the dataset - $Plot =& $Plotarea->addNew('Image_Graph_Plot_Odo',$Arrows); - $Plot->setRange(0, 100); - $Plot->setAngles(180, 180); - $Plot->setRadiusWidth(90); - $Plot->setLineColor('gray');//for range and outline - - $Marker =& $Plot->addNew('Image_Graph_Marker_Value', IMAGE_GRAPH_VALUE_Y); - $Plot->setArrowMarker($Marker); - - $Plotarea->hideAxis(); - /***************************Axis************************/ - // create a Y data value marker - - $Marker->setFillColor('transparent'); - $Marker->setBorderColor('transparent'); - $Marker->setFontSize(7); - $Marker->setFontColor('black'); - - // create a pin-point marker type - $Plot->setTickLength(14); - $Plot->setAxisTicks(5); - /********************************color of arrows*************/ - $FillArray = & Image_Graph::factory('Image_Graph_Fill_Array'); - $FillArray->addColor('red@0.8', 'GLOBAL'); - $FillArray->addColor('black.6', 'DATA'); - $FillArray->addColor('blue@0.6', 'MONITOR'); - - // create a line array - $LineArray =& Image_Graph::factory('Image_Graph_Line_Array'); - $LineArray->addColor('red', 'GLOBAL'); - $LineArray->addColor('black', 'DATA'); - $LineArray->addColor('blue', 'MONITOR'); - $Plot->setArrowLineStyle($LineArray); - $Plot->setArrowFillStyle($FillArray); - - /***************************MARKER OR ARROW************************/ - // create a Y data value marker - $Marker =& $Plot->addNew('Image_Graph_Marker_Value', IMAGE_GRAPH_VALUE_Y); - $Marker->setFillColor('white'); - $Marker->setBorderColor('white'); - $Marker->setFontSize(7); - $Marker->setFontColor('black'); - // create a pin-point marker type - $PointingMarker =& $Plot->addNew('Image_Graph_Marker_Pointing_Angular', array(20, &$Marker)); - // and use the marker on the plot - $Plot->setMarker($PointingMarker); - /**************************RANGE*******************/ - $Plot->addRangeMarker(0, 30); - $Plot->addRangeMarker(30, 70); - $Plot->addRangeMarker(70, 100); - // create a fillstyle for the ranges - $FillRangeArray = & Image_Graph::factory('Image_Graph_Fill_Array'); - $FillRangeArray->addColor('red@0.8'); - $FillRangeArray->addColor('yellow@0.8'); - $FillRangeArray->addColor('green@0.8'); - $Plot->setRangeMarkerFillStyle($FillRangeArray); - // output the Graph - $Graph->done(); + if ($progress > 100 || $progress < 0) { + graphic_error ('outof.png'); + } + + $engine = get_graph_engine (); + + $engine->width = $width; + $engine->height = $height; + $engine->fontpath = $config['fontpath']; + + if ($mode == 0) { + $engine->background_color = '#E6E6D2'; + $engine->show_title = false; + if ($progress > 70) + $color = '#B0FF54'; + elseif ($progress > 50) + $color = '#FFE654'; + elseif ($progress > 30) + $color = '#FF9A54'; + else + $color = '#EE0000'; + } else { + $engine->background_color = '#FFFFFF'; + $engine->show_title = true; + $engine->title = format_numeric ($progress).' %'; + $color = '#2C5196'; + } + + $engine->progress_bar ($progress, $color); } -function grafico_modulo_boolean ( $id_agente_modulo, $periodo, $show_event, - $width, $height , $title, $unit_name, $show_alert, $avg_only = 0, $pure=0, $date = 0 ) { - +function grafico_modulo_boolean ($id_agente_modulo, $period, $show_event, + $width, $height , $title, $unit_name, $show_alert, $avg_only = 0, $pure=0, + $date = 0 ) { global $config; $resolution = $config['graph_res'] * 50; // Number of "slices" we want in graph if (! $date) $date = get_system_time (); - //$unix_timestamp = strtotime($mysql_timestamp) // Convert MYSQL format tio utime - $fechatope = $date - $periodo; // limit date - $horasint = $periodo / $resolution; // Each intervalo is $horasint seconds length - $id_agente = get_agentmodule_agent ($id_agente_modulo); - $nombre_agente = get_agent_name ($id_agente); + + $datelimit = $date - $period; // limit date + $interval = $period / $resolution; // Each interval is $interval seconds length + $nombre_agente = get_agentmodule_agent_name ($id_agente_modulo); + $id_agente = dame_agente_id ($nombre_agente); $nombre_modulo = get_agentmodule_name ($id_agente_modulo); if ($show_event == 1) - $real_event = array(); + $real_event = array (); - if ($show_alert == 1){ - $alert_high = 0; - $alert_low = 10000000; + if ($show_alert == 1) { + $alert_high = false; + $alert_low = false; // If we want to show alerts limits - $sql1 = "SELECT MAX(max_value), MIN(min_value) FROM talert_template_modules WHERE id_agente_modulo = ".$id_agente_modulo; - $result = get_db_row_sql ($sql1); - if ($result !== false) { - $alert_high = $result["max"]; - $alert_low = $result["min"]; - } + + $alert_high = get_db_value ('MAX(dis_max)', 'talerta_agente_modulo', 'id_agente_modulo', (int) $id_agente_modulo); + $alert_low = get_db_value ('MIN(dis_min)', 'talerta_agente_modulo', 'id_agente_modulo', (int) $id_agente_modulo); + // if no valid alert defined to render limits, disable it - if (($alert_low == 10000000) && ($alert_high == 0)) { + if (($alert_low === false || $alert_low === NULL) && + ($alert_high === false || $alert_high === NULL)) { $show_alert = 0; } } - // intervalo - This is the number of "rows" we are divided the time + // interval - This is the number of "rows" we are divided the time // to fill data. more interval, more resolution, and slower. // periodo - Gap of time, in seconds. This is now to (now-periodo) secs // Init tables for ($i = 0; $i <= $resolution; $i++) { - $valores[$i][0] = 0; // SUM of all values for this interval - $valores[$i][1] = 0; // counter - $valores[$i][2] = $fechatope + ($horasint * $i); // [2] Top limit for this range - $valores[$i][3] = $fechatope + ($horasint * ($i+1)); // [3] Botom limit - $valores[$i][4] = -1; // MIN - $valores[$i][5] = -1; // MAX - $valores[$i][6] = -1; // Event + $data[$i]['sum'] = 0; // SUM of all values for this interval + $data[$i]['count'] = 0; // counter + $data[$i]['timestamp_bottom'] = $datelimit + ($interval * $i); // [2] Top limit for this range + $data[$i]['timestamp_top'] = $datelimit + ($interval * ($i + 1)); // [3] Botom limit + $data[$i]['min'] = 1; // MIN + $data[$i]['max'] = 0; // MAX + $data[$i]['last'] = 0; // Last value + $data[$i]['events'] = 0; // Event } // Init other general variables - if ($show_event == 1){ + if ($show_event == 1) { // If we want to show events in graphs - $sql1="SELECT utimestamp FROM tevento WHERE id_agente = $id_agente AND utimestamp > $fechatope"; - $result = get_db_all_rows_sql ($sql1); + $sql = "SELECT utimestamp FROM tevento WHERE id_agente = $id_agente AND utimestamp > $datelimit"; + $result = get_db_all_rows_sql ($sql); foreach ($result as $row) { - for ($i=0; $i <= $resolution; $i++) { - if ( ($row["utimestamp"] <= $valores[$i][3]) && ($row["utimestamp"] >= $valores[$i][2]) ){ - $real_event[$i] = 1; + $utimestamp = $row['utimestamp']; + for ($i = 0; $i <= $resolution; $i++) { + if ($utimestamp <= $data[$i]['timestamp_top'] && $utimestamp >= $data[$i]['timestamp_bottom']){ + $data['events']++; } } } } // Init other general variables $max_value = 0; - $min_value = 0; - // DEBUG ONLY (to get number of items for this graph) - /* - // Make "THE" query. Very HUGE. - $sql1="SELECT COUNT(datos) FROM tagente_datos WHERE id_agente = $id_agente AND id_agente_modulo = $id_agente_modulo AND utimestamp > fechatope"; - $result=mysql_query($sql1); - $row=mysql_fetch_array($result); - $title=$title." [C] ".$row[0]; - */ - $previous = 0; - // Get the first data outsite (to the left---more old) of the interval given - $sql1 = "SELECT datos FROM tagente_datos WHERE id_agente_modulo = $id_agente_modulo AND utimestamp < $fechatope ORDER BY utimestamp DESC"; - $result = get_db_sql ($sql1); - if (!empty ($result)) { - $previous = $result["datos"]; + $sql = sprintf ('SELECT datos,utimestamp + FROM tagente_datos + WHERE id_agente = %d + AND id_agente_modulo = %d + AND utimestamp > %d + ORDER BY utimestamp DESC', + $id_agente, $id_agente_modulo, $datelimit); + $all_data = get_db_all_rows_sql ($sql); + if ($all_data === false) { + $all_data = array (); } - $sql1="SELECT datos, utimestamp FROM tagente_datos WHERE id_agente_modulo = $id_agente_modulo AND utimestamp > $fechatope"; - - $result = get_db_all_rows_sql ($sql1); - if ($result === false) { - $result = array (); - } - - foreach ($result as $row) { - $datos = $row["datos"]; - $utimestamp = $row["utimestamp"]; - - $i = round(($utimestamp - $fechatope) / $horasint); - if (isset ($valores[$i][0])) { - $valores[$i][0] += $datos; - $valores[$i][1]++; - - if ($valores[$i][6] == -1) { - $valores[$i][6] = $datos; - } - - // Init min value - if ($valores[$i][4] == -1) { - $valores[$i][4] = $datos; - } else { - // Check min value - if ($datos < $valores[$i][4]) { - $valores[$i][4] = $datos; - } - } - // Check max value - if ($valores[$i][5] == -1) { - $valores[$i][5] = $datos; - } else { - if ($datos > $valores[$i][5]) { - $valores[$i][5] = $datos; - } + foreach ($all_data as $module_data) { + $real_data = intval ($module_data["datos"]) ? 1 : 0; + $utimestamp = $module_data["utimestamp"]; + for ($i = 0; $i <= $resolution; $i++) { + if ($utimestamp <= $data[$i]['timestamp_top'] && $utimestamp >= $data[$i]['timestamp_bottom']) { + /* If the data was down, it has prevalence, so ignore any other loop */ + $data[$i]['sum'] += $real_data; + $data[$i]['count']++; + + $data[$i]['last'] = $real_data; + + $data[$i]['min'] = min ($data[$i]['min'], $real_data); + $data[$i]['max'] = max ($data[$i]['max'], $real_data); } } - } - - - $last = $previous; - // Calculate Average value for $valores[][0] - for ($i =0; $i <= $resolution; $i++) { - //echo $valores[$i][6] . ", (" . $valores[$i][4] . ", " . $valores[$i][5] . ") : "; - - if ($valores[$i][6] == -1) - $valores[$i][6] = $last; - else - $valores[$i][6] = $valores[$i][4]; // min - - $last = $valores[$i][5] != -1 ? $valores[$i][5] : $valores[$i][6]; // max - if ($valores[$i][1] > 0) - $valores[$i][0] = $valores[$i][0]/$valores[$i][1]; - else { - $valores[$i][0] = $previous; - $valores[$i][4] = $previous; - $valores[$i][5] = $previous; - } - // Get max value for all graph - if ($valores[$i][5] > $max_value) - $max_value = $valores[$i][5]; - // Take prev. value - // TODO: CHeck if there are more than 24hours between - // data, if there are > 24h, module down. - $previous = $valores[$i][0]; - - //echo $valores[$i][6]; - //echo "
"; } - // Create graph - // ************* - $Graph =& Image_Graph::factory('graph', array($width, $height)); - // add a TrueType font - $Font =& $Graph->addNew ('font', $config['fontpath']); - $Font->setSize(6); - $Graph->setFont($Font); - - if ($periodo == 86400) - $title_period = "Last day"; - elseif ($periodo == 604800) - $title_period = "Last week"; - elseif ($periodo == 3600) - $title_period = "Last hour"; - elseif ($periodo == 2419200) - $title_period = "Last month"; - else - $title_period = "Last ".format_numeric(($periodo / (3600*24)),2)." days"; - if ($pure == 0){ - $Graph->add( - Image_Graph::vertical( - Image_Graph::vertical( - $Title = Image_Graph::factory('title', array(' Pandora FMS Graph - '.strtoupper($nombre_agente)." - ".$title_period, 10)), - $Subtitle = Image_Graph::factory('title', array(' '.$title, 7)), - 90 - ), - Image_Graph::horizontal( - $Plotarea = Image_Graph::factory('plotarea'), - $Legend = Image_Graph::factory('legend'), - 85 - ), - 15) - ); - $Legend->setPlotarea($Plotarea); - $Title->setAlignment(IMAGE_GRAPH_ALIGN_LEFT); - $Subtitle->setAlignment(IMAGE_GRAPH_ALIGN_LEFT); - } else { // Pure, without title and legends - $Graph->add($Plotarea = Image_Graph::factory('plotarea')); - } - // Create the dataset - // Merge data into a dataset object (sancho) - // $Dataset =& Image_Graph::factory('dataset'); - /* - if ($avg_only == 1) { - $dataset[0] = Image_Graph::factory('dataset'); - $dataset[0]->setName("Avg."); - } else { - $dataset[0] = Image_Graph::factory('dataset'); - $dataset[0]->setName("Max."); - $dataset[1] = Image_Graph::factory('dataset'); - $dataset[1]->setName("Avg."); - $dataset[2] = Image_Graph::factory('dataset'); - $dataset[2]->setName("Min."); - } - */ - $dataset[0] = Image_Graph::factory('dataset'); - $dataset[0]->setName("Value"); - - // Event dataset creation - if ($show_event == 1){ - $dataset_event = Image_Graph::factory('dataset'); - $dataset_event -> setName("Event Fired"); - } - // ... and populated with data ... + $previous = (float) get_previous_data ($id_agente_modulo, $datelimit); + // Calculate Average value for $data[][0] for ($i = 0; $i <= $resolution; $i++) { - $tdate = date('d/m', $valores[$i][2])."\n".date('H:i', $valores[$i][2]); - /* - if ($avg_only == 0) { - $dataset[1]->addPoint($tdate, $valores[$i][0]); - $dataset[0]->addPoint($tdate, $valores[$i][5]); - $dataset[2]->addPoint($tdate, $valores[$i][4]); + if ($data[$i]['count'] == 0) { + $data[$i]['sum'] = $previous; + $data[$i]['min'] = $previous; + $data[$i]['max'] = $previous; + $data[$i]['last'] = $previous; + + $previous = $data[$i]['sum']; } else { - $dataset[0]->addPoint($tdate, $valores[$i][6]); // 0:average 4:min 5:max 6:event + $previous = $data[$i]['sum']; + if ($data[$i]['count'] > 1) { + $previous = $data[$i]['last']; + $data[$i]['sum'] = floor ($data[$i]['sum'] / $data[$i]['count']); + } } - */ - $dataset[0]->addPoint($tdate, $valores[$i][6]); - if (($show_event == 1) AND (isset($real_event[$i]))) { - $dataset_event->addPoint($tdate, $valores[$i][5]); - } + // Get max value for all graph + $max_value = max ($max_value, $data[$i]['max']); } - - if ($max_value > 0){ - // Show alert limits - if ($show_alert == 1){ - $Plot =& $Plotarea->addNew('Image_Graph_Axis_Marker_Area', IMAGE_GRAPH_AXIS_Y); - $Plot->setFillColor( 'blue@0.1' ); - $Plot->setLowerBound( $alert_low); - $Plot->setUpperBound( $alert_high ); - } - - // create the 1st plot as smoothed area chart using the 1st dataset - $Plot =& $Plotarea->addNew('area', array(&$dataset)); - if ($avg_only == 1){ - $Plot->setLineColor('black@0.1'); - } else { - $Plot->setLineColor('yellow@0.2'); - } - - $AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X); - // $AxisX->Hide(); - - $AxisY =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y); - $AxisY->setDataPreprocessor(Image_Graph::factory('Image_Graph_DataPreprocessor_Function', 'format_for_graph')); - $AxisY->setLabelOption("showtext",true); - $yinterval = $height / 30; - $AxisY->setLabelInterval(ceil($max_value / $yinterval)); - $AxisY->showLabel(IMAGE_GRAPH_LABEL_ZERO); - if ($unit_name != "") - $AxisY->setTitle($unit_name, 'vertical'); - if ($periodo < 10000) - $xinterval = 8; - else - $xinterval = $resolution / 7 ; - $AxisX->setLabelInterval($xinterval) ; - //$AxisY->forceMinimum($minvalue); - $AxisY->forceMaximum($max_value+($max_value/12)) ; - $GridY2 =& $Plotarea->addNew('bar_grid', IMAGE_GRAPH_AXIS_Y_SECONDARY); - $GridY2->setLineColor('gray'); - $GridY2->setFillColor('lightgray@0.05'); - // set line colors - $FillArray =& Image_Graph::factory('Image_Graph_Fill_Array'); - - $Plot->setFillStyle($FillArray); - /* - if ($avg_only == 1){ - $FillArray->addColor('green@0.6'); - } else { - $FillArray->addColor('yellow@0.5'); - $FillArray->addColor('orange@0.6'); - $FillArray->addColor('#e37907@0.7'); - $FillArray->addColor('red@0.7'); - $FillArray->addColor('blue@0.7'); - $FillArray->addColor('green@0.7'); - $FillArray->addColor('black@0.7'); - } - */ - $FillArray->addColor('green@0.6'); - $AxisY_Weather =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y); - - // Show events ! - if ($show_event == 1){ - $Plot =& $Plotarea->addNew('Plot_Impulse', array($dataset_event)); - $Plot->setLineColor( 'red' ); - $Marker_event =& Image_Graph::factory('Image_Graph_Marker_Cross'); - $Plot->setMarker($Marker_event); - $Marker_event->setFillColor( 'red' ); - $Marker_event->setLineColor( 'red' ); - $Marker_event->setSize ( 5 ); - } - - $Graph->done(); - } else - graphic_error (); + + $grafica = array (); + foreach ($data as $d) { + $grafica[$d['timestamp_bottom']] = $d['sum']; + } + + if ($period <= 86400) + $title_period = __('Last day'); + elseif ($period <= 604800) + $title_period = __('Last week'); + elseif ($period <= 3600) + $title_period = __('Last hour'); + elseif ($period <= 2419200) + $title_period = __('Last month'); + else + $title_period = sprintf (__('Last %s days'), format_numeric (($period / (3600 * 24)), 2)); + + $engine = get_graph_engine ($period); + + $engine->width = $width; + $engine->height = $height; + $engine->data = &$grafica; + $engine->legend = array ($nombre_modulo); + $engine->title = ' '.strtoupper ($nombre_agente)." - ".__('Module').' '.$title; + $engine->subtitle = ' '.__('Period').': '.$title_period; + $engine->show_title = !$pure; + $engine->events = $show_event ? $real_event : false; + $engine->fontpath = $config['fontpath']; + $engine->alert_top = $show_alert ? $alert_high : false; + $engine->alert_bottom = $show_alert ? $alert_low : false;; + + if ($period < 10000) + $engine->xaxis_interval = 8; + else + $engine->xaxis_interval = $resolution / 7; + $engine->yaxis_interval = 1; + $engine->xaxis_format = 'date'; + + $engine->single_graph (); + + return; } // ************************************************************************** @@ -1935,7 +1066,7 @@ $id_agent = (int) get_parameter ('id_agent'); $tipo = (string) get_parameter ('tipo'); $pure = (bool) get_parameter ('pure'); $period = (int) get_parameter ('period', 86400); -$intervalo = (int) get_parameter ('intervalo', 300); +$interval = (int) get_parameter ('interval', 300); $id = (string) get_parameter ('id'); $weight_l = (string) get_parameter ('weight_l'); $width = (int) get_parameter ('width', 450); @@ -1958,14 +1089,9 @@ $stacked = get_parameter ("stacked", 0); $date = get_parameter ("date"); $graphic_type = (string) get_parameter ('tipo'); $mode = get_parameter ("mode", 1); -$url = get_parameter ('url',''); if ($graphic_type) { switch ($graphic_type) { - case 'string': - graphic_string_data ($id, $period, $width, $height, $date); - - break; case 'sparse': grafico_modulo_sparse ($id, $period, $draw_events, $width, $height, $label, $unit_name, $draw_alerts, $avg_only, $pure, $date); @@ -1975,7 +1101,7 @@ if ($graphic_type) { break; case "estado_incidente": - grafico_incidente_estados (); + graph_incidents_status (); break; case "prioridad_incidente": @@ -1983,65 +1109,61 @@ if ($graphic_type) { break; case "db_agente_modulo": - grafico_db_agentes_modulos($width, $height); + graph_db_agentes_modulos ($width, $height); break; case "db_agente_paquetes": - grafico_db_agentes_paquetes($width, $height); + grafico_db_agentes_paquetes ($width, $height); break; case "db_agente_purge": - grafico_db_agentes_purge($id, $width, $height); + grafico_db_agentes_purge ($id, $width, $height); break; case "event_module": graph_event_module ($width, $height, $id_agent); - - break; + + break; case "group_events": - grafico_eventos_grupo($width, $height, $url); + grafico_eventos_grupo ($width, $height); break; case "user_events": - grafico_eventos_usuario($width, $height); + grafico_eventos_usuario ($width, $height); break; case "total_events": - grafico_eventos_total(); + grafico_eventos_total (); break; case "group_incident": - graphic_incident_group(); + graphic_incident_group (); break; case "user_incident": - graphic_incident_user(); + graphic_incident_user (); break; case "source_incident": - graphic_incident_source(); + graphic_incident_source (); break; case "user_activity": - graphic_user_activity($width,$height); + graphic_user_activity ($width, $height); break; case "agentaccess": - graphic_agentaccess ($id, get_parameter ("periodo"), $width, $height); + graphic_agentaccess ($_GET["id"], $_GET["periodo"], $width, $height); break; case "agentmodules": - graphic_agentmodules($id, $width, $height); + graphic_agentmodules ($_GET["id"], $width, $height); break; case "progress": - $percent = get_parameter_get ("percent"); + $percent = (int) get_parameter ('percent'); progress_bar ($percent,$width,$height, $mode); - break; - case "odo_tactic": - odo_tactic ( $value1, $value2, $value3 ); - break; case "combined": // Split id to get all parameters @@ -2055,22 +1177,16 @@ if ($graphic_type) { break; case "alerts_fired_pipe": $data = array (); - $data[0] = (float) get_parameter ('fired'); - $data[1] = (float) get_parameter ('not_fired'); - $legends = array (); - $legends[0] = __('Alerts fired'); - $legends[1] = __('Alerts not fired'); - generic_pie_graph ($width, $height, $data, $legends); + $data[__('Alerts fired')] = (float) get_parameter ('fired'); + $data[__('Alerts not fired')] = (float) get_parameter ('not_fired'); + generic_pie_graph ($width, $height, $data); break; case 'monitors_health_pipe': $data = array (); - $data[1] = (float) get_parameter ('down'); - $data[0] = (float) get_parameter ('not_down'); - $legends = array (); - $legends[1] = __('Monitors BAD'); - $legends[0] = __('Monitors OK'); - generic_pie_graph ($width, $height, $data, $legends); + $data[__('Monitors OK')] = (float) get_parameter ('not_down'); + $data[__('Monitors BAD')] = (float) get_parameter ('down'); + generic_pie_graph ($width, $height, $data); break; default: diff --git a/pandora_console/reporting/pChart/pCache.class b/pandora_console/reporting/pChart/pCache.class new file mode 100644 index 0000000000..2eddfb3e59 --- /dev/null +++ b/pandora_console/reporting/pChart/pCache.class @@ -0,0 +1,120 @@ +. + + Class initialisation : + pCache($CacheFolder="Cache/") + Cache management : + IsInCache($Data) + GetFromCache($ID,$Data) + WriteToCache($ID,$Data,$Picture) + DeleteFromCache($ID,$Data) + ClearCache() + Inner functions : + GetHash($ID,$Data) + */ + + /* pCache class definition */ + class pCache + { + var $HashKey = ""; + var $CacheFolder = "Cache/"; + + /* Create the pCache object */ + function pCache($CacheFolder="Cache/") + { + $this->CacheFolder = $CacheFolder; + } + + /* This function is clearing the cache folder */ + function ClearCache() + { + if ($handle = opendir($this->CacheFolder)) + { + while (false !== ($file = readdir($handle))) + { + if ( $file != "." && $file != ".." ) + unlink($this->CacheFolder.$file); + } + closedir($handle); + } + } + + /* This function is checking if we have an offline version of this chart */ + function IsInCache($ID,$Data,$Hash="") + { + if ( $Hash == "" ) + $Hash = $this->GetHash($ID,$Data); + + if ( file_exists($this->CacheFolder.$Hash) ) + return(TRUE); + else + return(FALSE); + } + + /* This function is making a copy of drawn chart in the cache folder */ + function WriteToCache($ID,$Data,$Picture) + { + $Hash = $this->GetHash($ID,$Data); + $FileName = $this->CacheFolder.$Hash; + + imagepng($Picture->Picture,$FileName); + } + + /* This function is removing any cached copy of this chart */ + function DeleteFromCache($ID,$Data) + { + $Hash = $this->GetHash($ID,$Data); + $FileName = $this->CacheFolder.$Hash; + + if ( file_exists($FileName ) ) + unlink($FileName); + } + + /* This function is retrieving the cached picture if applicable */ + function GetFromCache($ID,$Data) + { + $Hash = $this->GetHash($ID,$Data); + if ( $this->IsInCache("","",$Hash ) ) + { + $FileName = $this->CacheFolder.$Hash; + + header('Content-type: image/png'); + @readfile($FileName); + exit(); + } + } + + /* This function is building the graph unique hash key */ + function GetHash($ID,$Data) + { + $mKey = "$ID"; + foreach($Data as $key => $Values) + { + $tKey = ""; + foreach($Values as $Serie => $Value) + $tKey = $tKey.$Serie.$Value; + $mKey = $mKey.md5($tKey); + } + return(md5($mKey)); + } + } +?> diff --git a/pandora_console/reporting/pChart/pChart.class b/pandora_console/reporting/pChart/pChart.class new file mode 100644 index 0000000000..21a24d4cb3 --- /dev/null +++ b/pandora_console/reporting/pChart/pChart.class @@ -0,0 +1,3568 @@ +. + + Class initialisation : + pChart($XSize,$YSize) + Draw methods : + drawBackground($R,$G,$B) + drawRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B) + drawFilledRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B,$DrawBorder=TRUE,$Alpha=100) + drawRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B) + drawFilledRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B) + drawCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0) + drawFilledCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0) + drawEllipse($Xc,$Yc,$Height,$Width,$R,$G,$B) + drawFilledEllipse($Xc,$Yc,$Height,$Width,$R,$G,$B) + drawLine($X1,$Y1,$X2,$Y2,$R,$G,$B,$GraphFunction=FALSE) + drawDottedLine($X1,$Y1,$X2,$Y2,$DotSize,$R,$G,$B) + drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B) + drawFromPNG($FileName,$X,$Y,$Alpha=100) + drawFromGIF($FileName,$X,$Y,$Alpha=100) + drawFromJPG($FileName,$X,$Y,$Alpha=100) + Graph setup methods : + addBorder($Width=3,$R=0,$G=0,$B=0) + clearScale() + clearShadow() + createColorGradientPalette($R1,$G1,$B1,$R2,$G2,$B2,$Shades) + drawGraphArea($R,$G,$B,$Stripe=FALSE) + drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1,$RightScale=FALSE) + drawRightScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1) + drawXYScale($Data,$DataDescription,$YSerieName,$XSerieName,$R,$G,$B,$WithMargin=0,$Angle=0,$Decimals=1) + drawGrid($LineWidth,$Mosaic=TRUE,$R=220,$G=220,$B=220,$Alpha=100) + drawLegend($XPos,$YPos,$DataDescription,$R,$G,$B,$Rs=-1,$Gs=-1,$Bs=-1,$Rt=0,$Gt=0,$Bt=0,$Border=FALSE) + drawPieLegend($XPos,$YPos,$Data,$DataDescription,$R,$G,$B) + drawTitle($XPos,$YPos,$Value,$R,$G,$B,$XPos2=-1,$YPos2=-1,$Shadow=FALSE) + drawTreshold($Value,$R,$G,$B,$ShowLabel=FALSE,$ShowOnRight=FALSE,$TickWidth=4,$FreeText=NULL) + drawArea($Data,$Serie1,$Serie2,$R,$G,$B,$Alpha = 50) + drawRadarAxis($Data,$DataDescription,$Mosaic=TRUE,$BorderOffset=10,$A_R=60,$A_G=60,$A_B=60,$S_R=200,$S_G=200,$S_B=200,$MaxValue=-1) + drawGraphAreaGradient($R,$G,$B,$Decay,$Target=TARGET_GRAPHAREA) + drawTextBox($X1,$Y1,$X2,$Y2,$Text,$Angle=0,$R=255,$G=255,$B=255,$Align=ALIGN_LEFT,$Shadow=TRUE,$BgR=-1,$BgG=-1,$BgB=-1,$Alpha=100) + getLegendBoxSize($DataDescription) + loadColorPalette($FileName,$Delimiter=",") + reportWarnings($Interface="CLI") + setGraphArea($X1,$Y1,$X2,$Y2) + setLabel($Data,$DataDescription,$SerieName,$ValueName,$Caption,$R=210,$G=210,$B=210) + setColorPalette($ID,$R,$G,$B) + setCurrency($Currency) + setDateFormat($Format) + setFontProperties($FontName,$FontSize) + setLineStyle($Width=1,$DotSize=0) + setFixedScale($VMin,$VMax,$Divisions=5,$VXMin=0,$VXMin=0,$XDivisions=5) + setShadowProperties($XDistance=1,$YDistance=1,$R=60,$G=60,$B=60,$Alpha) + writeValues($Data,$DataDescription,$Series) + Graphs methods : + drawPlotGraph($Data,$DataDescription,$BigRadius=5,$SmallRadius=2,$R2=-1,$G2=-1,$B2=-1,$Shadow=FALSE) + drawXYPlotGraph($Data,$DataDescription,$YSerieName,$XSerieName,$PaletteID=0,$BigRadius=5,$SmallRadius=2,$R2=-1,$G2=-1,$B2=-1) + drawLineGraph($Data,$DataDescription,$SerieName="") + drawXYGraph($Data,$DataDescription,$YSerieName,$XSerieName,$PaletteID=0) + drawFilledLineGraph($Data,$DataDescription,$Alpha=100,$AroundZero=FALSE) + drawCubicCurve($Data,$DataDescription,$Accuracy=.1,$SerieName="") + drawFilledCubicCurve($Data,$DataDescription,$Accuracy=.1,$Alpha=100,$AroundZero=FALSE) + drawOverlayBarGraph($Data,$DataDescription,$Alpha=50) + drawBarGraph($Data,$DataDescription,$Shadow=FALSE) + drawStackedBarGraph($Data,$DataDescription,$Alpha=50,$Contiguous=FALSE) + drawLimitsGraph($Data,$DataDescription,$R=0,$G=0,$B=0) + drawRadar($Data,$DataDescription,$BorderOffset=10,$MaxValue=-1) + drawFilledRadar($Data,$DataDescription,$Alpha=50,$BorderOffset=10,$MaxValue=-1) + drawBasicPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$R=255,$G=255,$B=255,$Decimals=0) + drawFlatPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$SpliceDistance=0,$Decimals = 0) + drawFlatPieGraphWithShadow($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$SpliceDistance=0,$Decimals = 0) + drawPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$EnhanceColors=TRUE,$Skew=60,$SpliceHeight=20,$SpliceDistance=0,$Decimals=0) + Other methods : + setImageMap($Mode=TRUE,$GraphID="MyGraph") + getImageMap($MapName,$Flush=TRUE) + Render($FileName) + Stroke() + */ + + /* Declare some script wide constants */ + define("SCALE_NORMAL",1); + define("SCALE_ADDALL",2); + define("SCALE_START0",3); + define("SCALE_ADDALLSTART0",4); + define("PIE_PERCENTAGE", 1); + define("PIE_LABELS",2); + define("PIE_NOLABEL",3); + define("PIE_PERCENTAGE_LABEL", 4); + define("TARGET_GRAPHAREA",1); + define("TARGET_BACKGROUND",2); + define("ALIGN_TOP_LEFT",1); + define("ALIGN_TOP_CENTER",2); + define("ALIGN_TOP_RIGHT",3); + define("ALIGN_LEFT",4); + define("ALIGN_CENTER",5); + define("ALIGN_RIGHT",6); + define("ALIGN_BOTTOM_LEFT",7); + define("ALIGN_BOTTOM_CENTER",8); + define("ALIGN_BOTTOM_RIGHT",9); + + /* pChart class definition */ + class pChart + { + /* Palettes definition */ + var $Palette = array("0"=>array("R"=>188,"G"=>224,"B"=>46), + "1"=>array("R"=>224,"G"=>100,"B"=>46), + "2"=>array("R"=>224,"G"=>214,"B"=>46), + "3"=>array("R"=>46,"G"=>151,"B"=>224), + "4"=>array("R"=>176,"G"=>46,"B"=>224), + "5"=>array("R"=>224,"G"=>46,"B"=>117), + "6"=>array("R"=>92,"G"=>224,"B"=>46), + "7"=>array("R"=>224,"G"=>176,"B"=>46)); + + /* Some static vars used in the class */ + var $XSize = NULL; + var $YSize = NULL; + var $Picture = NULL; + var $ImageMap = NULL; + + /* Error management */ + var $ErrorReporting = FALSE; + var $ErrorInterface = "CLI"; + var $Errors = NULL; + var $ErrorFontName = "Fonts/pf_arma_five.ttf"; + var $ErrorFontSize = 6; + + /* vars related to the graphing area */ + var $GArea_X1 = NULL; + var $GArea_Y1 = NULL; + var $GArea_X2 = NULL; + var $GArea_Y2 = NULL; + var $GAreaXOffset = NULL; + var $VMax = NULL; + var $VMin = NULL; + var $VXMax = NULL; + var $VXMin = NULL; + var $Divisions = NULL; + var $XDivisions = NULL; + var $DivisionHeight = NULL; + var $XDivisionHeight = NULL; + var $DivisionCount = NULL; + var $XDivisionCount = NULL; + var $DivisionRatio = NULL; + var $XDivisionRatio = NULL; + var $DivisionWidth = NULL; + var $DataCount = NULL; + var $Currency = "\$"; + + /* Text format related vars */ + var $FontName = NULL; + var $FontSize = NULL; + var $DateFormat = "d/m/Y"; + + /* Lines format related vars */ + var $LineWidth = 1; + var $LineDotSize = 0; + + /* Layer related vars */ + var $Layers = NULL; + + /* Set antialias quality : 0 is maximum, 100 minimum*/ + var $AntialiasQuality = 0; + + /* Shadow settings */ + var $ShadowActive = FALSE; + var $ShadowXDistance = 1; + var $ShadowYDistance = 1; + var $ShadowRColor = 60; + var $ShadowGColor = 60; + var $ShadowBColor = 60; + var $ShadowAlpha = 50; + var $ShadowBlur = 0; + + /* Image Map settings */ + var $BuildMap = FALSE; + var $MapFunction = NULL; + var $tmpFolder = "tmp/"; + var $MapID = NULL; + + /* This function create the background picture */ + function pChart($XSize,$YSize) + { + $this->XSize = $XSize; + $this->YSize = $YSize; + $this->Picture = imagecreatetruecolor($XSize,$YSize); + + $C_White =$this->AllocateColor($this->Picture,255,255,255); + imagefilledrectangle($this->Picture,0,0,$XSize,$YSize,$C_White); + imagecolortransparent($this->Picture,$C_White); + + $this->setFontProperties("tahoma.ttf",8); + } + + /* Set if warnings should be reported */ + function reportWarnings($Interface="CLI") + { + $this->ErrorReporting = TRUE; + $this->ErrorInterface = $Interface; + } + + /* Set the font properties */ + function setFontProperties($FontName,$FontSize) + { + $this->FontName = $FontName; + $this->FontSize = $FontSize; + } + + /* Set the shadow properties */ + function setShadowProperties($XDistance=1,$YDistance=1,$R=60,$G=60,$B=60,$Alpha=50,$Blur=0) + { + $this->ShadowActive = TRUE; + $this->ShadowXDistance = $XDistance; + $this->ShadowYDistance = $YDistance; + $this->ShadowRColor = $R; + $this->ShadowGColor = $G; + $this->ShadowBColor = $B; + $this->ShadowAlpha = $Alpha; + $this->ShadowBlur = $Blur; + } + + /* Remove shadow option */ + function clearShadow() + { + $this->ShadowActive = FALSE; + } + + /* Set Palette color */ + function setColorPalette($ID,$R,$G,$B) + { + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + $this->Palette[$ID]["R"] = $R; + $this->Palette[$ID]["G"] = $G; + $this->Palette[$ID]["B"] = $B; + } + + /* Create a color palette shading from one color to another */ + function createColorGradientPalette($R1,$G1,$B1,$R2,$G2,$B2,$Shades) + { + $RFactor = ($R2-$R1)/$Shades; + $GFactor = ($G2-$G1)/$Shades; + $BFactor = ($B2-$B1)/$Shades; + + for($i=0;$i<=$Shades-1;$i++) + { + $this->Palette[$i]["R"] = $R1+$RFactor*$i; + $this->Palette[$i]["G"] = $G1+$GFactor*$i; + $this->Palette[$i]["B"] = $B1+$BFactor*$i; + } + } + + /* Load Color Palette from file */ + function loadColorPalette($FileName,$Delimiter=",") + { + $handle = @fopen($FileName,"r"); + $ColorID = 0; + if ($handle) + { + while (!feof($handle)) + { + $buffer = fgets($handle, 4096); + $buffer = str_replace(chr(10),"",$buffer); + $buffer = str_replace(chr(13),"",$buffer); + $Values = split($Delimiter,$buffer); + if ( count($Values) == 3 ) + { + $this->Palette[$ColorID]["R"] = $Values[0]; + $this->Palette[$ColorID]["G"] = $Values[1]; + $this->Palette[$ColorID]["B"] = $Values[2]; + $ColorID++; + } + } + } + } + + /* Set line style */ + function setLineStyle($Width=1,$DotSize=0) + { + $this->LineWidth = $Width; + $this->LineDotSize = $DotSize; + } + + /* Set currency symbol */ + function setCurrency($Currency) + { + $this->Currency = $Currency; + } + + /* Set the graph area location */ + function setGraphArea($X1,$Y1,$X2,$Y2) + { + $this->GArea_X1 = $X1; + $this->GArea_Y1 = $Y1; + $this->GArea_X2 = $X2; + $this->GArea_Y2 = $Y2; + } + + /* Prepare the graph area */ + function drawGraphArea($R,$G,$B,$Stripe=FALSE) + { + $this->drawFilledRectangle($this->GArea_X1,$this->GArea_Y1,$this->GArea_X2,$this->GArea_Y2,$R,$G,$B,FALSE); + $this->drawRectangle($this->GArea_X1,$this->GArea_Y1,$this->GArea_X2,$this->GArea_Y2,$R-40,$G-40,$B-40); + + if ( $Stripe ) + { + $R2 = $R-15; if ( $R2 < 0 ) { $R2 = 0; } + $G2 = $R-15; if ( $G2 < 0 ) { $G2 = 0; } + $B2 = $R-15; if ( $B2 < 0 ) { $B2 = 0; } + + $LineColor =$this->AllocateColor($this->Picture,$R2,$G2,$B2); + $SkewWidth = $this->GArea_Y2-$this->GArea_Y1-1; + + for($i=$this->GArea_X1-$SkewWidth;$i<=$this->GArea_X2;$i=$i+4) + { + $X1 = $i; $Y1 = $this->GArea_Y2; + $X2 = $i+$SkewWidth; $Y2 = $this->GArea_Y1; + + + if ( $X1 < $this->GArea_X1 ) + { $X1 = $this->GArea_X1; $Y1 = $this->GArea_Y1 + $X2 - $this->GArea_X1 + 1; } + + if ( $X2 >= $this->GArea_X2 ) + { $Y2 = $this->GArea_Y1 + $X2 - $this->GArea_X2 +1; $X2 = $this->GArea_X2 - 1; } +// * Fixed in 1.27 * { $X2 = $this->GArea_X2 - 1; $Y2 = $this->GArea_Y2 - ($this->GArea_X2 - $X1); } + + imageline($this->Picture,$X1,$Y1,$X2,$Y2+1,$LineColor); + } + } + } + + /* Allow you to clear the scale : used if drawing multiple charts */ + function clearScale() + { + $this->VMin = NULL; + $this->VMax = NULL; + $this->VXMin = NULL; + $this->VXMax = NULL; + $this->Divisions = NULL; + $this->XDivisions = NULL; } + + /* Allow you to fix the scale, use this to bypass the automatic scaling */ + function setFixedScale($VMin,$VMax,$Divisions=5,$VXMin=0,$VXMax=0,$XDivisions=5) + { + $this->VMin = $VMin; + $this->VMax = $VMax; + $this->Divisions = $Divisions; + + if ( !$VXMin == 0 ) + { + $this->VXMin = $VXMin; + $this->VXMax = $VXMax; + $this->XDivisions = $XDivisions; + } + } + + /* Wrapper to the drawScale() function allowing a second scale to be drawn */ + function drawRightScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1) + { + $this->drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks,$Angle,$Decimals,$WithMargin,$SkipLabels,TRUE); + } + + /* Compute and draw the scale */ + function drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1,$RightScale=FALSE) + { + /* Validate the Data and DataDescription array */ + $this->validateData("drawScale",$Data); + + $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B); + + $this->drawLine($this->GArea_X1,$this->GArea_Y1,$this->GArea_X1,$this->GArea_Y2,$R,$G,$B); + $this->drawLine($this->GArea_X1,$this->GArea_Y2,$this->GArea_X2,$this->GArea_Y2,$R,$G,$B); + + if ( $this->VMin == NULL && $this->VMax == NULL) + { + if (isset($DataDescription["Values"][0])) + { + $this->VMin = $Data[0][$DataDescription["Values"][0]]; + $this->VMax = $Data[0][$DataDescription["Values"][0]]; + } + else { $this->VMin = 2147483647; $this->VMax = -2147483647; } + + /* Compute Min and Max values */ + if ( $ScaleMode == SCALE_NORMAL || $ScaleMode == SCALE_START0 ) + { + if ( $ScaleMode == SCALE_START0 ) { $this->VMin = 0; } + + foreach ( $Data as $Key => $Values ) + { + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + if (isset($Data[$Key][$ColName])) + { + $Value = $Data[$Key][$ColName]; + + if ( is_numeric($Value) ) + { + if ( $this->VMax < $Value) { $this->VMax = $Value; } + if ( $this->VMin > $Value) { $this->VMin = $Value; } + } + } + } + } + } + elseif ( $ScaleMode == SCALE_ADDALL || $ScaleMode == SCALE_ADDALLSTART0 ) /* Experimental */ + { + if ( $ScaleMode == SCALE_ADDALLSTART0 ) { $this->VMin = 0; } + + foreach ( $Data as $Key => $Values ) + { + $Sum = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + if (isset($Data[$Key][$ColName])) + { + $Value = $Data[$Key][$ColName]; + if ( is_numeric($Value) ) + $Sum += $Value; + } + } + if ( $this->VMax < $Sum) { $this->VMax = $Sum; } + if ( $this->VMin > $Sum) { $this->VMin = $Sum; } + } + } + + if ( $this->VMax > preg_replace('/\.[0-9]+/','',$this->VMax) ) + $this->VMax = preg_replace('/\.[0-9]+/','',$this->VMax)+1; + + /* If all values are the same */ + if ( $this->VMax == $this->VMin ) + { + if ( $this->VMax >= 0 ) { $this->VMax++; } + else { $this->VMin--; } + } + + $DataRange = $this->VMax - $this->VMin; + if ( $DataRange == 0 ) { $DataRange = .1; } + + /* Compute automatic scaling */ + $ScaleOk = FALSE; $Factor = 1; + $MinDivHeight = 25; $MaxDivs = ($this->GArea_Y2 - $this->GArea_Y1) / $MinDivHeight; + + if ( $this->VMin == 0 && $this->VMax == 0 ) + { $this->VMin = 0; $this->VMax = 2; $Scale = 1; $Divisions = 2;} + elseif ($MaxDivs > 1) + { + while(!$ScaleOk) + { + $Scale1 = ( $this->VMax - $this->VMin ) / $Factor; + $Scale2 = ( $this->VMax - $this->VMin ) / $Factor / 2; + $Scale4 = ( $this->VMax - $this->VMin ) / $Factor / 4; + + if ( $Scale1 > 1 && $Scale1 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale1); $Scale = 1;} + if ( $Scale2 > 1 && $Scale2 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale2); $Scale = 2;} + if (!$ScaleOk) + { + if ( $Scale2 > 1 ) { $Factor = $Factor * 10; } + if ( $Scale2 < 1 ) { $Factor = $Factor / 10; } + } + } + + if ( floor($this->VMax / $Scale / $Factor) != $this->VMax / $Scale / $Factor) + { + $GridID = floor ( $this->VMax / $Scale / $Factor) + 1; + $this->VMax = $GridID * $Scale * $Factor; + $Divisions++; + } + + if ( floor($this->VMin / $Scale / $Factor) != $this->VMin / $Scale / $Factor) + { + $GridID = floor( $this->VMin / $Scale / $Factor); + $this->VMin = $GridID * $Scale * $Factor; + $Divisions++; + } + } + else /* Can occurs for small graphs */ + $Scale = 1; + + if ( !isset($Divisions) ) + $Divisions = 2; + + if ($Scale == 1 && $Divisions%2 == 1) + $Divisions--; + } + else + $Divisions = $this->Divisions; + + $this->DivisionCount = $Divisions; + + $DataRange = $this->VMax - $this->VMin; + if ( $DataRange == 0 ) { $DataRange = .1; } + + $this->DivisionHeight = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $Divisions; + $this->DivisionRatio = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $DataRange; + + $this->GAreaXOffset = 0; + if ( count($Data) > 1 ) + { + if ( $WithMargin == FALSE ) + $this->DivisionWidth = ( $this->GArea_X2 - $this->GArea_X1 ) / (count($Data)-1); + else + { + $this->DivisionWidth = ( $this->GArea_X2 - $this->GArea_X1 ) / (count($Data)); + $this->GAreaXOffset = $this->DivisionWidth / 2; + } + } + else + { + $this->DivisionWidth = $this->GArea_X2 - $this->GArea_X1; + $this->GAreaXOffset = $this->DivisionWidth / 2; + } + + $this->DataCount = count($Data); + + if ( $DrawTicks == FALSE ) + return(0); + + $YPos = $this->GArea_Y2; $XMin = NULL; + for($i=1;$i<=$Divisions+1;$i++) + { + if ( $RightScale ) + $this->drawLine($this->GArea_X2,$YPos,$this->GArea_X2+5,$YPos,$R,$G,$B); + else + $this->drawLine($this->GArea_X1,$YPos,$this->GArea_X1-5,$YPos,$R,$G,$B); + + $Value = $this->VMin + ($i-1) * (( $this->VMax - $this->VMin ) / $Divisions); + $Value = round($Value * pow(10,$Decimals)) / pow(10,$Decimals); + if ( $DataDescription["Format"]["Y"] == "number" ) + $Value = $Value.$DataDescription["Unit"]["Y"]; + if ( $DataDescription["Format"]["Y"] == "time" ) + $Value = $this->ToTime($Value); + if ( $DataDescription["Format"]["Y"] == "date" ) + $Value = $this->ToDate($Value); + if ( $DataDescription["Format"]["Y"] == "metric" ) + $Value = $this->ToMetric($Value); + if ( $DataDescription["Format"]["Y"] == "currency" ) + $Value = $this->ToCurrency($Value); + + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value); + $TextWidth = $Position[2]-$Position[0]; + + if ( $RightScale ) + { + imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X2+10,$YPos+($this->FontSize/2),$C_TextColor,$this->FontName,$Value); + if ( $XMin < $this->GArea_X2+15+$TextWidth || $XMin == NULL ) { $XMin = $this->GArea_X2+15+$TextWidth; } + } + else + { + imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X1-10-$TextWidth,$YPos+($this->FontSize/2),$C_TextColor,$this->FontName,$Value); + if ( $XMin > $this->GArea_X1-10-$TextWidth || $XMin == NULL ) { $XMin = $this->GArea_X1-10-$TextWidth; } + } + + $YPos = $YPos - $this->DivisionHeight; + } + + /* Write the Y Axis caption if set */ + if ( isset($DataDescription["Axis"]["Y"]) ) + { + $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["Y"]); + $TextHeight = abs($Position[1])+abs($Position[3]); + $TextTop = (($this->GArea_Y2 - $this->GArea_Y1) / 2) + $this->GArea_Y1 + ($TextHeight/2); + + if ( $RightScale ) + imagettftext($this->Picture,$this->FontSize,90,$XMin+$this->FontSize,$TextTop,$C_TextColor,$this->FontName,$DataDescription["Axis"]["Y"]); + else + imagettftext($this->Picture,$this->FontSize,90,$XMin-$this->FontSize,$TextTop,$C_TextColor,$this->FontName,$DataDescription["Axis"]["Y"]); + } + + /* Horizontal Axis */ + $XPos = $this->GArea_X1 + $this->GAreaXOffset; + $ID = 1; $YMax = NULL; + foreach ( $Data as $Key => $Values ) + { + if ( $ID % $SkipLabels == 0 ) + { + $this->drawLine(floor($XPos),$this->GArea_Y2,floor($XPos),$this->GArea_Y2+5,$R,$G,$B); + $Value = $Data[$Key][$DataDescription["Position"]]; + if ( $DataDescription["Format"]["X"] == "number" ) + $Value = $Value.$DataDescription["Unit"]["X"]; + if ( $DataDescription["Format"]["X"] == "time" ) + $Value = $this->ToTime($Value); + if ( $DataDescription["Format"]["X"] == "date" ) + $Value = $this->ToDate($Value); + if ( $DataDescription["Format"]["X"] == "metric" ) + $Value = $this->ToMetric($Value); + if ( $DataDescription["Format"]["X"] == "currency" ) + $Value = $this->ToCurrency($Value); + + $Position = imageftbbox($this->FontSize,$Angle,$this->FontName,$Value); + $TextWidth = abs($Position[2])+abs($Position[0]); + $TextHeight = abs($Position[1])+abs($Position[3]); + + if ( $Angle == 0 ) + { + $YPos = $this->GArea_Y2+18; + imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-floor($TextWidth/2),$YPos,$C_TextColor,$this->FontName,$Value); + } + else + { + $YPos = $this->GArea_Y2+10+$TextHeight; + if ( $Angle <= 90 ) + imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value); + else + imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)+$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value); + } + if ( $YMax < $YPos || $YMax == NULL ) { $YMax = $YPos; } + } + + $XPos = $XPos + $this->DivisionWidth; + $ID++; + } + + /* Write the X Axis caption if set */ + if ( isset($DataDescription["Axis"]["X"]) ) + { + $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["X"]); + $TextWidth = abs($Position[2])+abs($Position[0]); + $TextLeft = (($this->GArea_X2 - $this->GArea_X1) / 2) + $this->GArea_X1 + ($TextWidth/2); + imagettftext($this->Picture,$this->FontSize,0,$TextLeft,$YMax+$this->FontSize+5,$C_TextColor,$this->FontName,$DataDescription["Axis"]["X"]); + } + } + + /* Compute and draw the scale for X/Y charts */ + function drawXYScale($Data,$DataDescription,$YSerieName,$XSerieName,$R,$G,$B,$WithMargin=0,$Angle=0,$Decimals=1) + { + /* Validate the Data and DataDescription array */ + $this->validateData("drawScale",$Data); + + $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B); + + $this->drawLine($this->GArea_X1,$this->GArea_Y1,$this->GArea_X1,$this->GArea_Y2,$R,$G,$B); + $this->drawLine($this->GArea_X1,$this->GArea_Y2,$this->GArea_X2,$this->GArea_Y2,$R,$G,$B); + + /* Process Y scale */ + if ( $this->VMin == NULL && $this->VMax == NULL) + { + $this->VMin = $Data[0][$YSerieName]; + $this->VMax = $Data[0][$YSerieName]; + + foreach ( $Data as $Key => $Values ) + { + if (isset($Data[$Key][$YSerieName])) + { + $Value = $Data[$Key][$YSerieName]; + if ( $this->VMax < $Value) { $this->VMax = $Value; } + if ( $this->VMin > $Value) { $this->VMin = $Value; } + } + } + + if ( $this->VMax > preg_replace('/\.[0-9]+/','',$this->VMax) ) + $this->VMax = preg_replace('/\.[0-9]+/','',$this->VMax)+1; + + $DataRange = $this->VMax - $this->VMin; + if ( $DataRange == 0 ) { $DataRange = .1; } + + /* Compute automatic scaling */ + $ScaleOk = FALSE; $Factor = 1; + $MinDivHeight = 25; $MaxDivs = ($this->GArea_Y2 - $this->GArea_Y1) / $MinDivHeight; + + if ( $this->VMin == 0 && $this->VMax == 0 ) + { $this->VMin = 0; $this->VMax = 2; $Scale = 1; $Divisions = 2;} + elseif ($MaxDivs > 1) + { + while(!$ScaleOk) + { + $Scale1 = ( $this->VMax - $this->VMin ) / $Factor; + $Scale2 = ( $this->VMax - $this->VMin ) / $Factor / 2; + $Scale4 = ( $this->VMax - $this->VMin ) / $Factor / 4; + + if ( $Scale1 > 1 && $Scale1 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale1); $Scale = 1;} + if ( $Scale2 > 1 && $Scale2 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale2); $Scale = 2;} + if (!$ScaleOk) + { + if ( $Scale2 > 1 ) { $Factor = $Factor * 10; } + if ( $Scale2 < 1 ) { $Factor = $Factor / 10; } + } + } + + if ( floor($this->VMax / $Scale / $Factor) != $this->VMax / $Scale / $Factor) + { + $GridID = floor ( $this->VMax / $Scale / $Factor) + 1; + $this->VMax = $GridID * $Scale * $Factor; + $Divisions++; + } + + if ( floor($this->VMin / $Scale / $Factor) != $this->VMin / $Scale / $Factor) + { + $GridID = floor( $this->VMin / $Scale / $Factor); + $this->VMin = $GridID * $Scale * $Factor; + $Divisions++; + } + } + else /* Can occurs for small graphs */ + $Scale = 1; + + if ( !isset($Divisions) ) + $Divisions = 2; + + if ( $this->isRealInt(($this->VMax-$this->VMin)/($Divisions-1))) + $Divisions--; + elseif ( $this->isRealInt(($this->VMax-$this->VMin)/($Divisions+1))) + $Divisions++; + } + else + $Divisions = $this->Divisions; + + $this->DivisionCount = $Divisions; + + $DataRange = $this->VMax - $this->VMin; + if ( $DataRange == 0 ) { $DataRange = .1; } + + $this->DivisionHeight = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $Divisions; + $this->DivisionRatio = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $DataRange; + + $YPos = $this->GArea_Y2; $XMin = NULL; + for($i=1;$i<=$Divisions+1;$i++) + { + $this->drawLine($this->GArea_X1,$YPos,$this->GArea_X1-5,$YPos,$R,$G,$B); + $Value = $this->VMin + ($i-1) * (( $this->VMax - $this->VMin ) / $Divisions); + $Value = round($Value * pow(10,$Decimals)) / pow(10,$Decimals); + if ( $DataDescription["Format"]["Y"] == "number" ) + $Value = $Value.$DataDescription["Unit"]["Y"]; + if ( $DataDescription["Format"]["Y"] == "time" ) + $Value = $this->ToTime($Value); + if ( $DataDescription["Format"]["Y"] == "date" ) + $Value = $this->ToDate($Value); + if ( $DataDescription["Format"]["Y"] == "metric" ) + $Value = $this->ToMetric($Value); + if ( $DataDescription["Format"]["Y"] == "currency" ) + $Value = $this->ToCurrency($Value); + + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value); + $TextWidth = $Position[2]-$Position[0]; + imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X1-10-$TextWidth,$YPos+($this->FontSize/2),$C_TextColor,$this->FontName,$Value); + + if ( $XMin > $this->GArea_X1-10-$TextWidth || $XMin == NULL ) { $XMin = $this->GArea_X1-10-$TextWidth; } + + $YPos = $YPos - $this->DivisionHeight; + } + + /* Process X scale */ + if ( $this->VXMin == NULL && $this->VXMax == NULL) + { + $this->VXMin = $Data[0][$XSerieName]; + $this->VXMax = $Data[0][$XSerieName]; + + foreach ( $Data as $Key => $Values ) + { + if (isset($Data[$Key][$XSerieName])) + { + $Value = $Data[$Key][$XSerieName]; + if ( $this->VXMax < $Value) { $this->VXMax = $Value; } + if ( $this->VXMin > $Value) { $this->VXMin = $Value; } + } + } + + if ( $this->VXMax > preg_replace('/\.[0-9]+/','',$this->VXMax) ) + $this->VXMax = preg_replace('/\.[0-9]+/','',$this->VXMax)+1; + + $DataRange = $this->VMax - $this->VMin; + if ( $DataRange == 0 ) { $DataRange = .1; } + + /* Compute automatic scaling */ + $ScaleOk = FALSE; $Factor = 1; + $MinDivWidth = 25; $MaxDivs = ($this->GArea_X2 - $this->GArea_X1) / $MinDivWidth; + + if ( $this->VXMin == 0 && $this->VXMax == 0 ) + { $this->VXMin = 0; $this->VXMax = 2; $Scale = 1; $XDivisions = 2;} + elseif ($MaxDivs > 1) + { + while(!$ScaleOk) + { + $Scale1 = ( $this->VXMax - $this->VXMin ) / $Factor; + $Scale2 = ( $this->VXMax - $this->VXMin ) / $Factor / 2; + $Scale4 = ( $this->VXMax - $this->VXMin ) / $Factor / 4; + + if ( $Scale1 > 1 && $Scale1 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $XDivisions = floor($Scale1); $Scale = 1;} + if ( $Scale2 > 1 && $Scale2 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $XDivisions = floor($Scale2); $Scale = 2;} + if (!$ScaleOk) + { + if ( $Scale2 > 1 ) { $Factor = $Factor * 10; } + if ( $Scale2 < 1 ) { $Factor = $Factor / 10; } + } + } + + if ( floor($this->VXMax / $Scale / $Factor) != $this->VXMax / $Scale / $Factor) + { + $GridID = floor ( $this->VXMax / $Scale / $Factor) + 1; + $this->VXMax = $GridID * $Scale * $Factor; + $XDivisions++; + } + + if ( floor($this->VXMin / $Scale / $Factor) != $this->VXMin / $Scale / $Factor) + { + $GridID = floor( $this->VXMin / $Scale / $Factor); + $this->VXMin = $GridID * $Scale * $Factor; + $XDivisions++; + } + } + else /* Can occurs for small graphs */ + $Scale = 1; + + if ( !isset($XDivisions) ) + $XDivisions = 2; + + if ( $this->isRealInt(($this->VXMax-$this->VXMin)/($XDivisions-1))) + $XDivisions--; + elseif ( $this->isRealInt(($this->VXMax-$this->VXMin)/($XDivisions+1))) + $XDivisions++; + } + else + $XDivisions = $this->XDivisions; + + $this->XDivisionCount = $Divisions; + $this->DataCount = $Divisions + 2; + + $XDataRange = $this->VXMax - $this->VXMin; + if ( $XDataRange == 0 ) { $XDataRange = .1; } + + $this->DivisionWidth = ( $this->GArea_X2 - $this->GArea_X1 ) / $XDivisions; + $this->XDivisionRatio = ( $this->GArea_X2 - $this->GArea_X1 ) / $XDataRange; + + $XPos = $this->GArea_X1; $YMax = NULL; + for($i=1;$i<=$XDivisions+1;$i++) + { + $this->drawLine($XPos,$this->GArea_Y2,$XPos,$this->GArea_Y2+5,$R,$G,$B); + + $Value = $this->VXMin + ($i-1) * (( $this->VXMax - $this->VXMin ) / $XDivisions); + $Value = round($Value * pow(10,$Decimals)) / pow(10,$Decimals); + if ( $DataDescription["Format"]["Y"] == "number" ) + $Value = $Value.$DataDescription["Unit"]["Y"]; + if ( $DataDescription["Format"]["Y"] == "time" ) + $Value = $this->ToTime($Value); + if ( $DataDescription["Format"]["Y"] == "date" ) + $Value = $this->ToDate($Value); + if ( $DataDescription["Format"]["Y"] == "metric" ) + $Value = $this->ToMetric($Value); + if ( $DataDescription["Format"]["Y"] == "currency" ) + $Value = $this->ToCurrency($Value); + + $Position = imageftbbox($this->FontSize,$Angle,$this->FontName,$Value); + $TextWidth = abs($Position[2])+abs($Position[0]); + $TextHeight = abs($Position[1])+abs($Position[3]); + + if ( $Angle == 0 ) + { + $YPos = $this->GArea_Y2+18; + imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-floor($TextWidth/2),$YPos,$C_TextColor,$this->FontName,$Value); + } + else + { + $YPos = $this->GArea_Y2+10+$TextHeight; + if ( $Angle <= 90 ) + imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value); + else + imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)+$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value); + } + + if ( $YMax < $YPos || $YMax == NULL ) { $YMax = $YPos; } + + $XPos = $XPos + $this->DivisionWidth; + } + + /* Write the Y Axis caption if set */ + if ( isset($DataDescription["Axis"]["Y"]) ) + { + $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["Y"]); + $TextHeight = abs($Position[1])+abs($Position[3]); + $TextTop = (($this->GArea_Y2 - $this->GArea_Y1) / 2) + $this->GArea_Y1 + ($TextHeight/2); + imagettftext($this->Picture,$this->FontSize,90,$XMin-$this->FontSize,$TextTop,$C_TextColor,$this->FontName,$DataDescription["Axis"]["Y"]); + } + + /* Write the X Axis caption if set */ + if ( isset($DataDescription["Axis"]["X"]) ) + { + $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["X"]); + $TextWidth = abs($Position[2])+abs($Position[0]); + $TextLeft = (($this->GArea_X2 - $this->GArea_X1) / 2) + $this->GArea_X1 + ($TextWidth/2); + imagettftext($this->Picture,$this->FontSize,0,$TextLeft,$YMax+$this->FontSize+5,$C_TextColor,$this->FontName,$DataDescription["Axis"]["X"]); + } + } + + /* Compute and draw the scale */ + function drawGrid($LineWidth,$Mosaic=TRUE,$R=220,$G=220,$B=220,$Alpha=100,$DrawVerticals=FALSE) + { + /* Draw mosaic */ + if ( $Mosaic ) + { + $LayerWidth = $this->GArea_X2-$this->GArea_X1; + $LayerHeight = $this->GArea_Y2-$this->GArea_Y1; + + $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight); + $C_White =$this->AllocateColor($this->Layers[0],255,255,255); + imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White); + imagecolortransparent($this->Layers[0],$C_White); + + $C_Rectangle =$this->AllocateColor($this->Layers[0],250,250,250); + + $YPos = $LayerHeight; //$this->GArea_Y2-1; + $LastY = $YPos; + for($i=0;$i<=$this->DivisionCount;$i++) + { + $LastY = $YPos; + $YPos = $YPos - $this->DivisionHeight; + + if ( $YPos <= 0 ) { $YPos = 1; } + + if ( $i % 2 == 0 ) + { + imagefilledrectangle($this->Layers[0],1,$YPos,$LayerWidth-1,$LastY,$C_Rectangle); + } + } + imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha); + imagedestroy($this->Layers[0]); + } + + /* Horizontal lines */ + $YPos = $this->GArea_Y2 - $this->DivisionHeight; + for($i=1;$i<=$this->DivisionCount;$i++) + { + if ( $YPos > $this->GArea_Y1 && $YPos < $this->GArea_Y2 ) + $this->drawDottedLine($this->GArea_X1,$YPos,$this->GArea_X2,$YPos,$LineWidth,$R,$G,$B); + + $YPos = $YPos - $this->DivisionHeight; + } + + /* Vertical lines */ + if (!$DrawVerticals) + return; + if ( $this->GAreaXOffset == 0 ) + { $XPos = $this->GArea_X1 + $this->DivisionWidth + $this->GAreaXOffset; $ColCount = $this->DataCount-2; } + else + { $XPos = $this->GArea_X1 + $this->GAreaXOffset; $ColCount = floor( ($this->GArea_X2 - $this->GArea_X1) / $this->DivisionWidth ); } + + for($i=1;$i<=$ColCount;$i++) + { + if ( $XPos > $this->GArea_X1 && $XPos < $this->GArea_X2 ) + $this->drawDottedLine(floor($XPos),$this->GArea_Y1,floor($XPos),$this->GArea_Y2,$LineWidth,$R,$G,$B); + $XPos = $XPos + $this->DivisionWidth; + } + } + + /* retrieve the legends size */ + function getLegendBoxSize($DataDescription) + { + if ( !isset($DataDescription["Description"]) ) + return(-1); + + /* <-10->[8]<-4->Text<-10-> */ + $MaxWidth = 0; $MaxHeight = 8; + foreach($DataDescription["Description"] as $Key => $Value) + { + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value); + $TextWidth = $Position[2]-$Position[0]; + $TextHeight = $Position[1]-$Position[7]; + if ( $TextWidth > $MaxWidth) { $MaxWidth = $TextWidth; } + $MaxHeight = $MaxHeight + $TextHeight + 4; + } + $MaxHeight = $MaxHeight - 3; + $MaxWidth = $MaxWidth + 32; + + return(array($MaxWidth,$MaxHeight)); + } + + /* Draw the data legends */ + function drawLegend($XPos,$YPos,$DataDescription,$R,$G,$B,$Rs=-1,$Gs=-1,$Bs=-1,$Rt=0,$Gt=0,$Bt=0,$Border=TRUE) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawLegend",$DataDescription); + + if ( !isset($DataDescription["Description"]) ) + return(-1); + + $C_TextColor =$this->AllocateColor($this->Picture,$Rt,$Gt,$Bt); + + /* <-10->[8]<-4->Text<-10-> */ + $MaxWidth = 0; $MaxHeight = 8; + foreach($DataDescription["Description"] as $Key => $Value) + { + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value); + $TextWidth = $Position[2]-$Position[0]; + $TextHeight = $Position[1]-$Position[7]; + if ( $TextWidth > $MaxWidth) { $MaxWidth = $TextWidth; } + $MaxHeight = $MaxHeight + $TextHeight + 4; + } + $MaxHeight = $MaxHeight - 5; + $MaxWidth = $MaxWidth + 32; + + if ( $Rs == -1 || $Gs == -1 || $Bs == -1 ) + { $Rs = $R-30; $Gs = $G-30; $Bs = $B-30; } + + if ( $Border ) + { + $this->drawFilledRoundedRectangle($XPos+1,$YPos+1,$XPos+$MaxWidth+1,$YPos+$MaxHeight+1,5,$Rs,$Gs,$Bs); + $this->drawFilledRoundedRectangle($XPos,$YPos,$XPos+$MaxWidth,$YPos+$MaxHeight,5,$R,$G,$B); + } + + $YOffset = 4 + $this->FontSize; $ID = 0; + foreach($DataDescription["Description"] as $Key => $Value) + { + $this->drawFilledRoundedRectangle($XPos+10,$YPos+$YOffset-4,$XPos+14,$YPos+$YOffset-4,2,$this->Palette[$ID]["R"],$this->Palette[$ID]["G"],$this->Palette[$ID]["B"]); + imagettftext($this->Picture,$this->FontSize,0,$XPos+22,$YPos+$YOffset,$C_TextColor,$this->FontName,$Value); + + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value); + $TextHeight = $Position[1]-$Position[7]; + + $YOffset = $YOffset + $TextHeight + 4; + $ID++; + } + } + + /* Draw the data legends */ + function drawPieLegend($XPos,$YPos,$Data,$DataDescription,$R,$G,$B) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawPieLegend",$DataDescription,FALSE); + $this->validateData("drawPieLegend",$Data); + + if ( !isset($DataDescription["Position"]) ) + return(-1); + + $C_TextColor =$this->AllocateColor($this->Picture,0,0,0); + + /* <-10->[8]<-4->Text<-10-> */ + $MaxWidth = 0; $MaxHeight = 8; + foreach($Data as $Key => $Value) + { + $Value2 = $Value[$DataDescription["Position"]]; + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value2); + $TextWidth = $Position[2]-$Position[0]; + $TextHeight = $Position[1]-$Position[7]; + if ( $TextWidth > $MaxWidth) { $MaxWidth = $TextWidth; } + + $MaxHeight = $MaxHeight + $TextHeight + 4; + } + $MaxHeight = $MaxHeight - 3; + $MaxWidth = $MaxWidth + 32; + + $this->drawFilledRoundedRectangle($XPos+1,$YPos+1,$XPos+$MaxWidth+1,$YPos+$MaxHeight+1,5,$R-30,$G-30,$B-30); + $this->drawFilledRoundedRectangle($XPos,$YPos,$XPos+$MaxWidth,$YPos+$MaxHeight,5,$R,$G,$B); + + $YOffset = 4 + $this->FontSize; $ID = 0; + foreach($Data as $Key => $Value) + { + $Value2 = $Value[$DataDescription["Position"]]; + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value2); + $TextHeight = $Position[1]-$Position[7]; + $this->drawFilledRectangle($XPos+10,$YPos+$YOffset-6,$XPos+14,$YPos+$YOffset-2,$this->Palette[$ID]["R"],$this->Palette[$ID]["G"],$this->Palette[$ID]["B"]); + + imagettftext($this->Picture,$this->FontSize,0,$XPos+22,$YPos+$YOffset,$C_TextColor,$this->FontName,$Value2); + $YOffset = $YOffset + $TextHeight + 4; + $ID++; + } + } + function drawOverlayBarGraphH($Data,$DataDescription,$Alpha=50) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawOverlayBarGraphH",$DataDescription); + $this->validateData("drawOverlayBarGraphH",$Data); + + $LayerWidth = $this->GArea_X2-$this->GArea_X1; + $LayerHeight = $this->GArea_Y2-$this->GArea_Y1; + + $GraphID = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + $ID = 0; + foreach ( $DataDescription["Description"] as $keyI => $ValueI ) + { + if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; + } + + $this->Layers[$GraphID] = imagecreatetruecolor($LayerWidth,$LayerHeight); + $C_White = imagecolorallocate($this->Layers[$GraphID],255,255,255); + $C_Black = imagecolorallocate($this->Layers[$GraphID],0,0,0); + $C_Graph = imagecolorallocate($this->Layers[$GraphID],$this->Palette[$GraphID]["R"],$this->Palette[$GraphID]["G"],$this->Palette[$GraphID]["B"]); + imagefilledrectangle($this->Layers[$GraphID],0,0,$LayerWidth,$LayerHeight,$C_White); + imagecolortransparent($this->Layers[$GraphID],$C_White); + + $XWidth = $this->DivisionWidth / 4; + $XPos = $this->GAreaXOffset; + $YZero = $LayerHeight - ((0-$this->VMin) * $this->DivisionRatio); + $XLast = -1; $PointsCount = 2; + $XZero = 0; + $YPad = 20; //$LayerHeight / count($Data); + $YHeight = 8; + $YPos = $YHeight; + $Divisi = ($LayerWidth / ($this->VMax - $this->VMin)); + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName]) ) + { + $Value = $Data[$Key][$ColName]; + $XPos = (($Value-$this->VMin) * $Divisi) ; + + imagefilledrectangle($this->Layers[$GraphID],$XPos,$YPos+$YHeight,$XZero,$YPos-$YHeight,$C_Graph); + imagettftext($this->Layers[$GraphID],$this->FontSize,0,$XZero+2,$YPos+4,$C_Black,$this->FontName, $Data[$Key][$DataDescription["Position"]]." (".round($Value,2).")"); + + $X1 = floor($XPos - $XWidth + $this->GArea_X1); $Y1 = floor($YPos+$this->GArea_Y1) + .2; + $X2 = floor($XPos + $XWidth + $this->GArea_X1); + if ( $X1 <= $this->GArea_X1 ) { $X1 = $this->GArea_X1 + 1; } + if ( $X2 >= $this->GArea_X2 ) { $X2 = $this->GArea_X2 - 1; } + if ( $this->BuildMap ) + $this->addToImageMap($X1,min($Y1,$Y2),$X2,max($Y1,$Y2),$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"oBar"); + + $this->drawLine($X1,$Y1,$X2,$Y1,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]); + } + $YPos= $YPos + $YPad; + } + + $GraphID++; + } + + for($i=0;$i<=($GraphID-1);$i++) + { + imagecopymerge($this->Picture,$this->Layers[$i],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha); + imagedestroy($this->Layers[$i]); + } + } + /* Draw the graph title */ + function drawTitle($XPos,$YPos,$Value,$R,$G,$B,$XPos2=-1,$YPos2=-1,$Shadow=FALSE) + { + $C_TextColor = $this->AllocateColor($this->Picture,$R,$G,$B); + + if ( $XPos2 != -1 ) + { + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value); + $TextWidth = $Position[2]-$Position[0]; + $XPos = floor(( $XPos2 - $XPos - $TextWidth ) / 2 ) + $XPos; + } + + if ( $YPos2 != -1 ) + { + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value); + $TextHeight = $Position[5]-$Position[3]; + $YPos = floor(( $YPos2 - $YPos - $TextHeight ) / 2 ) + $YPos; + } + + if ( $Shadow ) + { + $C_ShadowColor = $this->AllocateColor($this->Picture,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor); + imagettftext($this->Picture,$this->FontSize,0,$XPos+$this->ShadowXDistance,$YPos+$this->ShadowYDistance,$C_ShadowColor,$this->FontName,$Value); + } + + imagettftext($this->Picture,$this->FontSize,0,$XPos,$YPos,$C_TextColor,$this->FontName,$Value); + } + + /* Draw a text box with text align & alpha properties */ + function drawTextBox($X1,$Y1,$X2,$Y2,$Text,$Angle=0,$R=255,$G=255,$B=255,$Align=ALIGN_LEFT,$Shadow=TRUE,$BgR=-1,$BgG=-1,$BgB=-1,$Alpha=100) + { + $Position = imageftbbox($this->FontSize,$Angle,$this->FontName,$Text); + $TextWidth = $Position[2]-$Position[0]; + $TextHeight = $Position[5]-$Position[3]; + $AreaWidth = $X2 - $X1; + $AreaHeight = $Y2 - $Y1; + + if ( $BgR != -1 && $BgG != -1 && $BgB != -1 ) + $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$BgR,$BgG,$BgB,FALSE,$Alpha); + + if ( $Align == ALIGN_TOP_LEFT ) { $X = $X1+1; $Y = $Y1+$this->FontSize+1; } + if ( $Align == ALIGN_TOP_CENTER ) { $X = $X1+($AreaWidth/2)-($TextWidth/2); $Y = $Y1+$this->FontSize+1; } + if ( $Align == ALIGN_TOP_RIGHT ) { $X = $X2-$TextWidth-1; $Y = $Y1+$this->FontSize+1; } + if ( $Align == ALIGN_LEFT ) { $X = $X1+1; $Y = $Y1+($AreaHeight/2)-($TextHeight/2); } + if ( $Align == ALIGN_CENTER ) { $X = $X1+($AreaWidth/2)-($TextWidth/2); $Y = $Y1+($AreaHeight/2)-($TextHeight/2); } + if ( $Align == ALIGN_RIGHT ) { $X = $X2-$TextWidth-1; $Y = $Y1+($AreaHeight/2)-($TextHeight/2); } + if ( $Align == ALIGN_BOTTOM_LEFT ) { $X = $X1+1; $Y = $Y2-1; } + if ( $Align == ALIGN_BOTTOM_CENTER ) { $X = $X1+($AreaWidth/2)-($TextWidth/2); $Y = $Y2-1; } + if ( $Align == ALIGN_BOTTOM_RIGHT ) { $X = $X2-$TextWidth-1; $Y = $Y2-1; } + + $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B); + $C_ShadowColor =$this->AllocateColor($this->Picture,0,0,0); + if ( $Shadow ) + imagettftext($this->Picture,$this->FontSize,$Angle,$X+1,$Y+1,$C_ShadowColor,$this->FontName,$Text); + + imagettftext($this->Picture,$this->FontSize,$Angle,$X,$Y,$C_TextColor,$this->FontName,$Text); + } + + /* Compute and draw the scale */ + function drawTreshold($Value,$R,$G,$B,$ShowLabel=FALSE,$ShowOnRight=FALSE,$TickWidth=4,$FreeText=NULL) + { + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B); + $Y = $this->GArea_Y2 - ($Value - $this->VMin) * $this->DivisionRatio; + + if ( $Y <= $this->GArea_Y1 || $Y >= $this->GArea_Y2 ) + return(-1); + + if ( $TickWidth == 0 ) + $this->drawLine($this->GArea_X1,$Y,$this->GArea_X2,$Y,$R,$G,$B); + else + $this->drawDottedLine($this->GArea_X1,$Y,$this->GArea_X2,$Y,$TickWidth,$R,$G,$B); + + if ( $ShowLabel ) + { + if ( $FreeText == NULL ) + { $Label = $Value; } else { $Label = $FreeText; } + + if ( $ShowOnRight ) + imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X2+2,$Y+($this->FontSize/2),$C_TextColor,$this->FontName,$Label); + else + imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X1+2,$Y-($this->FontSize/2),$C_TextColor,$this->FontName,$Label); + } + } + + /* This function put a label on a specific point */ + function setLabel($Data,$DataDescription,$SerieName,$ValueName,$Caption,$R=210,$G=210,$B=210) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("setLabel",$DataDescription); + $this->validateData("setLabel",$Data); + $ShadowFactor = 100; + $C_Label =$this->AllocateColor($this->Picture,$R,$G,$B); + $C_Shadow =$this->AllocateColor($this->Picture,$R-$ShadowFactor,$G-$ShadowFactor,$B-$ShadowFactor); + $C_TextColor =$this->AllocateColor($this->Picture,0,0,0); + + $Cp = 0; $Found = FALSE; + foreach ( $Data as $Key => $Value ) + { + if ( $Data[$Key][$DataDescription["Position"]] == $ValueName ) + { $NumericalValue = $Data[$Key][$SerieName]; $Found = TRUE; } + if ( !$Found ) + $Cp++; + } + + $XPos = $this->GArea_X1 + $this->GAreaXOffset + ( $this->DivisionWidth * $Cp ) + 2; + $YPos = $this->GArea_Y2 - ($NumericalValue - $this->VMin) * $this->DivisionRatio; + + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Caption); + $TextHeight = $Position[3] - $Position[5]; + $TextWidth = $Position[2]-$Position[0] + 2; + $TextOffset = floor($TextHeight/2); + + // Shadow + $Poly = array($XPos+1,$YPos+1,$XPos + 9,$YPos - $TextOffset,$XPos + 8,$YPos + $TextOffset + 2); + imagefilledpolygon($this->Picture,$Poly,3,$C_Shadow); + $this->drawLine($XPos,$YPos+1,$XPos + 9,$YPos - $TextOffset - .2,$R-$ShadowFactor,$G-$ShadowFactor,$B-$ShadowFactor); + $this->drawLine($XPos,$YPos+1,$XPos + 9,$YPos + $TextOffset + 2.2,$R-$ShadowFactor,$G-$ShadowFactor,$B-$ShadowFactor); + $this->drawFilledRectangle($XPos + 9,$YPos - $TextOffset-.2,$XPos + 13 + $TextWidth,$YPos + $TextOffset + 2.2,$R-$ShadowFactor,$G-$ShadowFactor,$B-$ShadowFactor); + + // Label background + $Poly = array($XPos,$YPos,$XPos + 8,$YPos - $TextOffset - 1,$XPos + 8,$YPos + $TextOffset + 1); + imagefilledpolygon($this->Picture,$Poly,3,$C_Label); + $this->drawLine($XPos-1,$YPos,$XPos + 8,$YPos - $TextOffset - 1.2,$R,$G,$B); + $this->drawLine($XPos-1,$YPos,$XPos + 8,$YPos + $TextOffset + 1.2,$R,$G,$B); + $this->drawFilledRectangle($XPos + 8,$YPos - $TextOffset - 1.2,$XPos + 12 + $TextWidth,$YPos + $TextOffset + 1.2,$R,$G,$B); + + imagettftext($this->Picture,$this->FontSize,0,$XPos + 10,$YPos + $TextOffset,$C_TextColor,$this->FontName,$Caption); + } + + /* This function draw a plot graph */ + function drawPlotGraph($Data,$DataDescription,$BigRadius=5,$SmallRadius=2,$R2=-1,$G2=-1,$B2=-1,$Shadow=FALSE) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawPlotGraph",$DataDescription); + $this->validateData("drawPlotGraph",$Data); + + $GraphID = 0; + $Ro = $R2; $Go = $G2; $Bo = $B2; + + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + $ID = 0; + foreach ( $DataDescription["Description"] as $keyI => $ValueI ) + { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; } + + $R = $this->Palette[$ColorID]["R"]; + $G = $this->Palette[$ColorID]["G"]; + $B = $this->Palette[$ColorID]["B"]; + $R2 = $Ro; $G2 = $Go; $B2 = $Bo; + + if ( isset($DataDescription["Symbol"][$ColName]) ) + { + $Is_Alpha = ((ord ( file_get_contents ($DataDescription["Symbol"][$ColName], false, null, 25, 1)) & 6) & 4) == 4; + + $Infos = getimagesize($DataDescription["Symbol"][$ColName]); + $ImageWidth = $Infos[0]; + $ImageHeight = $Infos[1]; + $Symbol = imagecreatefromgif($DataDescription["Symbol"][$ColName]); + } + + $XPos = $this->GArea_X1 + $this->GAreaXOffset; + $Hsize = round($BigRadius/2); + $R3 = -1; $G3 = -1; $B3 = -1; + foreach ( $Data as $Key => $Values ) + { + $Value = $Data[$Key][$ColName]; + $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio); + + /* Save point into the image map if option activated */ + if ( $this->BuildMap ) + $this->addToImageMap($XPos-$Hsize,$YPos-$Hsize,$XPos+1+$Hsize,$YPos+$Hsize+1,$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"Plot"); + + if ( is_numeric($Value) ) + { + if ( !isset($DataDescription["Symbol"][$ColName]) ) + { + + if ( $Shadow ) + { + if ( $R3 !=-1 && $G3 !=-1 && $B3 !=-1 ) + $this->drawFilledCircle($XPos+2,$YPos+2,$BigRadius,$R3,$G3,$B3); + else + { + $R3 = $this->Palette[$ColorID]["R"]-20; if ( $R3 < 0 ) { $R3 = 0; } + $G3 = $this->Palette[$ColorID]["G"]-20; if ( $G3 < 0 ) { $G3 = 0; } + $B3 = $this->Palette[$ColorID]["B"]-20; if ( $B3 < 0 ) { $B3 = 0; } + $this->drawFilledCircle($XPos+2,$YPos+2,$BigRadius,$R3,$G3,$B3); + } + } + + $this->drawFilledCircle($XPos+1,$YPos+1,$BigRadius,$R,$G,$B); + + if ( $SmallRadius != 0 ) + { + if ( $R2 !=-1 && $G2 !=-1 && $B2 !=-1 ) + $this->drawFilledCircle($XPos+1,$YPos+1,$SmallRadius,$R2,$G2,$B2); + else + { + $R2 = $this->Palette[$ColorID]["R"]-15; if ( $R2 < 0 ) { $R2 = 0; } + $G2 = $this->Palette[$ColorID]["G"]-15; if ( $G2 < 0 ) { $G2 = 0; } + $B2 = $this->Palette[$ColorID]["B"]-15; if ( $B2 < 0 ) { $B2 = 0; } + + $this->drawFilledCircle($XPos+1,$YPos+1,$SmallRadius,$R2,$G2,$B2); + } + } + } + else + { + imagecopymerge($this->Picture,$Symbol,$XPos+1-$ImageWidth/2,$YPos+1-$ImageHeight/2,0,0,$ImageWidth,$ImageHeight,100); + } + } + + $XPos = $XPos + $this->DivisionWidth; + } + $GraphID++; + } + } + + /* This function draw a plot graph in an X/Y space */ + function drawXYPlotGraph($Data,$DataDescription,$YSerieName,$XSerieName,$PaletteID=0,$BigRadius=5,$SmallRadius=2,$R2=-1,$G2=-1,$B2=-1,$Shadow=TRUE) + { + $R = $this->Palette[$PaletteID]["R"]; + $G = $this->Palette[$PaletteID]["G"]; + $B = $this->Palette[$PaletteID]["B"]; + $R3 = -1; $G3 = -1; $B3 = -1; + + $YLast = -1; $XLast = -1; + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$YSerieName]) && isset($Data[$Key][$XSerieName]) ) + { + $X = $Data[$Key][$XSerieName]; + $Y = $Data[$Key][$YSerieName]; + + $Y = $this->GArea_Y2 - (($Y-$this->VMin) * $this->DivisionRatio); + $X = $this->GArea_X1 + (($X-$this->VXMin) * $this->XDivisionRatio); + + + if ( $Shadow ) + { + if ( $R3 !=-1 && $G3 !=-1 && $B3 !=-1 ) + $this->drawFilledCircle($X+2,$Y+2,$BigRadius,$R3,$G3,$B3); + else + { + $R3 = $this->Palette[$PaletteID]["R"]-20; if ( $R < 0 ) { $R = 0; } + $G3 = $this->Palette[$PaletteID]["G"]-20; if ( $G < 0 ) { $G = 0; } + $B3 = $this->Palette[$PaletteID]["B"]-20; if ( $B < 0 ) { $B = 0; } + $this->drawFilledCircle($X+2,$Y+2,$BigRadius,$R3,$G3,$B3); + } + } + + $this->drawFilledCircle($X+1,$Y+1,$BigRadius,$R,$G,$B); + + if ( $R2 !=-1 && $G2 !=-1 && $B2 !=-1 ) + $this->drawFilledCircle($X+1,$Y+1,$SmallRadius,$R2,$G2,$B2); + else + { + $R2 = $this->Palette[$PaletteID]["R"]+20; if ( $R > 255 ) { $R = 255; } + $G2 = $this->Palette[$PaletteID]["G"]+20; if ( $G > 255 ) { $G = 255; } + $B2 = $this->Palette[$PaletteID]["B"]+20; if ( $B > 255 ) { $B = 255; } + $this->drawFilledCircle($X+1,$Y+1,$SmallRadius,$R2,$G2,$B2); + } + } + } + + } + + /* This function draw an area between two series */ + function drawArea($Data,$Serie1,$Serie2,$R,$G,$B,$Alpha = 50) + { + /* Validate the Data and DataDescription array */ + $this->validateData("drawArea",$Data); + + $LayerWidth = $this->GArea_X2-$this->GArea_X1; + $LayerHeight = $this->GArea_Y2-$this->GArea_Y1; + + $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight); + $C_White =$this->AllocateColor($this->Layers[0],255,255,255); + imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White); + imagecolortransparent($this->Layers[0],$C_White); + + $C_Graph =$this->AllocateColor($this->Layers[0],$R,$G,$B); + + $XPos = $this->GAreaXOffset; + $LastXPos = -1; + foreach ( $Data as $Key => $Values ) + { + $Value1 = $Data[$Key][$Serie1]; + $Value2 = $Data[$Key][$Serie2]; + $YPos1 = $LayerHeight - (($Value1-$this->VMin) * $this->DivisionRatio); + $YPos2 = $LayerHeight - (($Value2-$this->VMin) * $this->DivisionRatio); + + if ( $LastXPos != -1 ) + { + $Points = ""; + $Points[] = $LastXPos; $Points[] = $LastYPos1; + $Points[] = $LastXPos; $Points[] = $LastYPos2; + $Points[] = $XPos; $Points[] = $YPos2; + $Points[] = $XPos; $Points[] = $YPos1; + + imagefilledpolygon($this->Layers[0],$Points,4,$C_Graph); + } + + $LastYPos1 = $YPos1; + $LastYPos2 = $YPos2; + $LastXPos = $XPos; + + $XPos = $XPos + $this->DivisionWidth; + } + + imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha); + imagedestroy($this->Layers[0]); + } + + + /* This function write the values of the specified series */ + function writeValues($Data,$DataDescription,$Series) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("writeValues",$DataDescription); + $this->validateData("writeValues",$Data); + + if ( !is_array($Series) ) { $Series = array($Series); } + + foreach($Series as $Key => $Serie) + { + $ID = 0; + foreach ( $DataDescription["Description"] as $keyI => $ValueI ) + { if ( $keyI == $Serie ) { $ColorID = $ID; }; $ID++; } + + $XPos = $this->GArea_X1 + $this->GAreaXOffset; + $XLast = -1; + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$Serie]) && is_numeric($Data[$Key][$Serie])) + { + $Value = $Data[$Key][$Serie]; + $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio); + + $Positions = imagettfbbox($this->FontSize,0,$this->FontName,$Value); + $Width = $Positions[2] - $Positions[6]; $XOffset = $XPos - ($Width/2); + $Height = $Positions[3] - $Positions[7]; $YOffset = $YPos - 4; + + $C_TextColor =$this->AllocateColor($this->Picture,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]); + imagettftext($this->Picture,$this->FontSize,0,$XOffset,$YOffset,$C_TextColor,$this->FontName,$Value); + } + $XPos = $XPos + $this->DivisionWidth; + } + + } + } + + /* This function draw a line graph */ + function drawLineGraph($Data,$DataDescription,$SerieName="") + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawLineGraph",$DataDescription); + $this->validateData("drawLineGraph",$Data); + + $GraphID = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + $ID = 0; + foreach ( $DataDescription["Description"] as $keyI => $ValueI ) + { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; } + + if ( $SerieName == "" || $SerieName == $ColName ) + { + $XPos = $this->GArea_X1 + $this->GAreaXOffset; + $XLast = -1; + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName])) + { + $Value = $Data[$Key][$ColName]; + $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio); + + /* Save point into the image map if option activated */ + if ( $this->BuildMap ) + $this->addToImageMap($XPos-3,$YPos-3,$XPos+3,$YPos+3,$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"Line"); + + if (!is_numeric($Value)) { $XLast = -1; } + if ( $XLast != -1 ) + $this->drawLine($XLast,$YLast,$XPos,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE); + + $XLast = $XPos; + $YLast = $YPos; + if (!is_numeric($Value)) { $XLast = -1; } + } + $XPos = $XPos + $this->DivisionWidth; + } + $GraphID++; + } + } + } + + /* This function draw a line graph */ + function drawXYGraph($Data,$DataDescription,$YSerieName,$XSerieName,$PaletteID=0) + { + $YLast = -1; $XLast = -1; + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$YSerieName]) && isset($Data[$Key][$XSerieName]) ) + { + $X = $Data[$Key][$XSerieName]; + $Y = $Data[$Key][$YSerieName]; + + $Y = $this->GArea_Y2 - (($Y-$this->VMin) * $this->DivisionRatio); + $X = $this->GArea_X1 + (($X-$this->VXMin) * $this->XDivisionRatio); + + if ($XLast != -1 && $YLast != -1) + { + $this->drawLine($XLast,$YLast,$X,$Y,$this->Palette[$PaletteID]["R"],$this->Palette[$PaletteID]["G"],$this->Palette[$PaletteID]["B"],TRUE); + } + + $XLast = $X; + $YLast = $Y; + } + } + } + + /* This function draw a cubic curve */ + function drawCubicCurve($Data,$DataDescription,$Accuracy=.1,$SerieName="") + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawCubicCurve",$DataDescription); + $this->validateData("drawCubicCurve",$Data); + + $GraphID = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + if ( $SerieName == "" || $SerieName == $ColName ) + { + $XIn = ""; $Yin = ""; $Yt = ""; $U = ""; + $XIn[0] = 0; $YIn[0] = 0; + + $ID = 0; + foreach ( $DataDescription["Description"] as $keyI => $ValueI ) + { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; } + + $Index = 1; + $XLast = -1; $Missing = ""; + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName]) ) + { + $Value = $Data[$Key][$ColName]; + $XIn[$Index] = $Index; + $YIn[$Index] = $Value; + if ( !is_numeric($Value) ) { $Missing[$Index] = TRUE; } + $Index++; + } + } + $Index--; + + $Yt[0] = 0; + $Yt[1] = 0; + $U[1] = 0; + for($i=2;$i<=$Index-1;$i++) + { + $Sig = ($XIn[$i] - $XIn[$i-1]) / ($XIn[$i+1] - $XIn[$i-1]); + $p = $Sig * $Yt[$i-1] + 2; + $Yt[$i] = ($Sig - 1) / $p; + $U[$i] = ($YIn[$i+1] - $YIn[$i]) / ($XIn[$i+1] - $XIn[$i]) - ($YIn[$i] - $YIn[$i-1]) / ($XIn[$i] - $XIn[$i-1]); + $U[$i] = (6 * $U[$i] / ($XIn[$i+1] - $XIn[$i-1]) - $Sig * $U[$i-1]) / $p; + } + + $qn = 0; + $un = 0; + $Yt[$Index] = ($un - $qn * $U[$Index-1]) / ($qn * $Yt[$Index-1] + 1); + + for($k=$Index-1;$k>=1;$k--) + $Yt[$k] = $Yt[$k] * $Yt[$k+1] + $U[$k]; + + $XPos = $this->GArea_X1 + $this->GAreaXOffset; + for($X=1;$X<=$Index;$X=$X+$Accuracy) + { + $klo = 1; + $khi = $Index; + $k = $khi - $klo; + while($k > 1) + { + $k = $khi - $klo; + If ( $XIn[$k] >= $X ) + $khi = $k; + else + $klo = $k; + } + $klo = $khi - 1; + + $h = $XIn[$khi] - $XIn[$klo]; + $a = ($XIn[$khi] - $X) / $h; + $b = ($X - $XIn[$klo]) / $h; + $Value = $a * $YIn[$klo] + $b * $YIn[$khi] + (($a*$a*$a - $a) * $Yt[$klo] + ($b*$b*$b - $b) * $Yt[$khi]) * ($h*$h) / 6; + + $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio); + + if ( $XLast != -1 && !isset($Missing[floor($X)]) && !isset($Missing[floor($X+1)]) ) + $this->drawLine($XLast,$YLast,$XPos,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE); + + $XLast = $XPos; + $YLast = $YPos; + $XPos = $XPos + $this->DivisionWidth * $Accuracy; + } + + // Add potentialy missing values + $XPos = $XPos - $this->DivisionWidth * $Accuracy; + if ( $XPos < ($this->GArea_X2 - $this->GAreaXOffset) ) + { + $YPos = $this->GArea_Y2 - (($YIn[$Index]-$this->VMin) * $this->DivisionRatio); + $this->drawLine($XLast,$YLast,$this->GArea_X2-$this->GAreaXOffset,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE); + } + + $GraphID++; + } + } + } + + /* This function draw a filled cubic curve */ + function drawFilledCubicCurve($Data,$DataDescription,$Accuracy=.1,$Alpha=100,$AroundZero=FALSE) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawFilledCubicCurve",$DataDescription); + $this->validateData("drawFilledCubicCurve",$Data); + + $LayerWidth = $this->GArea_X2-$this->GArea_X1; + $LayerHeight = $this->GArea_Y2-$this->GArea_Y1; + $YZero = $LayerHeight - ((0-$this->VMin) * $this->DivisionRatio); + if ( $YZero > $LayerHeight ) { $YZero = $LayerHeight; } + + $GraphID = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + $XIn = ""; $Yin = ""; $Yt = ""; $U = ""; + $XIn[0] = 0; $YIn[0] = 0; + + $ID = 0; + foreach ( $DataDescription["Description"] as $keyI => $ValueI ) + { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; } + + $Index = 1; + $XLast = -1; $Missing = ""; + foreach ( $Data as $Key => $Values ) + { + $Value = $Data[$Key][$ColName]; + $XIn[$Index] = $Index; + $YIn[$Index] = $Value; + if ( !is_numeric($Value) ) { $Missing[$Index] = TRUE; } + $Index++; + } + $Index--; + + $Yt[0] = 0; + $Yt[1] = 0; + $U[1] = 0; + for($i=2;$i<=$Index-1;$i++) + { + $Sig = ($XIn[$i] - $XIn[$i-1]) / ($XIn[$i+1] - $XIn[$i-1]); + $p = $Sig * $Yt[$i-1] + 2; + $Yt[$i] = ($Sig - 1) / $p; + $U[$i] = ($YIn[$i+1] - $YIn[$i]) / ($XIn[$i+1] - $XIn[$i]) - ($YIn[$i] - $YIn[$i-1]) / ($XIn[$i] - $XIn[$i-1]); + $U[$i] = (6 * $U[$i] / ($XIn[$i+1] - $XIn[$i-1]) - $Sig * $U[$i-1]) / $p; + } + + $qn = 0; + $un = 0; + $Yt[$Index] = ($un - $qn * $U[$Index-1]) / ($qn * $Yt[$Index-1] + 1); + + for($k=$Index-1;$k>=1;$k--) + $Yt[$k] = $Yt[$k] * $Yt[$k+1] + $U[$k]; + + $Points = ""; + $Points[] = $this->GAreaXOffset; + $Points[] = $LayerHeight; + + $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight); + $C_White =$this->AllocateColor($this->Layers[0],255,255,255); + imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White); + imagecolortransparent($this->Layers[0],$C_White); + + $YLast = NULL; + $XPos = $this->GAreaXOffset; $PointsCount = 2; + for($X=1;$X<=$Index;$X=$X+$Accuracy) + { + $klo = 1; + $khi = $Index; + $k = $khi - $klo; + while($k > 1) + { + $k = $khi - $klo; + If ( $XIn[$k] >= $X ) + $khi = $k; + else + $klo = $k; + } + $klo = $khi - 1; + + $h = $XIn[$khi] - $XIn[$klo]; + $a = ($XIn[$khi] - $X) / $h; + $b = ($X - $XIn[$klo]) / $h; + $Value = $a * $YIn[$klo] + $b * $YIn[$khi] + (($a*$a*$a - $a) * $Yt[$klo] + ($b*$b*$b - $b) * $Yt[$khi]) * ($h*$h) / 6; + + $YPos = $LayerHeight - (($Value-$this->VMin) * $this->DivisionRatio); + + if ( $YLast != NULL && $AroundZero && !isset($Missing[floor($X)]) && !isset($Missing[floor($X+1)])) + { + $aPoints = ""; + $aPoints[] = $XLast; + $aPoints[] = $YLast; + $aPoints[] = $XPos; + $aPoints[] = $YPos; + $aPoints[] = $XPos; + $aPoints[] = $YZero; + $aPoints[] = $XLast; + $aPoints[] = $YZero; + + $C_Graph =$this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]); + imagefilledpolygon($this->Layers[0],$aPoints,4,$C_Graph); + } + + if ( !isset($Missing[floor($X)]) || $YLast == NULL ) + { + $PointsCount++; + $Points[] = $XPos; + $Points[] = $YPos; + } + else + { + $PointsCount++; $Points[] = $XLast; $Points[] = $LayerHeight; + } + + $YLast = $YPos; $XLast = $XPos; + $XPos = $XPos + $this->DivisionWidth * $Accuracy; + } + + // Add potentialy missing values + $XPos = $XPos - $this->DivisionWidth * $Accuracy; + if ( $XPos < ($LayerWidth-$this->GAreaXOffset) ) + { + $YPos = $LayerHeight - (($YIn[$Index]-$this->VMin) * $this->DivisionRatio); + + if ( $YLast != NULL && $AroundZero ) + { + $aPoints = ""; + $aPoints[] = $XLast; + $aPoints[] = $YLast; + $aPoints[] = $LayerWidth-$this->GAreaXOffset; + $aPoints[] = $YPos; + $aPoints[] = $LayerWidth-$this->GAreaXOffset; + $aPoints[] = $YZero; + $aPoints[] = $XLast; + $aPoints[] = $YZero; + + $C_Graph =$this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]); + imagefilledpolygon($this->Layers[0],$aPoints,4,$C_Graph); + } + + if ( $YIn[$klo] != "" && $YIn[$khi] != "" || $YLast == NULL ) + { + $PointsCount++; + $Points[] = $LayerWidth-$this->GAreaXOffset; + $Points[] = $YPos; + } + } + + $Points[] = $LayerWidth-$this->GAreaXOffset; + $Points[] = $LayerHeight; + + if ( !$AroundZero ) + { + $C_Graph =$this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]); + imagefilledpolygon($this->Layers[0],$Points,$PointsCount,$C_Graph); + } + + imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha); + imagedestroy($this->Layers[0]); + + $this->drawCubicCurve($Data,$DataDescription,$Accuracy,$ColName); + + $GraphID++; + } + } + + /* This function draw a filled line graph */ + function drawFilledLineGraph($Data,$DataDescription,$Alpha=100,$AroundZero=FALSE) + { + $Empty = -2147483647; + + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawFilledLineGraph",$DataDescription); + $this->validateData("drawFilledLineGraph",$Data); + + $LayerWidth = $this->GArea_X2-$this->GArea_X1; + $LayerHeight = $this->GArea_Y2-$this->GArea_Y1; + + $GraphID = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + $ID = 0; + foreach ( $DataDescription["Description"] as $keyI => $ValueI ) + { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; } + + $aPoints = ""; + $aPoints[] = $this->GAreaXOffset; + $aPoints[] = $LayerHeight; + + $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight); + $C_White = $this->AllocateColor($this->Layers[0],255,255,255); + imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White); + imagecolortransparent($this->Layers[0],$C_White); + + $XPos = $this->GAreaXOffset; + $XLast = -1; $PointsCount = 2; + $YZero = $LayerHeight - ((0-$this->VMin) * $this->DivisionRatio); + if ( $YZero > $LayerHeight ) { $YZero = $LayerHeight; } + + $YLast = $Empty; + foreach ( $Data as $Key => $Values ) + { + $Value = $Data[$Key][$ColName]; + $YPos = $LayerHeight - (($Value-$this->VMin) * $this->DivisionRatio); + + /* Save point into the image map if option activated */ + if ( $this->BuildMap ) + $this->addToImageMap($XPos-3,$YPos-3,$XPos+3,$YPos+3,$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"FLine"); + + if ( !is_numeric($Value) ) + { + $PointsCount++; + $aPoints[] = $XLast; + $aPoints[] = $LayerHeight; + + $YLast = $Empty; + } + else + { + $PointsCount++; + if ( $YLast <> $Empty ) + { $aPoints[] = $XPos; $aPoints[] = $YPos; } + else + { $PointsCount++; $aPoints[] = $XPos; $aPoints[] = $LayerHeight; $aPoints[] = $XPos; $aPoints[] = $YPos; } + + if ($YLast <> $Empty && $AroundZero) + { + $Points = ""; + $Points[] = $XLast; $Points[] = $YLast; + $Points[] = $XPos; + $Points[] = $YPos; + $Points[] = $XPos; + $Points[] = $YZero; + $Points[] = $XLast; + $Points[] = $YZero; + + $C_Graph = $this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]); + imagefilledpolygon($this->Layers[0],$Points,4,$C_Graph); + } + $YLast = $YPos; + } + + $XLast = $XPos; + $XPos = $XPos + $this->DivisionWidth; + } + $aPoints[] = $LayerWidth - $this->GAreaXOffset; + $aPoints[] = $LayerHeight; + + if ( $AroundZero == FALSE ) + { + $C_Graph = $this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]); + imagefilledpolygon($this->Layers[0],$aPoints,$PointsCount,$C_Graph); + } + + imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha); + imagedestroy($this->Layers[0]); + $GraphID++; + $this->drawLineGraph($Data,$DataDescription,$ColName); + } + } + + /* This function draw a bar graph */ + function drawOverlayBarGraph($Data,$DataDescription,$Alpha=50) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawOverlayBarGraph",$DataDescription); + $this->validateData("drawOverlayBarGraph",$Data); + + $LayerWidth = $this->GArea_X2-$this->GArea_X1; + $LayerHeight = $this->GArea_Y2-$this->GArea_Y1; + + $GraphID = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + $ID = 0; + foreach ( $DataDescription["Description"] as $keyI => $ValueI ) + { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; } + + $this->Layers[$GraphID] = imagecreatetruecolor($LayerWidth,$LayerHeight); + $C_White = $this->AllocateColor($this->Layers[$GraphID],255,255,255); + $C_Graph = $this->AllocateColor($this->Layers[$GraphID],$this->Palette[$GraphID]["R"],$this->Palette[$GraphID]["G"],$this->Palette[$GraphID]["B"]); + imagefilledrectangle($this->Layers[$GraphID],0,0,$LayerWidth,$LayerHeight,$C_White); + imagecolortransparent($this->Layers[$GraphID],$C_White); + + $XWidth = $this->DivisionWidth / 4; + $XPos = $this->GAreaXOffset; + $YZero = $LayerHeight - ((0-$this->VMin) * $this->DivisionRatio); + $XLast = -1; $PointsCount = 2; + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName]) ) + { + $Value = $Data[$Key][$ColName]; + if ( is_numeric($Value) ) + { + $YPos = $LayerHeight - (($Value-$this->VMin) * $this->DivisionRatio); + + imagefilledrectangle($this->Layers[$GraphID],$XPos-$XWidth,$YPos,$XPos+$XWidth,$YZero,$C_Graph); + + $X1 = floor($XPos - $XWidth + $this->GArea_X1); $Y1 = floor($YPos+$this->GArea_Y1) + .2; + $X2 = floor($XPos + $XWidth + $this->GArea_X1); $Y2 = $this->GArea_Y2 - ((0-$this->VMin) * $this->DivisionRatio); + if ( $X1 <= $this->GArea_X1 ) { $X1 = $this->GArea_X1 + 1; } + if ( $X2 >= $this->GArea_X2 ) { $X2 = $this->GArea_X2 - 1; } + + /* Save point into the image map if option activated */ + if ( $this->BuildMap ) + $this->addToImageMap($X1,min($Y1,$Y2),$X2,max($Y1,$Y2),$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"oBar"); + + $this->drawLine($X1,$Y1,$X2,$Y1,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE); + } + } + $XPos = $XPos + $this->DivisionWidth; + } + + $GraphID++; + } + + for($i=0;$i<=($GraphID-1);$i++) + { + imagecopymerge($this->Picture,$this->Layers[$i],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha); + imagedestroy($this->Layers[$i]); + } + } + + /* This function draw a bar graph */ + function drawBarGraph($Data,$DataDescription,$Shadow=FALSE,$Alpha=100) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawBarGraph",$DataDescription); + $this->validateData("drawBarGraph",$Data); + + $GraphID = 0; + $Series = count($DataDescription["Values"]); + $SeriesWidth = $this->DivisionWidth / ($Series+1); + $SerieXOffset = $this->DivisionWidth / 2 - $SeriesWidth / 2; + + $YZero = $this->GArea_Y2 - ((0-$this->VMin) * $this->DivisionRatio); + if ( $YZero > $this->GArea_Y2 ) { $YZero = $this->GArea_Y2; } + + $SerieID = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + $ID = 0; + foreach ( $DataDescription["Description"] as $keyI => $ValueI ) + { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; } + + $XPos = $this->GArea_X1 + $this->GAreaXOffset - $SerieXOffset + $SeriesWidth * $SerieID; + $XLast = -1; + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName])) + { + if ( is_numeric($Data[$Key][$ColName]) ) + { + $Value = $Data[$Key][$ColName]; + $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio); + + /* Save point into the image map if option activated */ + if ( $this->BuildMap ) + { + $this->addToImageMap($XPos+1,min($YZero,$YPos),$XPos+$SeriesWidth-1,max($YZero,$YPos),$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"Bar"); + } + + if ( $Shadow && $Alpha == 100 ) + $this->drawRectangle($XPos+1,$YZero,$XPos+$SeriesWidth-1,$YPos,25,25,25,TRUE,$Alpha); + + $this->drawFilledRectangle($XPos+1,$YZero,$XPos+$SeriesWidth-1,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE,$Alpha); + } + } + $XPos = $XPos + $this->DivisionWidth; + } + $SerieID++; + } + } + + /* This function draw a stacked bar graph */ + function drawStackedBarGraph($Data,$DataDescription,$Alpha=50,$Contiguous=FALSE) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawBarGraph",$DataDescription); + $this->validateData("drawBarGraph",$Data); + + $GraphID = 0; + $Series = count($DataDescription["Values"]); + if ( $Contiguous ) + $SeriesWidth = $this->DivisionWidth; + else + $SeriesWidth = $this->DivisionWidth * .8; + + $YZero = $this->GArea_Y2 - ((0-$this->VMin) * $this->DivisionRatio); + if ( $YZero > $this->GArea_Y2 ) { $YZero = $this->GArea_Y2; } + + $SerieID = 0; $LastValue = ""; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + $ID = 0; + foreach ( $DataDescription["Description"] as $keyI => $ValueI ) + { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; } + + $XPos = $this->GArea_X1 + $this->GAreaXOffset - $SeriesWidth / 2; + $XLast = -1; + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName])) + { + if ( is_numeric($Data[$Key][$ColName]) ) + { + $Value = $Data[$Key][$ColName]; + + if ( isset($LastValue[$Key]) ) + { + $YPos = $this->GArea_Y2 - ((($Value+$LastValue[$Key])-$this->VMin) * $this->DivisionRatio); + $YBottom = $this->GArea_Y2 - (($LastValue[$Key]-$this->VMin) * $this->DivisionRatio); + $LastValue[$Key] += $Value; + } + else + { + $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio); + $YBottom = $YZero; + $LastValue[$Key] = $Value; + } + + /* Save point into the image map if option activated */ + if ( $this->BuildMap ) + $this->addToImageMap($XPos+1,min($YBottom,$YPos),$XPos+$SeriesWidth-1,max($YBottom,$YPos),$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"sBar"); + + $this->drawFilledRectangle($XPos+1,$YBottom,$XPos+$SeriesWidth-1,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE,$Alpha); + } + } + $XPos = $XPos + $this->DivisionWidth; + } + $SerieID++; + } + } + + /* This function draw a limits bar graphs */ + function drawLimitsGraph($Data,$DataDescription,$R=0,$G=0,$B=0) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawLimitsGraph",$DataDescription); + $this->validateData("drawLimitsGraph",$Data); + + $XWidth = $this->DivisionWidth / 4; + $XPos = $this->GArea_X1 + $this->GAreaXOffset; + + foreach ( $Data as $Key => $Values ) + { + $Min = $Data[$Key][$DataDescription["Values"][0]]; + $Max = $Data[$Key][$DataDescription["Values"][0]]; + $GraphID = 0; $MaxID = 0; $MinID = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + if ( isset($Data[$Key][$ColName]) ) + { + if ( $Data[$Key][$ColName] > $Max && is_numeric($Data[$Key][$ColName])) + { $Max = $Data[$Key][$ColName]; $MaxID = $GraphID; } + } + if ( isset($Data[$Key][$ColName]) && is_numeric($Data[$Key][$ColName])) + { + if ( $Data[$Key][$ColName] < $Min ) + { $Min = $Data[$Key][$ColName]; $MinID = $GraphID; } + $GraphID++; + } + } + + $YPos = $this->GArea_Y2 - (($Max-$this->VMin) * $this->DivisionRatio); + $X1 = floor($XPos - $XWidth); $Y1 = floor($YPos) - .2; + $X2 = floor($XPos + $XWidth); + if ( $X1 <= $this->GArea_X1 ) { $X1 = $this->GArea_X1 + 1; } + if ( $X2 >= $this->GArea_X2 ) { $X2 = $this->GArea_X2 - 1; } + + $YPos = $this->GArea_Y2 - (($Min-$this->VMin) * $this->DivisionRatio); + $Y2 = floor($YPos) + .2; + + $this->drawLine(floor($XPos)-.2,$Y1+1,floor($XPos)-.2,$Y2-1,$R,$G,$B,TRUE); + $this->drawLine(floor($XPos)+.2,$Y1+1,floor($XPos)+.2,$Y2-1,$R,$G,$B,TRUE); + $this->drawLine($X1,$Y1,$X2,$Y1,$this->Palette[$MaxID]["R"],$this->Palette[$MaxID]["G"],$this->Palette[$MaxID]["B"],FALSE); + $this->drawLine($X1,$Y2,$X2,$Y2,$this->Palette[$MinID]["R"],$this->Palette[$MinID]["G"],$this->Palette[$MinID]["B"],FALSE); + + $XPos = $XPos + $this->DivisionWidth; + } + } + + /* This function draw radar axis centered on the graph area */ + function drawRadarAxis($Data,$DataDescription,$Mosaic=TRUE,$BorderOffset=10,$A_R=60,$A_G=60,$A_B=60,$S_R=200,$S_G=200,$S_B=200,$MaxValue=-1) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawRadarAxis",$DataDescription); + $this->validateData("drawRadarAxis",$Data); + + $C_TextColor = $this->AllocateColor($this->Picture,$A_R,$A_G,$A_B); + + /* Draw radar axis */ + $Points = count($Data); + $Radius = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 - $BorderOffset; + $XCenter = ( $this->GArea_X2 - $this->GArea_X1 ) / 2 + $this->GArea_X1; + $YCenter = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 + $this->GArea_Y1; + + /* Search for the max value */ + if ( $MaxValue == -1 ) + { + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName])) + if ( $Data[$Key][$ColName] > $MaxValue ) { $MaxValue = $Data[$Key][$ColName]; } + } + } + } + + /* Draw the mosaic */ + if ( $Mosaic ) + { + $RadiusScale = $Radius / $MaxValue; + for ( $t=1; $t<=$MaxValue-1; $t++) + { + $TRadius = $RadiusScale * $t; + $LastX1 = -1; + + for ( $i=0; $i<=$Points; $i++) + { + $Angle = -90 + $i * 360/$Points; + $X1 = cos($Angle * 3.1418 / 180 ) * $TRadius + $XCenter; + $Y1 = sin($Angle * 3.1418 / 180 ) * $TRadius + $YCenter; + $X2 = cos($Angle * 3.1418 / 180 ) * ($TRadius+$RadiusScale) + $XCenter; + $Y2 = sin($Angle * 3.1418 / 180 ) * ($TRadius+$RadiusScale) + $YCenter; + + if ( $t % 2 == 1 && $LastX1 != -1) + { + $Plots = ""; + $Plots[] = $X1; $Plots[] = $Y1; + $Plots[] = $X2; $Plots[] = $Y2; + $Plots[] = $LastX2; $Plots[] = $LastY2; + $Plots[] = $LastX1; $Plots[] = $LastY1; + + $C_Graph = $this->AllocateColor($this->Picture,250,250,250); + imagefilledpolygon($this->Picture,$Plots,(count($Plots)+1)/2,$C_Graph); + } + + $LastX1 = $X1; $LastY1= $Y1; + $LastX2 = $X2; $LastY2= $Y2; + } + } + } + + + /* Draw the spider web */ + for ( $t=1; $t<=$MaxValue; $t++) + { + $TRadius = ( $Radius / $MaxValue ) * $t; + $LastX = -1; + + for ( $i=0; $i<=$Points; $i++) + { + $Angle = -90 + $i * 360/$Points; + $X = cos($Angle * 3.1418 / 180 ) * $TRadius + $XCenter; + $Y = sin($Angle * 3.1418 / 180 ) * $TRadius + $YCenter; + + if ( $LastX != -1 ) + $this->drawDottedLine($LastX,$LastY,$X,$Y,4,$S_R,$S_G,$S_B); + + $LastX = $X; $LastY= $Y; + } + } + + /* Draw the axis */ + for ( $i=0; $i<=$Points; $i++) + { + $Angle = -90 + $i * 360/$Points; + $X = cos($Angle * 3.1418 / 180 ) * $Radius + $XCenter; + $Y = sin($Angle * 3.1418 / 180 ) * $Radius + $YCenter; + + $this->drawLine($XCenter,$YCenter,$X,$Y,$A_R,$A_G,$A_B); + + $XOffset = 0; $YOffset = 0; + if (isset($Data[$i][$DataDescription["Position"]])) + { + $Label = $Data[$i][$DataDescription["Position"]]; + + $Positions = imagettfbbox($this->FontSize,0,$this->FontName,$Label); + $Width = $Positions[2] - $Positions[6]; + $Height = $Positions[3] - $Positions[7]; + + if ( $Angle >= 0 && $Angle <= 90 ) + $YOffset = $Height; + + if ( $Angle > 90 && $Angle <= 180 ) + { $YOffset = $Height; $XOffset = -$Width; } + + if ( $Angle > 180 && $Angle <= 270 ) + { $XOffset = -$Width; } + + imagettftext($this->Picture,$this->FontSize,0,$X+$XOffset,$Y+$YOffset,$C_TextColor,$this->FontName,$Label); + } + } + + /* Write the values */ + for ( $t=1; $t<=$MaxValue; $t++) + { + $TRadius = ( $Radius / $MaxValue ) * $t; + + $Angle = -90 + 360 / $Points; + $X1 = $XCenter; + $Y1 = $YCenter - $TRadius; + $X2 = cos($Angle * 3.1418 / 180 ) * $TRadius + $XCenter; + $Y2 = sin($Angle * 3.1418 / 180 ) * $TRadius + $YCenter; + + $XPos = floor(($X2-$X1)/2) + $X1; + $YPos = floor(($Y2-$Y1)/2) + $Y1; + + $Positions = imagettfbbox($this->FontSize,0,$this->FontName,$t); + $X = $XPos - ( $X+$Positions[2] - $X+$Positions[6] ) / 2; + $Y = $YPos + $this->FontSize; + + $this->drawFilledRoundedRectangle($X+$Positions[6]-2,$Y+$Positions[7]-1,$X+$Positions[2]+4,$Y+$Positions[3]+1,2,240,240,240); + $this->drawRoundedRectangle($X+$Positions[6]-2,$Y+$Positions[7]-1,$X+$Positions[2]+4,$Y+$Positions[3]+1,2,220,220,220); + imagettftext($this->Picture,$this->FontSize,0,$X,$Y,$C_TextColor,$this->FontName,$t); + } + } + + /* This function draw a radar graph centered on the graph area */ + function drawRadar($Data,$DataDescription,$BorderOffset=10,$MaxValue=-1) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawRadar",$DataDescription); + $this->validateData("drawRadar",$Data); + + $Points = count($Data); + $Radius = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 - $BorderOffset; + $XCenter = ( $this->GArea_X2 - $this->GArea_X1 ) / 2 + $this->GArea_X1; + $YCenter = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 + $this->GArea_Y1; + + /* Search for the max value */ + if ( $MaxValue == -1 ) + { + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName])) + if ( $Data[$Key][$ColName] > $MaxValue ) { $MaxValue = $Data[$Key][$ColName]; } + } + } + } + + $GraphID = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + $ID = 0; + foreach ( $DataDescription["Description"] as $keyI => $ValueI ) + { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; } + + $Angle = -90; + $XLast = -1; + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName])) + { + $Value = $Data[$Key][$ColName]; + $Strength = ( $Radius / $MaxValue ) * $Value; + + $XPos = cos($Angle * 3.1418 / 180 ) * $Strength + $XCenter; + $YPos = sin($Angle * 3.1418 / 180 ) * $Strength + $YCenter; + + if ( $XLast != -1 ) + $this->drawLine($XLast,$YLast,$XPos,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]); + + if ( $XLast == -1 ) + { $FirstX = $XPos; $FirstY = $YPos; } + + $Angle = $Angle + (360/$Points); + $XLast = $XPos; + $YLast = $YPos; + } + } + $this->drawLine($XPos,$YPos,$FirstX,$FirstY,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]); + $GraphID++; + } + } + + /* This function draw a radar graph centered on the graph area */ + function drawFilledRadar($Data,$DataDescription,$Alpha=50,$BorderOffset=10,$MaxValue=-1) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawFilledRadar",$DataDescription); + $this->validateData("drawFilledRadar",$Data); + + $Points = count($Data); + $LayerWidth = $this->GArea_X2-$this->GArea_X1; + $LayerHeight = $this->GArea_Y2-$this->GArea_Y1; + $Radius = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 - $BorderOffset; + $XCenter = ( $this->GArea_X2 - $this->GArea_X1 ) / 2; + $YCenter = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2; + + /* Search for the max value */ + if ( $MaxValue == -1 ) + { + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName])) + if ( $Data[$Key][$ColName] > $MaxValue && is_numeric($Data[$Key][$ColName])) { $MaxValue = $Data[$Key][$ColName]; } + } + } + } + + $GraphID = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + $ID = 0; + foreach ( $DataDescription["Description"] as $keyI => $ValueI ) + { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; } + + $Angle = -90; + $XLast = -1; + $Plots = ""; + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName])) + { + $Value = $Data[$Key][$ColName]; + if ( !is_numeric($Value) ) { $Value = 0; } + $Strength = ( $Radius / $MaxValue ) * $Value; + + $XPos = cos($Angle * 3.1418 / 180 ) * $Strength + $XCenter; + $YPos = sin($Angle * 3.1418 / 180 ) * $Strength + $YCenter; + + $Plots[] = $XPos; + $Plots[] = $YPos; + + $Angle = $Angle + (360/$Points); + $XLast = $XPos; + $YLast = $YPos; + } + } + + if (isset($Plots[0])) + { + $Plots[] = $Plots[0]; + $Plots[] = $Plots[1]; + + $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight); + $C_White = $this->AllocateColor($this->Layers[0],255,255,255); + imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White); + imagecolortransparent($this->Layers[0],$C_White); + + $C_Graph = $this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]); + imagefilledpolygon($this->Layers[0],$Plots,(count($Plots)+1)/2,$C_Graph); + + imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha); + imagedestroy($this->Layers[0]); + + for($i=0;$i<=count($Plots)-4;$i=$i+2) + $this->drawLine($Plots[$i]+$this->GArea_X1,$Plots[$i+1]+$this->GArea_Y1,$Plots[$i+2]+$this->GArea_X1,$Plots[$i+3]+$this->GArea_Y1,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]); + } + + $GraphID++; + } + } + + /* This function draw a flat pie chart */ + function drawBasicPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$R=255,$G=255,$B=255,$Decimals=0) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawBasicPieGraph",$DataDescription,FALSE); + $this->validateData("drawBasicPieGraph",$Data); + + /* Determine pie sum */ + $Series = 0; $PieSum = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + if ( $ColName != $DataDescription["Position"] ) + { + $Series++; + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName])) + $PieSum = $PieSum + $Data[$Key][$ColName]; $iValues[] = $Data[$Key][$ColName]; $iLabels[] = $Data[$Key][$DataDescription["Position"]]; + } + } + } + + /* Validate serie */ + if ( $Series != 1 ) + RaiseFatal("Pie chart can only accept one serie of data."); + + $SpliceRatio = 360 / $PieSum; + $SplicePercent = 100 / $PieSum; + + /* Calculate all polygons */ + $Angle = 0; $TopPlots = ""; + foreach($iValues as $Key => $Value) + { + $TopPlots[$Key][] = $XPos; + $TopPlots[$Key][] = $YPos; + + /* Process labels position & size */ + $Caption = ""; + if ( !($DrawLabels == PIE_NOLABEL) ) + { + $TAngle = $Angle+($Value*$SpliceRatio/2); + if ($DrawLabels == PIE_PERCENTAGE) + $Caption = (round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%"; + elseif ($DrawLabels == PIE_LABELS) + $Caption = $iLabels[$Key]; + elseif ($DrawLabels == PIE_PERCENTAGE_LABEL) + $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%"; + elseif ($DrawLabels == PIE_PERCENTAGE_LABEL) + $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%"; + + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Caption); + $TextWidth = $Position[2]-$Position[0]; + $TextHeight = abs($Position[1])+abs($Position[3]); + + $TX = cos(($TAngle) * 3.1418 / 180 ) * ($Radius+10) + $XPos; + + if ( $TAngle > 0 && $TAngle < 180 ) + $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+10) + $YPos + 4; + else + $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+4) + $YPos - ($TextHeight/2); + + if ( $TAngle > 90 && $TAngle < 270 ) + $TX = $TX - $TextWidth; + + $C_TextColor = $this->AllocateColor($this->Picture,70,70,70); + imagettftext($this->Picture,$this->FontSize,0,$TX,$TY,$C_TextColor,$this->FontName,$Caption); + } + + /* Process pie slices */ + for($iAngle=$Angle;$iAngle<=$Angle+$Value*$SpliceRatio;$iAngle=$iAngle+.5) + { + $TopX = cos($iAngle * 3.1418 / 180 ) * $Radius + $XPos; + $TopY = sin($iAngle * 3.1418 / 180 ) * $Radius + $YPos; + + $TopPlots[$Key][] = $TopX; + $TopPlots[$Key][] = $TopY; + } + + $TopPlots[$Key][] = $XPos; + $TopPlots[$Key][] = $YPos; + + $Angle = $iAngle; + } + $PolyPlots = $TopPlots; + + /* Set array values type to float --- PHP Bug with imagefilledpolygon casting to integer */ + foreach ($TopPlots as $Key => $Value) + { foreach ($TopPlots[$Key] as $Key2 => $Value2) { settype($TopPlots[$Key][$Key2],"float"); } } + + /* Draw Top polygons */ + foreach ($PolyPlots as $Key => $Value) + { + $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"]); + imagefilledpolygon($this->Picture,$PolyPlots[$Key],(count($PolyPlots[$Key])+1)/2,$C_GraphLo); + } + + $this->drawCircle($XPos-.5,$YPos-.5,$Radius,$R,$G,$B); + $this->drawCircle($XPos-.5,$YPos-.5,$Radius+.5,$R,$G,$B); + + /* Draw Top polygons */ + foreach ($TopPlots as $Key => $Value) + { + for($j=0;$j<=count($TopPlots[$Key])-4;$j=$j+2) + $this->drawLine($TopPlots[$Key][$j],$TopPlots[$Key][$j+1],$TopPlots[$Key][$j+2],$TopPlots[$Key][$j+3],$R,$G,$B); + } + } + + function drawFlatPieGraphWithShadow($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$SpliceDistance=0,$Decimals=0) + { + $this->drawFlatPieGraph($Data,$DataDescription,$XPos+$this->ShadowXDistance,$YPos+$this->ShadowYDistance,$Radius,PIE_NOLABEL,$SpliceDistance,$Decimals,TRUE); + $this->drawFlatPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius,$DrawLabels,$SpliceDistance,$Decimals,FALSE); + } + + /* This function draw a flat pie chart */ + function drawFlatPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$SpliceDistance=0,$Decimals=0,$AllBlack=FALSE) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawFlatPieGraph",$DataDescription,FALSE); + $this->validateData("drawFlatPieGraph",$Data); + + $ShadowStatus = $this->ShadowActive ; $this->ShadowActive = FALSE; + + /* Determine pie sum */ + $Series = 0; $PieSum = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + if ( $ColName != $DataDescription["Position"] ) + { + $Series++; + foreach ( $Data as $Key => $Values ) + { + if ( isset($Data[$Key][$ColName])) + $PieSum = $PieSum + $Data[$Key][$ColName]; $iValues[] = $Data[$Key][$ColName]; $iLabels[] = $Data[$Key][$DataDescription["Position"]]; + } + } + } + + /* Validate serie */ + if ( $Series != 1 ) + { + RaiseFatal("Pie chart can only accept one serie of data."); + return(0); + } + + $SpliceRatio = 360 / $PieSum; + $SplicePercent = 100 / $PieSum; + + /* Calculate all polygons */ + $Angle = 0; $TopPlots = ""; + foreach($iValues as $Key => $Value) + { + $XOffset = cos(($Angle+($Value/2*$SpliceRatio)) * 3.1418 / 180 ) * $SpliceDistance; + $YOffset = sin(($Angle+($Value/2*$SpliceRatio)) * 3.1418 / 180 ) * $SpliceDistance; + + $TopPlots[$Key][] = round($XPos + $XOffset); + $TopPlots[$Key][] = round($YPos + $YOffset); + + if ( $AllBlack ) + { $Rc = $this->ShadowRColor; $Gc = $this->ShadowGColor; $Bc = $this->ShadowBColor; } + else + { $Rc = $this->Palette[$Key]["R"]; $Gc = $this->Palette[$Key]["G"]; $Bc = $this->Palette[$Key]["B"]; } + + $XLineLast = ""; $YLineLast = ""; + + /* Process labels position & size */ + $Caption = ""; + if ( !($DrawLabels == PIE_NOLABEL) ) + { + $TAngle = $Angle+($Value*$SpliceRatio/2); + if ($DrawLabels == PIE_PERCENTAGE) + $Caption = (round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%"; + elseif ($DrawLabels == PIE_LABELS) + $Caption = $iLabels[$Key]; + elseif ($DrawLabels == PIE_PERCENTAGE_LABEL) + $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%"; + elseif ($DrawLabels == PIE_PERCENTAGE_LABEL) + $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%"; + + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Caption); + $TextWidth = $Position[2]-$Position[0]; + $TextHeight = abs($Position[1])+abs($Position[3]); + + $TX = cos(($TAngle) * 3.1418 / 180 ) * ($Radius+10+$SpliceDistance) + $XPos; + + if ( $TAngle > 0 && $TAngle < 180 ) + $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+10+$SpliceDistance) + $YPos + 4; + else + $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+$SpliceDistance+4) + $YPos - ($TextHeight/2); + + if ( $TAngle > 90 && $TAngle < 270 ) + $TX = $TX - $TextWidth; + + $C_TextColor = $this->AllocateColor($this->Picture,70,70,70); + imagettftext($this->Picture,$this->FontSize,0,$TX,$TY,$C_TextColor,$this->FontName,$Caption); + } + + /* Process pie slices */ + if ( !$AllBlack ) + $LineColor = $this->AllocateColor($this->Picture,$Rc,$Gc,$Bc); + else + $LineColor = $this->AllocateColor($this->Picture,$Rc,$Gc,$Bc); + + $XLineLast = ""; $YLineLast = ""; + for($iAngle=$Angle;$iAngle<=$Angle+$Value*$SpliceRatio;$iAngle=$iAngle+.5) + { + $PosX = cos($iAngle * 3.1418 / 180 ) * $Radius + $XPos + $XOffset; + $PosY = sin($iAngle * 3.1418 / 180 ) * $Radius + $YPos + $YOffset; + + $TopPlots[$Key][] = round($PosX); $TopPlots[$Key][] = round($PosY); + + if ( $iAngle == $Angle || $iAngle == $Angle+$Value*$SpliceRatio || $iAngle +.5 > $Angle+$Value*$SpliceRatio) + $this->drawLine($XPos+$XOffset,$YPos+$YOffset,$PosX,$PosY,$Rc,$Gc,$Bc); + + if ( $XLineLast != "" ) + $this->drawLine($XLineLast,$YLineLast,$PosX,$PosY,$Rc,$Gc,$Bc); + + $XLineLast = $PosX; $YLineLast = $PosY; + } + + $TopPlots[$Key][] = round($XPos + $XOffset); $TopPlots[$Key][] = round($YPos + $YOffset); + + $Angle = $iAngle; + } + $PolyPlots = $TopPlots; + + /* Draw Top polygons */ + foreach ($PolyPlots as $Key => $Value) + { + if ( !$AllBlack ) + $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"]); + else + $C_GraphLo = $this->AllocateColor($this->Picture,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor); + + imagefilledpolygon($this->Picture,$PolyPlots[$Key],(count($PolyPlots[$Key])+1)/2,$C_GraphLo); + } + $this->ShadowActive = $ShadowStatus; + } + + /* This function draw a pseudo-3D pie chart */ + function drawPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$EnhanceColors=TRUE,$Skew=60,$SpliceHeight=20,$SpliceDistance=0,$Decimals=0) + { + /* Validate the Data and DataDescription array */ + $this->validateDataDescription("drawPieGraph",$DataDescription,FALSE); + $this->validateData("drawPieGraph",$Data); + + /* Determine pie sum */ + $Series = 0; $PieSum = 0; $rPieSum = 0; + foreach ( $DataDescription["Values"] as $Key2 => $ColName ) + { + if ( $ColName != $DataDescription["Position"] ) + { + $Series++; + foreach ( $Data as $Key => $Values ) + if ( isset($Data[$Key][$ColName])) + { + if ( $Data[$Key][$ColName] == 0 ) + { $iValues[] = 0; $rValues[] = 0; $iLabels[] = $Data[$Key][$DataDescription["Position"]]; } + // Removed : $PieSum++; $rValues[] = 1; + else + { $PieSum += $Data[$Key][$ColName]; $iValues[] = $Data[$Key][$ColName]; $iLabels[] = $Data[$Key][$DataDescription["Position"]]; $rValues[] = $Data[$Key][$ColName]; $rPieSum += $Data[$Key][$ColName];} + } + } + } + + /* Validate serie */ + if ( $Series != 1 ) + RaiseFatal("Pie chart can only accept one serie of data."); + + $SpliceDistanceRatio = $SpliceDistance; + $SkewHeight = ($Radius * $Skew) / 100; + $SpliceRatio = (360 - $SpliceDistanceRatio * count($iValues) ) / $PieSum; + $SplicePercent = 100 / $PieSum; + $rSplicePercent = 100 / $rPieSum; + + /* Calculate all polygons */ + $Angle = 0; $CDev = 5; + $TopPlots = ""; $BotPlots = ""; + $aTopPlots = ""; $aBotPlots = ""; + foreach($iValues as $Key => $Value) + { + $XCenterPos = cos(($Angle-$CDev+($Value*$SpliceRatio+$SpliceDistanceRatio)/2) * 3.1418 / 180 ) * $SpliceDistance + $XPos; + $YCenterPos = sin(($Angle-$CDev+($Value*$SpliceRatio+$SpliceDistanceRatio)/2) * 3.1418 / 180 ) * $SpliceDistance + $YPos; + $XCenterPos2 = cos(($Angle+$CDev+($Value*$SpliceRatio+$SpliceDistanceRatio)/2) * 3.1418 / 180 ) * $SpliceDistance + $XPos; + $YCenterPos2 = sin(($Angle+$CDev+($Value*$SpliceRatio+$SpliceDistanceRatio)/2) * 3.1418 / 180 ) * $SpliceDistance + $YPos; + + $TopPlots[$Key][] = round($XCenterPos); $BotPlots[$Key][] = round($XCenterPos); + $TopPlots[$Key][] = round($YCenterPos); $BotPlots[$Key][] = round($YCenterPos + $SpliceHeight); + $aTopPlots[$Key][] = $XCenterPos; $aBotPlots[$Key][] = $XCenterPos; + $aTopPlots[$Key][] = $YCenterPos; $aBotPlots[$Key][] = $YCenterPos + $SpliceHeight; + + /* Process labels position & size */ + $Caption = ""; + if ( !($DrawLabels == PIE_NOLABEL) ) + { + $TAngle = $Angle+($Value*$SpliceRatio/2); + if ($DrawLabels == PIE_PERCENTAGE) + $Caption = (round($rValues[$Key] * pow(10,$Decimals) * $rSplicePercent)/pow(10,$Decimals))."%"; + elseif ($DrawLabels == PIE_LABELS) + $Caption = $iLabels[$Key]; + elseif ($DrawLabels == PIE_PERCENTAGE_LABEL) + $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%"; + + $Position = imageftbbox($this->FontSize,0,$this->FontName,$Caption); + $TextWidth = $Position[2]-$Position[0]; + $TextHeight = abs($Position[1])+abs($Position[3]); + + $TX = cos(($TAngle) * 3.1418 / 180 ) * ($Radius + 10)+ $XPos; + + if ( $TAngle > 0 && $TAngle < 180 ) + $TY = sin(($TAngle) * 3.1418 / 180 ) * ($SkewHeight + 10) + $YPos + $SpliceHeight + 4; + else + $TY = sin(($TAngle) * 3.1418 / 180 ) * ($SkewHeight + 4) + $YPos - ($TextHeight/2); + + if ( $TAngle > 90 && $TAngle < 270 ) + $TX = $TX - $TextWidth; + + $C_TextColor = $this->AllocateColor($this->Picture,70,70,70); + imagettftext($this->Picture,$this->FontSize,0,$TX,$TY,$C_TextColor,$this->FontName,$Caption); + } + + /* Process pie slices */ + for($iAngle=$Angle;$iAngle<=$Angle+$Value*$SpliceRatio;$iAngle=$iAngle+.5) + { + $TopX = cos($iAngle * 3.1418 / 180 ) * $Radius + $XPos; + $TopY = sin($iAngle * 3.1418 / 180 ) * $SkewHeight + $YPos; + + $TopPlots[$Key][] = round($TopX); $BotPlots[$Key][] = round($TopX); + $TopPlots[$Key][] = round($TopY); $BotPlots[$Key][] = round($TopY + $SpliceHeight); + $aTopPlots[$Key][] = $TopX; $aBotPlots[$Key][] = $TopX; + $aTopPlots[$Key][] = $TopY; $aBotPlots[$Key][] = $TopY + $SpliceHeight; + } + + $TopPlots[$Key][] = round($XCenterPos2); $BotPlots[$Key][] = round($XCenterPos2); + $TopPlots[$Key][] = round($YCenterPos2); $BotPlots[$Key][] = round($YCenterPos2 + $SpliceHeight); + $aTopPlots[$Key][] = $XCenterPos2; $aBotPlots[$Key][] = $XCenterPos2; + $aTopPlots[$Key][] = $YCenterPos2; $aBotPlots[$Key][] = $YCenterPos2 + $SpliceHeight; + + $Angle = $iAngle + $SpliceDistanceRatio; + } + + /* Draw Bottom polygons */ + foreach($iValues as $Key => $Value) + { + $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"],-20); + imagefilledpolygon($this->Picture,$BotPlots[$Key],(count($BotPlots[$Key])+1)/2,$C_GraphLo); + + if ( $EnhanceColors ) { $En = -10; } else { $En = 0; } + + for($j=0;$j<=count($aBotPlots[$Key])-4;$j=$j+2) + $this->drawLine($aBotPlots[$Key][$j],$aBotPlots[$Key][$j+1],$aBotPlots[$Key][$j+2],$aBotPlots[$Key][$j+3],$this->Palette[$Key]["R"]+$En,$this->Palette[$Key]["G"]+$En,$this->Palette[$Key]["B"]+$En); + } + + /* Draw pie layers */ + if ( $EnhanceColors ) { $ColorRatio = 30 / $SpliceHeight; } else { $ColorRatio = 25 / $SpliceHeight; } + for($i=$SpliceHeight-1;$i>=1;$i--) + { + foreach($iValues as $Key => $Value) + { + $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"],-10); + $Plots = ""; $Plot = 0; + foreach($TopPlots[$Key] as $Key2 => $Value2) + { + $Plot++; + if ( $Plot % 2 == 1 ) + $Plots[] = $Value2; + else + $Plots[] = $Value2+$i; + } + imagefilledpolygon($this->Picture,$Plots,(count($Plots)+1)/2,$C_GraphLo); + + $Index = count($Plots); + if ($EnhanceColors ) {$ColorFactor = -20 + ($SpliceHeight - $i) * $ColorRatio; } else { $ColorFactor = 0; } + + $this->drawAntialiasPixel($Plots[0],$Plots[1],$this->Palette[$Key]["R"]+$ColorFactor,$this->Palette[$Key]["G"]+$ColorFactor,$this->Palette[$Key]["B"]+$ColorFactor); + $this->drawAntialiasPixel($Plots[2],$Plots[3],$this->Palette[$Key]["R"]+$ColorFactor,$this->Palette[$Key]["G"]+$ColorFactor,$this->Palette[$Key]["B"]+$ColorFactor); + $this->drawAntialiasPixel($Plots[$Index-4],$Plots[$Index-3],$this->Palette[$Key]["R"]+$ColorFactor,$this->Palette[$Key]["G"]+$ColorFactor,$this->Palette[$Key]["B"]+$ColorFactor); + } + } + + /* Draw Top polygons */ + for($Key=count($iValues)-1;$Key>=0;$Key--) + { + $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"]); + imagefilledpolygon($this->Picture,$TopPlots[$Key],(count($TopPlots[$Key])+1)/2,$C_GraphLo); + + if ( $EnhanceColors ) { $En = 10; } else { $En = 0; } + for($j=0;$j<=count($aTopPlots[$Key])-4;$j=$j+2) + $this->drawLine($aTopPlots[$Key][$j],$aTopPlots[$Key][$j+1],$aTopPlots[$Key][$j+2],$aTopPlots[$Key][$j+3],$this->Palette[$Key]["R"]+$En,$this->Palette[$Key]["G"]+$En,$this->Palette[$Key]["B"]+$En); + } + } + + /* This function can be used to set the background color */ + function drawBackground($R,$G,$B) + { + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + $C_Background = $this->AllocateColor($this->Picture,$R,$G,$B); + imagefilledrectangle($this->Picture,0,0,$this->XSize,$this->YSize,$C_Background); + } + + /* This function can be used to set the background color */ + function drawGraphAreaGradient($R,$G,$B,$Decay,$Target=TARGET_GRAPHAREA) + { + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + if ( $Target == TARGET_GRAPHAREA ) { $X1 = $this->GArea_X1+1; $X2 = $this->GArea_X2-1; $Y1 = $this->GArea_Y1+1; $Y2 = $this->GArea_Y2; } + if ( $Target == TARGET_BACKGROUND ) { $X1 = 0; $X2 = $this->XSize; $Y1 = 0; $Y2 = $this->YSize; } + + /* Positive gradient */ + if ( $Decay > 0 ) + { + $YStep = ($Y2 - $Y1 - 2) / $Decay; + for($i=0;$i<=$Decay;$i++) + { + $R-=1;$G-=1;$B-=1; + $Yi1 = $Y1 + ( $i * $YStep ); + $Yi2 = ceil( $Yi1 + ( $i * $YStep ) + $YStep ); + if ( $Yi2 >= $Yi2 ) { $Yi2 = $Y2-1; } + + $C_Background = $this->AllocateColor($this->Picture,$R,$G,$B); + imagefilledrectangle($this->Picture,$X1,$Yi1,$X2,$Yi2,$C_Background); + } + } + + /* Negative gradient */ + if ( $Decay < 0 ) + { + $YStep = ($Y2 - $Y1 - 2) / -$Decay; + $Yi1 = $Y1; $Yi2 = $Y1+$YStep; + for($i=-$Decay;$i>=0;$i--) + { + $R+=1;$G+=1;$B+=1; + $C_Background = $this->AllocateColor($this->Picture,$R,$G,$B); + imagefilledrectangle($this->Picture,$X1,$Yi1,$X2,$Yi2,$C_Background); + + $Yi1+= $YStep; + $Yi2+= $YStep; + if ( $Yi2 >= $Yi2 ) { $Yi2 = $Y2-1; } + } + } + } + + /* This function create a rectangle with antialias */ + function drawRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B) + { + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B); + + $X1=$X1-.2;$Y1=$Y1-.2; + $X2=$X2+.2;$Y2=$Y2+.2; + $this->drawLine($X1,$Y1,$X2,$Y1,$R,$G,$B); + $this->drawLine($X2,$Y1,$X2,$Y2,$R,$G,$B); + $this->drawLine($X2,$Y2,$X1,$Y2,$R,$G,$B); + $this->drawLine($X1,$Y2,$X1,$Y1,$R,$G,$B); + } + + /* This function create a filled rectangle with antialias */ + function drawFilledRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B,$DrawBorder=TRUE,$Alpha=100,$NoFallBack=FALSE) + { + if ( $X2 < $X1 ) { list($X1, $X2) = array($X2, $X1); } + if ( $Y2 < $Y1 ) { list($Y1, $Y2) = array($Y2, $Y1); } + + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + if ( $Alpha == 100 ) + { + /* Process shadows */ + if ( $this->ShadowActive && !$NoFallBack ) + { + $this->drawFilledRectangle($X1+$this->ShadowXDistance,$Y1+$this->ShadowYDistance,$X2+$this->ShadowXDistance,$Y2+$this->ShadowYDistance,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,FALSE,$this->ShadowAlpha,TRUE); + if ( $this->ShadowBlur != 0 ) + { + $AlphaDecay = ($this->ShadowAlpha / $this->ShadowBlur); + + for($i=1; $i<=$this->ShadowBlur; $i++) + $this->drawFilledRectangle($X1+$this->ShadowXDistance-$i/2,$Y1+$this->ShadowYDistance-$i/2,$X2+$this->ShadowXDistance-$i/2,$Y2+$this->ShadowYDistance-$i/2,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,FALSE,$this->ShadowAlpha-$AlphaDecay*$i,TRUE); + for($i=1; $i<=$this->ShadowBlur; $i++) + $this->drawFilledRectangle($X1+$this->ShadowXDistance+$i/2,$Y1+$this->ShadowYDistance+$i/2,$X2+$this->ShadowXDistance+$i/2,$Y2+$this->ShadowYDistance+$i/2,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,FALSE,$this->ShadowAlpha-$AlphaDecay*$i,TRUE); + } + } + + $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B); + imagefilledrectangle($this->Picture,round($X1),round($Y1),round($X2),round($Y2),$C_Rectangle); + } + else + { + $LayerWidth = abs($X2-$X1)+2; + $LayerHeight = abs($Y2-$Y1)+2; + + $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight); + $C_White = $this->AllocateColor($this->Layers[0],255,255,255); + imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White); + imagecolortransparent($this->Layers[0],$C_White); + + $C_Rectangle = $this->AllocateColor($this->Layers[0],$R,$G,$B); + imagefilledrectangle($this->Layers[0],round(1),round(1),round($LayerWidth-1),round($LayerHeight-1),$C_Rectangle); + + imagecopymerge($this->Picture,$this->Layers[0],round(min($X1,$X2)-1),round(min($Y1,$Y2)-1),0,0,$LayerWidth,$LayerHeight,$Alpha); + imagedestroy($this->Layers[0]); + } + + if ( $DrawBorder ) + { + $ShadowSettings = $this->ShadowActive; $this->ShadowActive = FALSE; + $this->drawRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B); + $this->ShadowActive = $ShadowSettings; + } + } + + /* This function create a rectangle with rounded corners and antialias */ + function drawRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B) + { + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + if ( $Radius == 0 ) { $this->drawRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B); return; } + + $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B); + + $Step = 90 / ((3.1418 * $Radius)/2); + + for($i=0;$i<=90;$i=$i+$Step) + { + $X = cos(($i+180)*3.1418/180) * $Radius + $X1 + $Radius; + $Y = sin(($i+180)*3.1418/180) * $Radius + $Y1 + $Radius; + $this->drawAntialiasPixel($X,$Y,$R,$G,$B); + + $X = cos(($i-90)*3.1418/180) * $Radius + $X2 - $Radius; + $Y = sin(($i-90)*3.1418/180) * $Radius + $Y1 + $Radius; + $this->drawAntialiasPixel($X,$Y,$R,$G,$B); + + $X = cos(($i)*3.1418/180) * $Radius + $X2 - $Radius; + $Y = sin(($i)*3.1418/180) * $Radius + $Y2 - $Radius; + $this->drawAntialiasPixel($X,$Y,$R,$G,$B); + + $X = cos(($i+90)*3.1418/180) * $Radius + $X1 + $Radius; + $Y = sin(($i+90)*3.1418/180) * $Radius + $Y2 - $Radius; + $this->drawAntialiasPixel($X,$Y,$R,$G,$B); + } + + $X1=$X1-.2;$Y1=$Y1-.2; + $X2=$X2+.2;$Y2=$Y2+.2; + $this->drawLine($X1+$Radius,$Y1,$X2-$Radius,$Y1,$R,$G,$B); + $this->drawLine($X2,$Y1+$Radius,$X2,$Y2-$Radius,$R,$G,$B); + $this->drawLine($X2-$Radius,$Y2,$X1+$Radius,$Y2,$R,$G,$B); + $this->drawLine($X1,$Y2-$Radius,$X1,$Y1+$Radius,$R,$G,$B); + } + + /* This function create a filled rectangle with rounded corners and antialias */ + function drawFilledRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B) + { + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + if ( $Radius == 0 ) { $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B); return; } + $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B); + + $Step = 90 / ((3.1418 * $Radius)/2); + + for($i=0;$i<=90;$i=$i+$Step) + { + $Xi1 = cos(($i+180)*3.1418/180) * $Radius + $X1 + $Radius; + $Yi1 = sin(($i+180)*3.1418/180) * $Radius + $Y1 + $Radius; + + $Xi2 = cos(($i-90)*3.1418/180) * $Radius + $X2 - $Radius; + $Yi2 = sin(($i-90)*3.1418/180) * $Radius + $Y1 + $Radius; + + $Xi3 = cos(($i)*3.1418/180) * $Radius + $X2 - $Radius; + $Yi3 = sin(($i)*3.1418/180) * $Radius + $Y2 - $Radius; + + $Xi4 = cos(($i+90)*3.1418/180) * $Radius + $X1 + $Radius; + $Yi4 = sin(($i+90)*3.1418/180) * $Radius + $Y2 - $Radius; + + imageline($this->Picture,$Xi1,$Yi1,$X1+$Radius,$Yi1,$C_Rectangle); + imageline($this->Picture,$X2-$Radius,$Yi2,$Xi2,$Yi2,$C_Rectangle); + imageline($this->Picture,$X2-$Radius,$Yi3,$Xi3,$Yi3,$C_Rectangle); + imageline($this->Picture,$Xi4,$Yi4,$X1+$Radius,$Yi4,$C_Rectangle); + + $this->drawAntialiasPixel($Xi1,$Yi1,$R,$G,$B); + $this->drawAntialiasPixel($Xi2,$Yi2,$R,$G,$B); + $this->drawAntialiasPixel($Xi3,$Yi3,$R,$G,$B); + $this->drawAntialiasPixel($Xi4,$Yi4,$R,$G,$B); + } + + imagefilledrectangle($this->Picture,$X1,$Y1+$Radius,$X2,$Y2-$Radius,$C_Rectangle); + imagefilledrectangle($this->Picture,$X1+$Radius,$Y1,$X2-$Radius,$Y2,$C_Rectangle); + + $X1=$X1-.2;$Y1=$Y1-.2; + $X2=$X2+.2;$Y2=$Y2+.2; + $this->drawLine($X1+$Radius,$Y1,$X2-$Radius,$Y1,$R,$G,$B); + $this->drawLine($X2,$Y1+$Radius,$X2,$Y2-$Radius,$R,$G,$B); + $this->drawLine($X2-$Radius,$Y2,$X1+$Radius,$Y2,$R,$G,$B); + $this->drawLine($X1,$Y2-$Radius,$X1,$Y1+$Radius,$R,$G,$B); + } + + /* This function create a circle with antialias */ + function drawCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0) + { + if ( $Width == 0 ) { $Width = $Height; } + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + $C_Circle = $this->AllocateColor($this->Picture,$R,$G,$B); + $Step = 360 / (2 * 3.1418 * max($Width,$Height)); + + for($i=0;$i<=360;$i=$i+$Step) + { + $X = cos($i*3.1418/180) * $Height + $Xc; + $Y = sin($i*3.1418/180) * $Width + $Yc; + $this->drawAntialiasPixel($X,$Y,$R,$G,$B); + } + } + + /* This function create a filled circle/ellipse with antialias */ + function drawFilledCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0) + { + if ( $Width == 0 ) { $Width = $Height; } + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + $C_Circle = $this->AllocateColor($this->Picture,$R,$G,$B); + $Step = 360 / (2 * 3.1418 * max($Width,$Height)); + + for($i=90;$i<=270;$i=$i+$Step) + { + $X1 = cos($i*3.1418/180) * $Height + $Xc; + $Y1 = sin($i*3.1418/180) * $Width + $Yc; + $X2 = cos((180-$i)*3.1418/180) * $Height + $Xc; + $Y2 = sin((180-$i)*3.1418/180) * $Width + $Yc; + + $this->drawAntialiasPixel($X1-1,$Y1-1,$R,$G,$B); + $this->drawAntialiasPixel($X2-1,$Y2-1,$R,$G,$B); + + if ( ($Y1-1) > $Yc - max($Width,$Height) ) + imageline($this->Picture,$X1,$Y1-1,$X2-1,$Y2-1,$C_Circle); + } + } + + /* This function will draw a filled ellipse */ + function drawEllipse($Xc,$Yc,$Height,$Width,$R,$G,$B) + { $this->drawCircle($Xc,$Yc,$Height,$R,$G,$B,$Width); } + + /* This function will draw an ellipse */ + function drawFilledEllipse($Xc,$Yc,$Height,$Width,$R,$G,$B) + { $this->drawFilledCircle($Xc,$Yc,$Height,$R,$G,$B,$Width); } + + /* This function create a line with antialias */ + function drawLine($X1,$Y1,$X2,$Y2,$R,$G,$B,$GraphFunction=FALSE) + { + if ( $this->LineDotSize > 1 ) { $this->drawDottedLine($X1,$Y1,$X2,$Y2,$this->LineDotSize,$R,$G,$B,$GraphFunction); return(0); } + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + $Distance = sqrt(($X2-$X1)*($X2-$X1)+($Y2-$Y1)*($Y2-$Y1)); + if ( $Distance == 0 ) + return(-1); + $XStep = ($X2-$X1) / $Distance; + $YStep = ($Y2-$Y1) / $Distance; + + for($i=0;$i<=$Distance;$i++) + { + $X = $i * $XStep + $X1; + $Y = $i * $YStep + $Y1; + + if ( ($X >= $this->GArea_X1 && $X <= $this->GArea_X2 && $Y >= $this->GArea_Y1 && $Y <= $this->GArea_Y2) || !$GraphFunction ) + { + if ( $this->LineWidth == 1 ) + $this->drawAntialiasPixel($X,$Y,$R,$G,$B); + else + { + $StartOffset = -($this->LineWidth/2); $EndOffset = ($this->LineWidth/2); + for($j=$StartOffset;$j<=$EndOffset;$j++) + $this->drawAntialiasPixel($X+$j,$Y+$j,$R,$G,$B); + } + } + } + } + + /* This function create a line with antialias */ + function drawDottedLine($X1,$Y1,$X2,$Y2,$DotSize,$R,$G,$B,$GraphFunction=FALSE) + { + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + $Distance = sqrt(($X2-$X1)*($X2-$X1)+($Y2-$Y1)*($Y2-$Y1)); + + $XStep = ($X2-$X1) / $Distance; + $YStep = ($Y2-$Y1) / $Distance; + + $DotIndex = 0; + for($i=0;$i<=$Distance;$i++) + { + $X = $i * $XStep + $X1; + $Y = $i * $YStep + $Y1; + + if ( $DotIndex <= $DotSize) + { + if ( ($X >= $this->GArea_X1 && $X <= $this->GArea_X2 && $Y >= $this->GArea_Y1 && $Y <= $this->GArea_Y2) || !$GraphFunction ) + { + if ( $this->LineWidth == 1 ) + $this->drawAntialiasPixel($X,$Y,$R,$G,$B); + else + { + $StartOffset = -($this->LineWidth/2); $EndOffset = ($this->LineWidth/2); + for($j=$StartOffset;$j<=$EndOffset;$j++) + $this->drawAntialiasPixel($X+$j,$Y+$j,$R,$G,$B); + } + } + } + + $DotIndex++; + if ( $DotIndex == $DotSize * 2 ) + $DotIndex = 0; + } + } + + /* Load a PNG file and draw it over the chart */ + function drawFromPNG($FileName,$X,$Y,$Alpha=100) + { $this->drawFromPicture(1,$FileName,$X,$Y,$Alpha); } + + /* Load a GIF file and draw it over the chart */ + function drawFromGIF($FileName,$X,$Y,$Alpha=100) + { $this->drawFromPicture(2,$FileName,$X,$Y,$Alpha); } + + /* Load a JPEG file and draw it over the chart */ + function drawFromJPG($FileName,$X,$Y,$Alpha=100) + { $this->drawFromPicture(3,$FileName,$X,$Y,$Alpha); } + + /* Generic loader function for external pictures */ + function drawFromPicture($PicType,$FileName,$X,$Y,$Alpha=100) + { + if ( file_exists($FileName)) + { + $Infos = getimagesize($FileName); + $Width = $Infos[0]; + $Height = $Infos[1]; + if ( $PicType == 1 ) { $Raster = imagecreatefrompng($FileName); } + if ( $PicType == 2 ) { $Raster = imagecreatefromgif($FileName); } + if ( $PicType == 3 ) { $Raster = imagecreatefromjpeg($FileName); } + + imagecopymerge($this->Picture,$Raster,$X,$Y,0,0,$Width,$Height,$Alpha); + imagedestroy($Raster); + } + } + + /* Draw an alpha pixel */ + function drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B) + { + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + if ( $X < 0 || $Y < 0 || $X >= $this->XSize || $Y >= $this->YSize ) + return(-1); + + $RGB2 = imagecolorat($this->Picture, $X, $Y); + $R2 = ($RGB2 >> 16) & 0xFF; + $G2 = ($RGB2 >> 8) & 0xFF; + $B2 = $RGB2 & 0xFF; + + $iAlpha = (100 - $Alpha)/100; + $Alpha = $Alpha / 100; + + $Ra = floor($R*$Alpha+$R2*$iAlpha); + $Ga = floor($G*$Alpha+$G2*$iAlpha); + $Ba = floor($B*$Alpha+$B2*$iAlpha); + + $C_Aliased = $this->AllocateColor($this->Picture,$Ra,$Ga,$Ba); + imagesetpixel($this->Picture,$X,$Y,$C_Aliased); + } + + /* Color helper */ + function AllocateColor($Picture,$R,$G,$B,$Factor=0) + { + $R = $R + $Factor; + $G = $G + $Factor; + $B = $B + $Factor; + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + return(imagecolorallocate($Picture,$R,$G,$B)); + } + + /* Add a border to the picture */ + function addBorder($Size=3,$R=0,$G=0,$B=0) + { + $Width = $this->XSize+2*$Size; + $Height = $this->YSize+2*$Size; + + $Resampled = imagecreatetruecolor($Width,$Height); + $C_Background = $this->AllocateColor($Resampled,$R,$G,$B); + imagefilledrectangle($Resampled,0,0,$Width,$Height,$C_Background); + + imagecopy($Resampled,$this->Picture,$Size,$Size,0,0,$this->XSize,$this->YSize); + imagedestroy($this->Picture); + + $this->XSize = $Width; + $this->YSize = $Height; + + $this->Picture = imagecreatetruecolor($this->XSize,$this->YSize); + $C_White = $this->AllocateColor($this->Picture,255,255,255); + imagefilledrectangle($this->Picture,0,0,$this->XSize,$this->YSize,$C_White); + imagecolortransparent($this->Picture,$C_White); + imagecopy($this->Picture,$Resampled,0,0,0,0,$this->XSize,$this->YSize); + } + + /* Render the current picture to a file */ + function Render($FileName) + { + if ( $this->ErrorReporting ) + $this->printErrors($this->ErrorInterface); + + /* Save image map if requested */ + if ( $this->BuildMap ) + $this->SaveImageMap(); + + imagepng($this->Picture,$FileName); + } + + /* Render the current picture to STDOUT */ + function Stroke() + { + if ( $this->ErrorReporting ) + $this->printErrors("GD"); + + /* Save image map if requested */ + if ( $this->BuildMap ) + $this->SaveImageMap(); + + header('Content-type: image/png'); + imagepng($this->Picture); + } + + /* Private functions for internal processing */ + function drawAntialiasPixel($X,$Y,$R,$G,$B,$Alpha=100,$NoFallBack=FALSE) + { + /* Process shadows */ + if ( $this->ShadowActive && !$NoFallBack ) + { + $this->drawAntialiasPixel($X+$this->ShadowXDistance,$Y+$this->ShadowYDistance,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,$this->ShadowAlpha,TRUE); + if ( $this->ShadowBlur != 0 ) + { + $AlphaDecay = ($this->ShadowAlpha / $this->ShadowBlur); + + for($i=1; $i<=$this->ShadowBlur; $i++) + $this->drawAntialiasPixel($X+$this->ShadowXDistance-$i/2,$Y+$this->ShadowYDistance-$i/2,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,$this->ShadowAlpha-$AlphaDecay*$i,TRUE); + for($i=1; $i<=$this->ShadowBlur; $i++) + $this->drawAntialiasPixel($X+$this->ShadowXDistance+$i/2,$Y+$this->ShadowYDistance+$i/2,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,$this->ShadowAlpha-$AlphaDecay*$i,TRUE); + } + } + + if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; } + if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; } + if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; } + + $Plot = ""; + $Xi = floor($X); + $Yi = floor($Y); + + if ( $Xi == $X && $Yi == $Y) + { + if ( $Alpha == 100 ) + { + $C_Aliased = $this->AllocateColor($this->Picture,$R,$G,$B); + imagesetpixel($this->Picture,$X,$Y,$C_Aliased); + } + else + $this->drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B); + } + else + { + $Alpha1 = (((1 - ($X - floor($X))) * (1 - ($Y - floor($Y))) * 100) / 100) * $Alpha; + if ( $Alpha1 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi,$Yi,$Alpha1,$R,$G,$B); } + + $Alpha2 = ((($X - floor($X)) * (1 - ($Y - floor($Y))) * 100) / 100) * $Alpha; + if ( $Alpha2 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi+1,$Yi,$Alpha2,$R,$G,$B); } + + $Alpha3 = (((1 - ($X - floor($X))) * ($Y - floor($Y)) * 100) / 100) * $Alpha; + if ( $Alpha3 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi,$Yi+1,$Alpha3,$R,$G,$B); } + + $Alpha4 = ((($X - floor($X)) * ($Y - floor($Y)) * 100) / 100) * $Alpha; + if ( $Alpha4 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi+1,$Yi+1,$Alpha4,$R,$G,$B); } + } + } + + /* Validate data contained in the description array */ + function validateDataDescription($FunctionName,&$DataDescription,$DescriptionRequired=TRUE) + { + if (!isset($DataDescription["Position"])) + { + $this->Errors[] = "[Warning] ".$FunctionName." - Y Labels are not set."; + $DataDescription["Position"] = "Name"; + } + + if ( $DescriptionRequired ) + { + if (!isset($DataDescription["Description"])) + { + $this->Errors[] = "[Warning] ".$FunctionName." - Series descriptions are not set."; + foreach($DataDescription["Values"] as $key => $Value) + { + $DataDescription["Description"][$Value] = $Value; + } + } + + if (count($DataDescription["Description"]) < count($DataDescription["Values"])) + { + $this->Errors[] = "[Warning] ".$FunctionName." - Some series descriptions are not set."; + foreach($DataDescription["Values"] as $key => $Value) + { + if ( !isset($DataDescription["Description"][$Value])) + $DataDescription["Description"][$Value] = $Value; + } + } + } + } + + /* Validate data contained in the data array */ + function validateData($FunctionName,&$Data) + { + $DataSummary = array(); + + foreach($Data as $key => $Values) + { + foreach($Values as $key2 => $Value) + { + if (!isset($DataSummary[$key2])) + $DataSummary[$key2] = 1; + else + $DataSummary[$key2]++; + } + } + + if ( max($DataSummary) == 0 ) + $this->Errors[] = "[Warning] ".$FunctionName." - No data set."; + + foreach($DataSummary as $key => $Value) + { + if ($Value < max($DataSummary)) + { + $this->Errors[] = "[Warning] ".$FunctionName." - Missing data in serie ".$key."."; + } + } + } + + /* Print all error messages on the CLI or graphically */ + function printErrors($Mode="CLI") + { + if (count($this->Errors) == 0) + return(0); + + if ( $Mode == "CLI" ) + { + foreach($this->Errors as $key => $Value) + echo $Value."\r\n"; + } + elseif ( $Mode == "GD" ) + { + $this->setLineStyle($Width=1); + $MaxWidth = 0; + foreach($this->Errors as $key => $Value) + { + $Position = imageftbbox($this->ErrorFontSize,0,$this->ErrorFontName,$Value); + $TextWidth = $Position[2]-$Position[0]; + if ( $TextWidth > $MaxWidth ) { $MaxWidth = $TextWidth; } + } + $this->drawFilledRoundedRectangle($this->XSize-($MaxWidth+20),$this->YSize-(20+(($this->ErrorFontSize+4)*count($this->Errors))),$this->XSize-10,$this->YSize-10,6,233,185,185); + $this->drawRoundedRectangle($this->XSize-($MaxWidth+20),$this->YSize-(20+(($this->ErrorFontSize+4)*count($this->Errors))),$this->XSize-10,$this->YSize-10,6,193,145,145); + + $C_TextColor = $this->AllocateColor($this->Picture,133,85,85); + $YPos = $this->YSize - (18 + (count($this->Errors)-1) * ($this->ErrorFontSize + 4)); + foreach($this->Errors as $key => $Value) + { + imagettftext($this->Picture,$this->ErrorFontSize,0,$this->XSize-($MaxWidth+15),$YPos,$C_TextColor,$this->ErrorFontName,$Value); + $YPos = $YPos + ($this->ErrorFontSize + 4); + } + } + } + + /* Activate the image map creation process */ + function setImageMap($Mode=TRUE,$GraphID="MyGraph") + { + $this->BuildMap = $Mode; + $this->MapID = $GraphID; + } + + /* Add a box into the image map */ + function addToImageMap($X1,$Y1,$X2,$Y2,$SerieName,$Value,$CallerFunction) + { + if ( $this->MapFunction == NULL || $this->MapFunction == $CallerFunction ) + { + $this->ImageMap[] = round($X1).",".round($Y1).",".round($X2).",".round($Y2).",".$SerieName.",".$Value; + $this->MapFunction = $CallerFunction; + } + } + + /* Load and cleanup the image map from disk */ + function getImageMap($MapName,$Flush=TRUE) + { + /* Strip HTML query strings */ + $Values = $this->tmpFolder.$MapName; + $Value = split("\?",$Values); + $FileName = $Value[0]; + + if ( file_exists($FileName) ) + { + $Handle = fopen($FileName, "r"); + $MapContent = fread($Handle, filesize($FileName)); + fclose($Handle); + echo $MapContent; + + if ( $Flush ) + unlink($FileName); + + exit(); + } + else + { + header("HTTP/1.0 404 Not Found"); + exit(); + } + } + + /* Save the image map to the disk */ + function SaveImageMap() + { + if ( !$this->BuildMap ) { return(-1); } + + if ( $this->ImageMap == NULL ) + { + $this->Errors[] = "[Warning] SaveImageMap - Image map is empty."; + return(-1); + } + + $Handle = fopen($this->tmpFolder.$this->MapID, 'w'); + if ( !$Handle ) + { + $this->Errors[] = "[Warning] SaveImageMap - Cannot save the image map."; + return(-1); + } + else + { + foreach($this->ImageMap as $Key => $Value) + fwrite($Handle, htmlentities($Value)."\r"); + } + fclose ($Handle); + } + + /* Convert seconds to a time format string */ + function ToTime($Value) + { + $Hour = floor($Value/3600); + $Minute = floor(($Value - $Hour*3600)/60); + $Second = floor($Value - $Hour*3600 - $Minute*60); + + if (strlen($Hour) == 1 ) { $Hour = "0".$Hour; } + if (strlen($Minute) == 1 ) { $Minute = "0".$Minute; } + if (strlen($Second) == 1 ) { $Second = "0".$Second; } + + return($Hour.":".$Minute.":".$Second); + } + + /* Convert to metric system */ + function ToMetric($Value) + { + $Go = floor($Value/1000000000); + $Mo = floor(($Value - $Go*1000000000)/1000000); + $Ko = floor(($Value - $Go*1000000000 - $Mo*1000000)/1000); + $o = floor($Value - $Go*1000000000 - $Mo*1000000 - $Ko*1000); + + if ($Go != 0) { return($Go.".".$Mo."g"); } + if ($Mo != 0) { return($Mo.".".$Ko."m"); } + if ($Ko != 0) { return($Ko.".".$o)."k"; } + return($o); + } + + /* Convert to curency */ + function ToCurrency($Value) + { + $Go = floor($Value/1000000000); + $Mo = floor(($Value - $Go*1000000000)/1000000); + $Ko = floor(($Value - $Go*1000000000 - $Mo*1000000)/1000); + $o = floor($Value - $Go*1000000000 - $Mo*1000000 - $Ko*1000); + + if ( strlen($o) == 1 ) { $o = "00".$o; } + if ( strlen($o) == 2 ) { $o = "0".$o; } + + $ResultString = $o; + if ( $Ko != 0 ) { $ResultString = $Ko.".".$ResultString; } + if ( $Mo != 0 ) { $ResultString = $Mo.".".$ResultString; } + if ( $Go != 0 ) { $ResultString = $Go.".".$ResultString; } + + $ResultString = $this->Currency.$ResultString; + return($ResultString); + } + + /* Set date format for axis labels */ + function setDateFormat($Format) + { + $this->DateFormat = $Format; + } + + /* Convert TS to a date format string */ + function ToDate($Value) + { + return(date($this->DateFormat,$Value)); + } + + /* Check if a number is a full integer (for scaling) */ + function isRealInt($Value) + { + if ($Value == floor($Value)) + return(TRUE); + return(FALSE); + } + } + + function RaiseFatal($Message) + { + echo "[FATAL] ".$Message."\r\n"; + exit(); + } + +?> diff --git a/pandora_console/reporting/pChart/pData.class b/pandora_console/reporting/pChart/pData.class new file mode 100644 index 0000000000..bee85e938b --- /dev/null +++ b/pandora_console/reporting/pChart/pData.class @@ -0,0 +1,260 @@ +. + + Class initialisation : + pData() + Data populating methods : + ImportFromCSV($FileName,$Delimiter=",",$DataColumns=-1,$HasHeader=FALSE,$DataName=-1) + AddPoint($Value,$Serie="Serie1",$Description="") + Series manipulation methods : + AddSerie($SerieName="Serie1") + AddAllSeries() + RemoveSerie($SerieName="Serie1") + SetAbsciseLabelSerie($SerieName = "Name") + SetSerieName($Name,$SerieName="Serie1") + + SetSerieSymbol($Name,$Symbol) + SetXAxisName($Name="X Axis") + SetYAxisName($Name="Y Axis") + SetXAxisFormat($Format="number") + SetYAxisFormat($Format="number") + SetXAxisUnit($Unit="") + SetYAxisUnit($Unit="") + removeSerieName($SerieName) + removeAllSeries() + Data retrieval methods : + GetData() + GetDataDescription() + */ + + /* pData class definition */ + class pData + { + var $Data; + var $DataDescription; + + function pData() + { + $this->Data = ""; + $this->DataDescription = ""; + $this->DataDescription["Position"] = "Name"; + $this->DataDescription["Format"]["X"] = "number"; + $this->DataDescription["Format"]["Y"] = "number"; + $this->DataDescription["Unit"]["X"] = NULL; + $this->DataDescription["Unit"]["Y"] = NULL; + } + + function ImportFromCSV($FileName,$Delimiter=",",$DataColumns=-1,$HasHeader=FALSE,$DataName=-1) + { + $handle = @fopen($FileName,"r"); + if ($handle) + { + $HeaderParsed = FALSE; + while (!feof($handle)) + { + $buffer = fgets($handle, 4096); + $buffer = str_replace(chr(10),"",$buffer); + $buffer = str_replace(chr(13),"",$buffer); + $Values = split($Delimiter,$buffer); + + if ( $buffer != "" ) + { + if ( $HasHeader == TRUE && $HeaderParsed == FALSE ) + { + if ( $DataColumns == -1 ) + { + $ID = 1; + foreach($Values as $key => $Value) + { $this->SetSerieName($Value,"Serie".$ID); $ID++; } + } + else + { + $SerieName = ""; + + foreach($DataColumns as $key => $Value) + $this->SetSerieName($Values[$Value],"Serie".$Value); + } + $HeaderParsed = TRUE; + } + else + { + if ( $DataColumns == -1 ) + { + $ID = 1; + foreach($Values as $key => $Value) + { $this->AddPoint(intval($Value),"Serie".$ID); $ID++; } + } + else + { + $SerieName = ""; + if ( $DataName != -1 ) + $SerieName = $Values[$DataName]; + + foreach($DataColumns as $key => $Value) + $this->AddPoint($Values[$Value],"Serie".$Value,$SerieName); + } + } + } + } + fclose($handle); + } + } + + function AddPoint($Value,$Serie="Serie1",$Description="") + { + if (is_array($Value) && count($Value) == 1) + $Value = $Value[0]; + + $ID = 0; + for($i=0;$i<=count($this->Data);$i++) + { if(isset($this->Data[$i][$Serie])) { $ID = $i+1; } } + + if ( count($Value) == 1 ) + { + $this->Data[$ID][$Serie] = $Value; + if ( $Description != "" ) + $this->Data[$ID]["Name"] = $Description; + elseif (!isset($this->Data[$ID]["Name"])) + $this->Data[$ID]["Name"] = $ID; + } + else + { + foreach($Value as $key => $Val) + { + $this->Data[$ID][$Serie] = $Val; + if (!isset($this->Data[$ID]["Name"])) + $this->Data[$ID]["Name"] = $ID; + $ID++; + } + } + } + + function AddSerie($SerieName="Serie1") + { + if ( !isset($this->DataDescription["Values"]) ) + { + $this->DataDescription["Values"][] = $SerieName; + } + else + { + $Found = FALSE; + foreach($this->DataDescription["Values"] as $key => $Value ) + if ( $Value == $SerieName ) { $Found = TRUE; } + + if ( !$Found ) + $this->DataDescription["Values"][] = $SerieName; + } + } + + function AddAllSeries() + { + unset($this->DataDescription["Values"]); + + if ( isset($this->Data[0]) ) + { + foreach($this->Data[0] as $Key => $Value) + { + if ( $Key != "Name" ) + $this->DataDescription["Values"][] = $Key; + } + } + } + + function RemoveSerie($SerieName="Serie1") + { + if ( !isset($this->DataDescription["Values"]) ) + return(0); + + $Found = FALSE; + foreach($this->DataDescription["Values"] as $key => $Value ) + { + if ( $Value == $SerieName ) + unset($this->DataDescription["Values"][$key]); + } + } + + function SetAbsciseLabelSerie($SerieName = "Name") + { + $this->DataDescription["Position"] = $SerieName; + } + + function SetSerieName($Name,$SerieName="Serie1") + { + $this->DataDescription["Description"][$SerieName] = $Name; + } + + function SetXAxisName($Name="X Axis") + { + $this->DataDescription["Axis"]["X"] = $Name; + } + + function SetYAxisName($Name="Y Axis") + { + $this->DataDescription["Axis"]["Y"] = $Name; + } + + function SetXAxisFormat($Format="number") + { + $this->DataDescription["Format"]["X"] = $Format; + } + + function SetYAxisFormat($Format="number") + { + $this->DataDescription["Format"]["Y"] = $Format; + } + + function SetXAxisUnit($Unit="") + { + $this->DataDescription["Unit"]["X"] = $Unit; + } + + function SetYAxisUnit($Unit="") + { + $this->DataDescription["Unit"]["Y"] = $Unit; + } + + function SetSerieSymbol($Name,$Symbol) + { + $this->DataDescription["Symbol"][$Name] = $Symbol; + } + + function removeSerieName($SerieName) + { + if ( isset($this->DataDescription["Description"][$SerieName]) ) + unset($this->DataDescription["Description"][$SerieName]); + } + + function removeAllSeries() + { + foreach($this->DataDescription["Values"] as $Key => $Value) + unset($this->DataDescription["Values"][$Key]); + } + + function GetData() + { + return($this->Data); + } + + function GetDataDescription() + { + return($this->DataDescription); + } + } +?> diff --git a/pandora_console/reporting/pandora_graph.php b/pandora_console/reporting/pandora_graph.php new file mode 100644 index 0000000000..a8a8315149 --- /dev/null +++ b/pandora_console/reporting/pandora_graph.php @@ -0,0 +1,82 @@ + +// Please see http://pandora.sourceforge.net for full contribution list + +// This program 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 for version 2. +// This program 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 program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +abstract class PandoraGraphAbstract { + public $width = 300; + public $height = 200; + public $data; + public $legend = false; + public $fontpath; + public $three_dimensions = true; + public $graph_color = array (); + public $xaxis_interval = 1; + public $yaxis_interval = 5; + public $xaxis_format = 'numeric'; + public $yaxis_format = 'numeric'; + public $show_title = false; + public $show_legend = false; + public $background_gradient = false; + public $title = ""; + public $subtitle = ""; + public $stacked = false; + public $zoom = 85; + public $events = false; + public $alert_top = false; + public $alert_bottom = false; + public $date_format = "d/m"; + public $max_value = 0; + public $min_value = 0; + public $background_color = '#FFFFFF'; + public $border = true; + + abstract protected function pie_graph (); + abstract protected function horizontal_bar_graph (); + abstract protected function vertical_bar_graph (); + abstract protected function sparse_graph ($period, $avg_only, $min_value, $max_value, $unit_name); + abstract protected function single_graph (); + abstract protected function combined_graph ($values, $events, $alerts, $unit_name, $max_value, $stacked); + abstract protected function progress_bar ($value, $color); +} + +function get_graph_engine ($period = 3600) { + global $config; + + if (file_exists ('pchart_graph.php')) { + require_once ('pchart_graph.php'); + $engine = new PchartGraph (); + if (isset ($config['graphics_palette'])) + $engine->load_palette ($config['graphics_palette']); + } else { + exit; + } + $engine->graph_color[1] = $config['graph_color1']; + $engine->graph_color[2] = $config['graph_color2']; + $engine->graph_color[3] = $config['graph_color3']; + + if ($period <= 86400) + $engine->date_format = 'g:iA'; + elseif ($period <= 604800) + $engine->date_format = 'd/m'; + else + $engine->date_format = 'd/m/y'; + + return $engine; +} + +?> diff --git a/pandora_console/reporting/pchart_graph.php b/pandora_console/reporting/pchart_graph.php new file mode 100644 index 0000000000..2c37caaa34 --- /dev/null +++ b/pandora_console/reporting/pchart_graph.php @@ -0,0 +1,492 @@ + +// Please see http://pandora.sourceforge.net for full contribution list + +// This program 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 for version 2. +// This program 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 program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require_once ('pChart/pData.class'); +require_once ('pChart/pChart.class'); + +class PchartGraph extends PandoraGraphAbstract { + public $palette_path = false; + private $graph = NULL; + private $dataset = NULL; + private $x1; + private $x2; + private $y1; + private $y2; + + public function load_palette ($palette_path) { + $this->palette_path = $palette_path; + } + + public function pie_graph () { + // dataset definition + $this->dataset = new pData; + $this->dataset->AddPoint ($this->data, "Serie1", $this->legend); + $this->dataset->AddPoint ($this->legend, "Serie2"); + $this->dataset->AddAllSeries (); + $this->dataset->SetAbsciseLabelSerie ("Serie2"); + + // Initialise the graph + $this->graph = new pChart ($this->width, $this->height); + $this->graph->setFontProperties ($this->fontpath, 8); + if ($this->palette_path) { + $this->graph->loadColorPalette ($this->palette_path); + } + $this->add_background (); + + // Draw the pie chart + if ($this->three_dimensions) { + $this->graph->drawPieGraph ($this->dataset->GetData (), + $this->dataset->GetDataDescription (), + $this->width / 2, + $this->height / 2 - 15, $this->zoom, + PIE_PERCENTAGE_LABEL, 5, 70, 20, 5); + } else { + $this->graph->drawFlatPieGraph ($this->dataset->GetData (), + $this->dataset->GetDataDescription (), + $this->width / 2, + $this->height / 2, $this->zoom, + PIE_PERCENTAGE_LABEL, 5); + } + + if ($this->show_legend) { + $this->graph->drawPieLegend (10, 10, $this->dataset->GetData (), + $this->dataset->GetDataDescription (), 255, 255, 255); + } + + $this->graph->Stroke (); + } + + public function horizontal_bar_graph () { + // dataset definition + $this->dataset = new pData; + foreach ($this->data as $x => $y) { + $this->dataset->AddPoint ($y, "Serie1", $x); + } + $this->dataset->AddAllSeries (); + $this->dataset->SetXAxisFormat ("label"); + $this->dataset->SetYAxisFormat ($this->yaxis_format); + + // Initialise the graph + $this->graph = new pChart ($this->width, $this->height); + if ($this->palette_path) { + $this->graph->loadColorPalette ($this->palette_path); + } + $this->graph->setFontProperties ($this->fontpath, 8); + $this->add_background (); + $this->graph->drawGraphArea (255, 255, 255, true); + $this->graph->drawGrid (4, true, 230, 230, 230, 50); + + // Draw the bar graph + $this->graph->setFontProperties ($this->fontpath, 8); + $this->graph->setFixedScale (0, max ($this->data)); + $this->graph->drawOverlayBarGraphH ($this->dataset->GetData (), + $this->dataset->GetDataDescription (), 50); + + // Finish the graph + $this->add_legend (); + + $this->graph->Stroke (); + } + + public function single_graph () { + // Dataset definition + $this->dataset = new pData; + $this->graph = new pChart ($this->width, $this->height); + + foreach ($this->data as $x => $y) { + $this->dataset->AddPoint ($y, "Serie1", $x); + } + $color = $this->get_rgb_values ($this->graph_color[2]); + $this->graph->setColorPalette (0, $color['r'], $color['g'], $color['b']); + $this->dataset->AddAllSeries (); + if ($this->legend !== false) { + $this->dataset->SetSerieName ($this->legend[0], "Serie1"); + } + + if ($this->palette_path) { + $this->graph->loadColorPalette ($this->palette_path); + } + $this->graph->setFontProperties ($this->fontpath, 8); + $this->add_background (); + $this->dataset->SetXAxisFormat ($this->xaxis_format); + $this->dataset->SetYAxisFormat ($this->yaxis_format); + $this->graph->drawGraphArea (255, 255, 255, true); + + if ($this->max_value == 0 || $this->max_value == 1) + $this->graph->setFixedScale (0, 1, 1); + + $this->graph->drawScale ($this->dataset->GetData (), + $this->dataset->GetDataDescription (), + SCALE_START0, 80, 80, 80, true, + 0, 0, false, + $this->xaxis_interval); + + $this->graph->drawGrid (4, false, 0, 0, 0); + if ($this->max_value > 0) { + // Draw the graph + $this->graph->drawFilledLineGraph ($this->dataset->GetData (), + $this->dataset->GetDataDescription (), + 50, true); + } + + // Finish the graph + $this->add_legend (); + $this->add_events (); + $this->add_alert_levels (); + + $this->graph->Stroke (); + } + + public function sparse_graph ($period, $avg_only, $min_value, $max_value, $unit_name) { + // Dataset definition + $this->dataset = new pData; + $this->graph = new pChart ($this->width, $this->height); + $this->graph->setFontProperties ($this->fontpath, 8); + $this->legend = array (); + + if ($avg_only) { + foreach ($this->data as $data) { + $this->dataset->AddPoint ($data['sum'], "AVG", $data['timestamp_bottom']); + } + } else { + foreach ($this->data as $data) { + $this->dataset->AddPoint ($data['sum'], "AVG", $data['timestamp_bottom']); + $this->dataset->AddPoint ($data['min'], "MIN"); + $this->dataset->AddPoint ($data['max'], "MAX"); + } + $this->legend[1] = __("Min. Value"); + $this->legend[2] = __("Max. Value"); + $this->dataset->SetSerieName (__("Min. Value"), "MIN"); + $this->dataset->SetSerieName (__("Max. Value"), "MAX"); + } + $this->set_colors (); + $this->dataset->SetXAxisFormat ('date'); + $this->graph->setDateFormat ("Y"); + $this->dataset->SetYAxisFormat ('metric'); + $this->dataset->AddAllSeries (); + $this->dataset->SetSerieName (__("Avg. Value"), "AVG"); + $this->legend[0] = __("Avg. Value"); + + if ($this->palette_path) { + $this->graph->loadColorPalette ($this->palette_path); + } + + $this->add_background (); + $this->graph->drawGraphArea (255, 255, 255, true); + + $this->xaxis_interval = ($this->xaxis_interval / 7 >= 1) ? ($this->xaxis_interval / 7) : 10; + $this->graph->drawScale ($this->dataset->GetData (), + $this->dataset->GetDataDescription (), SCALE_START0, + 80, 80, 80, true, 0, 0, false, + $this->xaxis_interval); + /* NOTICE: The final "false" is a Pandora modificaton of pChart to avoid showing vertical lines. */ + $this->graph->drawGrid (1, true, 225, 225, 225, 100, false); + // Draw the graph + $this->graph->drawFilledLineGraph ($this->dataset->GetData(), $this->dataset->GetDataDescription(), 50, true); + + $this->add_legend (); + $this->add_events ("AVG"); + $this->add_alert_levels (); + + $this->graph->Stroke (); + } + + public function vertical_bar_graph () { + // dataset definition + $this->dataset = new pData; + foreach ($this->data as $x => $y) { + $this->dataset->AddPoint ($y, "Serie1", $x); + } + $this->dataset->AddAllSeries (); + $this->dataset->SetAbsciseLabelSerie (); + $this->dataset->SetXAxisFormat ($this->xaxis_format); + $this->dataset->SetYAxisFormat ($this->yaxis_format); + + // Initialise the graph + $this->graph = new pChart ($this->width, $this->height); + if ($this->palette_path) { + $this->graph->loadColorPalette ($this->palette_path); + } + $this->graph->setFontProperties ($this->fontpath, 8); + $this->add_background (); + $this->graph->drawGraphArea (255, 255, 255, true); + $this->graph->drawGrid (4, true, 230, 230, 230, 50); + + // Draw the bar graph + $this->graph->setFontProperties ($this->fontpath, 8); + $this->graph->drawScale ($this->dataset->GetData (), + $this->dataset->GetDataDescription (), + SCALE_START0, 80, 80, 80, + true, 0, 0, false, + $this->xaxis_interval); + $this->graph->drawOverlayBarGraph ($this->dataset->GetData (), + $this->dataset->GetDataDescription (), + 50); + $this->add_events ("Serie1"); + $this->add_alert_levels (); + + // Finish the graph + $this->graph->Stroke (); + } + + public function combined_graph ($values, $events, $alerts, $unit_name, $max_value, $stacked) { + set_time_limit (0); + // Dataset definition + $this->dataset = new pData; + $this->graph = new pChart ($this->width, $this->height); + + foreach ($this->data as $i => $data) { + foreach ($data as $j => $value) { + $this->dataset->AddPoint ($value, $this->legend[$i], + $values[$j]['timestamp_bottom']); + } + } + + foreach ($this->legend as $name) + $this->dataset->setSerieName($name, $name); + + $this->set_colors (); + $this->graph->setFontProperties ($this->fontpath, 8); + $this->dataset->SetXAxisFormat ('date'); + $this->dataset->SetYAxisFormat ('metric'); + $this->dataset->AddAllSeries (); + $this->add_background (); + $this->graph->drawGraphArea (255, 255, 255, true); + + // Draw the graph + if ($stacked == 1) { + $this->graph->drawScale ($this->dataset->GetData (), + $this->dataset->GetDataDescription (), + SCALE_ADDALL, 80, 80, 80, true, + 0, 0, false, + $this->xaxis_interval); + /* Stacked mode are only supported in bar charts */ + //$this->graph->DivisionWidth = ($this->x2 - $this->x1) / sizeof ($this->data[0]); + $this->graph->drawStackedBarGraph ($this->dataset->GetData (), + $this->dataset->GetDataDescription (), + 50); + } else if ($stacked == 2) { + $this->graph->drawScale ($this->dataset->GetData (), + $this->dataset->GetDataDescription (), + SCALE_START0, 80, 80, 80, true, 0, 0, false, + $this->xaxis_interval); + $this->graph->drawLineGraph ($this->dataset->GetData (), + $this->dataset->GetDataDescription ()); + } else { + $this->graph->drawScale ($this->dataset->GetData (), + $this->dataset->GetDataDescription (), + SCALE_START0, 80, 80, 80, true, 0, 0, false, + $this->xaxis_interval); + $this->graph->drawFilledCubicCurve ($this->dataset->GetData(), + $this->dataset->GetDataDescription(), 0.1, 50, true); + } + $this->add_legend (); + $this->add_events ($this->legend[0]); + $this->add_alert_levels (); + + $this->graph->Stroke (); + } + + public function progress_bar ($value, $color) { + set_time_limit (0); + + // Dataset definition + $this->graph = new pChart ($this->width, $this->height); + $this->graph->setFontProperties ($this->fontpath, 8); + + $radius = ($this->height > 18) ? 10 : 0; + + $ratio = (int) $value / 100 * $this->width; + + /* Color stuff */ + $r = hexdec (substr ($this->background_color, 1, 2)); + $g = hexdec (substr ($this->background_color, 3, 2)); + $b = hexdec (substr ($this->background_color, 5, 2)); + + /* Actual percentage */ + $this->graph->drawFilledRectangle (0, 0, $this->width, + $this->height, 255, 255, 255, false, 0); + $this->graph->drawFilledRoundedRectangle (0, 0, $this->width, + $this->height, $radius, $r, $g, $b); + + $r = hexdec (substr ($color, 1, 2)); + $g = hexdec (substr ($color, 3, 2)); + $b = hexdec (substr ($color, 5, 2)); + $this->graph->drawFilledRoundedRectangle (0, 0, $ratio, + $this->height, $radius, $r, $g, $b); + /* Under this value, the rounded rectangle is painted great */ + if ($ratio <= 16) { + /* Clean a bit of pixels */ + for ($i = 0; $i < 7; $i++) { + $this->graph->drawLine (0, $i, 6 - $i, $i, 255, 255, 255); + } + + $end = $this->height - 1; + for ($i = 0; $i < 7; $i++) { + $this->graph->drawLine (0, $end - $i, 5 - $i, $end - $i, 255, 255, 255); + } + } + + if ($this->show_title) { + $this->graph->drawTextBox (0, 0, $this->width, $this->height, + $this->title, 0, 255, 255, 255, ALIGN_CENTER, true); + } + if ($this->border) { + $this->graph->drawRoundedRectangle (0, 0, $this->width - 1 , $this->height - 1, + $radius, 0, 0, 0); + } + + $this->graph->Stroke (); + } + + /* Gets an array with each the components of a RGB string */ + private static function get_rgb_values ($rgb_string) { + $color = array ('r' => 0, 'g' => 0, 'b' => 0); + $offset = 0; + if ($rgb_string[0] == '#') + $offset = 1; + $color['r'] = hexdec (substr ($rgb_string, $offset, 2)); + $color['g'] = hexdec (substr ($rgb_string, $offset + 2, 2)); + $color['b'] = hexdec (substr ($rgb_string, $offset + 4, 2)); + return $color; + } + + private function add_alert_levels () { + if ($this->alert_top !== false) { + $this->graph->drawTreshold ($this->alert_top, 57, + 96, 255, true, true, 4, + "Alert top"); + } + if ($this->alert_bottom !== false) { + $this->graph->drawTreshold ($this->alert_bottom, 7, + 96, 255, true, true, 4, + "Alert bottom"); + } + } + + private function add_events ($serie = "Serie1") { + if (! $this->events) + return; + + /* Unfortunatelly, the events must be draw manually */ + + $first = $this->dataset->Data[0]["Name"]; + $len = count ($this->dataset->Data) - 1; + + $last = $this->dataset->Data[$len]["Name"]; + $ylen = $this->y2 - $this->y1; + + foreach ($this->data as $i => $data) { + /* Finally, check if there were events */ + + if (! $data['events']) + continue; + + $x1 = (int) ($this->x1 + $i * $this->graph->DivisionWidth); + $y1 = (int) ($this->y2 - ($this->dataset->Data[$i][$serie] * $this->graph->DivisionRatio)); + $this->graph->drawFilledCircle ($x1, $y1, 1.5, 255, 0, 0); + if ($y1 == $this->y2) + /* Lines in the same dot fails */ + continue; + + $this->graph->drawDottedLine ($x1 - 1, $y1, + $x1 - 1, $this->y2, + 5, 255, 150, 150); + } + } + + private function add_background () { + if ($this->graph == NULL) + return; + + $this->graph->setDateFormat ($this->date_format); + + $this->x1 = ($this->width > 300) ? 30 : 35; + $this->y1 = ($this->height > 200) ? 25 : 10; + $this->x2 = ($this->width > 300) ? $this->width - 30 : $this->width - 15; + $this->y2 = ($this->height > 200) ? $this->height - 25 : $this->height - 25; + + if ($this->max_value > 10000) + $this->x1 += 20; + + if ($this->background_gradient) + $this->graph->drawGraphAreaGradient (233, 243, 210, 50, TARGET_BACKGROUND); + else + $this->graph->drawGraphArea (255, 255, 255, true); + + if ($this->show_title) { + $this->y1 += 30; + } + + $this->graph->setFontProperties ($this->fontpath, 6); + $size = $this->graph->getLegendBoxSize ($this->dataset->GetDataDescription ()); + if (is_array ($size)) { + while ($size[1] > $this->y1) + $this->y1 += (int) $size[1] / 2; + if ($this->y1 > $this->y2) { + + $this->y1 = $this->y2; + } + } + + $this->graph->setGraphArea ($this->x1, $this->y1, $this->x2, $this->y2); + + if ($this->show_title) { + $this->graph->setFontProperties ($this->fontpath, 10); + $this->graph->drawTextBox (0, 0, $this->width, 20, $this->title, 0, + 255, 255, 255, ALIGN_LEFT, true, 0, 0, 0, 30); + + $this->graph->setFontProperties ($this->fontpath, 9); + $this->graph->drawTextBox (0, 20, $this->width, 40, $this->subtitle, + 0, 255, 255, 255, ALIGN_LEFT, true, 0, 0, 0, 0); + $this->graph->setFontProperties ($this->fontpath, 8); + } + + /* This is a tiny watermark. Remove safely */ + $this->graph->setFontProperties ($this->fontpath, 7); + $this->graph->drawTextBox ($this->width - 5, $this->height - 0, + $this->width - 240, $this->height - 0, 'Pandora FMS', 90, + 154, 154, 154, ALIGN_BOTTOM_LEFT, false); + } + + private function add_legend () { + if (! $this->show_title || $this->legend === false) { + return; + } + + /* Add legend */ + $this->graph->setFontProperties ($this->fontpath, 6); + $size = $this->graph->getLegendBoxSize ($this->dataset->GetDataDescription ()); + $this->graph->drawLegend ($this->width - $size[0] - 32, 5, + $this->dataset->GetDataDescription (), + 240, 240, 240); + } + + private function set_colors () { + if ($this->graph == NULL) + return; + + $color = $this->get_rgb_values ($this->graph_color[2]); + $this->graph->setColorPalette (0, $color['r'], $color['g'], $color['b']); + $color = $this->get_rgb_values ($this->graph_color[1]); + $this->graph->setColorPalette (1, $color['r'], $color['g'], $color['b']); + $color = $this->get_rgb_values ($this->graph_color[3]); + $this->graph->setColorPalette (2, $color['r'], $color['g'], $color['b']); + } +} +?>