Jump to content

Showing a function graph


YigalB

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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' />

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.