2 require_once ("external_packages/phplot/phplot.php");
19 function __construct( $filename = "graph.png" )
21 if( !empty($filename) )
22 $this->output_file = 'templates_c/' . $filename;
24 $this->output_file = 'templates_c/graph.png';
27 public function SetData( $data_in, $type, $data_type, $shading = 5 )
29 $this->data = $data_in;
31 $this->data_type = $data_type;
32 $this->shadding = $shading;
35 public function SetGraphSize( $width, $height )
37 $this->width = $width;
38 $this->height = $height;
41 public function SetTitle( $title )
44 $this->title = $title;
46 die( "Please provide a non empty title for the graph" );
49 public function SetColors( $colors )
51 if( is_array( $colors ) )
52 $this->colors = $colors;
54 die( "Please provide a array in BGraph->SetColors()" );
57 public function Get_Image_file()
59 return $this->output_file;
62 public function Render()
65 $this->plot = new PHPlot( $this->width, $this->height );
67 // Render to file instead of screen
68 $this->plot->SetIsInline( true );
69 $this->plot->SetOutputFile( $this->output_file );
71 $this->plot->SetImageBorderType('plain');
73 // Data, type and data type
74 $this->plot->SetPlotType( $this->type );
75 $this->plot->SetDataType( $this->data_type );
76 $this->plot->SetDataValues( $this->data );
79 $this->plot->SetDataColors( $this->colors );
82 $this->plot->SetShading( $this->shading );
85 $this->plot->SetImageBorderType( 'none' );
87 // Plot area (calculated regarding the width and height of the graph)
88 if( $this->type == 'pie' )
89 $this->plot->SetPlotAreaPixels( 10, 10, ($this->width / 2), $this->height-10 );
91 // Legend position (calculated regarding the width and height of the graph)
92 $this->plot->SetLegendPixels( ($this->width / 2) + 10, 25 );
94 // Labels scale position
95 if( $this->type == 'pie' )
96 $this->plot->SetLabelScalePosition( 0.2 );
99 $this->plot->SetTitle( $this->title );
101 // Setting up legends
102 if( $this->type != 'bars' ) {
104 foreach( $this->data as $key => $legend ) {
105 $this->plot->SetLegend( implode(': ',$legend) );
109 # Turn off X tick labels and ticks because they don't apply here:
110 $this->plot->SetXTickLabelPos('none');
111 $this->plot->SetXTickPos('none');
112 $this->plot->SetPlotAreaWorld(NULL, 0, NULL, NULL);
115 $this->plot->DrawGraph();
116 } // end function Render()
130 function BShowGraph($datos,$title,$xlabel,$ylabel,$leyenda,$tipo="lines") {
134 require_once ("external_packages/phplot/phplot.php");
136 if ( empty($this->sizex) || empty($this->sizey) ) { //Default size
137 $this->sizex = "600";
138 $this->sizey = "400";
140 if ( empty($this->MarginBottom) ) {
141 $this->MarginBottom = 120;
145 // $bgcolor = array(222,206,215); // Background color of graph
146 $bgcolor = array(207,231,231);
147 $fgcolor = array(110,41,57);
151 $graph = new PHPlot($this->sizex,$this->sizey,"","");
154 $graph->setDataType($type);
156 $graph->SetDataValues($datos);
157 $graph->SetPlotType($tipo);
158 // $graph->SetUseTTF(1);
159 $graph->SetBackgroundColor($bgcolor);
161 $graph->SetLegendPixels(1,20);
162 $graph->SetDataColors(array('SkyBlue','purple','PeachPuff','aquamarine1','#2CB04B','beige','#9F865F','#135568','orchid','navy','red', 'black', 'blue', 'green', 'brown', 'yellow','cyan','orange','#B9F5A7','#AFAFAF'));
163 $graph->SetTitle($title);
164 $graph->SetXLabel($xlabel);
165 $graph->SetYLabel($ylabel);
166 $graph->SetPlotAreaWorld("","","","");
168 if ( count($datos) > 5 )
169 $graph->SetXDataLabelAngle(90);
171 $graph->SetXDataLabelAngle(0);
172 $graph->SetNumXTicks(1);
173 // $graph->SetXDataLabelPos('none');
174 // $graph->SetXTickLabelPos('plotdown');
176 // $graph->SetXGridLabelType("time");
177 // $graph->SetXTimeFormat("%b ") ;
179 if ( $this->Leg == 1 ) {
180 $this->MarginLeftWithLegend($legend);
181 $graph->SetMarginsPixels($this->MarginLeft,10,35,$this->MarginBottom);
182 $graph->SetLegend($legend);
185 $graph->SetMarginsPixels(90,35,35,$this->MarginBottom);
186 // $graph->SetDataColors(array($fgcolor),array( "black"));
187 $graph->SetFileFormat( "png");
188 // $graph->DoScaleData(1,1);
189 // $graph->DoMovingAverage(1,1,1);
191 // FIX ME -- to round y axis.
192 $vtick = strlen (round ($graph->max_y));
194 for ($i=1;$i < $vtick; $i++)
196 if (strlen($graph->max_y-$res) != $vtick )
198 $graph->SetVertTickIncrement($res);
204 //Estupidez que tengo que cambiar. !!!!!!!!!!!
205 function SetDataType($typ) {
211 function MarginLeftWithLegend($clients) {
215 while (next($clients)) {
216 $tmp = strlen(current($clients));
217 if ( $tmp > $maxlen )
220 $this->MarginLeft = $maxlen * 9;
229 class BCreateGraph extends BGraph {
236 var $elapsed; // Default elapsed time to show complex graphs
240 function BCreateGraph() {
242 $this->StartDate = "1900-01-01";
243 $this->EndDate = "4000-01-01";
244 $this->elapsed = "86400"; // 24 hours in seconds.
250 function BCreate($server,$tipo_dato,$title,$tipo="bars",$xlabel="",$ylabel="") {
257 $this->clientes=array();
258 $DB_bacula = new Bweb();
259 $datos = $this->SQLPrepareData($server,$tipo_dato);
261 if ( empty($datos) ) { //No data = No stats = Empty graph
262 header("Content-type: image/png");
263 $img= @ImageCreate(200,100) or die ("Cannot intialize GD stream");
264 $bgc= ImageColorAllocate($img, 0, 255,255);
265 $txc= ImageColorAllocate($img, 0,0,0);
266 ImageString($img, 5, 4, 4, "No data to process", $txc);
272 if ( empty ($xlabel) ) { // If no label, table names like leyends
273 $xlabel=$derecha; $ylabel=$izquierda;
276 $this->SetDataType("text-data");
277 $this->BShowGraph($datos,$title,$xlabel,$ylabel,$this->clientes,$tipo);
283 function SQLPrepareData($servidor,$tipo_dato=0) { // Prepare bytes data from database.
289 if ( $tipo_dato<30 ) { // Simple graph. Only 2 data
293 case BACULA_TYPE_BYTES_FILES:
294 $izquierda="jobbytes";
297 case BACULA_TYPE_FILES_JOBID:
298 $izquierda="jobfiles";
302 $izquierda="jobbytes";
306 $result = $DB_bacula->db_link->query("select $derecha,$izquierda from Job where Name='$servidor' and EndTime < '$this->EndDate' and EndTime > '$this->StartDate' order by SchedTime asc")
307 or die ("classes.inc: Error at query: 5");
308 while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
309 $whole_result[] = $this->array_merge_php4($row["$derecha"],$row[$izquierda]);
312 } else { // Complex graph. 3 or more data.
314 switch ( $tipo_dato )
316 case '30': // Unused, at this time.
317 $result = $DB_bacula->db_link->query("select JobBytes,JobFiles,Jobid from Job where Name='$servidor' order by EndTime asc")
318 or die ("classes.inc: Error at query: 6");
319 while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC) )
320 $whole_result[] = array_merge($row["Jobid"],$row["JobFiles"],$row["JobBytes"]);
323 case BACULA_TYPE_BYTES_ENDTIME_ALLJOBS: // Special: Generic graph from all clientes.
324 $i = -1; // Counter of number of jobs of one client. SP: Contador del nmero de jobs totales de un cliente.
325 $i2 = 0; // Counter of number of keys of array. SP: Contador del nmero de valores del array.
327 if ($DB_bacula->driver == "mysql") {
328 $res = $DB_bacula->db_link->query("select Name from Job where UNIX_TIMESTAMP(EndTime) > UNIX_TIMESTAMP(NOW())-$this->elapsed group by Name order by Name desc")
329 or die ("classes.inc: Error at query: 7");
330 $resdata = $DB_bacula->db_link->query("select date_format(EndTime,\"%Y-%m-%d\") from Job where UNIX_TIMESTAMP(EndTime) > UNIX_TIMESTAMP(NOW())-$this->elapsed group by date_format(EndTime, \"%Y-%m-%d\") order by EndTime")
331 or die ("classes.inc: Error at query: 8");
333 else if ($DB_bacula->driver == "pgsql") {
334 $res = $DB_bacula->db_link->query("select Name from Job where EndTime > now() - 1*interval'$this->elapsed s' group by Name order by Name desc")
335 or die ("classes.inc: Error at query: 8");
336 $resdata = $DB_bacula->db_link->query("select to_char(EndTime,'YY-MM-DD') from Job where EndTime > NOW() - 1*interval'$this->elapsed s' group by EndTime order by EndTime")
337 or die ("classes.inc: Error at query: 9");
340 if (PEAR::isError($resdata))
341 die("classes.inc: Error at query: 9.1<br>".$resdata->getMessage());
342 while ( $tmpdata = $res->fetchRow() )
343 array_push($this->clientes,$tmpdata[0]);
346 // print_r ($this->clientes);
350 $spr = array(); // Temporal array
351 $spr2 = array(); // Temporal array
352 $whole_result = array();
355 while ( $tmpdata = $resdata->fetchRow() ) {
357 array_push($spr,$tmpdata[0]);
358 if ($DB_bacula->driver == "mysql")
359 $result = $DB_bacula->db_link->query("select date_format(EndTime,\"%Y-%m-%d\"),SUM(JobBytes) as sum,Name as name,count(Name) as Nname from Job WHERE EndTime like '$tmpdata[0]%' group by Name order by Name desc")
360 or die ("classes.inc: Error at query: 10");
361 else if ($DB_bacula->driver == "pgsql") {
362 $query = "select to_char(EndTime,'YY-MM-DD'),SUM(JobBytes) as sum,Name,count(Name) as Nname from Job WHERE EndTime like '%$tmpdata[0]%' group by EndTime,Name order by Name desc";
363 $result = $DB_bacula->db_link->query($query)
364 or die ("classes.inc: Error at query: 11");
366 while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
367 $spr2 = array_merge($spr2,array($row["name"]=>$row["sum"]));
368 $i = $result->numRows();
376 reset ($this->clientes);
378 if ( $spr2[current($this->clientes)] != NULL)
379 array_push($spr,$spr2[current($this->clientes)]);
382 } while ( next($this->clientes) );
387 if ( $tmpdata[0] != $row["EndTime"] )
388 array_push($whole_result,$spr);
394 for ( $i = 0; $i < count($whole_result); $i++ ) { // To equal the arrays so that the graph is not unsquared. SP:Igualamos las matrices para que la gr?ica no se descuadre
395 $tmp = count($whole_result[$i]);
396 if ( $i2 < $tmp ) // Estupidez?. Check this code later...
399 for ( $a = 0; $a <= $tmp; $a++ )
400 array_push($whole_result[$i],"0"); // Fill the array
403 // echo "DEBUG:<br>";
405 // print_r ($whole_result);
414 return $whole_result;
419 //Convert date from mysql to smarty. THE SAME FUNCTION AT 2 CLASSES. THIS WAY IS BUGGY. TO SOLVE LATER.
420 function PrepareDate($StartDateMonth,$StartDateDay,$StartDateYear,$EndDateMonth,$EndDateDay,$EndDateYear){
422 $this->StartDate = $StartDateYear."-".$StartDateMonth."-".$StartDateDay." 00:00:00";
423 $this->EndDate = $EndDateYear."-".$EndDateMonth."-".$EndDateDay." 23:59:00";
428 function array_merge_php4($array1,$array2) {
431 foreach(func_get_args() as $arg) {
435 foreach($arg as $key=>$val){