To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 04-01-2001, 07:04 PM   #1
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
2D Pie Chart source code!!!

2D Pie Chart source code, please feel free to sent me comments, thanks!
PHP Code:
<?php
    
/********************************
      2D Pie Chart Version 1.0
      Programer: Xiao Bin Zhao
      Date: 03/31/2001
      All Rights Reserved 2001.
    ********************************/
    
    /*************Configuration Starts Here******************/
    
$chartTitle = "Most Popular Stars";            //pie chart name
    
$logo = "www.asianstar.com";                   //logo
    /*************************End****************************/
    
    /*****************For Programers Only********************/
    
$imageWidth = 300;                             //image width
    
$imageHeight = 200;                            //image height
    
$diameter = 150;                               //pie diameter
    
$centerX = 100;                                //pie center pixels x
    
$centerY = 100;                                //pie center pixels y
    
$labelWidth = 10;                              //label width, no need to change
    /*************************End****************************/
    
    
$data = array( 156, 20, 80, 100, 124, 56, 34, 32, 67 );
    
$item = array( "Vivian", "Alan", "Valen", "Chow", "Chen", "COCO", "SiSi", "Lee", "Zhao" );
    for(
$i = 0; $i < count( $data ); $i++ )
    {
        
$dataTotal += $data[ $i ];
    }
    
    function
circlePoint( $deg, $dia )
    {
        
$x = cos( deg2rad( $deg ) ) * ( $dia / 2 );
        
$y = sin( deg2rad( $deg ) ) * ( $dia / 2 );    
        return array(
$x, $y );
    }
    
    
$im = ImageCreate( $imageWidth, $imageHeight );
    
    
$color[] = ImageColorAllocate( $im, 255, 0, 0 ); //red
    
$color[] = ImageColorAllocate( $im, 255, 204, 0 );//yellow
    
$color[] = ImageColorAllocate( $im, 153, 204, 0 );//green
    
$color[] = ImageColorAllocate( $im, 153, 51, 255 );//purple
    
$color[] = ImageColorAllocate( $im, 0, 128, 255 );//blue
    
$color[] = ImageColorAllocate( $im, 255, 0, 128 );//pink
    
$color[] = ImageColorAllocate( $im, 153, 51, 255 );//purple
    
$color[] = ImageColorAllocate( $im, 192, 192, 192 );//grey
    
$color[] = ImageColorAllocate( $im, 204, 204, 0 );
    
$color[] = ImageColorAllocate( $im, 64, 128, 128 );
    
$color[] = ImageColorAllocate( $im, 204, 102, 153 );
    
$white = ImageColorAllocate( $im, 255, 255, 255 );
    
$black = ImageColorAllocate( $im, 0, 0, 0 );
    
$grey = ImageColorAllocate( $im, 215, 215, 215 );

    
ImageFill( $im, 0, 0, $white );
    
    
$degree = 0;
    for(
$i = 0; $i < count( $data ); $i++ )
    {
        
$startDegree = round( $degree );
        
$degree += ( $data[ $i ] / $dataTotal ) * 360;
        
$endDegree = round( $degree );
        
        
$currentColor = $color[ $i % ( count( $color ) ) ];
        
        
ImageArc( $im, $centerX, $centerY, $diameter, $diameter, $startDegree, $endDegree, $currentColor );
        
        list(
$arcX, $arcY ) = circlePoint( $startDegree, $diameter );
        
ImageLine( $im, $centerX, $centerY, floor( $centerX + $arcX ), floor( $centerY + $arcY ), $currentColor );
        
        list(
$arcX, $arcY ) = circlePoint( $endDegree, $diameter );
        
ImageLine( $im, $centerX, $centerY, ceil( $centerX + $arcX ), ceil( $centerY + $arcY ), $currentColor );
        
        
$midPoint = round( ( ( $endDegree - $startDegree ) / 2 ) + $startDegree );
        list(
$arcX, $arcY ) = circlePoint( $midPoint, $diameter / 1.5 );
        
ImageFillToBorder( $im, floor( $centerX + $arcX ), floor( $centerY + $arcY ), $currentColor, $currentColor );
        
ImageString( $im, 2, floor( $centerX + $arcX ), floor( $centerY + $arcY ), intval( round( $data[ $i ] / $dataTotal * 100 ) ) . "%", $black );
    }
    
    
$labelX = $centerX + $diameter / 2 + 10;
    
$labelY = $centerY - $diameter / 4;
    
$titleX = $labelX - $diameter / 4;
    
$titleY = $centerY - $diameter / 2;
    
ImageString( $im, 3, $titleX + 1, $titleY + 1, $chartTitle, $grey );
    
ImageString( $im, 3, $titleX, $titleY, $chartTitle, $black );
    
ImageString( $im, 1, $labelX, $titleY + 14, date( "Y-m-d H:i:sa" ), $black );
    
    for(
$i = 0; $i < count( $item ); $i++ )
    {
        
$currentColor = $color[ $i % ( count( $color ) ) ];
        
ImageRectangle( $im, $labelX, $labelY, $labelX + $labelWidth, $labelY + $labelWidth, $black );
        
ImageFilledRectangle( $im, $labelX + 1, $labelY + 1, $labelX + $labelWidth, $labelY + $labelWidth, $currentColor );
        
ImageString( $im, 2, $labelX + $labelWidth + 5, $labelY, $item[ $i ], $black );
        
ImageString( $im, 2, $labelX + $labelWidth + 60, $labelY, $data[ $i ], $black );
        
$labelY += $labelWidth + 2;
    }
    
ImageString( $im, 3, $labelX, $labelY, "Total:", $black );
    
ImageString( $im, 3, $labelX + $labelWidth + 60, $labelY, $dataTotal, $black );
    
ImageString( $im, 2, $labelX, $labelY + 15, $logo, $black );
    
Header( "Content-type: image/PNG" );
    
ImagePNG( $im );
    
ImageDestroy( $im );
?>

Last edited by bradgrafelman; 06-11-2007 at 06:51 PM. Reason: added bbcode tags
Anon is offline   Reply With Quote
Old 04-01-2001, 07:06 PM   #2
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: 2D Pie Chart source code!!!

For output go to http://oberon.spaceports.com/~music4u/test1.png
Anon is offline   Reply With Quote
Old 04-17-2001, 12:14 PM   #3
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: 2D Pie Chart source code!!!

Thankyou Alan! Your code for pie graph generation is great!
Only today i tested it, sorry for my later response.
Thanks tnx tnx again!
Anon is offline   Reply With Quote
Old 05-02-2001, 03:10 PM   #4
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: 2D Pie Chart source code!!!

Sir Alan:

I´m trying to use your code pie graph but it generates the next error:

Fatal error: Call to undefined function: imagecreate() in /mnt/clientes/sancho/html/postobon/graficar.php on line 38

Can you help me ?

Best Regards.



Anon is offline   Reply With Quote
Old 05-03-2001, 09:58 AM   #5
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: 2D Pie Chart source code!!!

This script should work perfectly if you install GD library.
Check out more about GD library la.
good luck!
Anon is offline   Reply With Quote
Old 05-07-2001, 07:45 AM   #6
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: 2D Pie Chart source code!!!

Dear Alan:

Can you give the ways for installing GD library.

Thanks for your cooperation.

JL.

Anon is offline   Reply With Quote
Old 05-07-2001, 01:40 PM   #7
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: 2D Pie Chart source code!!!

For instruction installing GD library on Linux( I assumed you using Linux )
please go to:
http://www.e-gineer.com/instructions/install-gd13-for-php-with-apache-on-linux.phtml

If you are not using Linux but Win e-mail me again.

good luck@!
Anon is offline   Reply With Quote
Old 05-07-2001, 01:41 PM   #8
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: 2D Pie Chart source code!!!

For instruction installing GD library on Linux( I assumed you using Linux )
please go to:
http://www.e-gineer.com/instructions/install-gd13-for-php-with-apache-on-linux.phtml

If you are not using Linux but Win e-mail me again.

good luck@!
Anon is offline   Reply With Quote
Old 02-20-2002, 07:24 PM   #9
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: 2D Pie Chart source code!!!

I'm using Windows, how can i do that?
Anon is offline   Reply With Quote
Old 02-21-2002, 03:48 AM   #10
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: 2D Pie Chart source code!!!

Are you using windows? What's the problem?
I used that snippet on a windows php installation and i got no problem.
Anyway,i found this too: http://www.aditus.nu/jpgraph/index.php
Take a look, may be can help you.
Paolo Dina
Anon is offline   Reply With Quote
Old 02-21-2002, 03:02 PM   #11
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: 2D Pie Chart source code!!!

Change your setting in 'php.ini'.
Anon is offline   Reply With Quote
Old 06-11-2007, 01:57 PM   #12
jonki
Senior Member
 
Join Date: Jan 2004
Location: Cabo Blanco
Posts: 193
trouble passing variables

I love your 2D Pie Chart but I have having some trouble when I pass variables across from another page.

Basically I want to display many Pie Charts together on the same page and I display them like this;

PHP Code:
 echo '<IMG src="2d.php">';
Which works fine .... but if I do this;

PHP Code:
$data = array( 200, 43, 34, 232, 234, 23, 434, 43, 344 );
$item = array( "BMW", "MERCEDEZ", "VOLKSWAGEN", "OPEL", "FORD", "TOYOTA", "AUDI", "SEAT", "Otros" );

echo
'<IMG src="2d.php?data=$data&item=$item">';
It doesnt work

Any Ideas?

Many thanks
jonki is offline   Reply With Quote
Old 06-11-2007, 06:53 PM   #13
bradgrafelman
Pna lbh ernq guvf?
 
Join Date: Jul 2004
Location: 40.566N -89.731W, ~469ft above sea level
Posts: 11,488
You can't simply output an array inside of a string. You'll either have to serialize() it, implode() it, or transfer it to the second PHP script by some other means (e.g. a session).

Dont forget that URI's are limited to a certain number of characters (255 or something?).
__________________
***If your problem has been solved, PLEASE click the RESOLVED LINK under "Thread Tools"***

"Well Bones, do the new medical facilities meet with your approval?" -- Kirk
"They do not. It's like working in a damn computer center" -- McCoy (Star Trek: TMP)

Useful links: Debugging 101 || NJOE || (Sig image) || Rolla Engineered Solutions, LLC
bradgrafelman is offline   Reply With Quote
Old 06-11-2007, 07:27 PM   #14
jonki
Senior Member
 
Join Date: Jan 2004
Location: Cabo Blanco
Posts: 193
Thanks a lot for your help. I noticed the mistake you mentioned but I have tried imploding and exploding, I have even tried passing the variables individually but I just cant seem to get it to work.

Here is my latest version; if anyone can spot a silly mistake then I will be very grateful;

PHP Code:
 $d1="BMW";            $i1=540;
$d2="Mercedes";        $i2=340;
$d3="Toyota";        $i3=200;
$d4="Audi";            $i4=150;
$d5="Opel";            $i5=78;
$d6="SEAT";            $i6=50;
$d7="Ford";            $i7=50;
$d8="Jaguar";        $i8=45;
$d9="Skoda";        $i9=30;
$d10="Dacia";        $i10=30;
$d11="Ferrari";        $i11=20;
$d12="Otros";    $i12=100;
    


echo
'<IMG src="2d.php?d1=.$d1.&d2=.$d2.&d3=.$d3.&d4=.$d4.&d5=.$d5.&d6=.$d6.&d7=.$d7.&d8=.$d8.&d9=.$d9.&d10=.$d10.&d11=.$d11.&12=.$d12.
                    &i1='
.$i1.'&i2='.$i2.'&i3='.$i3.'&i4='.$i4.'&i5='.$i5.'&i6='.$i6.'&i7='.$i7.'&i8='.$i8.'&i9='.$i9.'&i10='.$i10.'&i11='.$i11.'&i12='.$i12.'">';
And the only difference from the script written above in 2c.php is;

PHP Code:

//$d1 = $_GET['d1']; $d2 = $_GET['d2']; $d3 = $_GET['d3']; $d4 = $_GET['d4']; $d5 = $_GET['d5']; $d6 = $_GET['d6']; $d7 = $_GET['d7']; $d8 = $_GET['d8']; $d9 = $_GET['d9']; $d10 = $_GET['d10']; $d11 = $_GET['d11']; $d12 = $_GET['d12'];
//$i1 =  $_GET['i1']; $i2 =  $_GET['i2']; $i3 =  $_GET['i3']; $i4 =  $_GET['i4']; $i5 =  $_GET['i5']; $i6 =  $_GET['i6']; $i7 =  $_GET['i7']; $i8 =  $_GET['i8']; $i9 =  $_GET['i9']; $i10 =  $_GET['i10']; $i11 =  $_GET['i11']; $i12 =  $_GET['i12'];



//$data = array( '.$d1.', '.$d2.', '.$d3.', '.$d4.', '.$d5.', '.$d6.', '.$d7.', '.$d8.', '.$d9.' , '.$d10.' , '.$d11.' , '.$d12.' );
//$item = array( '.$i1.', '.$i2.', '.$i3.', '.$i4.', '.$i5.', '.$i6.', '.$i7.', '.$i8.', '.$i9.' , '.$i10.' , '.$i11.' , '.$i12.' );

$data = array( $d1, $d2, $d3, $d4, $d5, $d6, $d7, $d8, $d9 ,$d10 , $d11 , $d12 );
$item = array( $i1, $i2, $i3, $i4, $i5, $i6, $i7, $i8, $i9 , $i10 , $i11 , $i12 );

As you can see, I have tried many combinations, not all of which are shown here.

If anyone can help me out I would be very grateful

Thanks
jonki is offline   Reply With Quote
Old 06-11-2007, 07:31 PM   #15
bradgrafelman
Pna lbh ernq guvf?
 
Join Date: Jul 2004
Location: 40.566N -89.731W, ~469ft above sea level
Posts: 11,488
So what's an example of the URL that is outputted?
__________________
***If your problem has been solved, PLEASE click the RESOLVED LINK under "Thread Tools"***

"Well Bones, do the new medical facilities meet with your approval?" -- Kirk
"They do not. It's like working in a damn computer center" -- McCoy (Star Trek: TMP)

Useful links: Debugging 101 || NJOE || (Sig image) || Rolla Engineered Solutions, LLC
bradgrafelman is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 08:12 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.