Jump to content

a logic issue in creating multiple graphs using phpgraphlib


ajoo

Recommended Posts

Hi, 

 

I am creating charts using phpgraphlib - a very easy to use graphing library. once the graph is created in graph.php it just needs to be called from the HTML as follows:-

<html>
    <img src="graph.php" />
</html>

and the graph is plotted.  Within graph.php a data array is required which carries the values to be plotted. 

 

Now I need to plot about 10 graphs and in my HTML file I have created a 10 Tabbed layout that should each display one of the 10 graphs. In my Graph.php I have  have also created the 10 data arrays from mysql that I require for the 10 graphs. So almost all the work has been done in graph.php and all I need to do is to configure the graph and display the 10 graphs like this.

//configure graph
$graph->addData($dataArray);
$graph->setTitle("Sales by Group");
$graph->setGradient("lime", "green");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>

But then I need the 10 graphs in 10 files like graph1.php, graph2.php and so on which can be called from their respective tabbed HTML and I need to pass each of these 10 files their data array calculated in graph.php.

 

If I was to repeat the entire code of graph.php in graph1.php, graph2.php and so on for all 10 files  & each of these files had to calculate their own data and invoke the graph it will work but it would be a lot of ( 10 times of ) recalculation of the data arrays. ( All data for the 10 graphs can be calculated from the same single query). 

 

I was just wondering if there can be some method by which I can somehow achieve this without repeating the same process 10 times. 

 

I hope I was able to explain the issue well. Looking for some way out of this. 

 

Thanks all.

 

 

 

 

Link to comment
Share on other sites

If the only thing that changes in each of the 10 graphs is the data then use the same "graph.php" but pass the different data (and maybe a caption) each time. Use something like this

$data_1 = array(1,2,3);
$data_2 = array(5,6,7);

$g1_data = json_encode($data_1);    // convert to a string
$g2_data = json_encode($data_2);    // convert to a string

echo "<img src='graph.php?caption=chart_1&data=$g1_data' />";
echo "<img src='graph.php?caption=chart_2&data=$g2_data' />";

In graph.php the data will be in $_GET['data']

$data_array = json_decode($_GET['data'], 1);  // reconstruct data array  
// create graph
Link to comment
Share on other sites

Hi Guru Barand, 

 

Thanks for the reply. Somehow it fails to display the graph. I have used your files almost as is. I am appending my code below. I do not get any errors either. 

 

index1.php: ( same as yours)

<?php
$data_1 = array(1,2,3,4,5,6,7);
$data_2 = array(5,6,7,7,7,6,5);

$g1_data = json_encode($data_1);    // convert to a string
$g2_data = json_encode($data_2);    // convert to a string

echo "<img src='graph1.php?caption=chart_1&data=$g1_data' />";
// echo "<img src='testGraph1.php?caption=chart_2&data=$g2_data' />";
?>

Graph1.php:

<?php
include('../phpgraphlib-master/phpgraphlib.php');
$mysqli_driver = new mysqli_driver();
$mysqli_driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;	// For error handling as pointed by guru barand

try
{
	$data_array = json_decode($_GET['data'], 1);  // reconstruct data array  
	print_r($data_array);
	$caption = $_GET['caption'];
}
catch (Exception $e) 
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

$graph = new PHPGraphLib(650,350);
$graph->addData($data_array);	
$graph->setLineColor("#ff0000");
$graph->setGradient('Red', 'maroon');	 

$graph->setTitle('caption');
$graph->setBars(true);
$graph->setLine(true);

$graph->setDataValues(true);
$graph->setDataValueColor('maroon');

$graph->createGraph();
?>

I cannot spot the error. Please help.

 

phpgraphlib was downloaded from : http://www.ebrueggeman.com/phpgraphlib/download

 

Thanks loads !

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.