2 require_once ("external_packages/phplot/phplot.php");
19 function __construct( $filename = "graph.png" )
21 if( !empty($filename) )
22 $this->output_file = "graph.png";
24 $this->output_file = $filename;
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 //$this->plot->SetLegend(array('Engineering', 'Manufacturing', 'Administration'));
102 // Setting up legends
104 foreach( $this->data as $key => $legend ) {
105 $this->plot->SetLegend( implode(': ',$legend) );
108 # Turn off X tick labels and ticks because they don't apply here:
109 $this->plot->SetXTickLabelPos('none');
110 $this->plot->SetXTickPos('none');
112 $this->plot->DrawGraph();
113 } // end function Render()
127 function BShowGraph($datos,$title,$xlabel,$ylabel,$leyenda,$tipo="lines") {
131 require_once ("external_packages/phplot/phplot.php");
133 if ( empty($this->sizex) || empty($this->sizey) ) { //Default size
134 $this->sizex = "600";
135 $this->sizey = "400";
137 if ( empty($this->MarginBottom) ) {
138 $this->MarginBottom = 120;
142 // $bgcolor = array(222,206,215); // Background color of graph
143 $bgcolor = array(207,231,231);
144 $fgcolor = array(110,41,57);
148 $graph = new PHPlot($this->sizex,$this->sizey,"","");
151 $graph->setDataType($type);
153 $graph->SetDataValues($datos);
154 $graph->SetPlotType($tipo);
155 // $graph->SetUseTTF(1);
156 $graph->SetBackgroundColor($bgcolor);
158 $graph->SetLegendPixels(1,20);
159 $graph->SetDataColors(array('SkyBlue','purple','PeachPuff','aquamarine1','#2CB04B','beige','#9F865F','#135568','orchid','navy','red', 'black', 'blue', 'green', 'brown', 'yellow','cyan','orange','#B9F5A7','#AFAFAF'));
160 $graph->SetTitle($title);
161 $graph->SetXLabel($xlabel);
162 $graph->SetYLabel($ylabel);
163 $graph->SetPlotAreaWorld("","","","");
165 if ( count($datos) > 5 )
166 $graph->SetXDataLabelAngle(90);
168 $graph->SetXDataLabelAngle(0);
169 $graph->SetNumXTicks(1);
170 // $graph->SetXDataLabelPos('none');
171 // $graph->SetXTickLabelPos('plotdown');
173 // $graph->SetXGridLabelType("time");
174 // $graph->SetXTimeFormat("%b ") ;
176 if ( $this->Leg == 1 ) {
177 $this->MarginLeftWithLegend($legend);
178 $graph->SetMarginsPixels($this->MarginLeft,10,35,$this->MarginBottom);
179 $graph->SetLegend($legend);
182 $graph->SetMarginsPixels(90,35,35,$this->MarginBottom);
183 // $graph->SetDataColors(array($fgcolor),array( "black"));
184 $graph->SetFileFormat( "png");
185 // $graph->DoScaleData(1,1);
186 // $graph->DoMovingAverage(1,1,1);
188 // FIX ME -- to round y axis.
189 $vtick = strlen (round ($graph->max_y));
191 for ($i=1;$i < $vtick; $i++)
193 if (strlen($graph->max_y-$res) != $vtick )
195 $graph->SetVertTickIncrement($res);
201 //Estupidez que tengo que cambiar. !!!!!!!!!!!
202 function SetDataType($typ) {
208 function MarginLeftWithLegend($clients) {
212 while (next($clients)) {
213 $tmp = strlen(current($clients));
214 if ( $tmp > $maxlen )
217 $this->MarginLeft = $maxlen * 9;
226 class BCreateGraph extends BGraph {
233 var $elapsed; // Default elapsed time to show complex graphs
237 function BCreateGraph() {
239 $this->StartDate = "1900-01-01";
240 $this->EndDate = "4000-01-01";
241 $this->elapsed = "86400"; // 24 hours in seconds.
247 function BCreate($server,$tipo_dato,$title,$tipo="bars",$xlabel="",$ylabel="") {
254 $this->clientes=array();
255 $DB_bacula = new Bweb();
256 $datos = $this->SQLPrepareData($server,$tipo_dato);
258 if ( empty($datos) ) { //No data = No stats = Empty graph
259 header("Content-type: image/png");
260 $img= @ImageCreate(200,100) or die ("Cannot intialize GD stream");
261 $bgc= ImageColorAllocate($img, 0, 255,255);
262 $txc= ImageColorAllocate($img, 0,0,0);
263 ImageString($img, 5, 4, 4, "No data to process", $txc);
269 if ( empty ($xlabel) ) { // If no label, table names like leyends
270 $xlabel=$derecha; $ylabel=$izquierda;
273 $this->SetDataType("text-data");
274 $this->BShowGraph($datos,$title,$xlabel,$ylabel,$this->clientes,$tipo);
280 function SQLPrepareData($servidor,$tipo_dato=0) { // Prepare bytes data from database.
286 if ( $tipo_dato<30 ) { // Simple graph. Only 2 data
290 case BACULA_TYPE_BYTES_FILES:
291 $izquierda="jobbytes";
294 case BACULA_TYPE_FILES_JOBID:
295 $izquierda="jobfiles";
299 $izquierda="jobbytes";
303 $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")
304 or die ("classes.inc: Error at query: 5");
305 while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
306 $whole_result[] = $this->array_merge_php4($row["$derecha"],$row[$izquierda]);
309 } else { // Complex graph. 3 or more data.
311 switch ( $tipo_dato )
313 case '30': // Unused, at this time.
314 $result = $DB_bacula->db_link->query("select JobBytes,JobFiles,Jobid from Job where Name='$servidor' order by EndTime asc")
315 or die ("classes.inc: Error at query: 6");
316 while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC) )
317 $whole_result[] = array_merge($row["Jobid"],$row["JobFiles"],$row["JobBytes"]);
320 case BACULA_TYPE_BYTES_ENDTIME_ALLJOBS: // Special: Generic graph from all clientes.
321 $i = -1; // Counter of number of jobs of one client. SP: Contador del nmero de jobs totales de un cliente.
322 $i2 = 0; // Counter of number of keys of array. SP: Contador del nmero de valores del array.
324 if ($DB_bacula->driver == "mysql") {
325 $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")
326 or die ("classes.inc: Error at query: 7");
327 $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")
328 or die ("classes.inc: Error at query: 8");
330 else if ($DB_bacula->driver == "pgsql") {
331 $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")
332 or die ("classes.inc: Error at query: 8");
333 $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")
334 or die ("classes.inc: Error at query: 9");
337 if (PEAR::isError($resdata))
338 die("classes.inc: Error at query: 9.1<br>".$resdata->getMessage());
339 while ( $tmpdata = $res->fetchRow() )
340 array_push($this->clientes,$tmpdata[0]);
343 // print_r ($this->clientes);
347 $spr = array(); // Temporal array
348 $spr2 = array(); // Temporal array
349 $whole_result = array();
352 while ( $tmpdata = $resdata->fetchRow() ) {
354 array_push($spr,$tmpdata[0]);
355 if ($DB_bacula->driver == "mysql")
356 $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")
357 or die ("classes.inc: Error at query: 10");
358 else if ($DB_bacula->driver == "pgsql") {
359 $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";
360 $result = $DB_bacula->db_link->query($query)
361 or die ("classes.inc: Error at query: 11");
363 while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
364 $spr2 = array_merge($spr2,array($row["name"]=>$row["sum"]));
365 $i = $result->numRows();
373 reset ($this->clientes);
375 if ( $spr2[current($this->clientes)] != NULL)
376 array_push($spr,$spr2[current($this->clientes)]);
379 } while ( next($this->clientes) );
384 if ( $tmpdata[0] != $row["EndTime"] )
385 array_push($whole_result,$spr);
391 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
392 $tmp = count($whole_result[$i]);
393 if ( $i2 < $tmp ) // Estupidez?. Check this code later...
396 for ( $a = 0; $a <= $tmp; $a++ )
397 array_push($whole_result[$i],"0"); // Fill the array
400 // echo "DEBUG:<br>";
402 // print_r ($whole_result);
411 return $whole_result;
416 //Convert date from mysql to smarty. THE SAME FUNCTION AT 2 CLASSES. THIS WAY IS BUGGY. TO SOLVE LATER.
417 function PrepareDate($StartDateMonth,$StartDateDay,$StartDateYear,$EndDateMonth,$EndDateDay,$EndDateYear){
419 $this->StartDate = $StartDateYear."-".$StartDateMonth."-".$StartDateDay." 00:00:00";
420 $this->EndDate = $EndDateYear."-".$EndDateMonth."-".$EndDateDay." 23:59:00";
425 function array_merge_php4($array1,$array2) {
428 foreach(func_get_args() as $arg) {
432 foreach($arg as $key=>$val){