#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    260
    Rep Power
    0

    Output dynamic variables for jpgraph


    Hi all, I'm trying to integrate jpGraph to create graphics from statistics I have. I've played around with the examples, works fine, but data on the file are static, I want to make them dynamic, from data on MySQL.

    So I tried numerous things inside the file and it doesn't work. What I'm trying to do now is to create a dynamic legend.

    Here how it works, for each element on the legend, I must declare this line;

    PHP Code:
    $lineplot->SetLegend("Field #1"); 
    this is only the first element, for each subsequent element, I must a an incremental number to $lineplot, like this;

    PHP Code:
    $lineplot2->SetLegend("Field #2"); 
    PHP Code:
    $lineplot3->SetLegend("Field #3"); 
    PHP Code:
    $lineplot4->SetLegend("Field #4"); 
    and so on. If I write them manually, it works, but like I said, I want them to be outputed dynamically, in a while loop, so if there's 7 rows, it will automatically print out this line from $lineplot to $lineplot7 (NOTE : $lineplot1 does not work, must be $lineplot)

    I tried this and it does not work;

    PHP Code:
    $countnum=1;

    while (
    $countnum 5)
    {
        
    $lineplot$countnum ->SetLegend("Field #$countnum");
        
        
    $countnum++;

    Someone has an idea on how I could make $lineplot a real variable WITH the number at the end, that would make $lineplot, $lineplot2, $lineplot3, etc?

    If need more explanation, feel free to ask!

    Thanks all
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    The substitution on the left will not happen as you are expecting. I am not familiar with jpgraph. How are you defining the lineplot object? Why can you not use an array for them?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    260
    Rep Power
    0
    Originally Posted by gw1500se
    The substitution on the left will not happen as you are expecting. I am not familiar with jpgraph. How are you defining the lineplot object? Why can you not use an array for them?
    I'm not familiar too, but I tried the array, and it does not work either.

    Just tried this and it doesn't work too;

    PHP Code:
    $countnum=1;

    while (
    $countnum 5)
    {
        
    $countnum++;
        ${
    'lineplot'.$countnum};
        
        
    $lineplot->SetLegend("Field #$countnum");

    it only gives me the last entry, but if I write this, it works, but it's not dynamic

    PHP Code:
    $lineplot->SetLegend("Field #1");
    $lineplot2->SetLegend("Field #2");
    $lineplot3->SetLegend("Field #3");
    $lineplot4->SetLegend("Field #4"); 
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    No, you need to do that where you initialize the lineplot objects. Where are the statements like:
    PHP Code:
    $lineplot1=<something>; 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    260
    Rep Power
    0
    Originally Posted by gw1500se
    No, you need to do that where you initialize the lineplot objects. Where are the statements like:
    PHP Code:
    $lineplot1=<something>; 
    It needs to be separated, the lineplot object needs to get one per line, like I stated;

    PHP Code:
    $lineplot->SetLegend("Field #1"); 
    $lineplot2->SetLegend("Field #2"); 
    $lineplot3->SetLegend("Field #3"); 
    $lineplot4->SetLegend("Field #4"); 
    Maybe I don't get what you mean...

    will try some other tests.

    Thanks
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    I think you need some OOP education to proceed. Those are object references. $lineplot1 contains a reference to some object which has a method called SetLegend. If $lineplot1 is not instantiated somehow it will produce an error. Where are they instantiated? There must be something like:
    PHP Code:
    $lineplot=new jpgraph(); 
    Or whatever instantiates that object.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    260
    Rep Power
    0
    Originally Posted by gw1500se
    I think you need some OOP education to proceed. Those are object references. $lineplot1 contains a reference to some object which has a method called SetLegend. If $lineplot1 is not instantiated somehow it will produce an error. Where are they instantiated? There must be something like:
    PHP Code:
    $lineplot=new jpgraph(); 
    Or whatever instantiates that object.
    No, I'm not used to this, this is the whole original page source code.

    PHP Code:
    <?php // content="text/plain; charset=utf-8"
    require_once ('jpgraph/jpgraph.php');
    require_once (
    'jpgraph/jpgraph_log.php');
    require_once (
    'jpgraph/jpgraph_line.php');

    $ydata = array(11,3,8,12,5,1,9,13,5,7);
    $y2data = array(354,200,265,99,111,91,198,225,293,251);
    $datax=array("Jan","Feb","Mar","Apr","Maj","Jun","Jul","Aug","Sep");

    // Create the graph. These two calls are always required
    $graph = new Graph(350,200);    
    $graph->img->SetMargin(40,110,20,40);
    $graph->SetScale("textlog");
    $graph->SetY2Scale("log");
    $graph->SetShadow();

    $graph->ygrid->Show(true,true);
    $graph->xgrid->Show(true,false);

    // Create the linear plot
    $lineplot=new LinePlot($ydata);
    $lineplot2=new LinePlot($y2data);

    $graph->yaxis->scale->ticks->SupressFirst();
    $graph->y2axis->scale->ticks->SupressFirst();
    // Add the plot to the graph
    $graph->Add($lineplot);
    $graph->AddY2($lineplot2);
    $lineplot2->SetColor("orange");
    $lineplot2->SetWeight(2);
    $graph->y2axis->SetColor("orange");

    $graph->title->Set("Example 10");
    $graph->xaxis->title->Set("X-title");
    $graph->yaxis->title->Set("Y-title");

    $graph->title->SetFont(FF_FONT1,FS_BOLD);
    $graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
    $graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);

    $lineplot->SetColor("blue");
    $lineplot->SetWeight(2);

    $lineplot2->SetColor("orange");
    $lineplot2->SetWeight(2);

    $graph->yaxis->SetColor("blue");

    $lineplot->SetLegend("Plot 1");
    $lineplot2->SetLegend("Plot 2");

    $graph->legend->Pos(0.05,0.5,"right","center");

    $graph->xaxis->SetTickLabels($datax);
    $graph->xaxis->SetTextTickInterval(2);

    // Display the graph
    $graph->Stroke();
    ?>
    This code actually works, but what I want to do is get my values from MySQL and insert them into this so I can get real time graphics.

    I tried to change those ones;

    // Create the linear plot
    $lineplot=new LinePlot($ydata);
    $lineplot2=new LinePlot($y2data);

    to create dynamic content like I stated, but I can't get it to work.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    This is what I was looking for. Set the array like this:
    PHP Code:
    $lineplot[0]=new LinePlot($ydata);
    $lineplot[1]=new LinePlot($y2data); 
    Then reference them like this:
    PHP Code:
    while ($countnum 5)
    {
        
    $lineplot[$countnum]->SetLegend("Field #$countnum");
        
    $countnum++;

    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo