Jump to content


Photo

variable handling/management


  • Please log in to reply
3 replies to this topic

#1 harleydude

harleydude
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 17 October 2006 - 01:04 PM

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):
// Create the first line
$p1 = new LinePlot($datay1);
$p1->SetColor("navy");
$graph->Add($p1);

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:
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);
}
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
Success = 1% inspiration + 99% persperation!
Support PHP Freaks! TODAY!

#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 17 October 2006 - 01:35 PM

Try this

$p=$("p".$i);
to this
$p = "p".$i;

Ray

#3 harleydude

harleydude
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 17 October 2006 - 02:33 PM

That helps but the chart is blank...

I have an array ($dt) with the following data:
** > 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
        )
Here is what I have for each line of the graph:
$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);
}	

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?
Success = 1% inspiration + 99% persperation!
Support PHP Freaks! TODAY!

#4 harleydude

harleydude
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 17 October 2006 - 02:52 PM

Nevermind, I got it... the technique implied earlier works fine, there was an error in the code (DOH!!)

Thanks for the help!!
Success = 1% inspiration + 99% persperation!
Support PHP Freaks! TODAY!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users