<?php
/*******************************************************************************
** Basic Analysis and Security Engine (BASE)
** Copyright (C) 2004 BASE Project Team
** Copyright (C) 2000 Carnegie Mellon University
**
** (see the file 'base_main.php' for license details)
**
** Project Lead: Kevin Johnson <kjohnson@secureideas.net>
** Built upon work by Roman Danyliw <rdd@cert.org>, <roman@danyliw.com>
**
** Purpose: Purpose: Displays the actual .GIF/.PNG/.TIFF image
**          of the chart
**
** Input GET/POST variables
**   - width: chart width
**   - height: chart width
**   - pmargin0-3: plot margins
**   - title: chart title
**   - xaxis_label: x-axis label
**   - yaxis_label: y-axis label
**   - xdata[][]: data and label array for the y-axis
**   - yaxis_scale: (boolean) 0: linear; 1: logarithmic
**   - rotate_xaxis_lbl: (boolean) rotate X-axis labels 90 degrees
**   - style: [bar|line] chooses the style of the chart
********************************************************************************
** Authors:
********************************************************************************
** Kevin Johnson <kjohnson@secureideas.net
**
********************************************************************************
*/

  include ("base_conf.php");
  include ("$BASE_path/includes/base_state_common.inc.php");
  include ("$BASE_path/base_graph_common.php");

  session_start();

  $xdata = $HTTP_SESSION_VARS['xdata'];
  $width = ImportHTTPVar("width");
  $height = ImportHTTPVar("height");
  $pmargin0 = ImportHTTPVar("pmargin0");
  $pmargin1 = ImportHTTPVar("pmargin1");
  $pmargin2 = ImportHTTPVar("pmargin2");
  $pmargin3 = ImportHTTPVar("pmargin3");
  $title = ImportHTTPVar("title");
  $xaxis_label = ImportHTTPVar("xaxis_label");
  $yaxis_label = ImportHTTPVar("yaxis_label");
  $yaxis_scale = ImportHTTPVar("yaxis_scale");
  $xaxis_grid = ImportHTTPVar("xaxis_grid");
  $yaxis_grid = ImportHTTPVar("yaxis_grid");
  $rotate_xaxis_lbl = ImportHTTPVar("rotate_xaxis_lbl");
  $style = ImportHTTPVar("style");

  LoadGraphingLib($ChartLib_path);

  /* Create the data and label array */
  for ($i = 0; $i < count($xdata); $i++)
  {
      $xlabel[$i] = $xdata[$i][0];
      $ydata[$i] = $xdata[$i][1];
  }

  if ( $style == "pie" )
     $graph = new PieGraph($width, $height);
  else
     $graph = new Graph($width, $height);

  /* Set Margins */
  $graph->img->SetMargin($pmargin0,$pmargin1,$pmargin2,$pmargin3);    
  $graph->img->SetAntiAliasing();

  /* Set Plot type */
  switch($style)
  {
     case "bar":
        $plot[0] = new BarPlot($ydata);
        break;
     case "pie":
        $plot[0] = new PiePlot3D($ydata);
        break;
     case "line":
        $plot[0] = new LinePlot($ydata);
        break;
  }

  if ( ($style == "bar") || ($style == "line") )
  {
     /* Set Scale */
     if ( $yaxis_scale == 1 )
        $graph->SetScale("textlog");
     else
        $graph->SetScale("textlin");

     $plot[0]->SetFillColor("#BE0505");

     /* Set Gridlines */
     if ( $xaxis_grid == 1 )
        $graph->xgrid->Show(true);
     else
        $graph->xgrid->Show(false);

     if ( $yaxis_grid == 1 )
        $graph->ygrid->Show(true);
     else
        $graph->ygrid->Show(false);
  
     /* Set Axis Labels */
     $graph->xaxis->title->Set($xaxis_label);
     $graph->yaxis->title->Set($yaxis_label);

     $graph->xaxis->SetTickLabels($xlabel);

     if ( $rotate_xaxis_lbl == 1 )
        $graph->xaxis->SetLabelAngle(90);
  }

  if ( $style == "pie" )
  {
     $plot[0]->SetLegends($xlabel);
  }

  /* Set Title */
  $graph->title->Set($title);


  $graph->Add($plot[0]);

  $graph->Stroke();
?>









