YigalB Posted August 4, 2012 Share Posted August 4, 2012 I would like to show a function graph embedded with other content on the web page. At first, it should be a linear line, such as y=aX+b. later I would like to extend it to parabola, sinus etc. Can someone recommend how to do it (hopefully with example), with ability to set start point, end point, the resolution of the function (every integer, every tenth of integer etc), and also how to draw the X & Y axis. Quote Link to comment https://forums.phpfreaks.com/topic/266672-showing-a-function-graph/ Share on other sites More sharing options...
cpd Posted August 4, 2012 Share Posted August 4, 2012 http://bit.ly/OPNfTi Quote Link to comment https://forums.phpfreaks.com/topic/266672-showing-a-function-graph/#findComment-1366755 Share on other sites More sharing options...
YigalB Posted August 4, 2012 Author Share Posted August 4, 2012 Thanks. I would appreciate a simple example I can use as a jump start. Quote Link to comment https://forums.phpfreaks.com/topic/266672-showing-a-function-graph/#findComment-1366758 Share on other sites More sharing options...
cpd Posted August 4, 2012 Share Posted August 4, 2012 Drawing a graph with PHP isn't "simple". As you can see from the first link on google you must do a lot of stuff to create a professional looking graph and that's using a pre-written library. The simplest example you'll get is someone using the GD library to draw a line on an image and that's it. I don't mean to be brutal but I doubt anybody is going to sit here and write code out for you to create a good looking graph, its a lot of time and effort. Quote Link to comment https://forums.phpfreaks.com/topic/266672-showing-a-function-graph/#findComment-1366767 Share on other sites More sharing options...
YigalB Posted August 4, 2012 Author Share Posted August 4, 2012 Thanks, I understand. I was sure there is a "ready to use" graph drawing function, but I guess I was wrong. So if i need to step back and learn, what would you recommend a newbie like me to do in order to reach the graph drawing? BTW - the target is educational. Quote Link to comment https://forums.phpfreaks.com/topic/266672-showing-a-function-graph/#findComment-1366772 Share on other sites More sharing options...
trq Posted August 4, 2012 Share Posted August 4, 2012 So if i need to step back and learn, what would you recommend a newbie like me to do in order to reach the graph drawing? Read some of the tutorials resulting from the linked to Google search. Quote Link to comment https://forums.phpfreaks.com/topic/266672-showing-a-function-graph/#findComment-1366774 Share on other sites More sharing options...
Barand Posted August 4, 2012 Share Posted August 4, 2012 Here's a rudimentary sample to graph y = Ax + B. When drawing charts you'll find most of the effort goes into things like axis scaling, axis labels, legends etc (so I missed these out). There is a baaChart link in my sig if you need more. <?php $im = imagecreate(500, 300); $bgd = imagecolorallocate($im, 0,0,0); $wht = imagecolorallocate($im, 0xff, 0xff, 0xff); $fgd = imagecolorallocate($im, 0xff, 0xff, 0); // scale $xscale = 40; // pix per unit - normally calculated depending on ranges required for x and y $yscale = 5; // origins and axes $ox = 50; $oy = 250; imageline($im, $ox, $oy, $ox, 50, $wht); imageline($im, $ox, $oy, 450, $oy, $wht); //you would label the axes too // graph $a = $_GET['a']; $b = $_GET['b']; $prevx = $prevy = null; for ($x=0; $x<=10; ++$x) { $y = $a * $x + $b; // y = Ax + b $xpos = $x*$xscale + $ox; $ypos = $oy - $y*$yscale; // y values increase from top to bottom, so compensate if (!is_null($prevx)) { imageline($im, $prevx, $prevy, $xpos, $ypos, $fgd); } $prevx = $xpos; $prevy = $ypos; } header("content-type: image/png"); imagepng($im); imagedestroy($im); ?> Save it as say, "mygraph.php" and display it on another page using an img tag EG <img src='mygraph.php?a=3&b=10' /> Quote Link to comment https://forums.phpfreaks.com/topic/266672-showing-a-function-graph/#findComment-1366848 Share on other sites More sharing options...
jcbones Posted August 4, 2012 Share Posted August 4, 2012 Or, you could do it with systems that are already available. Like Fusion Charts Free Quote Link to comment https://forums.phpfreaks.com/topic/266672-showing-a-function-graph/#findComment-1366857 Share on other sites More sharing options...
YigalB Posted August 5, 2012 Author Share Posted August 5, 2012 Or, you could do it with systems that are already available. Like Fusion Charts Free I think that Fusion can display graphs that are only on the positive edges of the X & Y. I didn't find a way to display the negative sides as well, and also to display the scaling. Quote Link to comment https://forums.phpfreaks.com/topic/266672-showing-a-function-graph/#findComment-1366990 Share on other sites More sharing options...
YigalB Posted August 5, 2012 Author Share Posted August 5, 2012 Here's a rudimentary sample to graph y = Ax + B. When drawing charts you'll find most of the effort goes into things like axis scaling, axis labels, legends etc (so I missed these out). There is a baaChart link in my sig if you need more. Save it as say, "mygraph.php" and display it on another page using an img tag EG Thanks! Looks like a nice start. I will try using it as a base example. Quote Link to comment https://forums.phpfreaks.com/topic/266672-showing-a-function-graph/#findComment-1366991 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.