So I am looking for a little help to figure out what I am doing wrong here. I am trying to use data in a mysql database to create multiple graphs for each row returned. I'm not doing it correctly, and was hoping someone would help me out.
Here is the full code:
Code:
<?php
$conn = mysql_connect("localhost","user","passwrd") or die(mysql_error());
mysql_select_db("db1", $conn) or die(mysql_error());
include("/usr/share/php/jpgraph-3.0.7/src/jpgraph.php");
include("/usr/share/php/jpgraph-3.0.7/src/jpgraph_line.php");
include('/usr/share/php/jpgraph-3.0.7/src/jpgraph_date.php');
include('/usr/share/php/jpgraph-3.0.7/src/jpgraph_utils.inc.php');
$servers = array('host1','host2','host3','host4');
foreach ($servers as $servername)
{
$sql = mysql_query("SELECT * FROM (SELECT * FROM db1.stattable1 WHERE host = '" . $servername . "' ORDER BY id DESC LIMIT 150) AS ttbl ORDER by id ASC");
while($row = mysql_fetch_array($sql))
{
$time[] = $row['dt'];
$load[] = $row['load'];
$mem[] = $row['mem'];
$cpu[] = $row['cpu'];
}
// Setup the graph
$graph = new Graph(1500,320,'auto');
$graph->SetScale("textlin");
$graph->yaxis->scale->SetGrace(25);
$graph->img->SetMargin(40,10,10,81);
$graph->SetShadow();
$graph->title->Set("$servername");
$graph->yaxis->HideZeroLabel();
$graph->yaxis->HideLine();
$graph->yaxis->HideTicks(false,false);
$graph->yaxis->title->Set("Title");
$graph->yaxis->SetTitleMargin(75);
$graph->xgrid->Show();
$graph->xaxis->HideLine();
$graph->xgrid->SetLineStyle("solid");
$graph->xaxis->SetTickLabels($time);
$graph->xaxis->SetLabelAngle(90);
$graph->xaxis->SetFont(FF_FONT0);
$graph->xgrid->SetColor('#E3E3E3');
$p1 = new LinePlot($load);
$graph->Add($p1);
$p1->mark->SetType(MARK_FILLEDCIRCLE);
$p1->mark->SetColor('#B22222');
$p1->mark->SetFillColor('#B22222');
$p1->mark->SetSize(2);
$p1->SetWeight(2);
$p1->SetColor("#B22222");
$p1->SetLegend("LoadAvg");
$p2 = new LinePlot($mem);
$graph->Add($p2);
$p2->mark->SetType(MARK_FILLEDCIRCLE);
$p2->mark->SetColor('#006600');
$p2->mark->SetFillColor('#006600');
$p2->mark->SetSize(2);
$p2->SetWeight(2);
$p2->SetColor("#006600");
$p2->SetLegend("Mem%");
$p3 = new LinePlot($cpu);
$graph->Add($p3);
$p3->mark->SetType(MARK_FILLEDCIRCLE);
$p3->mark->SetColor('#0000FF');
$p3->mark->SetFillColor('#0000FF');
$p3->mark->SetSize(2);
$p3->SetWeight(2);
$p3->SetColor("#0000FF");
$p3->SetLegend("CPU%");
$graph->legend->SetFrameWeight(1);
$graph->legend->SetPos(0,0,'right','top');
//Output line
$graph->Stroke();
}
?>
If this were me doing it, I would first use only one server 'host' and get it working with just one. Once one of them works, you'll know the others will work.
As TFLan mentioned, we don't know what those other scripts require, as per the query result arrays you are creating. Your query result is an array of various numbers and somehow those other scripts need to know what those numbers are. We don't know how those other scripts work.
Perhaps there are four graphs but they all occupy the same space? Or, by re-using $graph, it is over-writing the previous version?
I would consider using an array $graph[] and outputting a break <br> between each Stroke(). But you haven't explained what exactly goes wrong..
I'm with mlseim as well: get it working with one.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Thanks checking this out. I will add more info. Only the first two includes are being used, these are part of the jpgraph suite that calls all the functions that help build the graph.
include("/usr/share/php/jpgraph-3.0.7/src/jpgraph.php");
include("/usr/share/php/jpgraph-3.0.7/src/jpgraph_line.php");
These are the only 2 that are actually being used here. I will attach those to this response.
Using the code without the servers array, and the mysql query to just one host works fine. And even how I have it pasted above with the servers array it does output the first host graph.
I've used jpgraph before to graph output from a single DB query, but haven't tried doing this where I want to loop through multiple DB queries and have it create a graph for each iteration.
The problem I am having is that I am doing something incorrectly with the generation of the graphs because I only get the output of that first one. I actually half expected that I would get the output of the last host graph, due to it overwriting all of the others when generating the graph, but that doesn't seem to be the case. The output is a graph showing my mem,cpu and load usage for host1.
The suggestion to make $graph into an array is a good idea and I will try that. My other thought was somehow getting the output for each member of servers array to be placed in a separate image file.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
So are all four of these supposed to put lines on the same graph?
And if so, are they lines of different color?
I don't think I'm following this correctly.
So are all four of these supposed to put lines on the same graph?
And if so, are they lines of different color?
I don't think I'm following this correctly.
There are supposed to be four separate graphs.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS