Thread: [Secureideas-cvs] base-php4 base_graph_display.php,1.16,1.17
Brought to you by:
secureideas,
sinukas
From: Juergen L. <jle...@us...> - 2008-01-27 14:04:52
|
Update of /cvsroot/secureideas/base-php4 In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv9934/base-php4 Modified Files: base_graph_display.php Log Message: I have fixed several minor, but glaring bugs and made some changes to the graphics (added grid lines, e.g.) and the html formular. There are certainly still some bugs left. Some problems are caused by the PEAR::Image_Graph library: 1. Missing legends: Cf. http://pear.php.net/bugs/bug.php?id=8055 Can be fixed by commenting out two lines in file pear/Image/Graph/Plot/Pie.php: Line 502: // $this->_clip(true); Line 616: // $this->_clip(false); 2. Vertical x axis labels have problems with their positioning (alignment): They get written into the bars and lines of the plot, instead of being written below the x axis in an appropriate distance. Vertical labels are needed, if one single label is too long to be written in a normal horizontal way without interfering with the other labels. With the current code every label is written horizontally by default. It is up to the user to change this, if he feels a vertical alignment would be better, by checking "Rotate Axis Labels (90 degrees)". I applied the workaround suggested in http://pear.php.net/bugs/bug.php?id=8675 but it is not sufficient. 3. When there's just one single element (e.g. 1 alert), image_graph produces nothing at all. Therefore I added a sort of dummy element, i.e. a non-existing element, in such cases, so that always at least 2 alerts are displayed, even if the query has returned only 1 alert. This is not a perfect workaround, but at least better than an empty plotarea. Cf. http://pear.php.net/bugs/bug.php?id=12763 Index: base_graph_display.php =================================================================== RCS file: /cvsroot/secureideas/base-php4/base_graph_display.php,v retrieving revision 1.16 retrieving revision 1.17 diff -u -d -r1.16 -r1.17 --- base_graph_display.php 2 Sep 2007 20:36:01 -0000 1.16 +++ base_graph_display.php 27 Jan 2008 14:04:45 -0000 1.17 @@ -23,7 +23,7 @@ ** - xdata[][]: data and label array for the y-axis ** - yaxis_scale: (boolean) 0: linear; 1: logarithmic ** - rotate_xaxis_lbl: (boolean) rotate X-axis labels 90 degrees -** - style: [bar|line] chooses the style of the chart +** - style: [bar|line|pie] chooses the style of the chart ******************************************************************************** ** Authors: ******************************************************************************** @@ -56,74 +56,274 @@ $chart_type = ImportHTTPVar("chart_type", VAR_DIGIT); // Create the graph area, legends on bottom -- Alejandro - $Graph =& new Image_Graph(array('driver'=>'gd', 'width'=>$width,'height'=>$height)); - $Graph->add( - Image_Graph::vertical( - Image_Graph::factory('title', array($title, 16)), + $Graph =& new Image_Graph(array('driver'=>'gd', + 'width'=>$width, + 'height'=>$height)); + + + if ($yaxis_scale == 1) + /* then a logarithmic y axis has been requested */ + { + if ($style == "pie") + // in this case we ignore logarithm + { + $Graph->add( Image_Graph::vertical( - $Plotarea = Image_Graph::factory('plotarea'), - $Legend = Image_Graph::factory('legend'), + Image_Graph::factory('title', array($title, 16)), + Image_Graph::horizontal( + $Plotarea = Image_Graph::factory('plotarea'), + $Legend = Image_Graph::factory('legend'), + 80 + ), + 10 + ) + ); + } + else + // bar, line + { + $Graph->add( + Image_Graph::vertical( + Image_Graph::factory('title', array($title, 16)), + Image_Graph::vertical( + $Plotarea = Image_Graph::factory('plotarea' , array('axis', 'axis_log')), + $Legend = Image_Graph::factory('legend'), + 85 + ), + 10 + ) + ); + } + } + else + // linear axis + { + if ($style == "pie") + { + $Graph->add( + Image_Graph::vertical( + Image_Graph::factory('title', array($title, 16)), + Image_Graph::horizontal( + $Plotarea = Image_Graph::factory('plotarea'), + $Legend = Image_Graph::factory('legend'), + 80 // 85 + ), + 10 + ) + ); + } + else + { + $Graph->add( + Image_Graph::vertical( + Image_Graph::factory('title', array($title, 16)), + Image_Graph::vertical( + $Plotarea = Image_Graph::factory('plotarea'), + $Legend = Image_Graph::factory('legend'), 85 - ), - 10 - ) - ); + ), + 10 + ) + ); + } + } + + + $Font =& $Graph->addNew('font', 'Verdana'); + $Font->setSize(8); + $Graph->setFont($Font); + + + // Configure plotarea + if($style == "pie") { + $Legend->setPlotarea($Plotarea); + } + else + { + $Plotarea->setAxisPadding(30, 'top'); + $Plotarea->setAxisPadding(30, 'bottom'); + $Plotarea->setAxisPadding(10, 'left'); + $Plotarea->setAxisPadding(10, 'right'); + } - if($style == "pie") { - $Legend->setPlotarea($Plotarea); - } $AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X); + $AxisY =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y); - // Create the dataset -- Alejandro + + if ($style != "pie") + { + // Grid lines for y axis requested? + if ($yaxis_grid == 1) + { + $GridY =& $Plotarea->addNew('bar_grid', IMAGE_GRAPH_AXIS_Y); + $GridY->setFillStyle(Image_Graph::factory('gradient', array(IMAGE_GRAPH_GRAD_VERTICAL, 'white', 'lightgrey'))); + } + + + // Grid lines for x axis requested? + if ($xaxis_grid == 1) + { + $Plotarea->addNew('line_grid', true, IMAGE_GRAPH_AXIS_X); + } + } + + + // Create the dataset -- Alejandro $Dataset =& Image_Graph::factory('dataset'); for ($i = 0; $i < count($xdata); $i++) { - $Dataset->addPoint($xdata[$i][0], $xdata[$i][1]); - /** - * I'm limiting the number of elements by 15. Can't get nice graphs with - * more than this. -- Alejandro - */ - if($chart_type > 4) { - if($i == 14) { - break; - } - } - } - $Plot =& $Plotarea->addNew($style, $Dataset); + $Dataset->addPoint($xdata[$i][0], $xdata[$i][1]); + + /** + * I'm limiting the number of elements by 15. Can't get nice graphs with + * more than this. -- Alejandro + * jl: extended this limit to all chart_types, + * not just to $chart_types >= 4, as Alejandro originally had it + */ + if($chart_type >= 1) { + if($i >= 14) { + break; + } + } + } + $number_elements = $i; + + + // Design plot: + if ($style == "line") + // then we correct this style and replace it by "area": + { + $Plot =& $Plotarea->addNew('area', $Dataset); + } + else + { + $Plot =& $Plotarea->addNew($style, $Dataset); + } + + // What about the axes? if ( $style == "pie" ) { $Plotarea->hideAxis(); $Plot->explode(10); } else { $ArrayData =& Image_Graph::factory('Image_Graph_DataPreprocessor_Array',$Dataset); $AxisX->setDataPreprocessor($ArrayData); - $AxisX->setFontAngle('vertical'); + + + + // One possibility: We could make the decision here: + /* + if ( + ($number_elements > 1) && + ( + ($xaxis_label == _CHRTTIME) || + ($xaxis_label == _CHRTSIP) || + ($xaxis_label == _CHRTDIP) || + ($xaxis_label == _CHRTCLASS) + ) + ) + */ + + // Another possibility: Let the user decide: + if ($rotate_xaxis_lbl == 1) + { + $AxisX->setFontAngle('vertical'); + $AxisX->setTitle($xaxis_label, array('angle' => 0, 'size' => 10)); + $AxisX->setLabelOption('showOffset', true); + // Workaround according to + // For ip addresses: + $AxisX->setLabelOption('offset', 60); + // For date/time labels: + //$AxisX->setLabelOption('offset', 140); + } + else + { + if ((isset($xaxis_label)) && (strlen($xaxis_label) > 0)) + { + $AxisX->setTitle($xaxis_label, array('size' => 10)); + } + } + + + if ((isset($yaxis_label)) && (strlen($yaxis_label) > 0)) + { + $AxisY->setTitle($yaxis_label, array('angle' => 90, 'size' => 10)); + } } -$Marker =& $Plot->addNew('Image_Graph_Marker_Value', IMAGE_GRAPH_PCT_Y_TOTAL); -$PointingMarker =& $Plot->addNew('Image_Graph_Marker_Pointing_Angular', array(20, &$Marker)); -$Plot->setMarker($PointingMarker); -$Marker->setDataPreprocessor(Image_Graph::factory('Image_Graph_DataPreprocessor_Formatted', '%0.1f%%')); -$fill =& Image_Graph::factory('Image_Graph_Fill_Array'); -if($style == "pie") { - for($ff = 0; $ff < $i; $ff++) { - // Need to be revisited. Thinking on getting a random color from the array. -- Alejandro - $fill->addColor($named_colors[$ff]); - } -} else { - // Need to be revisited. If bar or line graphs, there's only one color. -- Alejandro - $fill->addColor("cyan"); -} + $Marker =& $Plot->addNew('Image_Graph_Marker_Value', IMAGE_GRAPH_PCT_Y_TOTAL); + $PointingMarker =& $Plot->addNew('Image_Graph_Marker_Pointing_Angular', array(20, &$Marker)); + $Plot->setMarker($PointingMarker); + $Marker->setDataPreprocessor(Image_Graph::factory('Image_Graph_DataPreprocessor_Formatted', '%0.1f%%')); -// Default 'cosmetic' -- Alejandro -$Graph->setBackgroundColor('gray@0.2'); -$Graph->setBorderColor('black'); -$Graph->setPadding(10); -$Plotarea->setBackgroundColor('white'); -$Plotarea->setBorderColor('black'); -$Plotarea->setPadding(10); -$Plot->setFillStyle($fill); -$Plot->Radius = 2; + // background of the whole drawing board: + $Graph->setBackground(Image_Graph::factory('gradient', array(IMAGE_GRAPH_GRAD_VERTICAL, 'silver@0.5', 'white'))); -// Show time! -- Alejandro -$Graph->done(); + $Graph->setBorderColor('black'); + $Graph->setPadding(10); + + // background of the plotarea only: + $Plotarea->setBackgroundColor('white'); + $Plotarea->setBorderColor('black'); + $Plotarea->setPadding(20); + $Plotarea->showShadow(); + + + // and now all the filling tasks (gradients and the like) of the plot: + if ($style == "bar") + { + $Plot->setFillStyle(Image_Graph::factory('gradient', array(IMAGE_GRAPH_GRAD_VERTICAL, 'white', 'red'))); + } + elseif ($style == "line") + { + $Plot->setFillStyle(Image_Graph::factory('gradient', array(IMAGE_GRAPH_GRAD_VERTICAL, 'orange', 'lightyellow'))); + } + elseif ($style == "pie") + // colours are each time determined randomly: + // TODO: + // While each colour name is taken only once rather than twice + // or multiple times, the colours for two different colour names + // may appear on the screen as if they were identical. Some names + // may also simply be aliases. This can only be solved by removing + // the corresponding colour names from this list: + { + $mycolors = "aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgreen darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkturquoise darkviolet deeppink deepskyblue dimgray dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray green greenyellow honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgreen lightgrey lightpink lightsalmon lightseagreen lightskyblue lightslategray lightsteelblue lightyellow lime limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray snow springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen"; + $color_array = explode(" ", $mycolors); + $num_colors = count($color_array); + shuffle($color_array); + $FillArray =& Image_Graph::factory('Image_Graph_Fill_Array'); + for ($n = 0, $array_index = 0; + $n < $number_elements; + $n++, $array_index++) + { + if ($array_index >= $num_colors) + // then restart from the beginning + { + $array_index = 0; + } + $color_to_use = $color_array[$array_index]; + $FillArray->addNew('gradient', array(IMAGE_GRAPH_GRAD_RADIAL, 'white', $color_to_use)); + } + + // If there are a lot elements, we need some more space at the bottom: + // (not really a good solution) + /* + if ($number_elements >= 10) + { + $Graph->setPadding(90); + } + */ + $Plot->setFillStyle($FillArray); + $Plot->Radius = 2; + } + else + { + error_log("$style is an unsupported chart style"); + } + + // Show time! -- Alejandro + $Graph->done(); + + /* + * vim:shiftwidth=2:tabstop=2 + */ ?> |