]> git.sur5r.net Git - bacula/bacula/blob - gui/bacula-web/includes/graph/bgraph.class.php
bacula-web: Improved php code in CountJobsbyLevel() function
[bacula/bacula] / gui / bacula-web / includes / graph / bgraph.class.php
1 <?php
2 /* 
3 +-------------------------------------------------------------------------+
4 | Copyright 2010-2011, Davide Franco                                              |
5 |                                                                         |
6 | This program is free software; you can redistribute it and/or           |
7 | modify it under the terms of the GNU General Public License             |
8 | as published by the Free Software Foundation; either version 2          |
9 | of the License, or (at your option) any later version.                  |
10 |                                                                         |
11 | This program is distributed in the hope that it will be useful,         |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
14 | GNU General Public License for more details.                            |
15 +-------------------------------------------------------------------------+ 
16 */
17
18 class BGraph{
19         private $title;
20         private $ytitle;
21         
22         private $data;
23         private $data_type;
24         private $type;
25         
26         private $colors;
27         private $shading;
28         
29         private $width;
30         private $height;
31         private $output_file;
32         private $plot;
33         
34         function __construct( $filename = "graph.png" )
35         {
36                 $this->output_file = 'templates_c/' . $filename;
37         }
38         
39         public function SetData( $data_in, $type, $data_type, $shading = 5 )
40         {
41                 $this->data             = $data_in;
42                 $this->type             = $type;
43                 $this->data_type        = $data_type;
44                 $this->shadding         = $shading;
45         }
46         
47         public function SetGraphSize( $width, $height )
48         {
49                 $this->width  = $width;
50                 $this->height = $height;
51         }
52         
53         public function SetTitle( $title )
54         {
55                 if( !empty($title) )
56                         $this->title = $title;
57                 else
58                         die( "Please provide a non empty title for the graph" );
59         }
60         
61         public function SetYTitle( $ytitle )
62         {
63                 if( !empty($ytitle) )
64                         $this->ytitle = $ytitle;
65                 else
66                         die( "Please provide a non empty title for the Y axis" );
67         }
68         
69         public function SetColors( $colors )
70         {
71                 if( is_array( $colors ) )
72                         $this->colors = $colors;
73                 else
74                         die( "Please provide a array in BGraph->SetColors()" );
75         }
76         
77         public function Get_Image_file()
78         {
79                 return $this->output_file;
80         }
81         
82         public function Render()
83         {
84                 // Setting the size
85                 $this->plot = new PHPlot( $this->width, $this->height );
86                 
87                 // Render to file instead of screen
88                 $this->plot->SetOutputFile( $this->output_file );
89                 $this->plot->SetFileFormat("png");
90                 $this->plot->SetIsInline( true );
91                 
92                 
93                 $this->plot->SetImageBorderType('plain');
94
95                 // Data, type and data type
96                 $this->plot->SetPlotType( $this->type );
97                 $this->plot->SetDataType( $this->data_type );
98                 $this->plot->SetDataValues( $this->data );
99                 
100                 // Plot colors
101                 $this->plot->SetDataColors( $this->colors );
102                 
103                 // Plot shading
104                 $this->plot->SetShading( $this->shading );
105                 
106                 // Image border
107                 $this->plot->SetImageBorderType( 'none' );
108
109                 // Plot area (calculated regarding the width and height of the graph)
110                 if( $this->type == 'pie' )
111                         $this->plot->SetPlotAreaPixels( 10, 10, ($this->width / 2), $this->height-10 );
112                 
113                 // Legend position (calculated regarding the width and height of the graph)
114                 $this->plot->SetLegendPixels( ($this->width / 2) + 10, 25 );
115
116                 // Labels scale position
117                 if( $this->type == 'pie' )
118                         $this->plot->SetLabelScalePosition( 0.2 );
119                 
120                 // Graph title
121                 $this->plot->SetTitle( $this->title );
122                 $this->plot->SetYTitle( $this->ytitle );
123
124                 // Setting up legends
125                 if( $this->type != 'bars' ) {
126                         $legends = array();
127                         foreach( $this->data as $key => $legend ) {
128                                 $this->plot->SetLegend( implode(': ',$legend) );
129                         }
130                 }
131
132                 # Turn off X tick labels and ticks because they don't apply here:
133                 $this->plot->SetXTickLabelPos('none');
134                 $this->plot->SetXTickPos('none');
135                 $this->plot->SetPlotAreaWorld(NULL, 0, NULL, NULL);
136
137
138                 $this->plot->DrawGraph();
139         } // end function Render()
140 } // end BGraph classe
141
142 ?>