1. 7. 2015

PHP - generovanie grafu z DB cez jpgraph

Pre vykreslenie grafov je možné v PHP použiť knižnicu JPGRAPH.
Po načítaní údajov z DB za zobrazovaný rozsah do polí sa tieto premenné použijú pre vygenerovanie grafu hodnôt. Najrozsiahlejšou časťou je potom naformátovanie grafu.




Knižnicu je potrebné uložiť do filesystému a zaradiť ju do kódu
    require_once ('../jpgraph.350/jpgraph.php');

Príklad PHP kódu zobrazujúceho obsadenosť páskových mechaník:

<?php
// NASTAVENE KONSTANTY, AKO PRIPOJENIE DB, CESTY A POD
  define('DB_SERVER_USERNAME', 'graf1');
  define('DB_SERVER_PASSWORD', 'pwd1');
  define('DB_CHARSET', 'AL32UTF8');
  define('DB_DATABASE', '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.100)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=DB1)))');
  date_default_timezone_set("Europe/Bratislava");

// PRVOTNE PRIPOJENIE DO DB
 $connection = oci_pconnect(DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE, DB_CHARSET);
 $i=0;

// PRIPOJENIE USPESNE
 if ($connection !== FALSE)
  {
// NASTAVIME FORMAT DATUMU
   $statement=oci_parse($connection,"alter session set NLS_DATE_FORMAT='dd.mm. hh24:mi'");
   oci_execute($statement);
   oci_free_statement($statement);

   $query ="select *";
   $query.="  from bko_tapes";
   $query.="  where datum >= to_date('".$datm.".2015','DD.MM.YYYY')";
   $query.="  and   datum <  to_date('".$datm.".2015','DD.MM.YYYY') + 2";
   $query.="  order by datum";

// BLOK PRE SELECT A NATIAHNUTIE DAT
   $statement=oci_parse($connection, $query);
   if ($statement !== FALSE)
     {
       if (oci_execute($statement))
       {
         $i=0;
         while ($row=oci_fetch_array($statement,OCI_BOTH))
         {
           $NDatum[$i]   =$row['DATUM'];
           $NE05A[$i]  =$row['E05A'];
           $NE05B[$i]  =$row['E05B'];

           $i++;
         }
       }
       oci_free_statement($statement);
     }
     oci_close($connection);

// MOZEME KRESLIT

   if ( $i > 10 ) {
    require_once ('../jpgraph.350/jpgraph.php');
    require_once ('../jpgraph.350/jpgraph_line.php');

 // Rozmery a typ grafu
    $graph = new Graph(900,650);
 // NADPIS
    $graph->title->Set('OBSADENOST MECHANIK');
    $graph->title->SetFont(FF_TIMES,FS_BOLD,18);
    $graph->SetScale('textlin',0,10); // nastavi napevno y os 0-10

 // os Y
    $graph->yaxis->title->Set("[ks mechanik]"); // Wording on the yaxis graph
    $graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD,14); // Text font
    $graph->yaxis->SetTitlemargin(-14); // Specifies the distance between the axis and the title
//  $graph->yscale->SetGrace(15); // Gives X percentage of space at the to of the graph\
 // TIMESTAMP
    $txtDATUM = new Text('generated'. date('d.m.Y H:i:s',time()));
    $txtDATUM->SetPos(750,620,'left','top');
    $txtDATUM->SetFont(FF_ARIAL,FS_NORMAL,7);
    $graph->Add($txtDATUM);
 // FORMATOVANIE
    $graph->xaxis->SetTickLabels($NDatum);
    $graph->xaxis->SetTextTickInterval(12);
    $graph->xaxis->SetLabelAngle(90);
    $graph->xaxis->SetFont(FF_FONT1,FS_NORMAL);
    $graph->yaxis->SetFont(FF_FONT1,FS_NORMAL);
    $graph->yaxis->scale->SetAutoMin(0);
    $graph->xgrid->SetLineStyle('solid');
    $graph->xgrid->SetColor('#b3b3b3');
    $graph->xgrid->Show();

    $graph->img->SetAntiAliasing(false);
 // SAMOTNE GRAFY
    $hrub=5;

    if ($lok != 2) { $lpt=new LinePlot($NE05A); $lpt->SetLegend('Lokalita A');$graph->Add($lpt); $lpt->SetWeight($hrub); }
    if ($lok != 1) { $lpt=new LinePlot($NE05B); $lpt->SetLegend('Lokalita B');   $graph->Add($lpt); $lpt->SetWeight($hrub); }

 // LEGENDARY
    $graph->legend->SetLayout(LEGEND_VERT);
    $graph->legend->SetFont(FF_FONT1,FS_NORMAL);
    $graph->legend->Pos(0.02,0.02,'right','top');
    $graph->legend->SetColumns(2);
 // VYKRESLI
    $graph->Stroke('/var/www/html/brano/tmp/tapes_graf1.png');

    echo '<center><img src="../tmp/tapes_graf1.png?c='.rand(1,10000).'"></center>'."\n";
  } else {
    echo 'Ziadne data za dany den!<br />';
  }
 }
?>


Žiadne komentáre:

Zverejnenie komentára