[gosa.git] / gosa-core / plugins / generic / statistics / chartClasses / class_categoryActionOverTime.inc
1 <?php
3 class categoryActionsOverTime extends statChart
4 {
5 var $title = 'Plugin usage over time';
6 var $enableCaching = TRUE;
8 function __construct(&$config)
9 {
10 parent::__construct($config);
11 $this->graphName = get_class();
12 }
15 /*! \brief Generates the line-graph which displays the plugin usage over time.
16 */
17 function render()
18 {
19 $lineMax = 100;
20 $gData = $this->graphData;
21 $errorMax = (max($gData['errorsPerInterval']) < 100)? 100:max($gData['errorsPerInterval']);
22 $dataSet = new pData;
23 $seriesCnt = 0;
24 foreach($gData['actionsPerInterval'] as $category => $entriesPerDate){
25 if(empty($category) || in_array($category, $this->skipSeries)) continue;
27 // Add results to our data set.
28 $dataSet->AddPoint($entriesPerDate, $category);
29 $dataSet->SetSerieName($this->getCategoryTranslation($category), $category);
30 $dataSet->AddSerie($category);
32 // Detect maximum value, to adjust the Y-Axis
33 $tmpMax = max($entriesPerDate);
34 if($tmpMax > $lineMax) $lineMax = $tmpMax;
35 $seriesCnt ++;
36 }
38 // Keep a list of all selecteable data-series, to allow the user to disable
39 // or enable series on demand.
40 $this->seriesList = array();
41 foreach($gData['actionsPerInterval'] as $key => $data){
42 $this->seriesList[$key] = $this->getCategoryTranslation($key);
43 }
44 $this->seriesList['errorsPerInterval'] = _("Error");
46 // Create a dataSet containing all series
47 $allSeriesDataSet = clone $dataSet;
48 if(!in_array('errorsPerInterval', $this->skipSeries)){
49 $allSeriesDataSet->AddPoint($gData['errorsPerInterval'], 'Errors');
50 $allSeriesDataSet->SetSerieName(_('Error'), 'Errors');
51 $allSeriesDataSet->AddSerie('Errors');
52 }
54 // Add timeline
55 $dataSet->AddPoint($gData['dates'], 'date');
56 $dataSet->SetAbsciseLabelSerie('date');
58 // Read graph from cache?
59 $pCache = new pCache('/var/spool/gosa/');
60 if($this->enableCaching && $pCache->IsInCache(get_class(),$allSeriesDataSet->GetData())){
61 $filename = $pCache->GetHash(get_class(),$allSeriesDataSet->GetData());
62 $filename = '/var/spool/gosa/'.$filename;
63 if(file_exists($filename) && is_readable($filename)){
64 $this->graphID = preg_replace("/[^0-9]/","",microtime(TRUE)).rand(0,99999);
65 session::set('statistics::graphFile'.$this->graphID,$filename);
66 return;
67 }
68 }
70 $chart = new pChart(900,230);
71 $chart->setFixedScale(0.000,$lineMax);
72 $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
73 $chart->setGraphArea(50,8,830,200);
74 $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
75 $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);
76 $chart->drawGraphArea(255,255,255,TRUE);
77 $chart->drawGrid(4,TRUE,200,200,200,50);
78 $chart->drawTreshold(0,143,55,72,TRUE,TRUE);
79 $chart->drawTitle(50,22,_($this->title),50,50,50,585);
80 $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, TRUE);
82 // Only draw this graph if we've at least one series to draw!
83 if($seriesCnt){
84 $chart->drawFilledLineGraph($dataSet->GetData(),$dataSet->GetDataDescription(),50,TRUE);
85 }
87 // Do we've to add the errors series?
88 // If we have to, then add the error-data-series.
89 // and set the color for the new error-series to red.
90 if(!in_array('errorsPerInterval', $this->skipSeries)){
92 // Set the color for the error Series to 'red'.
93 // This has to be done before drawing the legend.
94 $chart->setColorPalette($seriesCnt,255,0,0);
96 $dataSet->AddPoint($gData['errorsPerInterval'], 'Errors');
97 $dataSet->SetSerieName(_('Error'), 'Errors');
98 $dataSet->AddSerie('Errors');
99 }
101 $chart->drawLegend(750,30,$dataSet->GetDataDescription(),255,255,255);
103 // Draw the error graph on top of the other graphs now.
104 // But remove the category-graph before.
105 if(!in_array('errorsPerInterval', $this->skipSeries)){
107 // Remove all graph series and add the error-series, then draw the new graph.
108 // (It is not relevant if it was really added before, so we simply remove all series!)
109 foreach($gData['actionsPerInterval'] as $category => $data){
110 $dataSet->RemoveSerie($category);
111 }
112 $chart->setFixedScale(0,$errorMax);
113 $chart->drawRightScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,120,150,150,TRUE,0,2, TRUE);
114 $chart->drawBarGraph($dataSet->GetData(),$dataSet->GetDataDescription());
115 }
117 // Generate new and unique graph id
118 $this->graphID = preg_replace("/[^0-9]/","",microtime(TRUE));
119 $file = '/tmp/graph_'.$this->graphID;
120 $chart->Render($file);
121 session::set('statistics::graphFile'.$this->graphID,$file);
122 $pCache->WriteToCache(get_class(),$allSeriesDataSet->GetData(),$chart);
124 return;
125 }
127 }
128 ?>