scallywag05 Posted April 14, 2006 Share Posted April 14, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/7411-does-anyone-know-how-to-draw-graphs-using-php/ Share on other sites More sharing options...
Barand Posted April 14, 2006 Share Posted April 14, 2006 There's a baaChart link in my sig.Also JPGraph in the tutorials on this site Quote Link to comment https://forums.phpfreaks.com/topic/7411-does-anyone-know-how-to-draw-graphs-using-php/#findComment-26991 Share on other sites More sharing options...
scallywag05 Posted April 14, 2006 Author Share Posted April 14, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/7411-does-anyone-know-how-to-draw-graphs-using-php/#findComment-26993 Share on other sites More sharing options...
Barand Posted April 14, 2006 Share Posted April 14, 2006 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]<?phpinclude '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 Quote Link to comment https://forums.phpfreaks.com/topic/7411-does-anyone-know-how-to-draw-graphs-using-php/#findComment-27061 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.