Jump to content

Creating Graphs??


ali_2kool2002

Recommended Posts

Hi i am having a lot of problems trying to get software to create graphs using php/mysql

could anyone suggest somewhere where i can easily create simple bar graphs?

 

hope someone knows the answer, or even know how to code in php 2 enter a few details as a graph... :'( :'(

Link to comment
Share on other sites

For a simple bar

 

bar.php

<?php
// set dimensions
     $w = 102;
     $h = 12;
// create image
     $im = imagecreate($w, $h);
// set colours to be used
     $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
     $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
     $barcolor  = imagecolorallocate($im, 0xFF, 0x00, 0x00);
// draw border
     imagerectangle($im, 0,0,$w-1,$h-1,$black);
// get value and max value from query string
     $val = $_GET['val'];
     $max = $_GET['max'];
// calculate dimensions of inner bar
     $barw = $max ? floor(($w-2) * $val / $max) : 0;
     $barh = $h - 2;
// draw inner bar
 if ($barw)
     	imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);
// send image header
     header("content-type: image/png");
// send png image
     imagepng($im);
     imagedestroy($im);
?>

 

To display

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META http-equiv="content-language" content="en">
<META name="author" content="Barand">
<META name="generator" content="PHPEd 4.5">
<title>Bar sample</title>
</head>
<body>
<table width="300px" border='1'>
   <tr>
      <td>
         Item A
      </td>
      <td>
         20
      </td>
      <td>
         <img src='bar.php?val=20&max=50'>
      </td>
   </tr>
   <tr>
      <td>
         Item B
      </td>
      <td>
         40
      </td>
      <td>
         <img src='bar.php?val=40&max=50'>
      </td>
   </tr>
   <tr>
      <td>
         Item C
      </td>
      <td>
         30
      </td>
      <td>
         <img src='bar.php?val=30&max=50'>
      </td>
   </tr>
   <tr>
      <td>
         Item D
      </td>
      <td>
         50
      </td>
      <td>
         <img src='bar.php?val=50&max=50'>
      </td>
   </tr>
</table>

</body>
</html>

Link to comment
Share on other sites

Yes you do need GD

 

Check your php.ini file to see what your extension_dir is defined as

; Directory in which the loadable extensions (modules) reside.

extension_dir = "./"

and make sure php_gd2.dll ( or php_gd2.so if linux) is in there

 

Then find this line and remove the ";" from the beginning:

;extension=php_gd2.dll

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

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.