Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  • #2
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    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.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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

  • #5
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Attached Files

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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.

  • #8
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •