Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Adding a graph

  • 28-09-2007 12:18PM
    #1
    Registered Users, Registered Users 2 Posts: 3,485 ✭✭✭


    Lads,
    Looking for a bit of direction here where I want to add a persons score to a bell curve, i.e. show graphically his score against a bell curve?
    Thanks
    Gary


Comments

  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    do you need to generate the original bell curve ?

    Is this just to add a point to a graph ?

    There are librarys that can do nice things for you ... usually commercial though ...


    otherwise doing it yourself ...
    http://www.php.net/manual/en/function.imagecreate.php


  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    from someones example on the page I linked to ...
    // A simple XY graph
    
    <html>
    <head>
           
     <title>XY Graph</title>
    
    <h2>Practice XY Graph</h2>
    </head>
    <body>
    
    <?php
    $left = 0;
    $top = 0;
    $x_size = 400;
    $y_size = 400;
    
    $char_width = 8;
    $char_height = 11;
    
    $x_start = $x_left + 100;
    $y_start = $top + $char_height * 1.5;
    $x_end = $x_start + $x_size;
    $y_end = $y_start + $y_size;
    $right = $x_start + $x_size + 40;
    $bottom = $y_start + $y_size + $char_height * 1.5;
    
    $graph_n = 100;
    for($i = 0; $i < $graph_n; $i++ )
        {
        $graph_x[$i] = $i;
        $graph_y[$i] = $i * $i;
        }
    
        $min_x = 9e99;
        $min_y = 9e99;
        $max_x = -9e99;
        $max_y = -9e99;
    
        $avg_y = 0.0;
       
        for($i = 0; $i < $graph_n; $i++ )
            {
            if( $graph_x[$i] < $min_x )
                $min_x = $graph_x[$i];
    
            if( $graph_x[$i] > $max_x )
                $max_x = $graph_x[$i];
    
            if( $graph_y[$i] < $min_y )
                $min_y = $graph_y[$i];
    
            if( $graph_y[$i] > $max_y )
                $max_y = $graph_y[$i];
    
            $avg_y += $graph_y[$i];
            }
       
        $avg_y = $avg_y / $graph_n;
    
        $min_x = 0;
        $min_y = 0;
        $max_x += $max_x * 0.05;
        $max_y += $max_y * 0.05;
    
    $image = ImageCreate($right - $left, $bottom - $top);
    $background_color = imagecolorallocate($image, 255, 255, 255);
    $text_color = imagecolorallocate($image, 233, 14, 91);
    
    $grey = ImageColorAllocate($image, 204, 204, 204);
    $white = imagecolorallocate($image, 255, 255, 255);
    $black = imagecolorallocate($image, 0, 0, 0);
    $red = imagecolorallocate($image, 255, 0, 0);
    
    imagerectangle($image, $left, $top, $right - 1, $bottom - 1, $black );
    imagerectangle($image, $x_start, $y_start, $x_end, $y_end, $grey );
    
    for($i = 0; $i < $graph_n; $i++ )
       {
       $pt_x = $x_start + ($x_end-$x_start)*($graph_x[$i]-$min_x)/($max_x-$min_x);
       $pt_y = $y_end - ($y_end - $y_start)*($graph_y[$i]-$min_y)/($max_y-$min_y);
    
     //  imagesetpixel( $image, $pt_x, $pt_y, $black );
       imagechar($image, 2, $pt_x - 3, $pt_y - 10, '.', $black);
       }
    
    $string = sprintf("%2.5f", $max_y);
    imagestring($image, 4, $x_start - strlen($string) * $char_width, $y_start - $char_width, $string, $black);
    
    $string = sprintf("%2.5f", $min_y);
    imagestring($image, 4, $x_start - strlen($string) * $char_width, $y_end - $char_height, $string, $black);
    
    $string = sprintf("%2.5f", $min_x);
    imagestring($image, 4, $x_start - (strlen($string) * $char_width)/2, $y_end, $string, $black);
       
    $string = sprintf("%2.5f", $max_x);
    imagestring($image, 4, $x_end - (strlen($string) * $char_width) / 2, $y_end, $string, $black);
    
    $x_title = 'x axis';
    $y_title = 'y axis';
    
    imagestring($image, 4, $x_start + ($x_end - $x_start) / 2 - strlen($x_title) * $char_width / 2, $y_end, $x_title, $black);
    
    imagestring($image, 4, $char_width, ($y_end - $y_start) / 2, $y_title, $black);
    
    header('Content-type: image/png');
    $filename = sprintf("%d.png", time());
    ImagePNG($image,$filename);
    ImageDestroy($image);
    
    printf("<img src='%s'> ", $filename);
    ?>
    
    </body>
    </html>
    


  • Registered Users, Registered Users 2 Posts: 3,485 ✭✭✭randombar


    I guess I'd like to have the point on/over a generic bell curve to start off?

    A Problem with the script above is you're writing to a folder (security issues?)


  • Registered Users, Registered Users 2 Posts: 6,680 ✭✭✭daymobrew


    forbairt wrote:
    There are libraries that can do nice things for you ... usually commercial though ...
    One of those in PHP/SWF Charts (it is free to download and use). Very slick and easy.


  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    daymobrew wrote:
    One of those in PHP/SWF Charts (it is free to download and use). Very slick and easy.

    Its a commercial license though unless its only for personal stuff ... its also a flash file so you can't save it to disk


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    GaryCocs wrote:
    A Problem with the script above is you're writing to a folder (security issues?)

    No security issues ... assuming you've got good security :D


  • Registered Users, Registered Users 2 Posts: 1,262 ✭✭✭di11on


    Wouldn't an easy enough way of doing this to be... find/generate an image for your generic bell curve.

    Then use this image as the backgorund image for a table.

    Then you just need to create 4 cells in your table and place your "point" image in the bottom right hand corner of the top left cell. Then it's just a matter of working out the height and width of the top left cell.

    Would this work?


Advertisement