Jump to content

Archived

This topic is now archived and is closed to further replies.

harleydude

variable handling/management

Recommended Posts

I am using JPGraph to create some basic line graphs. According to the spec's i have, there can be anywhere from 1 line to 10 lines on a graph at a time depending on the SQL criteria being passed.

Now, in JPGraph, you define a line like this (truncated for simplicity):
[code]
// Create the first line
$p1 = new LinePlot($datay1);
$p1->SetColor("navy");
$graph->Add($p1);
[/code]

Note that the line is a standard variable name ($p1). To add another line you just rinse and repeat this code. NOW, since the number of lines is arbitrary, is there a way to pass a variable variable to the JPGraph and replace the $p1 with a more usable scoped variable?

I was looking for something like:
[code]
for($i=0;$i<sizeof($chartarray);$i++){
  $p=$("p".$i);
  $$p = new LinePlot($chartarray[$i]); // get list stored in $i position
  $$p->SetColor("navy");
  $graph->Add($$p);
}
[/code]
I just read some on variable variables but am not sure of how they work and this bit odf code is off the top of my head and is not tested. I will play with this some but need to know if the concept works and best way to impliment it.

Thanks  in advance,

HD

Share this post


Link to post
Share on other sites
Try this

[code]$p=$("p".$i);[/code]
to this
[code]$p = "p".$i;[/code]

Ray

Share this post


Link to post
Share on other sites
That helps but the chart is blank...

I have an array ($dt) with the following data:
[code]
** > y dataArray
(
    [0] => Array
        (
            [0] => 1.00
            [1] => 1.00
            [2] => 1.00
            [3] => 1.00
            [4] => 1.00
        )

    [1] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 1.50
            [4] => 1.50
        )

    [2] => Array
        (
            [0] => 0
            [1] => 2.47
            [2] => 2.50
            [3] => 2.51
            [4] => 2.51
        )
[/code]
Here is what I have for each line of the graph:
[code]
$graph = new Graph(350,250,"auto");   
$graph->SetScale("textlin");
for($i=0;$i<sizeof($dt);$i++){
$p="p".$i;
$$p = new LinePlot($dt[$i]); // get list stored in $i position
$graph->Add($$p);
}
[/code]

image outputs empty indicating that jpgraph does not like the data.

I have also tried this by setting $i=0 and commenting out the for loop with no success either. JPGraph does not like the $p="p".$i; varaible name.

Can anyone offer help or ideas on how to fix the above code snippet?

Share this post


Link to post
Share on other sites
Nevermind, I got it... the technique implied earlier works fine, there was an error in the code (DOH!!)

Thanks for the help!!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.