18 function __construct( $filename = "graph.png" )
20 $this->output_file = 'templates_c/' . $filename;
23 public function SetData( $data_in, $type, $data_type, $shading = 5 )
25 $this->data = $data_in;
27 $this->data_type = $data_type;
28 $this->shadding = $shading;
31 public function SetGraphSize( $width, $height )
33 $this->width = $width;
34 $this->height = $height;
37 public function SetTitle( $title )
40 $this->title = $title;
42 die( "Please provide a non empty title for the graph" );
45 public function SetColors( $colors )
47 if( is_array( $colors ) )
48 $this->colors = $colors;
50 die( "Please provide a array in BGraph->SetColors()" );
53 public function Get_Image_file()
55 return $this->output_file;
58 public function Render()
61 $this->plot = new PHPlot( $this->width, $this->height );
63 // Render to file instead of screen
64 $this->plot->SetOutputFile( $this->output_file );
65 $this->plot->SetFileFormat("png");
66 $this->plot->SetIsInline( true );
69 $this->plot->SetImageBorderType('plain');
71 // Data, type and data type
72 $this->plot->SetPlotType( $this->type );
73 $this->plot->SetDataType( $this->data_type );
74 $this->plot->SetDataValues( $this->data );
77 $this->plot->SetDataColors( $this->colors );
80 $this->plot->SetShading( $this->shading );
83 $this->plot->SetImageBorderType( 'none' );
85 // Plot area (calculated regarding the width and height of the graph)
86 if( $this->type == 'pie' )
87 $this->plot->SetPlotAreaPixels( 10, 10, ($this->width / 2), $this->height-10 );
89 // Legend position (calculated regarding the width and height of the graph)
90 $this->plot->SetLegendPixels( ($this->width / 2) + 10, 25 );
92 // Labels scale position
93 if( $this->type == 'pie' )
94 $this->plot->SetLabelScalePosition( 0.2 );
97 $this->plot->SetTitle( $this->title );
100 if( $this->type != 'bars' ) {
102 foreach( $this->data as $key => $legend ) {
103 $this->plot->SetLegend( implode(': ',$legend) );
107 # Turn off X tick labels and ticks because they don't apply here:
108 $this->plot->SetXTickLabelPos('none');
109 $this->plot->SetXTickPos('none');
110 $this->plot->SetPlotAreaWorld(NULL, 0, NULL, NULL);
113 $this->plot->DrawGraph();
114 } // end function Render()
115 } // end BGraph classe
129 function BShowGraph($datos,$title,$xlabel,$ylabel,$leyenda,$tipo="lines") {
133 require_once ("external_packages/phplot/phplot.php");
135 if ( empty($this->sizex) || empty($this->sizey) ) { //Default size
136 $this->sizex = "600";
137 $this->sizey = "400";
139 if ( empty($this->MarginBottom) ) {
140 $this->MarginBottom = 120;
144 // $bgcolor = array(222,206,215); // Background color of graph
145 $bgcolor = array(207,231,231);
146 $fgcolor = array(110,41,57);
150 $graph = new PHPlot($this->sizex,$this->sizey,"","");
153 $graph->setDataType($type);
155 $graph->SetDataValues($datos);
156 $graph->SetPlotType($tipo);
157 // $graph->SetUseTTF(1);
158 $graph->SetBackgroundColor($bgcolor);
160 $graph->SetLegendPixels(1,20);
161 $graph->SetDataColors(array('SkyBlue','purple','PeachPuff','aquamarine1','#2CB04B','beige','#9F865F','#135568','orchid','navy','red', 'black', 'blue', 'green', 'brown', 'yellow','cyan','orange','#B9F5A7','#AFAFAF'));
162 $graph->SetTitle($title);
163 $graph->SetXLabel($xlabel);
164 $graph->SetYLabel($ylabel);
165 $graph->SetPlotAreaWorld("","","","");
167 if ( count($datos) > 5 )
168 $graph->SetXDataLabelAngle(90);
170 $graph->SetXDataLabelAngle(0);
171 $graph->SetNumXTicks(1);
172 // $graph->SetXDataLabelPos('none');
173 // $graph->SetXTickLabelPos('plotdown');
175 // $graph->SetXGridLabelType("time");
176 // $graph->SetXTimeFormat("%b ") ;
178 if ( $this->Leg == 1 ) {
179 $this->MarginLeftWithLegend($legend);
180 $graph->SetMarginsPixels($this->MarginLeft,10,35,$this->MarginBottom);
181 $graph->SetLegend($legend);
184 $graph->SetMarginsPixels(90,35,35,$this->MarginBottom);
185 // $graph->SetDataColors(array($fgcolor),array( "black"));
186 $graph->SetFileFormat( "png");
187 // $graph->DoScaleData(1,1);
188 // $graph->DoMovingAverage(1,1,1);
190 // FIX ME -- to round y axis.
191 $vtick = strlen (round ($graph->max_y));
193 for ($i=1;$i < $vtick; $i++)
195 if (strlen($graph->max_y-$res) != $vtick )
197 $graph->SetVertTickIncrement($res);
203 //Estupidez que tengo que cambiar. !!!!!!!!!!!
204 function SetDataType($typ) {
210 function MarginLeftWithLegend($clients) {
214 while (next($clients)) {
215 $tmp = strlen(current($clients));
216 if ( $tmp > $maxlen )
219 $this->MarginLeft = $maxlen * 9;
228 class BCreateGraph extends BGraph {
235 var $elapsed; // Default elapsed time to show complex graphs
239 function BCreateGraph() {
241 $this->StartDate = "1900-01-01";
242 $this->EndDate = "4000-01-01";
243 $this->elapsed = LAST_DAY; // 24 hours in seconds.
249 function BCreate($server,$tipo_dato,$title,$tipo="bars",$xlabel="",$ylabel="") {
256 $this->clientes=array();
257 $DB_bacula = new Bweb();
258 $datos = $this->SQLPrepareData($server,$tipo_dato);
260 if ( empty($datos) ) { //No data = No stats = Empty graph
261 header("Content-type: image/png");
262 $img= @ImageCreate(200,100) or die ("Cannot intialize GD stream");
263 $bgc= ImageColorAllocate($img, 0, 255,255);
264 $txc= ImageColorAllocate($img, 0,0,0);
265 ImageString($img, 5, 4, 4, "No data to process", $txc);
271 if ( empty ($xlabel) ) { // If no label, table names like leyends
272 $xlabel=$derecha; $ylabel=$izquierda;
275 $this->SetDataType("text-data");
276 $this->BShowGraph($datos,$title,$xlabel,$ylabel,$this->clientes,$tipo);
282 function SQLPrepareData($servidor,$tipo_dato=0) { // Prepare bytes data from database.
288 if ( $tipo_dato<30 ) { // Simple graph. Only 2 data
292 case BACULA_TYPE_BYTES_FILES:
293 $izquierda="jobbytes";
296 case BACULA_TYPE_FILES_JOBID:
297 $izquierda="jobfiles";
301 $izquierda="jobbytes";
305 $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")
306 or die ("classes.inc: Error at query: 5");
307 while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
308 $whole_result[] = $this->array_merge_php4($row["$derecha"],$row[$izquierda]);
311 } else { // Complex graph. 3 or more data.
313 switch ( $tipo_dato )
315 case '30': // Unused, at this time.
316 $result = $DB_bacula->db_link->query("select JobBytes,JobFiles,Jobid from Job where Name='$servidor' order by EndTime asc")
317 or die ("classes.inc: Error at query: 6");
318 while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC) )
319 $whole_result[] = array_merge($row["Jobid"],$row["JobFiles"],$row["JobBytes"]);
322 case BACULA_TYPE_BYTES_ENDTIME_ALLJOBS: // Special: Generic graph from all clientes.
323 $i = -1; // Counter of number of jobs of one client. SP: Contador del nmero de jobs totales de un cliente.
324 $i2 = 0; // Counter of number of keys of array. SP: Contador del nmero de valores del array.
326 if ($DB_bacula->driver == "mysql") {
327 $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")
328 or die ("classes.inc: Error at query: 7");
329 $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")
330 or die ("classes.inc: Error at query: 8");
332 else if ($DB_bacula->driver == "pgsql") {
333 $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")
334 or die ("classes.inc: Error at query: 8");
335 $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")
336 or die ("classes.inc: Error at query: 9");
339 if (PEAR::isError($resdata))
340 die("classes.inc: Error at query: 9.1<br>".$resdata->getMessage());
341 while ( $tmpdata = $res->fetchRow() )
342 array_push($this->clientes,$tmpdata[0]);
345 // print_r ($this->clientes);
349 $spr = array(); // Temporal array
350 $spr2 = array(); // Temporal array
351 $whole_result = array();
354 while ( $tmpdata = $resdata->fetchRow() ) {
356 array_push($spr,$tmpdata[0]);
357 if ($DB_bacula->driver == "mysql")
358 $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")
359 or die ("classes.inc: Error at query: 10");
360 else if ($DB_bacula->driver == "pgsql") {
361 $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";
362 $result = $DB_bacula->db_link->query($query)
363 or die ("classes.inc: Error at query: 11");
365 while ( $row = $result->fetchRow(DB_FETCHMODE_ASSOC) ) {
366 $spr2 = array_merge($spr2,array($row["name"]=>$row["sum"]));
367 $i = $result->numRows();
375 reset ($this->clientes);
377 if ( $spr2[current($this->clientes)] != NULL)
378 array_push($spr,$spr2[current($this->clientes)]);
381 } while ( next($this->clientes) );
386 if ( $tmpdata[0] != $row["EndTime"] )
387 array_push($whole_result,$spr);
393 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
394 $tmp = count($whole_result[$i]);
395 if ( $i2 < $tmp ) // Estupidez?. Check this code later...
398 for ( $a = 0; $a <= $tmp; $a++ )
399 array_push($whole_result[$i],"0"); // Fill the array
402 // echo "DEBUG:<br>";
404 // print_r ($whole_result);
413 return $whole_result;
418 //Convert date from mysql to smarty. THE SAME FUNCTION AT 2 CLASSES. THIS WAY IS BUGGY. TO SOLVE LATER.
419 function PrepareDate($StartDateMonth,$StartDateDay,$StartDateYear,$EndDateMonth,$EndDateDay,$EndDateYear){
421 $this->StartDate = $StartDateYear."-".$StartDateMonth."-".$StartDateDay." 00:00:00";
422 $this->EndDate = $EndDateYear."-".$EndDateMonth."-".$EndDateDay." 23:59:00";
427 function array_merge_php4($array1,$array2) {
430 foreach(func_get_args() as $arg) {
434 foreach($arg as $key=>$val){