Jump to content


Photo

Does anyone know how to draw graphs using PHP?


  • Please log in to reply
3 replies to this topic

#1 scallywag05

scallywag05
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 14 April 2006 - 03:55 PM

Im trying to pull data from my MySQL database that the user selects and want to display it in a graph for trend analysis. Does anyone know how using PHP I can do this?
Thanks

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 14 April 2006 - 04:06 PM

There's a baaChart link in my sig.

Also JPGraph in the tutorials on this site
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 scallywag05

scallywag05
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 14 April 2006 - 04:14 PM

What exactly is baaChart? im not really familiar with php. I have created a form which allows users to select 2 options and also months in which they want the options to be displayed. i want php to pull the data from the database and display it in a graph within a html page. i have installed jpGraph but dont really understand how to draw a graph and embed it within a html page at run time where each graph will be different depending on what the user selects? any help would be great.
thanks


#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 14 April 2006 - 08:39 PM

Here's a sample. You need GD graphics.

Save as "mygraph.php"
<?php
   // get sales and put in an array
   $sales = explode (',', $_GET['sales']);

   // chart area will be 200px wide and 100px high
   // centered in image (50px margins)

   $im = imagecreate(300, 200);

   $bg = imagecolorallocate($im, 0xFF, 0xFF, 0xFF); // white bg
   $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
   $grey = imagecolorallocate($im, 0xCC, 0xCC, 0xCC);
   $line = imagecolorallocate($im, 0x00, 0x00, 0xFF);

   // draw axes (origin at 50, 150)

   imageline($im, 50, 150, 50, 50, $black);
   imageline($im, 50, 150, 250, 150, $black);

   // draw grid
   for ($x=90; $x<=250; $x += 40) {
        imageline($im, $x, 150, $x, 50, $grey);
   }
   for ($y=50; $y<=130; $y += 20) {
        imageline($im, 50, $y, 250, $y, $grey);
   }

   // calculate cords
   $coords = array();
   foreach($sales as $month => $sval) {
           $x = 70 + $month * 40;
           $y = 150 - $sval;
           $coords[] = array($x, $y);
   }

   // plot
   $k = count ($coords);
   for ($i=1; $i<$k; $i++) {
        imageline($im, $coords[$i-1][0], $coords[$i-1][1], $coords[$i][0], $coords[$i][1], $line);
   }

   // output graph
   header("content-type: image/png");
   imagepng($im);
   imagedestroy($im);
?>

Similar using baaChart. Save as "mygraph2.php"
(Needs baachart.php downloaded)
<?php
include 'baachart.php';

// get sales 
   $sales = $_GET['sales'];

$g = new baaChart(300,200);
$g->setXAxis ('Month', 1);
$g->setYAxis ('Sales', 0, 100, 20, 0);
$g->setXLabels('1,2,3,4,5');
$g->addDataSeries('L', 5, $sales, '');
$g->drawGraph();
?>

To produce the graphs and enter data, Save and run this form "sample.php"
<HTML>
<HEAD>
<TITLE>Sample graph</TITLE>
</HEAD>
<BODY>

<?php
  if (isset($_POST['submit'])) {
      $sales = join (',', $_POST['sales']);
        // show the graph with sales values
      echo "<IMG src='mygraph.php?sales=$sales' width='300' height='200'>";
      echo '<br><br>';
      echo "<IMG src='mygraph2.php?sales=$sales' width='300' height='200'>";
  }

?>

<FORM METHOD='POST' ACTION='' name='form1'>
<h3>Enter monthly sales (0 - 100)</h3>
Month 1 <INPUT TYPE='TEXT'  name='sales[]' size='3' maxlength='3'><br>
Month 2 <INPUT TYPE='TEXT'  name='sales[]' size='3' maxlength='3'><br>
Month 3 <INPUT TYPE='TEXT'  name='sales[]' size='3' maxlength='3'><br>
Month 4 <INPUT TYPE='TEXT'  name='sales[]' size='3' maxlength='3'><br>
Month 5 <INPUT TYPE='TEXT'  name='sales[]' size='3' maxlength='3'><br>
<INPUT TYPE='SUBMIT'  name='submit' value='Submit'>
</FORM>

</BODY>
</HTML>


HTH
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users