[ Index ]
 

Code source de Dolibarr 2.0.1

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/scripts/energie/ -> graph.php (source)

   1  <?PHP
   2  /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
   3   *
   4   * This program is free software; you can redistribute it and/or modify
   5   * it under the terms of the GNU General Public License as published by
   6   * the Free Software Foundation; either version 2 of the License, or
   7   * (at your option) any later version.
   8   *
   9   * This program is distributed in the hope that it will be useful,
  10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12   * GNU General Public License for more details.
  13   *
  14   * You should have received a copy of the GNU General Public License
  15   * along with this program; if not, write to the Free Software
  16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17   *
  18   * $Id: graph.php,v 1.6 2005/10/18 14:03:06 eldy Exp $
  19   * $Source: /cvsroot/dolibarr/dolibarr/scripts/energie/graph.php,v $
  20   */
  21  
  22  require_once ("../../htdocs/master.inc.php");
  23  
  24  require_once(DOL_DOCUMENT_ROOT."/energie/EnergieCompteur.class.php");
  25  require_once(DOL_DOCUMENT_ROOT."/energie/EnergieGroupe.class.php");
  26  
  27  include_once(JPGRAPH_DIR."jpgraph.php");
  28  include_once(JPGRAPH_DIR."jpgraph_line.php");
  29  include_once(JPGRAPH_DIR."jpgraph_bar.php");
  30  include_once(JPGRAPH_DIR."jpgraph_pie.php");
  31  include_once(JPGRAPH_DIR."jpgraph_error.php");
  32  include_once(JPGRAPH_DIR."jpgraph_canvas.php");
  33  
  34  $error = 0;
  35  
  36  $sql_c = "SELECT rowid";
  37  $sql_c .= " FROM ".MAIN_DB_PREFIX."energie_compteur";
  38  
  39  $resql_c = $db->query($sql_c);
  40  
  41  if ($resql_c)
  42  {
  43    $num_c = $db->num_rows($resql_c);
  44    $i_c = 0;
  45  
  46    if ($num_c > 0)
  47      {
  48        while ($i_c < $num_c)
  49      {
  50        $obj_c = $db->fetch_object($resql_c);
  51  
  52        $compteur_id = $obj_c->rowid;
  53  
  54        $labels = array();
  55        $datas = array();
  56        
  57        $ydatas = array();
  58        $mdatas = array();
  59        $wdatas = array();
  60  
  61  
  62        $sql = "SELECT ".$db->pdate("date_releve")." as date_releve, valeur";
  63        $sql .= " FROM ".MAIN_DB_PREFIX."energie_compteur_releve";
  64        $sql .= " WHERE fk_compteur = ".$obj_c->rowid;
  65        $sql .= " ORDER BY date_releve ASC";
  66  
  67        $resql = $db->query($sql);
  68  
  69        if ($resql)
  70          {
  71            $num = $db->num_rows($resql);
  72            $i = 0;
  73            
  74            if ($num > 0)
  75          {
  76            $obj = $db->fetch_object($resql);
  77            
  78            //print strftime("%Y-%m-%d", $obj->date_releve) . "\t\t".$obj->valeur."\n";
  79            
  80            $previous_date  = $obj->date_releve;
  81            $previous_value = $obj->valeur;
  82            
  83            $i++;
  84          }
  85            
  86            $datas = array();
  87            $k = 0;
  88            
  89            while ($i < $num)
  90          {
  91            $obj = $db->fetch_object($resql);
  92            
  93            $delta = (($obj->date_releve - $previous_date) / 86400 );
  94            
  95            if ($delta > 1)
  96              {
  97                for ($j = 1 ; $j < $delta ; $j++)
  98              {
  99                $value = $previous_value + ((($obj->valeur - $previous_value) / $delta) * $j);
 100                
 101                $datas[$k][0] = $value;
 102                $datas[$k][1] = ($previous_date + (86400 * $j));
 103                $k++;
 104                
 105                //print strftime("%Y-%m-%d", ($previous_date + (86400 * $j))) . "\t$j\t".$value."\n";
 106              }
 107              }
 108            
 109            //print strftime("%Y-%m-%d", $obj->date_releve) . "\t\t".$obj->valeur."\n";
 110            
 111            $datas[$k][0] = $obj->valeur;
 112            $datas[$k][1] = $obj->date_releve;
 113            $k++;
 114            
 115            $previous_date = $obj->date_releve;
 116            $previous_value = $obj->valeur;
 117            $i++;      
 118          }
 119            
 120            // Graph
 121            $maxa = 0;
 122   
 123            for ($i = 1 ; $i < sizeof($datas) ; $i++)
 124          {
 125            $xa = ($datas[$i][0] - $datas[($i-1)][0]);
 126            
 127            $maxa = max($maxa, $xa);
 128            
 129            $gdatas[$i-1] = $xa;
 130            $glabels[$i-1] = '';//strftime("%d%m",$datas[$i][1]);
 131            
 132            $month = strftime("%m%y",$datas[$i][1]);
 133            
 134            $mdatas[$compteur_id][$month] = $mdatas[$compteur_id][$month] + $xa;
 135  
 136            $week = strftime("%W%y",$datas[$i][1]);
 137  
 138            $wdatas[$compteur_id][$week] = $wdatas[$compteur_id][$week] + $xa;
 139  
 140            $year = strftime("%Y",$datas[$i][1]);
 141  
 142            $ydatas[$compteur_id][$year] = $ydatas[$compteur_id][$year] + $xa;
 143          }
 144            
 145            $width = 750;
 146            $height = 300;
 147            if (sizeof($gdatas) > 2)
 148          {
 149            $graph = new Graph($width, $height,"auto");    
 150  
 151            $graph->SetScale("textlin");
 152            $graph->yaxis->scale->SetGrace(2);
 153            $graph->SetFrame(1);
 154            $graph->img->SetMargin(40,20,20,35);
 155            
 156            $b2plot = new LinePlot($gdatas);          
 157  
 158            $b2plot->SetColor("blue");
 159            $b2plot->SetWeight(2);
 160  
 161            $graph->title->Set("Consommation par jour");
 162            
 163            $graph->xaxis->SetTickLabels($glabels);   
 164            $graph->xaxis->title->Set(strftime("%d/%m/%y %H:%M:%S", time()));
 165            
 166            $graph->Add($b2plot);
 167            $graph->img->SetImgFormat("png");
 168            
 169            $file= DOL_DATA_ROOT."/energie/graph/day.".$obj_c->rowid.".png";
 170            
 171            $graph->Stroke($file);
 172          }
 173            $width = 450;
 174            $height = 300;
 175            
 176            // Mensuel
 177            $i=0;
 178            foreach ($mdatas[$compteur_id] as $key => $value)
 179          {
 180            $gmdatas[$i] = $value;
 181            $gmlabels[$i] = $key;
 182            $i++;
 183          }
 184            if (sizeof($gmdatas))
 185          {
 186            $graph = new Graph($width, $height,"auto");    
 187            $graph->SetScale("textlin");
 188            
 189            $graph->yaxis->scale->SetGrace(2);
 190            $graph->SetFrame(1);
 191            $graph->img->SetMargin(40,20,20,35);
 192            
 193            $b2plot = new BarPlot($gmdatas);          
 194            $b2plot->SetFillColor("blue");
 195            
 196            $graph->title->Set("Consommation par mois");
 197            
 198            $graph->xaxis->SetTickLabels($gmlabels);   
 199            $graph->xaxis->title->Set(strftime("%d/%m/%y %H:%M:%S", time()));
 200            
 201            $graph->Add($b2plot);          
 202            $graph->img->SetImgFormat("png");
 203            
 204            $file= DOL_DATA_ROOT."/energie/graph/month.".$obj_c->rowid.".png";
 205                      
 206            $graph->Stroke($file);
 207          }
 208            // Hebdomadaire
 209            $width = 750;
 210            $height = 300;
 211            $i=0;
 212            foreach ($wdatas[$compteur_id] as $key => $value)
 213          {
 214            $gwdatas[$i] = $value;
 215            $gwlabels[$i] = substr($key,0,2);
 216            $i++;
 217          }
 218            if (sizeof($gwdatas))
 219          {
 220            $graph = new Graph($width, $height,"auto");    
 221            $graph->SetScale("textlin");
 222            
 223            $graph->yaxis->scale->SetGrace(2);
 224            $graph->SetFrame(1);
 225            $graph->img->SetMargin(40,20,20,35);
 226            
 227            $b2plot = new BarPlot($gwdatas);
 228            $graph->xaxis->SetTickLabels($gwlabels);
 229            
 230            $b2plot->SetFillColor("blue");
 231            $graph->title->Set("Consommation par semaine");
 232            
 233            $graph->xaxis->title->Set(strftime("%d/%m/%y %H:%M:%S", time()));
 234            
 235            $graph->Add($b2plot);
 236            
 237            $graph->img->SetImgFormat("png");
 238            
 239            $file= DOL_DATA_ROOT."/energie/graph/week.".$obj_c->rowid.".png";
 240                                
 241            $graph->Stroke($file);
 242          }
 243  
 244            // Annuel
 245            $width = 450;
 246            $height = 300;
 247            $i=0;
 248            foreach ($ydatas[$compteur_id] as $key => $value)
 249          {
 250            $gydatas[$i] = $value;
 251            $gylabels[$i] = $key;
 252            $i++;
 253          }
 254           
 255            if (sizeof($gydatas))
 256          {
 257            $graph = new Graph($width, $height,"auto");    
 258            $graph->SetScale("textlin");
 259            
 260            $graph->yaxis->scale->SetGrace(2);
 261            $graph->SetFrame(1);
 262            $graph->img->SetMargin(40,20,20,35);
 263            
 264            $b2plot = new BarPlot($gydatas);
 265            $graph->xaxis->SetTickLabels($gylabels);
 266            
 267            $b2plot->SetFillColor("blue");
 268            $graph->title->Set("Consommation annuelle");
 269            
 270            $graph->xaxis->title->Set(strftime("%d/%m/%y %H:%M:%S", time()));
 271            
 272            $graph->Add($b2plot);
 273            
 274            $graph->img->SetImgFormat("png");
 275            
 276            $file= DOL_DATA_ROOT."/energie/graph/year.".$obj_c->rowid.".png";
 277                      
 278            $graph->Stroke($file);
 279          }          
 280          }
 281        else
 282          {
 283            dolibarr_syslog("Erreur SQL");
 284            dolibarr_syslog("$sql");
 285          }
 286        $i_c++;
 287      }
 288      }
 289  }
 290  else
 291  {
 292    dolibarr_syslog("Erreur SQL");
 293    dolibarr_syslog($db->error($resql_c));
 294  }
 295  
 296  
 297  $sql_g = "SELECT distinct fk_energie_groupe";
 298  $sql_g .= " FROM ".MAIN_DB_PREFIX."energie_compteur_groupe";
 299  
 300  $resql_g = $db->query($sql_g);
 301  
 302  if ($resql_g)
 303  {
 304    $num_g = $db->num_rows($resql_g);
 305    $i_g = 0;
 306  
 307    while ($i_g < $num_g)
 308      {
 309        $row_g = $db->fetch_row($resql_g);
 310              
 311        $sql_c = "SELECT fk_energie_compteur";
 312        $sql_c .= " FROM ".MAIN_DB_PREFIX."energie_compteur_groupe";
 313        $sql_c .= " WHERE fk_energie_groupe = ".$row_g[0];      
 314        
 315        $resql_c = $db->query($sql_c);
 316        
 317        if ($resql_c)
 318      {
 319        $num_c = $db->num_rows($resql_c);
 320        $i_c = 0;
 321        
 322        $compteurs = array();
 323        while ($i_c < $num_c)
 324          {
 325            $obj_c = $db->fetch_object($resql_c);
 326            
 327            array_push($compteurs,$obj_c->fk_energie_compteur);
 328            $i_c++;
 329          }
 330                         
 331        $width = 450;
 332        $height = 300;
 333  
 334        //
 335        $graph = new Graph($width, $height,"auto");    
 336        $graph->SetScale("textlin");
 337  
 338        $graph->SetFrame(1);
 339        $graph->img->SetMargin(40,20,20,35);
 340        $graph->img->SetImgFormat("png");
 341        $graph->title->Set("Consommation hebdomadaire");
 342        $graph->xaxis->title->Set(strftime("%d/%m/%y %H:%M:%S", time()));
 343  
 344        $gbspl = array();
 345        foreach ($compteurs as $cx)
 346          {
 347            $gydatas = array();
 348            $gylabels = array();
 349  
 350            $i=0;
 351            foreach ($wdatas[$cx] as $key => $value)
 352          {
 353            $gydatas[$i] = $value;
 354            $gylabels[$i] = $key;
 355            $i++;
 356          }
 357            $bplot = new BarPlot($gydatas);
 358            $cc = new EnergieCompteur($db,0);
 359            $cc->fetch($cx);
 360            $bplot->SetFillColor($cc->couleurs[$cc->energie]);
 361            
 362            array_push($gbspl, $bplot);
 363          }
 364  
 365        $gbplot = new GroupBarPlot($gbspl);
 366  
 367        $graph->xaxis->SetTickLabels($gylabels);      
 368        $graph->Add($gbplot);
 369                          
 370        $file= DOL_DATA_ROOT."/energie/graph/groupe.week.".$row_g[0].".png";
 371        if (sizeof($gbspl))
 372          $graph->Stroke($file);
 373        //
 374        //
 375        //
 376        $graph = new Graph($width, $height,"auto");    
 377        $graph->SetScale("textlin");
 378  
 379        $graph->SetFrame(1);
 380        $graph->img->SetMargin(40,20,20,35);
 381        $graph->img->SetImgFormat("png");
 382        $graph->title->Set("Consommation mensuelle");
 383        $graph->xaxis->title->Set(strftime("%d/%m/%y %H:%M:%S", time()));
 384  
 385        $gbspl = array();
 386        foreach ($compteurs as $cx)
 387          {
 388            $gydatas = array();
 389            $gylabels = array();
 390  
 391            $i=0;
 392            foreach ($mdatas[$cx] as $key => $value)
 393          {
 394            $gydatas[$i] = $value;
 395            $gylabels[$i] = $key;
 396            $i++;
 397          }
 398            $bplot = new BarPlot($gydatas);
 399  
 400            $cc = new EnergieCompteur($db,0);
 401            $cc->fetch($cx);
 402            $bplot->SetFillColor($cc->couleurs[$cc->energie]);
 403  
 404            array_push($gbspl, $bplot);
 405          }
 406  
 407        $gbplot = new GroupBarPlot($gbspl);
 408  
 409        $graph->xaxis->SetTickLabels($gylabels);      
 410        $graph->Add($gbplot);
 411                          
 412        $file= DOL_DATA_ROOT."/energie/graph/groupe.month.".$row_g[0].".png";      
 413        if (sizeof($gbspl))
 414          $graph->Stroke($file);
 415        //
 416        //
 417        //
 418        $graph = new Graph($width, $height,"auto");    
 419        $graph->SetScale("textlin");
 420  
 421        $graph->SetFrame(1);
 422        $graph->img->SetMargin(40,20,20,35);
 423        $graph->img->SetImgFormat("png");
 424        $graph->title->Set("Consommation annuelle");
 425        $graph->xaxis->title->Set(strftime("%d/%m/%y %H:%M:%S", time()));
 426  
 427        $gbspl = array();
 428        foreach ($compteurs as $cx)
 429          {
 430            $gydatas = array();
 431            $gylabels = array();
 432  
 433            $i=0;
 434            foreach ($ydatas[$cx] as $key => $value)
 435          {
 436            $gydatas[$i] = $value;
 437            $gylabels[$i] = $key;
 438            $i++;
 439          }
 440            $bplot = new BarPlot($gydatas);
 441  
 442            $cc = new EnergieCompteur($db,0);
 443            $cc->fetch($cx);
 444            $bplot->SetFillColor($cc->couleurs[$cc->energie]);
 445            
 446            array_push($gbspl, $bplot);
 447          }
 448  
 449        $gbplot = new GroupBarPlot($gbspl);
 450  
 451        $graph->xaxis->SetTickLabels($gylabels);      
 452        $graph->Add($gbplot);
 453                          
 454        $file= DOL_DATA_ROOT."/energie/graph/groupe.year.".$row_g[0].".png";      
 455        if (sizeof($gbspl))
 456          $graph->Stroke($file);
 457        //
 458      }
 459        else
 460      {
 461        dolibarr_syslog("Erreur SQL");
 462        print $sql_c;
 463      }
 464        $i_g++;
 465      }
 466  }
 467  else
 468  {
 469    dolibarr_syslog("Erreur SQL");
 470    print $sql;
 471  }
 472  ?>


Généré le : Mon Nov 26 12:29:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics