Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-08-2013, 04:42 PM   PM User | #1
rayTX
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
rayTX is an unknown quantity at this point
Help generating multiple graphs

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();

}

?>
rayTX is offline   Reply With Quote
Old 01-08-2013, 05:29 PM   PM User | #2
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
Without a full understanding of these:
PHP Code:
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'); 
Not many people will be able to help you.

Please post those as well.
TFlan is offline   Reply With Quote
Old 01-08-2013, 07:36 PM   PM User | #3
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 01-08-2013, 07:49 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 01-10-2013, 02:21 PM   PM User | #5
rayTX
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
rayTX is an unknown quantity at this point
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.
Attached Files
File Type: zip jpgraph.php.zip (46.8 KB, 6 views)
rayTX is offline   Reply With Quote
Old 01-10-2013, 03:49 PM   PM User | #6
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Try also running with the 2nd graph first:

$servers = array('host2','host3','host4');

This will help narrow down the cause..
__________________
"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
AndrewGSW is offline   Reply With Quote
Old 01-10-2013, 09:36 PM   PM User | #7
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 01-10-2013, 11:08 PM   PM User | #8
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
Originally Posted by mlseim View Post
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
AndrewGSW is offline   Reply With Quote
Old 01-11-2013, 01:42 AM   PM User | #9
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
So how do the four hosts in the array know which of the four graphs to use?
What is the purpose for looping? Why not run 4 separate scripts as one?
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:40 AM.


Advertisement
Log in to turn off these ads.