PHP jpgraph不会显示多个图形

时间:2012-06-15 12:29:55

标签: php jpgraph

我一直在尝试使用jpgraph php代码在一个屏幕上显示两个图形。根据jpgraph,可以通过以下两种方法之一来完成,here。虽然我可以使用$ graph-> Stroke()来单独显示四个图中的任何一个;我不能为我的生活找出如何使用jpgraph示例中规定的方法一起显示它们。附件中的mgraph代码对我来说是正确的,但没有任何内容会出现。任何帮助,将不胜感激。干杯

    <?php require("sess_start.php");?><?php if ($_GET[sessid] > 0) { //show page if user has a valid session ?><?php
//    AppServerType:PHP4

require("./../adodb/adodb.inc.php");
require("./../Connections/m_water.php");


include ("./../jpgraph/src/jpgraph.php");
include ("./../jpgraph/src/jpgraph_line.php");

$ydata = array(2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32);
$datax = array("1","2","3","4","5","6","7","8","9","10","11","12");
$max_inshore_effort = array("8", "8", "8", "8", "8", "8", "8", "8", "8", "8", "8",     "8", "8", "8", "8", "8");
$max_offshore_effort = array("12", "12", "12", "12", "12", "12", "12", "12", "12", "12", "12", "12", "12", "12", "12", "12");


//--------------------------------------------------------------------------------------------------------------------------------
//GRAPH 1

// Create the graph. These two calls are always required
//$graph = new Graph(550,400,"auto");
//$graph->SetScale("textlin");

//$graph->xaxis-> SetTickLabels($datax);
//$graph->xaxis->SetFont(FF_FONT2);
//$graph->yaxis->SetFont(FF_FONT2);


// Create the linear plot
//$lineplot=new LinePlot($ydata);


  // Add the plot to the graph
  //$graph->Add($lineplot);
  //$lineplot->SetColor('#DC143C');  //set the colour of the line this case dark red
 //$lineplot->SetWeight(1.5); // set the thickness of the line
 //$lineplot->SetLegend('Total group effort'); // add a description to the legend
//$lineplot->mark->SetType(MARK_FILLEDCIRCLE,'',1.0); // determine what type of marker is used
 //$lineplot->mark->SetType(MARK_SQUARE,'',2.0); // determine what type of marker is used
 //$lineplot->mark->SetSize(6);
//$lineplot->mark->SetColor('#000000');   // determine the colour of the outer line surrounding the marker
//$lineplot->mark->SetFillColor('#DC143C'); // set the fill for the marker


//$graph->img->SetMargin(60,60,50,80);
//$graph->xaxis->SetTitle('Round', 'center');
//$graph->yaxis->SetTitle('Total group effort', 'center');
 //$graph->xaxis->SetTitleMargin(12);
 //$graph->yaxis->SetTitleMargin(32);

// Setup a nice title with a striped bevel background
//$graph->title->Set("State of Area 1 (Inshore)"); // alter the title heading
//$graph->title->SetFont(FF_ARIAL,FS_BOLD,16); // alter the font of the title and size
   //$graph->title->SetColor('#FFFFFF');   // alter the colour of the title
    //$graph->SetTitleBackground('#4682B4',TITLEBKG_STYLE1,TITLEBKG_FRAME_BEVEL); // insert 3d bevel for title
             //$graph->SetTitleBackgroundFillStyle(TITLEBKG_FILLSTYLE_HSTRIPED,'blue','darkgreen');  // insert alternating horizontal lines across title fill box;


 //$graph->tabtitle->SetColor('navy','lightyellow','navy');

 //$graph->yaxis->SetColor('#000000'); //set the colour of the y axis
 //$graph->xaxis->SetColor('#000000'); // set the colour of the x axis

  // Change the fonts of the axis and title
  //$graph->title->SetFont(FF_FONT1,FS_BOLD);
  //$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
 //$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
//$graph->xaxis->SetTickSide(SIDE_DOWN);
 //$graph->xaxis->SetLabelMargin(6);
//$graph->yaxis->SetTickSide(SIDE_LEFT);
 //$graph->yaxis->SetLabelMargin(8);

  //$lineplot->SetColor("#DC143C");

 // Add a second axis displaying the maximum effort allowed for inshore area
 //$p1 = new LinePlot($max_inshore_effort);
 //$graph->Add($p1);
 //$p1->SetColor("#2E8B57");
//$p1->SetLegend('Max effort inshore to stay abundant');
//$p1->SetWeight(3); // set the thickness of the line
 //$p1->SetCenter();
//$p1->SetStyle('longdashed');

 //$graph->SetShadow();

   // Adjust the legend position
  //$graph->legend->SetLayout(LEGEND_HOR);
 //$graph->legend->Pos(0.15,0.94,"bottom","center");  //adjust the position of the    legend on the graph
   //$graph->legend->SetFillColor('#D3D3D3');

    //--------------------------------------------------------------------------------------------------------------------------------
//GRAPH 2

   // Create the graph. These two calls are always required
$graph2 = new Graph(550,400,"auto");
$graph2->SetScale("textlin");

$graph2->xaxis-> SetTickLabels($datax);
 $graph2->xaxis->SetFont(FF_FONT2);
$graph2->yaxis->SetFont(FF_FONT2);


// Create the linear plot
 $lineplot2=new LinePlot($ydata);


    // Add the plot to the graph
   $graph2->Add($lineplot2);
   $lineplot2->SetColor('#DC143C');  //set the colour of the line this case dark red
    $lineplot2->SetWeight(1.5); // set the thickness of the line
  $lineplot2->SetLegend('Total group effort'); // add a description to the legend
   //$lineplot->mark->SetType(MARK_FILLEDCIRCLE,'',1.0); // determine what type of marker is used
    $lineplot2->mark->SetType(MARK_SQUARE,'',2.0); // determine what type of marker is used
    $lineplot2->mark->SetSize(6);
   $lineplot2->mark->SetColor('#000000');   // determine the colour of the outer line surrounding the marker
    $lineplot2->mark->SetFillColor('#DC143C'); // set the fill for the marker


 $graph2->img->SetMargin(60,60,50,80);
 $graph2->xaxis->SetTitle('Round', 'center');
$graph2->yaxis->SetTitle('Total group effort', 'center');
$graph2->xaxis->SetTitleMargin(12);
$graph2->yaxis->SetTitleMargin(32);

 // Setup a nice title with a striped bevel background
    $graph2->title->Set("State of Area 1 (Offshore)"); // alter the title heading
    $graph2->title->SetFont(FF_ARIAL,FS_BOLD,16); // alter the font of the title and size
    $graph2->title->SetColor('#FFFFFF');   // alter the colour of the title
    $graph2->SetTitleBackground('#4682B4',TITLEBKG_STYLE1,TITLEBKG_FRAME_BEVEL); // insert 3d bevel for title
 //$graph->SetTitleBackgroundFillStyle(TITLEBKG_FILLSTYLE_HSTRIPED,'blue','darkgreen');     //      insert alternating horizontal lines across title fill box;


$graph2->tabtitle->SetColor('navy','lightyellow','navy');

  $graph2->yaxis->SetColor('#000000'); //set the colour of the y axis
 $graph2->xaxis->SetColor('#000000'); // set the colour of the x axis

 // Change the fonts of the axis and title
 $graph2->title->SetFont(FF_FONT1,FS_BOLD);
 $graph2->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph2->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph2->xaxis->SetTickSide(SIDE_DOWN);
$graph2->xaxis->SetLabelMargin(6);
 $graph2->yaxis->SetTickSide(SIDE_LEFT);
 $graph2->yaxis->SetLabelMargin(8);

 $lineplot2->SetColor("#DC143C");

// Add a second axis displaying the maximum effort allowed for inshore area
$p12 = new LinePlot($max_offshore_effort);
$graph2->Add($p12);
$p12->SetColor("#0000CD");
$p12->SetLegend('Max effort offshore to stay abundant');
$p12->SetWeight(3); // set the thickness of the line
$p12->SetCenter();
$p12->SetStyle('longdashed');

 $graph2->SetShadow();

 // Adjust the legend position
 $graph2->legend->SetLayout(LEGEND_HOR);
$graph2->legend->Pos(0.15,0.94,"bottom","center");  //adjust the position of the legend on the graph
$graph2->legend->SetFillColor('#D3D3D3');
 //$handle2 =  $graph2->Stroke( _IMG_HANDLER);

// Display the individual graph
$graph->Stroke();
?>
?>

// Create a combined graph
       //$mgraph = new MGraph();
     //$xpos1=3;$ypos1=3;
      //$xpos2=3;$ypos2=200;
      //$mgraph->Add($graph);
     //$mgraph->Add($graph2,$xpos2,$ypos2);
    //$mgraph->Stroke();

?>
   <?php } //show page if user has a valid session ?>

2 个答案:

答案 0 :(得分:1)

在您的代码段中,您使用的是$graph->Stroke();,但您的变量名称为$graph2
我不知道这是否是你问题的原因,但在这种情况下$graph->Stroke();将不输出任何内容。

答案 1 :(得分:0)

据我所知graph->Stroke()将输出作为图像文件,graph->Stroke; graph2->Stroke;等的多个实例将覆盖仅显示最终图像的前一个实例。 我建议做的是,制作一个类似的文件 的 plotgraph.php:

<?php

$xdata=explode(",",$_GET['xdata']);
$ydata=explode(",",$_GET['ydata']);
$title=$_GET['graphtitle'];
// and any other variales that you need
// Your graph routines
// .. including $p->add($xdata,$ydata); etc statements
$graph->Stroke();
?>

在主文件中添加语句,如

<img src="plotgraph.php?xdata=comma_separated_xdata&ydata=comma_separated_ydata&graphtitle=your_graph_title">

这样,您可以使用各种参数插入任意数量的图形。我希望稍微澄清一下事情。 (请注意,我没有添加所有图形格式等,只是给你举例。如果你想为每个图形单独的字体等格式,你必须用url导出它并用$ _GET获取。其他常见的设置可以bbe在你的plotgraph.php文件中)