Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JDGraph Multiple X & Y in Same Graph

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
1
16
US
I'm using a simple function to generate a line plot in JPGraph but in actuality, it is receiving multiple columns at the same time and I am not sure how to make the graph show more than the single one. The graph will have up to 8 plots and there may also be multiple sets of data for each of the 8. Probably the plots would need to be not only different colors but also different line types with the legend showing which is which.

Because I know the graph will be getting crowded with up to 8 sets of data from multiple files, I have added a secondary form above the graph (not shown here) where certain columns can be deselected via checkboxes but I have not yet added all the coding for that until the graph is fully functional. The tips, or peaks, of the graph should have the column name too so as to know which is which and those can be supplied as an array if needed.

Here is the function as it is currently and I kept it basic due to the rush to get it done. I don't know classes at all so a simple function seemed the best.

Code:
// $ShowGraph variable is reserved for use in submitting secondary form of which columns to graph
function LineGraph($datay, $datax, $JPGraphPath, $ImageName, $ProcessType, $ShowGraph, $UpdateSQL, $mysqli) {
	require_once ($JPGraphPath."/jpgraph.php");
	require_once ($JPGraphPath."/jpgraph_line.php");
	require_once ($JPGraphPath."/jpgraph_log.php");	
 
	// Create the graph and set a scale.
	// These two calls are always required
	$graph = new Graph(800,450);
	$graph->SetImgFormat('png',60);
	
	//$graph->SetScale("linlin",0,800);	
	$graph->SetScale('intlog');

	$graph->yaxis->SetLabelFormatCallback('number_format');

	$graph->img->SetMargin(100,40,40,75);
	$graph->SetShadow();

	$graph->title->Set($ProcessType." Data");
	$graph->title->SetFont(FF_ARIAL,FS_BOLD,18);

	// Set Y-Axis appearance
	$graph->yaxis->SetTitle("Counts",'middle');
	//$graph->yaxis->scale->ticks->Set(100,25);	
	$graph->yaxis->title->SetFont(FF_ARIAL,FS_BOLD,10);
	$graph->yaxis->title->SetMargin(50);

	// Set X-Axis title
	$graph->xaxis->SetTitle("Dark Current",'middle');
	$graph->xaxis->scale->ticks->Set(40,20);	
	$graph->xaxis->title->SetFont(FF_ARIAL,FS_BOLD,10);
	$graph->xaxis->title->SetMargin(20);

	$colors = array('yellow','black','blue','red','green','purple','lightblue');	

	foreach($datay as $key=>$_datay):
		// Query to fetch legend file name from file_uploads table
		$queryFN = "SELECT FileName FROM file_uploads ";
                $queryFN .= "WHERE ID = ".$key." ";
                if ($ShowGraph):
                     $queryFN .= "AND .....";
                endif;
                $queryFN .= "ORDER by FileName";
			if ($result = $mysqli->query($queryFN)):
				$row = $result->fetch_row();
				$FileName = $row[0];
				$result->close();
			endif;
		$sp[$key] = new LinePlot($_datay,$datax[$key]);
		$graph->Add($sp[$key]);
		$sp[$key]->mark->SetFillColor( isset($colors[$key]) ? $colors[$key] : $colors[0] );
		$sp[$key]->SetLegend(basename($FileName, ".csv"));
		//$sp[$key]->SetLegend(basename("FileName".$key.".csv"));	
		// Link plot points (unremark next three lines to use)
		//$sp[$key]->link->show();
		//$sp[$key]->link->setWeight(1);
		//$sp[$key]->link->setColor(  isset($colors[$key]) ? $colors[$key] : $colors[0]   );
	endforeach; 

	// Produce an image, display the graph
	$fileName = $JPGraphPath."/Temp/".$ImageName.".png";	
	$graph->Stroke($fileName); // Creates image file

	$OutputImage = "<div class=\"ImageGraph\">\n";
	$OutputImage .= "<img src=\"jpgraph/Temp/".$ImageName.".png\">\n";
	$OutputImage .= "</div>\n\n";

	//clean_old_tmp_files();
	//unlink($fileName);

	echo $OutputImage;
}
 
I inadvertently pasted in an older version without the built-in query. Below is the action code with the query:

Code:
function LineGraph($JPGraphPath, $ImageName, $ProcessType, $ShowGraph, $mysqli) {
if ($result = $mysqli->query("$ShowGraph")) :
	while ($row = $result->fetch_array()) :
		$datax[$row[1]][] = $row[2];
		$datay[$row[1]][] = $row[3];
	endwhile;
	$result->close();
else :
	echo "<div class=\"Message\">\n"; 
	printf("MySQLi Error: %s\n", $mysqli->error);
	echo "</div>\n\n";
endif;
	require_once ($JPGraphPath."/jpgraph.php");
	require_once ($JPGraphPath."/jpgraph_line.php");
	require_once ($JPGraphPath."/jpgraph_log.php");	
 
	// Create the graph and set a scale.
	// These two calls are always required
	$graph = new Graph(800,450);
	$graph->SetImgFormat('png',60);
	
	//$graph->SetScale("linlin",0,800);	
	$graph->SetScale('intlog');

	$graph->yaxis->SetLabelFormatCallback('number_format');

	$graph->img->SetMargin(100,40,40,75);
	$graph->SetShadow();

	$graph->title->Set($ProcessType." Data");
	$graph->title->SetFont(FF_ARIAL,FS_BOLD,18);

	// Set Y-Axis appearance
	$graph->yaxis->SetTitle("Counts",'middle');
	//$graph->yaxis->scale->ticks->Set(100,25);	
	$graph->yaxis->title->SetFont(FF_ARIAL,FS_BOLD,10);
	$graph->yaxis->title->SetMargin(50);

	// Set X-Axis title
	$graph->xaxis->SetTitle("Dark Current",'middle');
	$graph->xaxis->scale->ticks->Set(40,20);	
	$graph->xaxis->title->SetFont(FF_ARIAL,FS_BOLD,10);
	$graph->xaxis->title->SetMargin(20);

	$colors = array('yellow','black','blue','red','green','purple','lightblue');	

	foreach($datay as $key=>$_datay):
		// Query to fetch legend file name from file_uploads table
		$queryFN = "SELECT FileName FROM file_uploads ";
		$queryFN .= "WHERE ID = ".$key." ";
		$queryFN .= "ORDER by FileName";

		$sp[$key] = new LinePlot($_datay,$datax[$key]);
		$graph->Add($sp[$key]);
		$sp[$key]->mark->SetFillColor( isset($colors[$key]) ? $colors[$key] : $colors[0] );
		$sp[$key]->SetLegend(basename($FileName, ".csv"));
		//$sp[$key]->SetLegend(basename("FileName".$key.".csv"));	
		// Link plot points (unremark next three lines to use)
		//$sp[$key]->link->show();
		//$sp[$key]->link->setWeight(1);
		//$sp[$key]->link->setColor(  isset($colors[$key]) ? $colors[$key] : $colors[0]   );
	endforeach; 

	// Produce an image, display the graph
	$fileName = $JPGraphPath."/Temp/".$ImageName.".png";	
	$graph->Stroke($fileName); // Creates image file

	$OutputImage = "<div class=\"ImageGraph\">\n";
	$OutputImage .= "<img src=\"jpgraph/Temp/".$ImageName.".png\">\n";
	$OutputImage .= "</div>\n\n";

	//clean_old_tmp_files();
	//unlink($fileName);

	echo $OutputImage;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top