help with php and line graphs

Discussion in 'Web Design and Development' started by twoodcc, Jan 13, 2009.

  1. twoodcc macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #1
    so i am having trouble making a line graph in php from data in a mysql database. can anyone help me out with this?

    i can create an image with a line on it, but when i try to pull the data from the database, it only gives me the first row in the database
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
  3. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #3
    here is my code. there is more than that, but i wanted to show the main part of the image and database part. i didn't put the database connection on here, but it is in the code and works.

    Code:
    $result = mysql_query("SELECT elevation FROM lakedata");
    	if (!$result) die('Could not query:' . mysql_error());
    	
    	//$array;
    	$i=0;
    	
    	while($row = mysql_fetch_array($result))
    	{
    		$array[] = $row[$i];
    		$i++;	
    	}
    	echo $array[1];
    
    	// set up image
    	$height = 800;
    	$width = 1000;
    	$im = imagecreatetruecolor($width, $height);
    	$white = imagecolorallocate ($im, 255, 255, 255);
    	$blue = imagecolorallocate ($im, 0, 0, 255);
    	$red = imagecolorallocate ($im, 64, 0, 0);
    	
    	// draw on image
    	imagefill($im, 0, 0, $blue);
    	// y-axis
    	imageline($im, 100, 0, 100, $height-100, $white);
    	// x-axis
    	imageline($im, 100, $height-100, $width, $height-100, $white);
    	
    	imageline($im, 125, $array[0]*.5, 325, $array[0]*.5, $white);
    	imageline($im, 325, $array[0]*.5, 325, $array[0]*.5, $red);
    	imagestring($im, 4, 50, 150, 'Sales', $white);
    	
    	// output image
    	Header ('Content-type: image/png');
    	imagepng ($im);
    	
    	//clean up
    	imagedestroy($im);
    	
    	mysql_close($con);
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    Try this, unless you're expecting an array for each record. The $i wasn't needed. The while loop is already taking care of that.
    PHP:
        while($row mysql_fetch_array($result))
        {
            
    $array[] = $row;
        }
    Edit: $i is needed, just not the $i++
     
  5. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #5
    well, i get this error now: Fatal error: Unsupported operand types

    i get this from this line:

    Code:
    imageline($im, 125, $array[0]*.5, 325, $array[0]*.5, $white);
    what is the best way to call a number from the array?
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    That's my bad. The $i was fine, you simply didn't need to $i++ at the end of the loop.

    PHP:
    while($row mysql_fetch_array($result))
        {
            
    $array[] = $row[$i];
        }
    Though you may be better off accessing $row['elevation'] rather then $i.
     
  7. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #7
    thanks. but it still doesn't work right. the first imageline works:

    Code:
    imageline($im, 125, $array[0]*.5, 325, $array[1]*.5, $white);
    but this one doesn't:

    Code:
    imageline($im, 325, $array[1]*.5, 325, $array[2]*.5, $white);
    should i call $row['elevation']? basically, elevation is a column in a database. i want to plot each data point in that column on the image. draw a line from one point to the next. a line graph.

    but i have to draw it to scale.
     
  8. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #8
    Right, using $row['elevation'] would access that column element. It's just a more straight forward way of accessing the right array index since sometimes your query results in multiple columns. Have tried printing out the contents of $array to make sure it's getting the values you believe are there? You could do a print statement in the while loop as it's going through the results. That would be the easiest nest step in troubleshooting.
     
  9. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #9
    ok, when i print out the objects in the array like this:

    Code:
    echo $array[$i];
    it prints all the data correctly. that is in the while loop.

    but when i get outside the while loop, the data is gone except $array[0], but $array[1] has nothing.

    any idea on what i'm doing wrong?
     
  10. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #10
    In the code you presented before you have
    PHP:
    //$array
    which is commented out. If that variable is not defined elsewhere then the issue here is a scoping issuing, meaning that $array only last as long as that while loop since that's where it was defined. I saw that commented line before and meant to ask about it, but forgot until I went back to look at the code more.
     
  11. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #11
    thanks. okay, i think it works now.

    but i need to scale the numbers and then draw the lines. right now it's just a straight line on the screen. how can i scale the numbers to fit on the image correctly?
     
  12. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #12
    Glad that got at the source of the problem. Your new question, I don't really know what you're working with to give much advice. It'll likely be math related and I don't know your skills there. Below is a basic example.

    image = 800 x 600 (pixels)
    datapoint = 50 (max 100) x 10 (max 100)

    Place datapoint at: (50/100)*800 x (10/100)*600
     
  13. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #13
    thanks! i feel like i'm getting somewhere!

    my image is height = 800, width = 1000, but i drew a line for x-axis and y-axis, so we're only using 700 and 900, instead of 800 and 1000.

    can i use max and min in php?
     
  14. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #14
    How are you wanting to use min and max? I don't know what your elevation data looks like (as in what data ranges exist) and how you want to place it on the image. Also not sure what the purpose is of placing the lines and whatnot on the image.
     
  15. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #15
    basically i want to graph the elevation as it changes. it doesn't change much, less than a foot. the range is like 1063.39 - 1065.83 for right now.
     
  16. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #16
    Here's some pseudo code to work from. Should be pretty easy to follow.
    Code:
    $imgX = 900
    $imgY = 700
    $min = 1060
    $max = 1066
    $range = $max - $min
    
    $array(1060.5, 1061.1, 1061.8, 1063.0, 1064.9)
    $len = count($array)
    
    $i = 1
    loop ($i) {
      // Coord is an x by y value in pixels
      $Coord = (($imgX / $len * $i), (($array[0] - $min) / $range * $imgX))
      $i++
    }
     
  17. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #17
    thanks! i'll see what i can do with it
     

Share This Page