Location: PHPKode > projects > Skat Statistics > skatstat-1.7.1/skatstat/inc/graphen.php
<?php
/* POST Parameter
 * GraphType : Spielwert, Perf, Fieber, Spielliste
 * SpielerId: Array of Ids, Single Value
 * CatId:     Cateory Id
 * FieberType:  1: Platzierung, 2: Punkte
 * LastNumListen:  Anzahl Listen der Vergangenheit
 * NumValidPlayers: Single Value
 * */

require_once('authentication.php');
require_once('smfunc.php');
require_once('jpgraph/jpgraph.php');
require_once('jpgraph/jpgraph_line.php');
require_once('jpgraph/jpgraph_bar.php');
require_once('jpgraph/jpgraph_date.php');
require_once('jpgraph/jpgraph_scatter.php');
require_once('jpgraph/jpgraph_radar.php');

check_auth();

$xmin = NULL;
$ymin = NULL;
$xmax = NULL;
$ymax = NULL;

/* POST Parameter */
$LastNumListen    = $_GET['LastNumListen'];
$FieberType       = $_GET['FieberType'];
$SpielerId        = $_GET['SpielerId'];
$CategoryId       = $_GET['CatId'];
$NumSpieler       = $_GET['NumValidPlayer'];
$PlotType         = $_GET['GraphType'];

// Start here to build the graph
$graph = new Graph(800,600, "auto");
// array of plots
$Plots = array();

if( $PlotType == "Spielwert" )
{
  if( 1 == count( $SpielerId ) && $SpielerId[0] )
  {
    $sql = generateSpielwertVerteilungSQL( $CategoryId, $SpielerId[0]);
  }
  else
  {
    $sql = generateSpielwertVerteilungSQL( $CategoryId );
  }
}
elseif( $PlotType == "GewonnenVerloren" )
{
  # smaller Graph
  if( 1 == count( $SpielerId ) && $SpielerId[0] )
  {
    $sql = sqlGewonnenVerlorenVerteilung( $CategoryId, $SpielerId[0] );
  }
  else
  {
    $sql = sqlGewonnenVerlorenVerteilung( $CategoryId );
  }
}
elseif( $PlotType == "Spielliste" )
{
  if( 1 == count( $SpielerId ) && $SpielerId[0] )
  {
    $sql = generateSpiellistenVerteilungSQL( $CategoryId, $SpielerId[0]);
    $sqlex = generateSpiellistenVerteilungSQLEx( $CategoryId, $SpielerId[0]);
  }
  else
  {
    $sql = generateSpiellistenVerteilungSQL( $CategoryId );
    $sqlex = generateSpiellistenVerteilungSQLEx( $CategoryId );
  }
}
elseif( $PlotType == "Perf" )
{
  $sql = generateWeeklyPerformanceSQL( $CategoryId, $SpielerId[0] );
  # To guarantee, performance graph on all values
  $LastNumListen  = -1;
}
elseif( $PlotType == "Fieber" )
{
  $sql = generateFieberSQL( $CategoryId, $FieberType );
}
else
{
  exit;
}

if( $sql )
{
  $sqlExec = processSQL( $sql );

  // y-Value: Array of positive y values
  $ydata = array();

  // row-Value: Index of the ith x value
  $xdata = array();
  $xdatacolor = array();

  if( $PlotType == "Spielwert" )
  {
    // y-value: array of necg y values
    $ydataneg = array();

    $sql  = sqlBestOfCategory( $CategoryId );
    $exec = processSQL( $sql );

    $i = 0;
    $legend = array();
    while( $line = mysql_fetch_array( $exec ) )
    {
      $ydataneg[ $i ]   = array();
      $ydata[ $i ]      = array();
      $pos[ $line[3] ]  = $i;  # Mapping Name -> Index
      $legend[$i]       = $line[3]; # Player name
      $i++;                   # Index
    }

    $i         = -1;
    $lastx     = -1;
    while ( $line = mysql_fetch_array($sqlExec) )
    {
      if( $lastx != $line[0] )
      {
        $i++;
        $lastx = $line[0];

        for( $j = 0; $j < count($legend); $j++ )
        {
          if( $lastx < 0 )
          {
            $ydataneg[ $j ][$lastx / -2 ] = 0;
            $ydata[ $j ][ $lastx / -2 ]   = 0;
          }
          else
          {

            $ydata[ $j ][ $lastx ] = 0;
          }
        }
      }
      if( $line[0] < 0 )
      {
        $ydataneg[ $pos[$line[1]] ][ $line[0] / -2] = -$line[2];
      }
      else
      {
        $ydata[ $pos[$line[1]] ][ $line[0] ] = $line[2];
      }
    }


    $ymaxa = array();
    $ymina = array();

    $accbars = array();
    $accbarsneg = array();
    for( $i = 0; $i < count( $ydata ); $i++ )
    {
      $xdata = array();
      $ydat1 = array();
      $ydat2 = array();
      $keys  = array_keys( $ydata[ $i ] );
      sort( $keys );
      foreach(  $keys as $key  )
      {
        if( ! array_key_exists( $key, $ymaxa))
        {
          $ymaxa[ $key ] = 0;
        }
        if( ! array_key_exists( $key, $ymina))
        {
          $ymina[ $key ] = 0;
        }

        $xdata[] = $key;

        $ydat1[] = $ydata[ $i ][ $key ];
        $ymaxa[ $key ] += $ydata[ $i ][ $key ];

        if( array_key_exists( $key, $ydataneg[ $i ] ) )
        {
          $ydat2[] = $ydataneg[ $i ][ $key ];
          $ymina[ $key ] += $ydataneg[ $i ][ $key ];
        }
        else
        {
          $ydat2[] = 0;
        }
      }
      if( count( $ydat1 ) )
      {
        $p = new BarPlot( $ydat1 );
        $p->SetLegend( $legend[ $i ] );
        $p->SetWidth( 1 );
        $p->SetAlign( "center" );
        $p->SetFillColor( Color( $i ) );
        $accbars[] = $p;
      }

      if( count( $ydat2 ) )
      {
        $p = new BarPlot( $ydat2 );
        $p->SetWidth( 1 );
        $p->SetAlign( "center" );
        if( 1 == count( $SpielerId ) && $SpielerId[0] )
        {
          $p->SetFillColor( ColorNeg( $i ) );
        }
        else
        {
          $p->SetFillColor( Color( $i ) );
        }
        $accbarsneg[] = $p;
      }
    }

    // Add new AccBarPlot to the plots to display
    if( count( $accbars ) )
    {
      $Plots[] = new AccBarPlot( $accbars );
    }
    if( count( $accbarsneg ) )
    {
      $Plots[] = new AccBarPlot( $accbarsneg );
    }

    if( 0 < count( $Plots ) )
    {
      $sline   = new PlotLine (HORIZONTAL,0, "black",1);
      $Plots[] = $sline;
      list($xmax, $ymax) = $Plots[0]->Max();
      list($xmin, $ymin) = $Plots[1]->Min();

      $ymax = $ymax == 0 ? 1 : $ymax;

      // Graph look and feel
      $graph->SetScale("textint", $ymin,  $ymax );
      #$graph->SetScale("textint" , 0, 0);
      $graph->SetMargin(50,40,20,70);

      $graph->xaxis->SetTickLabels( $xdata );
      $graph->xaxis->SetTextLabelInterval(1);

      $tickstep = floor( ( $ymax - $ymin )  / min( NOYSAMPLES, max(1, ($ymax-$ymin) ) ) );


      $tickstepmin = max( 1, floor( $tickstep / 2 ) );
      $graph->yaxis->scale->ticks->Set($tickstep, $tickstepmin );

      $graph->yaxis->title->Set( "Anzahl" );
      $graph->xaxis->title->Set( "Spielwert" );
      $graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
      $graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
      $graph->xaxis->SetPos( 'min' );

      $graph->xaxis->SetTitleMargin( 15 );
      $graph->yaxis->SetTitleMargin( 35 );
      $graph->yaxis->SetPos( 'min' );


      $graph->legend->Pos(0.5, 0.990, "center", "bottom");
      $graph->legend->SetLayout(LEGEND_HOR);
    }
  }
  elseif( $PlotType == "GewonnenVerloren" )
  {
    $ydata    = array();
    $xdata    = array();
    $xmin     = 1000000;
    $xmax     = -1000000;
    $ymin     = 1000000;
    $ymax     = -1000000;
    $max      = -1000000;
    $min      =  1000000;
    while( $line = mysql_fetch_array($sqlExec) )
    {
      $xmin = $line[gewonnen] < $xmin ? $line[gewonnen] : $xmin;
      $xmax = $line[gewonnen] > $xmax ? $line[gewonnen] : $xmax;
      $ymin = $line[verloren] < $ymin ? $line[verloren] : $ymin;
      $ymax = $line[verloren] > $ymax ? $line[verloren] : $ymax;
      $max = $line[anzahl] > $max ? $line[anzahl] : $max;
      $min = $line[anzahl] < $min ? $line[anzahl] : $min;

      $xdata[] = $line[gewonnen];
      $ydata[] = $line[verloren];
    }

    $graph = new RadarGraph( 300, 200, "auto" );
    $graph->SetCenter( 0.5, 0.5 );

    $xdata = array( 0.61, 0.02, 0.35, 0.61, 0.08, 0.15, 0.1 );
    $ydata = array( 0.34, 0.03, 0.21, .40, 0.08, 0.20, 0.42 );

    $b1 = new  RadarPlot( $xdata );
    $b1->SetFill(false);
    $b1->SetColor("red");
    $b2 = new  RadarPlot( $ydata );

    $Plots[] = $b1;
    $Plots[] = $b2;


  }
  elseif( $PlotType == "Fieber" || $PlotType == "Perf" )
  {
    $line = mysql_fetch_array($sqlExec);
    $lastSpiellisteId = -1;
    $CountListen = 0;
    $ymax = -100000;
    $ymin =  100000;
    do {
      # All player are in sequence
      # when ID is changing, add values
      #if( $line[ 'SpiellisteId' ] != $lastSpiellisteId )
      if( $line[ 'Datum' ] != $lastSpiellisteId )
      {
        if( $CountListen++ >= $LastNumListen )
        {
          if( 2 != $FieberType )
          {
            arsort( $values, SORT_NUMERIC );
          }
          else
          {
            asort( $values, SORT_NUMERIC );
          }
          $i = 1;
          $werte = array();
          foreach( array_keys( $values ) as $oi )
          {
            #echo $oi."\n";
            $werte[ $oi ] = $i++;
          }

          # x-axis
          array_push( $xdata , 1 + date( "z", strtotime($lastSpiellisteId) ) );
          foreach( $SpielerId as $SpId )
          {
            # make array
            if( !is_array( $ydata[ $SpId ] ) )
            {
              $ydata[ $SpId ] = array();
              $ydata2[ $SpId ] = array();
            }
            # y axis points
            if( 2 == $FieberType || $PlotType == "Perf" )
            {
              array_push( $ydata[$SpId], $values[ $SpId ] );
              #
              # Y-values for the scatter plot for every day
              if( 0  < $meancountperday[ $SpId ] )
              {
                array_push( $ydata2[$SpId], $meanperday[ $SpId ] );
                $ymax = $meanperday[ $SpId ] < $ymax ? $ymax : $meanperday[$SpId];
                $ymin = $meanperday[ $SpId ] > $ymin ? $ymin : $meanperday[$SpId];
              }
              else
              {
                array_push( $ydata2[$SpId], "" );
              }
              $meanperday[ $SpId ] = 0;
              $meancountperday[ $SpId ] = 0;
            }
            else
            {
              if( $werte[ $SpId ] )
              {
                array_push( $ydata[$SpId], - $werte[ $SpId ] );
              }
              else
              {
                array_push( $ydata[$SpId], "" );
              }
            }
          }
        }
        $lastSpiellisteId = $line[ 'Datum' ];
      }

#     if( $CountListen >= $LastNumListen )
#     {
        # remember the names for the legend
        $name[ $line['SpielerId'] ] = $line['SpielerName'];;

        # n + 1
        $count[ $line['SpielerId'] ] *= 1;
        $count[ $line['SpielerId'] ] += 1;

        $meancountperday[ $line['SpielerId'] ] *= 1;
        $meancountperday[ $line['SpielerId'] ] += 1;

        # here some mean value calculation should take place
        # mean_new
        $meanperday[ $line['SpielerId'] ] =
          ( $line['Punkte'] + ( $meancountperday[ $line['SpielerId'] ] - 1 )
          * $meanperday[ $line['SpielerId'] ]  ) /  $meancountperday[ $line['SpielerId'] ];

        $values[ $line['SpielerId'] ] =
          ( $line['Punkte'] + ( $count[ $line['SpielerId'] ] - 1 )
          * $values[ $line['SpielerId'] ]  ) /  $count[ $line['SpielerId'] ];
#     }

      $datum = strtotime($line['Datum']);
    } while ( $line = mysql_fetch_array($sqlExec) );

    foreach( $SpielerId as $SpId )
    {
      $ymax = $meanperday[ $SpId ] < $ymax ? $ymax : $meanperday[$SpId];
      $ymin = $meanperday[ $SpId ] > $ymin ? $ymin : $meanperday[$SpId];
    }

    if( 2 != $FieberType )
    {
      arsort( $values, SORT_NUMERIC );
    }
    else
    {
      asort( $values, SORT_NUMERIC );
    }

    $i = 1;
    $werte = array();
    foreach( array_keys( $values ) as $oi )
    {
      $werte[ $oi ] = $i++;
    }
    array_push( $xdata , date( "z", $datum ) + 1 );
    #
    #create legend and last data point
    $i = 0;
    foreach( $SpielerId as $SpId )
    {
      # y axis points
      if( 2 == $FieberType || "Perf" == $PlotType )
      {
        $ydata[$SpId][] = $values[ $SpId ];
        if( 0  < $meancountperday[ $SpId ] )
        {
          array_push( $ydata2[$SpId], $meanperday[ $SpId ] );
        }
        else
        {
          array_push( $ydata2[$SpId], "" );
        }
        if( "Perf" == $PlotType )
        {
          $s = new ScatterPlot( $ydata2[ $SpId ], $xdata );
          $s->SetImpuls();
          #         $s->mark->SetColor( Color( $i ) );
          #         $s->mark->SetFillColor( Color( $i ) );
          $Plots[] = $s;
        }
      }
      else
      {

        if( $werte[ $SpId ] )
        {
          $ydata[$SpId][] = -$werte[ $SpId ];
        }
        else
        {
          $ydata[$SpId][] = "x";
        }
      }

      if( 1 < count( $ydata[ $SpId ] ) )
      {
        $b = new LinePlot( $ydata[$SpId], $xdata );
        $b->SetColor( Color( $i++ ) );
      }
      else
      {
        $b = new ScatterPlot( $ydata[ $SpId ], $xdata );
        $b->mark->SetColor( Color( $i ) );
        $b->mark->SetFillColor( Color( $i++ ) );
      }
      $b->SetWeight( 2 );
      $b->SetLegend( $name[ $SpId ] );
      $Plots[] =  $b;
    }

    if( 1 == $FieberType )
    {
      $graph->SetScale('intint');
      $graph->yaxis->SetLabelFormatCallback("_cb_negate");
      $graph->yaxis->title->Set( "Platzierung" );
      $graph->xaxis->SetPos( 'min' );
    }
    else if( "Perf" == $PlotType )
    {
      if( $ymin == $ymax )
      {
        $ymin -= 1;
        $ymax += 1;
      }
      $graph->SetScale('intlin',  $ymin, $ymax);

      $tickstep = floor( ( $ymax - $ymin )  / min(NOYSAMPLES, max( 1, $ymax - $ymin ) ) );
      $tickstepmin = max( 0, floor( $tickstep / 2 ) );

      $graph->yaxis->scale->ticks->Set($tickstep, $tickstepmin);
      $graph->xaxis->SetPos( 'min' );
      $graph->yaxis->title->Set( "Punkte pro Spielliste" );
    }
    else
    {
      $graph->SetScale('intlin');
      $graph->xaxis->SetPos( 'min' );
      $graph->yaxis->title->Set( "mittl. Punkte pro Spielliste" );
    }

    $graph->SetMargin(50,40,20,70);
    $graph->xaxis->SetLabelAngle( 90 );
    $graph->xaxis->SetTitleMargin( 15 );
    $graph->xaxis->title->Set( "Tag des Jahres (TdJ)" );
    $graph->xaxis->title->SetFont( FF_FONT1, FS_BOLD );

    $graph->yaxis->SetTitleMargin( 35 );
    $graph->yaxis->title->SetFont( FF_FONT1, FS_BOLD );

    $graph->legend->SetLayout(LEGEND_HOR);
    $graph->legend->Pos(0.5, 0.98, "center", "bottom");
  }
  elseif( $PlotType == "Spielliste" )
  {
    # determin extreme values for the distribution plot
    $sqlExecEx  = processSQL( $sqlex );
    $line       = mysql_fetch_array($sqlExecEx);
    $xmin     =  $line[0];
    $xmax     =  $line[1];

    $ymin = 000;
    $ymax =-1000;

    $sumanzahl  = 0;
    $firstrun = true;
    $bucketsize = max( 1, ( $xmax - $xmin ) / NOXSAMPLES );

    $ydata[0] = array();
    $ydata[1] = array();
    $xdata    = array();
    while( $line = mysql_fetch_array($sqlExec) )
    {
      $wert   = $line[0];
      $anzahl = $line[1];

      if( $firstrun )
      {
        $firstrun = false;
        $oldwert  = $wert;
        $sumanzahl  = 0;
      }
      if( $wert > $oldwert + $bucketsize )
      {
        $xdata[] = $oldwert;
        $xdatacolor[] = $oldwert < 0 ? ColorNeg() : ColorPos();
        if( $oldwert > 0 && $sumanzahl > 0)
        {
          array_push( $ydata[1], "" );
          array_push( $ydata[0], $sumanzahl );
        }
        else
        {
          array_push( $ydata[1], $sumanzahl );
          array_push( $ydata[0], "" );
        }
        $ymax = $sumanzahl > $ymax ? $sumanzahl : $ymax;

        $sumanzahl = $anzahl;
        $oldwert   = $wert;
      }
      else
      {
        $sumanzahl += $anzahl;
      }
    }
    $ymax = $sumanzahl > $ymax ? $sumanzahl : $ymax;
    $xdata[] = $oldwert;
    $xdatacolor[] = $oldwert < 0 ? ColorNeg() : ColorPos();
    if( $oldwert > 0 && $sumanzahl > 0)
    {
      array_push( $ydata[0], $sumanzahl );
      array_push( $ydata[1], "" );
    }
    else
    {
      array_push( $ydata[1], $sumanzahl );
      array_push( $ydata[0], "" );
    }

    $plot1 =  new BarPlot( $ydata[0]  );
    $plot1->SetFillColor( ColorPos() );
    $plot1->SetAbsWidth( 10 );
    $plot1->SetFillGradient("lightblue", "blue", GRAD_VER);

    $plot2 =  new BarPlot( $ydata[1]  );
    $plot2->SetFillColor( ColorNeg() );
    $plot2->SetFillGradient("orange", "red", GRAD_VER);
    $plot2->SetAbsWidth( 10 );

    $Plots[] =  new GroupBarPlot( array( $plot1, $plot2 ) );

    $graph->SetScale("textint", $ymin, 1.05 * $ymax );

    $graph->xaxis->SetTickLabels( $xdata, $xdatacolor );
    $graph->xaxis->SetTextLabelInterval(1);

    $graph->xaxis->title->Set("Punkte pro Spielliste");
    $graph->yaxis->title->Set("Absolute Haeufigkeit");
    $graph->xaxis->title->SetFont( FF_FONT1, FS_BOLD );
    $graph->yaxis->title->SetFont( FF_FONT1, FS_BOLD );
    $graph->SetMargin(50,40,40,50);
    $graph->xaxis->SetTitleMargin( 15 );
    $graph->yaxis->SetTitleMargin( 35 );

    # determine ticks
    $tickstep = floor(  $bucketsize  );
    $tickstepmin = 0;
    $graph->xaxis->scale->ticks->Set($tickstep, $tickstepmin);

    $tickstep = floor( ( $ymax - $ymin )  / min(NOYSAMPLES, max(1,( $ymax - $ymin ) )  ) );
    $tickstepmin = max( 1, floor($tickstep / 2 ) );
    $graph->yaxis->scale->ticks->Set($tickstep, $tickstepmin );

    $graph->xaxis->SetLabelAngle( 90 );
    $graph->yaxis->SetPos( 'min' );
  }
  else
  {
    exit;
  }

  for( $i = 0; $i < count( $Plots ) ; $i++ )
  {
    $graph->Add( $Plots[ $i ] );
  }
  if( count( $Plots ) )
  {
    $graph->Stroke();
  }
}

function Color( $index )
{
  // Array of nice looking colors
  $ColorMap = array( "gold", "firebrick", "dodgerblue", "forestgreen", "yellow3", "hotpink", "chocolate", "lightgreen", "pink", "orange3", "navy", "salmon", "sienna", "tomato4" );
  $l = count( $ColorMap );

  return( $ColorMap[ $index % $l ] );
}

function ColorPos()
{
  return "royalblue";
}

function ColorNeg()
{
  return "red";
}

// Callback to negate the argument
function _cb_negate($aVal)
{
  return -$aVal;
}

?>
Return current item: Skat Statistics