SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Object class to string

    Code:
    $i=0;
    foreach($params as $p){
    $lineplot.$i = new LinePlot($values[$p]);
    $graph->Add($lineplot.$i);
    }
    I am getting an error as
    Object of class LinePlot could not be converted to string

    Based on the values in the array $param,
    I have to create a class.

    Suppose if $param has two values like cpu,mem

    $lineplot1=new LinePlot($ydata1);

    $lineplot2=new LinePlot($ydata2);

    How can i do this in a loop based on the values of $param array

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Surely you don't need the intermediate variable, you only seem to be using it to hold the LinePlot instance then pass it to the graph object.

    You may as well just pass the LinePlot instance directly.

    PHP Code:
    <?php
    foreach($params as $p){
    $graph->Add(new LinePlot($values[$p]));
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it doesnot work. I have to plot many number of line graphs in a single graph.

    This gives an error the values for y-axis is not taken.

    Any other alternative way to create a instance as the number of values on array increases.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What library are you using to create the graphs? Without knowing how it works, I'm going to be quite limited.

    Could you provide a sample that works without the loop?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    What library are you using to create the graphs? Without knowing how it works, I'm going to be quite limited.

    Could you provide a sample that works without the loop?
    I am using Jpgraphs.

    Code:
    include ("/Path_wherelibrary_is_installed/jpgraph.php");
    include ("/Path_wherelibrary_is_installed/jpgraph_line.php");
    $ydata1 = array(11,3,8,12,5,1,9,13,5,7,0);
    $ydata2 = array(1,19,15,7,22,14,5,9,0);
    $ydata3 = array(10,9,5,7,2,4,5,);
    $graph = new Graph(300,200,"auto");
    $graph->SetScale("textlin");
    $lineplot1 = new LinePlot($ydata1);
    
    $lineplot2=new LinePlot($ydata2);
    $lineplot3=new LinePlot($ydata3);
    
    $graph->Add($lineplot1);
    $graph->Add($lineplot2);
    $graph->Add($lineplot3);
    
    $graph->img->SetMargin(40,20,20,40);
    $graph->title->Set("Example 4");
    $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);
    
    $lineplot1->SetColor("blue");
    $lineplot1->SetWeight(2);
    $graph->yaxis->SetColor("red");
    $graph->yaxis->SetWeight(2);
    $graph->SetShadow();
    
    // Display the graph
    $graph->Stroke();
    This is the code without using loop

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    LinePlot expects to be given an array, not an item out of an array.

    $lineplot3=new LinePlot($ydata3);

    Is $params a multidimensional array?

    If so you should probably be doing this:
    PHP Code:
    foreach($params as $p){
    $graph->Add( new LinePlot$p ) );



Bookmarks

Posting Permissions

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