Jump to content

Archived

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

scallywag05

Does anyone know how to draw graphs using PHP?

Recommended Posts

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

Share this post


Link to post
Share on other sites
There's a baaChart link in my sig.

Also JPGraph in the tutorials on this site

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Here's a sample. You need GD graphics.

Save as "mygraph.php"
[code]<?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);
?>[/code]

Similar using baaChart. Save as "mygraph2.php"
(Needs baachart.php downloaded)
[code]<?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();
?>[/code]

To produce the graphs and enter data, Save and run this form "sample.php"
[code]<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>

[/code]

HTH

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.