473,491 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Drawing a pie chart

21 New Member
how to use pie chart?

plz give me suggestion?
Jul 9 '07 #1
5 1926
Rhishabh07
21 New Member
how to make pie chart using php?

plz suggest me.
Jul 9 '07 #2
pbmods
5,821 Recognized Expert Expert
Heya, Rhishabh07.

Did you mean how to create a pie chart in PHP?

Check out the PHP Pie Chart class.
Jul 9 '07 #3
Rhishabh07
21 New Member
hi pbmods

thanx at first i try then reply u back.
Jul 10 '07 #4
pbmods
5,821 Recognized Expert Expert
Heya, Rhishabh.

Sounds good. If you run into any trouble, post back anytime :)
Jul 10 '07 #5
Rhishabh07
21 New Member
thanx a lot.i did it.

here is the code:


<?php
/********************************
2D Pie Chart Version 1.0
Programer: Xiao Bin Zhao
E-mail: love1001_98@yahoo.com
Date: 03/31/2001
All Rights Reserved 2001.
********************************/

/*************Configuration Starts Here******************/

$chartTitle = "Percentage of Products in Database"; //pie chart name

/*************************End********************** ******/

/*****************For Programers Only********************/
$imageWidth = 600; //image width
$imageHeight = 400; //image height
$diameter = 250; //pie diameter
$centerX = 225; //pie center pixels x
$centerY = 225; //pie center pixels y
$labelWidth = 10; //label width, no need to change
/*************************End********************** ******/

//db connection info.
$dbuser = 'db';

$dbhost = 'localhost';

$dbpass = '';

$dbname = 'rhishabh';

$mysql_link = mysql_connect($dbhost,$dbuser,$dbpass) or die ("Could not connect"); //connect to db

mysql_select_db ($dbname) or die ("DB select failed"); //select db

$query1 = "select productname, count(*) as groupcount from product group by productname"; //get product count by groups of products

$query2 = "select count(*) as countammount from product"; //get the ammount of products in the db

$result1 = mysql_db_query($dbname,$query1); //queryresult1

$result2 = mysql_db_query($dbname,$query2); //queryresult2

$result3 = mysql_db_query($dbname,$query1); //queryresult3

$num_products = mysql_num_rows($result1); //count number of products

while ($row = mysql_fetch_assoc ($result2)) //put total ammount of products in db from query in a var
{
$dataTotal = $row[countammount];
}

function circlePoint( $deg, $dia )
{
$x = cos( deg2rad( $deg ) ) * ( $dia / 2 );
$y = sin( deg2rad( $deg ) ) * ( $dia / 2 );
return array( $x, $y );
}

$im = ImageCreate( $imageWidth, $imageHeight ); //make image area

//color array for chart. addmore colors if you have more data than colors below.
$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 ); //make image background

$degree = 0;

/**********************************
make the pie chart with percentages
**********************************/

$i=-1; //set color array counter

while ($row = mysql_fetch_assoc ($result1))
{

$i < sizeof( $row ); $i++; //counter for color array

$startDegree = round( $degree );
$degree += ( $row[groupcount] / $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 );
//joe: i added -6 to center %'s better
ImageString( $im, 2, floor( $centerX + $arcX - 6 ), floor( $centerY + $arcY - 6 ), intval( round( $row[groupcount] / $dataTotal * 100 ) ) . "%", $white );

}

/**********************************
setup for the menu and print title
**********************************/

$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 ); //print chart title
ImageString( $im, 1, $labelX, $titleY + 14, date( "Y-m-d H:i:sa" ), $black ); //print date

/**********************************
make the menu,lables,totals
**********************************/

$i=-1; //set color array counter

while ($row1 = mysql_fetch_assoc ($result3))
{

$i < sizeof( $row1 ); $i++; //counter for color array

$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, $row1[productname], $black );
//ImageString( $im, 2, $labelX + $labelWidth + 60, $labelY, $row1[groupcount], $black );
$labelY += $labelWidth + 2;
}

/**********************************
make the total
**********************************/

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/jpeg" ); //output image
Imagejpeg( $im );
ImageDestroy( $im ); //remove image from memory
?>
Jul 11 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
2229
by: Paul Charlton-Thomson | last post by:
Hi, I am passing 8 values each from 1 to 10 to a php page and at the moment I am drawing a bar chart using shaded cells to represent the magnitude of each bar. I would also like to draw these...
4
4615
by: Lothar Scholz | last post by:
Hello, is there something like "JpGraph" for python ? For those who don't know this PHP library: It is a high level drawing library that generates gif files with all kind of charts. Or (maybe...
2
5300
by: Espen | last post by:
Hi I have a C# application that should display a chart, the chart drawing is done by a COM object. I have tried to use dllimport with gdi32.dll and created compatibleDC and compatibleBitmap with...
1
1626
by: anthonyroche | last post by:
Does anyone know of a product that allows a user to draw a line on a chart? eg A historical stock tracker would have a line graph of say Aug 03 to Dec 03, which is a line graph representing a...
3
2423
by: pradnyapatil29 | last post by:
I am trying to draw a chart similar to GANTT chart in asp.net. Right now, I am drawing the rectangles on bitmap...but I dont want to use bitmap . Coz loading bitmap takes time...Is there...
0
7256
by: ImageAnalyst | last post by:
Have you ever wondered what the colors are of those dozens of colors you can choose from when using System.Drawing.Color.* ? I ran across this nice chart that shows color patches of all of them: ...
0
50964
debasisdas
by: debasisdas | last post by:
Here's a simple VB6 code snippet that uses the MSChart control to display Charts in VB6.0. To use this sample, please following steps Create a new project in VB6 Pull down the Project menu and...
0
941
by: oyster | last post by:
I am porting www.rmchart.com to python, now the code is almost finished, and I have supplied some examples.py which write to picturefiles directly, but the example to draw the chart on GUI stops...
15
2920
by: bH | last post by:
Hi All, I have been looking at javascript drawing from this website : http://www.cwdjr.net/geometricDraw/pentagon_draw.html" and I am wondering why the author made it into two images : upper...
2
2310
by: ThatsIT.net.au | last post by:
I have this code that writes a pie chart in a asp.net page, but I want to use it in a server control. When I try I get a error on the last line "Response.OutputStream" Obviously there is no...
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7190
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7360
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1392
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.