Pie Chart or Spider Graph help PLEASE?!

images php coding issues or problems here.

Moderators: macek, egami, gesf

mtoynbee
New php-forum User
New php-forum User
Posts: 7
Joined: Mon Apr 18, 2005 6:16 am

Pie Chart or Spider Graph help PLEASE?!

Postby mtoynbee » Mon Apr 18, 2005 6:21 am

Hi all, I need to create a spider chart using GD Library and Im a bit stuck. I need to create a pie chart as a background grid to a spider-chart. Does anyone know how to create a circle separated in segments easily.

I've Googled this to infinity but no luck so far.

PS - I'm not able to use PHP class add-ons as the server adminstrators won't allow it at this stage to progression code ideas only please.

Thanks in advance

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Mon Apr 18, 2005 9:44 am

What do you mean "server administrators won't allow it"? As perhaps you have discovered, PEAR::Image_Graph is the best graphing package. You should have no trouble installing it. If your admins won't let you run the PEAR install app, you can just download the package and then upload it to your web space via FTP.

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Postby ruturajv » Mon Apr 18, 2005 7:31 pm

you can as well try out this...
JPGraph

mtoynbee
New php-forum User
New php-forum User
Posts: 7
Joined: Mon Apr 18, 2005 6:16 am

Postby mtoynbee » Mon Apr 18, 2005 11:18 pm

Hi there, without going into too much detail bout the company infrastructure - I can't use plugins, I can't install anything else on the server and I don't have access to an FTP port.

What I need is a section of code that will draw a simple circle with segments in good old PHP progressive code. Does anyone know where I can find such a thing as it doesn't seem to exist on the Internet?

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Tue Apr 19, 2005 6:05 am

If you don't have access to FTP (or SSH, or a network drive), how do you intend to get your PHP code onto the server in the first place.

Anyway, such a thing does exist on the internet, and it can be found at the links that I an ruturajv have provided to you. No "plugins" required, except for GD, which is an extension, but without GD you're flat out of luck.

mtoynbee
New php-forum User
New php-forum User
Posts: 7
Joined: Mon Apr 18, 2005 6:16 am

Postby mtoynbee » Tue Apr 19, 2005 9:07 am

I have access to the network drive but only parts of it. Due to time restraints I may not have time to get the IS company to install PEAR in time as we they get twitchy when installing stuff onto a intranet server that 8000 people need as part of their job, without a full test period.

Will look into JPGraph as I assume no installation is needed?

Failing that, does can anyone provide raw PHP code to draw a simple pie chart?

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Tue Apr 19, 2005 9:57 am

No installation is needed for PEAR packages. Using the installer is optional. You can just download the files and upload them to your server directly. It might take a little extra time futzing with paths, etc., but it'll save you a lot of trouble.

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Postby ruturajv » Tue Apr 19, 2005 7:31 pm

hey!
try the script that I wrote...

Code: Select all

<?php
global $deg;

function get_polar($xrel, $yrel, $ang, $radius) {
    $i = $ang;
    $ang = ($ang * pi())/ 180;
   
    $ix = abs($radius*cos($ang));
    $iy = abs($radius*sin($ang));
   
    if ($i>=0 && $i<=90) {
        $ix = $xrel + $ix;
        $iy = $yrel - $iy;
    }
    if ($i>90 && $i<=180) {
        $ix = $xrel - $ix;
        $iy = $yrel - $iy;
    }
    if ($i>180 && $i<=270) {
        $ix = $xrel - $ix;
        $iy = $yrel + $iy;
    }
    if ($i>270 && $i<=360) {
        $ix = $xrel + $ix;
        $iy = $yrel + $iy;
    }

    $ix = floor($ix);
    $iy = floor($iy);
    //echo ($ix . " $iy<br>");
    $returnvals = array (
                        'x1' => $xrel,
                        'y1' => $yrel,
                        'x2' => $ix,
                        'y2' => $iy
                    );
    return $returnvals;
}

function get_degtotal($degindex)
{
    global $deg;
    if ($degindex == 0 ) {
       return (  $deg[$degindex] );
    }
    else {       
        return ( $deg[$degindex] + get_degtotal($degindex-1) );
    }   
}


$im  = imagecreate (400, 400);
$w   = imagecolorallocate ($im, 255, 255, 255);
$black   = imagecolorallocate ($im, 0, 0, 0);
$red = imagecolorallocate ($im, 255, 0, 0);
$green = imagecolorallocate ($im, 0, 180, 0);

$randcolor[0] = imagecolorallocate($im, 243, 54, 163);
$randcolor[1] = imagecolorallocate($im, 179, 51, 247);
$randcolor[2] = imagecolorallocate($im, 103, 48, 250);
$randcolor[3] = imagecolorallocate($im, 53, 145, 244);
$randcolor[4] = imagecolorallocate($im, 54, 243, 243);
$randcolor[5] = imagecolorallocate($im, 107, 245, 180);
$randcolor[6] = imagecolorallocate($im, 203, 242, 111);
$randcolor[7] = imagecolorallocate($im, 248, 201, 105);

$data[0] = 30;
$data[1] = 20;
$data[2] = 15;
$data[3] = 10;
$data[4] = 8;
$data[5] = 7;
$data[6] = 5;
$data[7] = 5;

$datasum = array_sum($data);

$deg[0] = number_format((30 / $datasum * 360), 2, ".", "");
$deg[1] = number_format((20 / $datasum * 360), 2, ".", "");
$deg[2] = number_format((15 / $datasum * 360), 2, ".", "");
$deg[3] = number_format((10 / $datasum * 360), 2, ".", "");
$deg[4] = number_format((8 / $datasum * 360), 2, ".", "");
$deg[5] = number_format((7 / $datasum * 360), 2, ".", "");
$deg[6] = number_format((5 / $datasum * 360), 2, ".", "");
$deg[7] = number_format((5 / $datasum * 360), 2, ".", "");
echo ('<pre>');

//print_r($deg);

$datadeg = array();
$datapol = array();
$degbetween = array();
$databetweenpol = array();

for ($i=0; $i < count($deg) ; $i++) {
    $datadeg[$i] = get_degtotal($i);
    $datapol[$i] = get_polar(200, 200, $datadeg[$i], 100);
}

for ($i=0; $i < count($datadeg) ; $i++) {
    /*this is a trick where you take 2deg angle before
    and get the smaller radius so that you can have a pt to
    `imagefill` the chartboundary
    */
    $degbetween[$i] = ($datadeg[$i]-2);
    $databetweenpol[$i] = get_polar(200, 200, $degbetween[$i], 50);
}

print_r($datadeg);
print_r($degbetween);
print_r($databetweenpol);
//exit;

for ($i=0; $i<count($deg); $i++) {
    imageline ($im, 200, 200, $datapol[$i]['x2'], $datapol[$i]['y2'], $black);
}
imagearc($im, 200, 200, 200, 200, 0, 360, $black);

for ($i=0; $i<count($deg); $i++) {
    imagefill ($im, $databetweenpol[$i]['x2'], $databetweenpol[$i]['y2'], $randcolor[$i]);

}

//header ("Content-type: image/png");
imagepng($im, 'piechart.png');
?>
<img src='piechart.png'>

mtoynbee
New php-forum User
New php-forum User
Posts: 7
Joined: Mon Apr 18, 2005 6:16 am

AT LAST!

Postby mtoynbee » Wed Apr 20, 2005 11:29 am

At last someone who actually does proper coding! Your the man, will check this out tomorrow at work. Looks great though!

Good to see us logical PHP programmers aren't dying out!


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest