phpUserTrack
[ class tree: phpUserTrack ] [ index: phpUserTrack ] [ all elements ]

Source for file graphPageviews.php

Documentation is available at graphPageviews.php

  1. <?php
  2.  
  3. require_once './../include/version.php';
  4. require_once './../lib/core.php';
  5.  
  6. include ('jpgraph/jpgraph.php');
  7. include ('jpgraph/jpgraph_line.php');
  8. include ('jpgraph/jpgraph_date.php');
  9.  
  10. global $plugins;
  11.  
  12. function calcMovingAverage(&$item, $key) {
  13.     if ($key < $_GET['movavg']) {
  14.         $item = '';
  15.         return;
  16.     }
  17.     global $data;
  18.     $sum = 0;
  19.     for ($i = 0; $i < $_GET['movavg']; $i++) {
  20.         $sum += $data[$key - $i];
  21.     }
  22.     $item = $sum / $_GET['movavg'];
  23. }
  24.  
  25. $whereSQL = '';
  26.  
  27. function buildWhere($newCond) {
  28.   global $whereSQL;
  29.   if (substr($whereSQL, 0, 6) == ' where') {
  30.     $whereSQL .= ' and ' . $newCond . ' ';
  31.   } else {
  32.     $whereSQL = ' where ' . $newCond . ' ';
  33.   }
  34. }
  35.  
  36. $db = new phpusertrack_db();
  37. if (!$_GET['notitle']) {
  38.   $text = 'Pageviews';
  39. }
  40. if (isset($_GET['ip'])) {
  41.   buildWhere('phpusertrack_ip.ip="' . $_GET['ip'] . '"');
  42.   $text .= ' - ' . $_GET['ip'];
  43. }
  44. if (isset($_GET['site'])) {
  45.   buildWhere('phpusertrack_data.site_id="' . $_GET['site'] . '"');
  46.   $sitenameSQL = "select * from phpusertrack_sites where id=" . $_GET['site'];
  47.   $sitename = $db->_query($sitenameSQL);
  48.   $text .= ' - ' . $sitename[0]['name'];
  49. }
  50. if (isset($_GET['path'])) {
  51.   buildWhere('phpusertrack_data.path="' . $_GET['path'] . '"');
  52.   $subtitle .= 'Path: ' . $_GET['path'];
  53. }
  54. if (isset($_GET['startdate'])) {
  55.   buildWhere('phpusertrack_data.timestamp >= "' . $_GET['startdate'] . '"');
  56.   $subtitle .= 'Start Date: ' . $_GET['startdate'];
  57. }
  58. $sql = 'select count(phpusertrack_data.id) as count, UNIX_TIMESTAMP(phpusertrack_data.timestamp) as date from phpusertrack_ip inner join phpusertrack_data on phpusertrack_ip.id=phpusertrack_data.ip_id ' . $whereSQL . ' group by DATE_FORMAT(phpusertrack_data.timestamp, \'%e %b %Y\') order by phpusertrack_data.timestamp asc';
  59. $pageviews = $db->_query($sql);
  60. $data = array();
  61. $xdata = array();
  62. $movingaverage = array();
  63.  
  64. foreach ($pageviews as $view) {
  65.     array_push($data, (int)$view['count']);
  66.     array_push($xdata, (int)$view['date']);
  67. }
  68.  
  69. // Create the graph. These two calls are always required
  70. $graph = new Graph(500,233);   
  71. $graph->img->SetAntiAliasing(); 
  72. $graph->SetScale("datint");
  73. $graph->yaxis->scale->SetAutoMin(0);
  74.  
  75. // Adjust the margin a bit to make more room for titles
  76. $graph->img->SetMargin(40,30,22,30);
  77.  
  78. // Create a bar pot
  79. $graph->xaxis->scale->SetDateAlign(YEARADJ_2);
  80. $graph->xaxis->HideTicks(true,false);
  81. $graph->xaxis->SetTextLabelInterval(5);
  82. $lplot = new LinePlot($data, $xdata);
  83. $graph->Add($lplot);
  84. $lplot->SetFillColor( "#BBCCFF");
  85.  
  86.  
  87. if ((isset($_GET['movavg'])) && ($_GET['movavg'] == true)) {
  88.     $movingaverage = $data;
  89.     array_walk($movingaverage, 'calcMovingAverage');
  90.     $mplot = new LinePlot($movingaverage, $xdata);
  91.     $graph->Add($mplot);
  92.     $mplot->SetColor( "red");
  93.     $mplot->SetWeight( 2);
  94.     $mplot->SetFillColor( "red@0.95");
  95.     $lplot->SetLegend ("Pageviews");
  96.     $mplot->SetLegend($_GET['movavg'] . " day Moving Average");
  97.     $graph ->legend->SetAbsPos( 50,30,"left" ,"top");
  98.     $graph->legend->SetFillColor("#FFFFFF@0.5");
  99.     $graph->legend->SetFont(FF_VERA,FS_NORMAL, 7.5);
  100.     $graph->legend->SetMarkAbsSize(6);
  101. }
  102.  
  103. // Setup the titles
  104. $graph->yaxis->title->Set("Pageviews");
  105.  
  106. if (!$_GET['notitle']) {
  107.     $graph->title->Set($text);
  108.     $graph->subtitle->Set($subtitle);
  109. }
  110. $graph->title->SetFont(FF_VERA,FS_BOLD, 9);
  111. $graph->subtitle->SetFont(FF_VERA,FS_NORMAL, 8);
  112.  
  113. $graph->title->SetFont(FF_VERA,FS_BOLD);
  114. $graph->yaxis->title->SetFont(FF_VERA,FS_BOLD, 7);
  115. $graph->xaxis->scale->SetDateFormat('d M Y');
  116. $graph->xaxis->SetTickSide(SIDE_BOTTOM); 
  117. $graph->yaxis->HideZeroLabel();
  118. $graph->xaxis->SetPos('min');
  119. $graph->xaxis->scale->ticks->Set (60*60*24*14, 60*60*24); 
  120. $graph->xaxis->scale->SetDateAlign(DAYADJ_7); 
  121. $graph->yaxis->scale->SetGrace(10);
  122.  
  123. $graph->SetTickDensity (TICKD_DENSE); 
  124. $graph->xaxis->SetFont(FF_VERA,FS_NORMAL, 6);
  125. $graph->yaxis->SetFont(FF_VERA,FS_NORMAL, 6);
  126. $graph->xgrid->Show(true ,true);
  127.  
  128. #list($tickPositions, $minTickPositions) = DateScaleUtils::GetTicks($xdata);
  129.  
  130. $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#BBCCFF@0.5');
  131. $graph->SetMarginColor("#ffffff");
  132. $graph->SetFrame(false);
  133.  
  134. $graph->xgrid->Show(true, false);
  135.  
  136. // Display the graph
  137. $graph->Stroke();
  138. //echo $sql;
  139. //print_r($xdata);
  140. ?>

Documentation generated on Tue, 06 Nov 2007 09:21:49 -0800 by phpDocumentor 1.4.0a2