htdocs/graph.php
changeset 43 ce2e9caf2dfa
parent 34 a8daa2016cf1
child 44 73f74d395f95
--- a/htdocs/graph.php	Sun Jan 04 12:19:27 2009 -0500
+++ b/htdocs/graph.php	Sun Jan 04 16:40:36 2009 -0500
@@ -7,14 +7,34 @@
 $first_channel = $channel_list[0];
 $channel = ( isset($_REQUEST['channel']) && in_array($_REQUEST['channel'], $channel_list) ) ? $_REQUEST['channel'] : $first_channel;
 
-$g = new GraphMaker(); // _Compat();
-
-$g->SetGraphPadding(20, 30, 20, 15);
-$g->SetGraphAreaHeight(200);
-$g->SetBarDimensions(26, 0);
-$g->SetBarPadding(7);
-$g->SetGraphBackgroundTransparent(240, 250, 255, 0);
-$g->SetGraphTransparency(25);
+function makeGraph($type = 'bar')
+{
+  $class = ( $type == 'line' ) ? 'LineGraph' : 'BarGraph';
+  $g = new $class(); // _Compat();
+  
+  $g->SetGraphAreaHeight(200);
+  $g->SetGraphPadding(30, 30, 20, 15);
+  if ( get_class($g) == 'BarGraph' )
+  {
+    $g->SetBarPadding(7);
+    $g->SetBarFont(1);
+    $g->SetBarDimensions(25, 7);
+  }
+  else if ( get_class($g) == 'LineGraph' )
+  {
+    $g->SetGraphAreaWidth(800);
+  }
+  $g->SetAxisDeepness(7);
+  $g->SetGraphTitleFont(2);
+  $g->SetGraphBackgroundTransparent(false, 240, 250, 255);
+  $g->SetGraphTransparency(25);
+  $g->SetAxisScaleColor(90, 90, 90);
+  $g->SetAxisScaleFont(1);
+  $g->SetScaleRoundY(0);
+  $g->SetScaleRoundX(0);
+  $g->SetAxisStepSize(7);
+  return $g;
+}
 
 // generate the data
 // we're doing this by absolute hours, not by strictly "24 hours ago", e.g. on-the-hour stats
@@ -23,6 +43,7 @@
 {
   case 'lastday':
   default:
+    $g = makeGraph();
     $graph_title = $channel . ' message count - last 24 hours';
     $this_hour = gmmktime(gmdate('H'), 0, 0);
     $graphdata = array();
@@ -36,6 +57,7 @@
     }
     break;
   case 'lastweek':
+    $g = makeGraph();
     $graph_title = $channel . ' activity - last 14 days';
     $this_day = gmmktime(0, 0, 0);
     $graphdata = array();
@@ -50,6 +72,7 @@
     $g->SetBarPadding(12);
     break;
   case 'lastmonth':
+    $g = makeGraph();
     $graph_title = $channel . ' activity - last 30 days';
     $this_day = gmmktime(0, 0, 0);
     $graphdata = array();
@@ -63,6 +86,41 @@
     }
     $g->SetBarPadding(15);
     break;
+  case 'lasthour':
+    $g = makeGraph('line');
+    $g->SetAxisStepX(5);
+    $g->SetScaleFunctionX('lasthour_scaler');
+    function lasthour_scaler($v, $g)
+    {
+      $k = array_keys($g->data);
+      return ( isset($k[$v]) ) ? $k[$v] : 'now';
+    }
+    $graph_title = $channel . ' activity - last hour';
+    $data = stats_raw_data($channel, 59);
+    $agg = array();
+    foreach ( $data as $message )
+    {
+      $tid = intval(ltrim(date('i', $message['time']), '0'));
+      if ( !isset($agg[$tid]) )
+        $agg[$tid] = 0;
+      $agg[$tid]++;
+    }
+    $graphdata = array();
+    $minutenow = intval(ltrim(date('i', NOW), '0'));
+    $hournow = intval(ltrim(date('H', NOW), '0'));
+    $hourthen = intval(ltrim(date('H', NOW-3600), '0'));
+    for ( $i = $minutenow + 1; $i < 60; $i++ )
+    {
+      $istr = ( $i < 10 ) ? "0$i" : "$i";
+      $graphdata["$hourthen:$istr"] = ( isset($agg[$i]) ) ? $agg[$i] : 0;
+    }
+    for ( $i = 0; $i <= $minutenow; $i++ )
+    {
+      $istr = ( $i < 10 ) ? "0$i" : "$i";
+      $graphdata["$hournow:$istr"] = ( isset($agg[$i]) ) ? $agg[$i] : 0;
+    }
+    
+    break;
 }
 
 $max = max($graphdata);
@@ -94,9 +152,10 @@
 if ( $max > 30000 )
   $interval = round($max / 10);
 
-$g->SetBarData($graphdata);
-$g->SetAxisStep($interval);
+$g->data = $graphdata;
+
 $g->SetGraphTitle($graph_title);
+$g->SetAxisStepY($interval);
 
 $g->DrawGraph();