PHP jpgraph не будет отображать несколько графиков
Я пытался отобразить два графика на одном экране, используя PHP-код PHP. Согласно jpgraph это может быть выполнено, следуя одному из двух способов, см. Здесь. Хотя я могу заставить любой из четырех графиков отображаться по отдельности, используя $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 ответа
В вашем фрагменте кода вы используете $graph->Stroke();
но имя вашей переменной $graph2
,
Я не знаю, является ли это причиной вашей проблемы, но в этом случае $graph->Stroke();
ничего не выведет.
Насколько я понимаю graph->Stroke()
собирается дать вывод в виде файла изображения и нескольких экземпляров graph->Stroke;
graph2->Stroke;
etc переопределит предыдущие, отображая только окончательное изображение. То, что я хотел бы предложить сделать, это сделать файл что-то вроде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)