Click to See Complete Forum and Search --> : 2D Pie Chart source code!!!


Anon
04-01-2001, 07:04 PM
2D Pie Chart source code, please feel free to sent me comments, thanks!
<?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 );
?>

Anon
04-01-2001, 07:06 PM
For output go to http://oberon.spaceports.com/~music4u/test1.png

Anon
04-17-2001, 12:14 PM
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
05-02-2001, 03:10 PM
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
05-03-2001, 09:58 AM
This script should work perfectly if you install GD library.
Check out more about GD library la.
good luck!

Anon
05-07-2001, 07:45 AM
Dear Alan:

Can you give the ways for installing GD library.

Thanks for your cooperation.

JL.

Anon
05-07-2001, 01:40 PM
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
05-07-2001, 01:41 PM
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
02-20-2002, 07:24 PM
I'm using Windows, how can i do that?

Anon
02-21-2002, 03:48 AM
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
02-21-2002, 03:02 PM
Change your setting in 'php.ini'.

jonki
06-11-2007, 01:57 PM
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;

echo '<IMG src="2d.php">';

Which works fine .... but if I do this;


$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

bradgrafelman
06-11-2007, 06:53 PM
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?).

jonki
06-11-2007, 07:27 PM
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;

$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;



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

bradgrafelman
06-11-2007, 07:31 PM
So what's an example of the URL that is outputted?

jonki
06-12-2007, 10:21 AM
This is the problem; as it is a <IMG src="xx" > I can't see it. If I go directly to 2d.php ofcourse the variables aren't passed and there is a division by zero.

I have tried passing the variables through a link in the same way and I still get the division by zeros caused by dataTotal


for( $i = 0; $i < count( $data ); $i++ )
{
$dataTotal += $data[ $i ];
}
//.....
for( $i = 0; $i < count( $data ); $i++ )
{
$startDegree = round( $degree );
$degree += ( $data[ $i ] / $dataTotal ) * 360;
$endDegree = round( $degree );
// ........ etc


And then a Headers error saying that something has already been sent.

jonki
06-12-2007, 10:31 AM
I think the problem is caused when creating the array;

Is it possible to simply put;

$data = array( $d1, $d2, $d3, $d4, $d5, $d6, $d7, $d8, $d9 ,$d10 , $d11 , $d12 );

?

bradgrafelman
06-12-2007, 02:19 PM
Is it possible to simply put;

$data = array( $d1, $d2, $d3, $d4, $d5, $d6, $d7, $d8, $d9 ,$d10 , $d11 , $d12 );

?Yes, of course it is, but then you're back to the same problem as before - how to pass an array in the URL.

This is the problem; as it is a <IMG src="xx" > I can't see it.As I asked before - can you post the URL that is outputted in the HTML?

With as many variables as you're trying to pass from script to script, it might be easier to store the data in session.

jonki
06-12-2007, 02:54 PM
Done it!!

Due to some problems I had, I echoed out the variables so that I could check that they were being sent, and this caused the problem with the header, as information had already been sent. (This is way over my head as to why this causes problems but at least getting rid of this cured it)

Many many thanks for all your help.

Weedpacket
06-12-2007, 05:50 PM
I echoed out the variables so that I could check that they were being sent, and this caused the problem with the header, as information had already been sent.Headers are sent before anything else; if you send anything else, the headers get sent first, then the "anything else" is sent. Then you can't send any more headers.

This is way over my head as to why this causes problems but at least getting rid of this cured itBecause, based on the content-type header that it received, the browser is expecting image data in the response. Having other stuff in the response (like echoed variables or error messages) will corrupt that data. Hence, a broken image.

scrupul0us
06-12-2007, 09:05 PM
2 questions:

1) how can i add a 1px border around the chart?
2) how can i add a 1px border around each slice?

1 comment:

if you have two variables... and one is 100 and the other is 0, you still get a mini slice in the pie... shouldn't it not show the slice at all

1 code fix:

i had to declare
$dataTotal ="";

before it was called in the for loop

1 suggestion:

try to incorporate anti-aliasing: http://icewind.ic.funpic.de/ellipse.php