Sign in to follow this  

[web] Free PHP graphing control - GD1 only

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm developing a website on which reports need to be generated with simple graphs on them - bar charts or the like. I've hacked together a very simplistic graphing control, but it doesn't do things like label values up the axes - and I have no idea how to do that. It would save on time if someone could point me to a decent free PHP file to do this - I have GD1 only and no access to the actual machine to update the version of PHP (4.1) running on it. The ability to export to PNG/JPEG would be ideal (as a file) so I could also link them together to the PDF class I'm using - though this is less of a consideration. Many thanks in advance [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by evolutional
phpDynagraph may help (GD 1.4) ... phpGD Bar Graph too...

I'm assuming by coding your own you mean hack together a chart using scaled gifs? [grin]


Why would I do that? [grin] No, it generates a PNG through PHP:

<script language="php">
# Graphing control for PHP.
# Output format: PNG (because PNG rocks).
# To use:
# Append settings to the URL.
# For example: to draw a graph with simple data of 1,2,3,4, size 640x480 do this:
#
#
# Settings:
# Name Type Comments
# width number width of image (320)
# height number height of image (200)
# border number padding around graph itself (32)
# data number list list of variables, comma separated
# labels number list list of labels for the data, comma separated
# colours hex list list of colours for each data element (list of colours)
# drawaxes boolean draw axes (true)
# backcolour hex graph background colour (FFFFFF)
# labelcolour hex colour for axes, data labels &c {000000)
#
# Types:
# number: just a number (eg: 120)
# number list: comma separated list (eg: 12,43,12)
# hex: 3-byte hex value (eg: 00FFCC)
# hex list: comma separated list of hex numbers (eg: CC9900,330099,99CC00)
# boolean: true or false boolean switch

# Set up the correct header:
header("Content-type: image/png");

# Get width and height details:

if (isset($_REQUEST['width'])) {
$width = $_REQUEST['width'];
} else {
$width = 320;
}

if (isset($_REQUEST['height'])) {
$height = $_REQUEST['height'];
} else {
$height = 200;
}

if (isset($_REQUEST['border'])) {
$border = $_REQUEST['border'];
} else {
$border = 32;
}

if (isset($_REQUEST['backcolour'])) {
$backcolour = $_REQUEST['backcolour'];
} else {
$backcolour = 'FFFFFF';
}

if (isset($_REQUEST['labelcolour'])) {
$labelcolour = $_REQUEST['labelcolour'];
} else {
$labelcolour = '000000';
}

if (isset($_REQUEST['gridcolour'])) {
$gridcolour = $_REQUEST['gridcolour'];
} else {
$gridcolour = '808080';
}

if (!isset($_REQUEST['drawaxes'])) {
$drawaxes = true;
} else {
if ($_REQUEST['drawaxes']=='true') {
$drawaxes = true;
} else {
$drawaxes = false;
}
}

if (isset($_REQUEST['colours'])) {
$colours = explode(',',$_REQUEST['colours'].',');
} else {
$colours = array
(
'000000',
'000080',
'0000FF',
'008000',
'00FF00',
'00FF80',
'00FFFF',
'800000',
'FF0000',
'FF0080',
'FF00FF',
'FF80FF'
);
}

$graph = ImageCreate($width,$height);

# Clear to back colour
ImageFill($graph,0,0,setColour($backcolour));

if (!isset($_REQUEST['data'])) {
imagestring($graph, 2, 0, 0, "No data specified", imagecolorallocate($graph,0,0,0));
imagepng($graph);
imagedestroy($graph);
die();
} else {
$data = explode(',',$_REQUEST['data'].',');
$datapoints = -1;
foreach ($data as $d) {
$datapoints++;
}
}



if (isset($_REQUEST['labels'])) {
$labels = explode(',',$_REQUEST['labels'].',');
}

if (!isset($_REQUEST['type'])) {
$type = "bar";
} else {
$type = $_REQUEST['type'];
}

if ($type=="bar") {

if ($drawaxes) {
imageline($graph,$border,$border,$border,$height-$border,setColour($labelcolour));
imageline($graph,$border,$height-$border,$width-$border,$height-$border,setColour($labelcolour));
}

$max = 0;
foreach($data as $val) {
if ($val>$max) {
$max = $val;
}
}

$colw = ($width-2*$border)/$datapoints;
$cpointer = 0;
for ($c=0; $c<$datapoints; $c++) {
$colh = $data[$c]/$max*($height-2*$border);
imagefilledrectangle ($graph, $c*$colw+$border, $height-$border-$colh , ($c+1)*$colw+$border-1, $height-$border,setColour($colours[$cpointer]));
$cpointer++;
if ($cpointer>=count($colours)-1) {
$cpointer=0;
}
}

if ($drawaxes) {
imageline($graph,$border,$border,$border,$height-$border,setColour($labelcolour));
imageline($graph,$border,$height-$border,$width-$border,$height-$border,setColour($labelcolour));
}
}

# Stream and destroy from memory
imagepng($graph);
imagedestroy($graph);

function setColour($colour) {
global $graph;
$r = hexdec(substr($colour,0,2));
$g = hexdec(substr($colour,2,2));
$b = hexdec(substr($colour,4,2));
return imagecolorallocate($graph,$r,$g,$b);
}

</script>



As you can tell; I haven't got all too far with it [sad]
Thanks; I'll look into the ones you mentioned!

Share this post


Link to post
Share on other sites

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this