Guest User

gasoline price charts

a guest
Jan 24th, 2012
116
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.  
  4. class degvielasCenuMonitorings
  5. {
  6. private $mysqlTable;
  7. private $mysqlPassw;
  8. private $mysqlHost;
  9. private $mysqlDB;
  10.  
  11. public $marka;
  12. public $adreses;
  13. public $values;
  14. public $popValues;
  15.  
  16. function __construct($marka,$adreses)
  17.     {  
  18.     $this->mysqlUser="usern";
  19.     $this->mysqlPassw="passw";
  20.     $this->mysqlHost="00.00.00.00";
  21.     $this->mysqlDB="db";
  22.     $this->mysqlTable="Z_didzis_gas_test";
  23.    
  24.     $this->marka=$marka;
  25.     $this->adreses=explode(",",$adreses);
  26.     $this->values = array();
  27.     $this->popValues=array();
  28.     }//ef
  29.    
  30. function mySqlConnect()
  31.     {
  32.     mysql_connect($this->mysqlHost, $this->mysqlUser="intoone",$this->mysqlPassw);// or throw new Exception('cannot connect mysql.');
  33.     mysql_select_db($this->mysqlDB);// or throw new Exception('cannot select db.');
  34.     }//ef
  35.  
  36. function getMarka()
  37.     {
  38.     return $this->marka;
  39.     //ef
  40.     }
  41.    
  42. function getAdreses()
  43.     {  
  44.     return $this->adreses;
  45.     //ef
  46.     }
  47.  
  48. function QueryDB()
  49.     {
  50.     $nosaukums=$this->adreses[0];
  51.     $adrese=$this->adreses[1];
  52.     $query="SELECT  `date`,  `cena`, Z_didzis_gas_stacijas.stacija, Z_didzis_gas_stacijas.adrese, Z_didzis_gas_markas.marka
  53. FROM $this->mysqlTable
  54.  
  55. LEFT JOIN Z_didzis_gas_stacijas
  56. ON Z_didzis_gas_stacijas.id=Z_didzis_gas_test.stacija
  57.  
  58. Left JOIN Z_didzis_gas_markas
  59. on Z_didzis_gas_markas.id=Z_didzis_gas_test.marka
  60.  
  61. where Z_didzis_gas_markas.marka='$this->marka'
  62. and Z_didzis_gas_stacijas.stacija='$nosaukums'
  63. and Z_didzis_gas_stacijas.adrese='$adrese'
  64. and Z_didzis_gas_test.date> FROM_DAYS(TO_DAYS(NOW())-60)
  65.  
  66. order by date asc";
  67.     $result=mysql_query($query);
  68.    
  69.     while($row = mysql_fetch_array($result))
  70.         {
  71.         //$row[0] datums
  72.         //$row[1] cena
  73.         //$row[2] nosaukums
  74.         //$row[3] adrese
  75.         //$row[4]
  76.         //$this->values[$row[0]]=(double)$row[1];
  77.         array_push($this->values,array($row[0],$row[1]));
  78.         }
  79.     }//ef
  80.    
  81. function populateDaily()
  82.     {
  83.  
  84.     //iterācija no pirmā atgrieztā ieraksta līdz
  85.     //echo $this->values[0][0];
  86.    
  87.     //$firstDay=strtotime($this->values[0][0]);
  88.     //$lastDay=strtotime($this->values[count($this->values)-1][0]);
  89.    
  90.     // ja starp n un n+1 ierakstu ir brīvas dienas, tad populate brīvajās dienās n vērtību
  91.     // else ja brīvas dienas ir nulle, tad ir ik katru dienu veikts mērījums vai arī vairāki mērījumi. populate tikai tad, ja dienu atšķirība ir >0
  92.  
  93.     // iterating over the array elements.
  94.     // conditional loop creates inside the cycle - day count between this and next element. can be done without conditional, btw.
  95.     // if there are days without values between n and n+1 elements, then we need to populate the value in these days without values.
  96.     // else if there are no days without values=> daysBetween=0, then take the value from
  97.     // in this case i am not interpolating, but it is possible to interpolate values lineary here.
  98.     //
  99.     // $this->values[0][0]="2012-01-20 09:40:00";
  100.     // $this->values[1][0]="2012-01-21 09:40:00";
  101.     // $this->values[2][0]="2012-01-25 09:40:00";
  102.     // $this->values[3][0]="2012-01-26 09:40:00";
  103.     // $this->values[4][0]="2012-01-29 09:40:00";
  104.    
  105.     // $this->values[0][1]=0;
  106.     // $this->values[1][1]=2;
  107.     // $this->values[2][1]=4;
  108.     // $this->values[3][1]=6;
  109.     // $this->values[4][1]=8;
  110.     //echo count($this->values);
  111.    
  112.     for ($i=0; $i<=count($this->values)-2; $i++)
  113.         {
  114.         $days=(date("d",strtotime($this->values[$i+1][0]))-date("d",strtotime($this->values[$i][0])))-1; //day count between this and next element
  115.         if ($days>0)
  116.             {
  117.             $tDiena=strtotime($this->values[$i][0]);
  118.             for ($j=0;$j<=$days;$j++)
  119.                 {          
  120.                
  121.                 //echo date("d-m-Y",$tDiena);
  122.                 array_push($this->popValues,array(date("Y-m-d",$tDiena),$this->values[$i][1]));
  123.                 $tDiena=$tDiena+86000;
  124.                 //$k=$k+1; since we have array_push, no need for element counter. array_push can be used for multidimensional arrays
  125.                 }
  126.             }
  127.         else
  128.             {
  129.             array_push($this->popValues,array($this->values[$i][0],$this->values[$i][1]));
  130.             //$k=$k+1;
  131.             }//fi      
  132.         }//rof
  133.     array_push($this->popValues,array($this->values[count($this->values)-1][0],$this->values[count($this->values)-1][1])); //adding the last element in the populated array
  134.     }//ef
  135.    
  136. function createDataForJQPlot()
  137.     {
  138.     foreach ($this->popValues as $value)
  139.         {
  140.         echo "['".$value[0]."', ". $value[1]."],\n";
  141.         }
  142.     }//ef
  143. //end class
  144. }
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156. $marka="E95";
  157. $adreses="Trest,Dārzciema iela 127";
  158.  
  159. $dcm=new degvielasCenuMonitorings($marka,$adreses);
  160. $dcm->mySqlConnect();
  161. $dcm->QueryDB();
  162. $dcm->populateDaily();
  163.  
  164. //$dcm->createDataForJQPlot();
  165.  
  166. //die;
  167.  
  168. //var_dump($dcm->popValues);
  169. //var_dump($dcm->values);
  170.  
  171. //var_dump ($dcm->getAdreses());
  172.  
  173.  
  174.  
  175. ?>
  176.  
  177. <!--[if lt IE 9]><script language="javascript" type="text/javascript" src="excanvas.js"></script><![endif]-->
  178. <script language="javascript" type="text/javascript" src="javacharts/jquery.min.js"></script>
  179. <script language="javascript" type="text/javascript" src="javacharts/jquery.jqplot.min.js"></script>
  180.  
  181. <script type="text/javascript" src="javacharts/plugins/jqplot.highlighter.min.js"></script>
  182. <script type="text/javascript" src="javacharts/plugins/jqplot.cursor.min.js"></script>
  183. <script type="text/javascript" src="javacharts/plugins/jqplot.dateAxisRenderer.min.js"></script>
  184.  
  185. <link rel="stylesheet" type="text/css" href="javacharts/jquery.jqplot.css" />
  186.  
  187.  
  188.  
  189. <?php
  190.  
  191.  
  192. if (count($dcm->popValues)>1)
  193. {
  194. ?>
  195.  
  196. <script class="code" type="text/javascript">
  197. //javascript becames generated if values are more than 1.
  198. $(document).ready(function()
  199. {
  200.  
  201. var line1=[
  202.  
  203.     ///*
  204.    
  205.     //*/
  206.     <?php
  207.     $dcm->createDataForJQPlot();   
  208.     ?>
  209.   ];
  210.  
  211. var plot1 = $.jqplot('chart1', [line1],
  212.     {
  213.           title:'<?php echo $dcm->marka; ?>',
  214.         axes:
  215.         {
  216.                 xaxis:
  217.                 {
  218.                 renderer:$.jqplot.DateAxisRenderer,
  219.                 tickOptions:
  220.                     {
  221.                     formatString:'%b %#d, %Y',
  222.                     }
  223.                 },
  224.  
  225.             yaxis:
  226.             {
  227.                 tickOptions:
  228.                     {
  229.                     formatString:'%.3f LVL',
  230.                     } //,
  231.             //min:0.8,
  232.             //max:1.
  233.             }
  234.         },
  235.          
  236.         highlighter:
  237.         {
  238.             show: true,
  239.             sizeAdjust: 7.5
  240.         },
  241.  
  242.         cursor:
  243.         {
  244.             show: false
  245.         }
  246.     });
  247. });
  248. </script>
  249. <?php
  250. }
  251. ?>
  252. <body>
  253. <div id=chart1 style="height:200px;width:640px;">
  254. </div>
RAW Paste Data