pantz.org banner
Create a piechart with PHP
Posted on 11-03-2002 02:30:00 UTC | Updated on 11-03-2002 02:30:00 UTC
Section: /software/php/ | Permanent Link

This code uses data taken from a MYSQL database and makes a piechart from it. I did the MYSQL mods and a guy named Xiao did the pie chart. The PHP install on the webserver has to be compiled with the GD, Freetype2, and libjpeg (libpng looks better for graphs IMHO). To make the graph appear just access it like its a picture file.

<?php

// Example of html to access the bar graph in html: 

/********************************
2D Pie Chart Version 1.0
Programer: Xiao Bin Zhao
E-mail: [email protected]
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 = 'database.here.com';

$dbpass = 'poe23451!@#';

$dbname = 'test';

$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 ) ) . "%", $black );

}

/**********************************
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
?>

Reddit!

Related stories


RSS Feed RSS feed logo

About


3com

3ware

alsa

alsactl

alsamixer

amd

android

apache

areca

arm

ati

auditd

awk

badblocks

bash

bind

bios

bonnie

cable

carp

cat5

cdrom

cellphone

centos

chart

chrome

chromebook

cifs

cisco

cloudera

comcast

commands

comodo

compiz-fusion

corsair

cpufreq

cpufrequtils

cpuspeed

cron

crontab

crossover

cu

cups

cvs

database

dbus

dd

dd_rescue

ddclient

debian

decimal

dhclient

dhcp

diagnostic

diskexplorer

disks

dkim

dns

dos

dovecot

drac

dsniff

dvdauthor

e-mail

echo

editor

emerald

encryption

ethernet

expect

ext3

ext4

fat32

fedora

fetchmail

fiber

filesystems

firefox

firewall

flac

flexlm

floppy

flowtools

fonts

format

freebsd

ftp

gdm

gmail

gnome

google

gpg

greasemonkey

greylisting

growisofs

grub

hacking

hadoop

harddrive

hba

hex

hfsc

html

html5

http

https

hulu

idl

ie

ilo

intel

ios

iperf

ipmi

iptables

ipv6

irix

javascript

kde

kernel

kickstart

kmail

kprinter

krecord

kubuntu

kvm

lame

ldap

linux

logfile

lp

lpq

lpr

maradns

matlab

memory

mencoder

mhdd

mkinitrd

mkisofs

moinmoin

motherboard

mouse

movemail

mplayer

multitail

mutt

myodbc

mysql

mythtv

nagios

nameserver

netflix

netflow

nginx

nic

ntfs

ntp

nvidia

odbc

openbsd

openntpd

openoffice

openssh

openssl

openvpn

opteron

parted

partimage

patch

perl

pf

pfflowd

pfsync

photorec

php

pop3

pop3s

ports

postfix

power

procmail

proftpd

proxy

pulseaudio

putty

pxe

python

qemu

r-studio

raid

recovery

redhat

router

rpc

rsync

ruby

saltstack

samba

schedule

screen

scsi

seagate

seatools

sed

sendmail

sgi

shell

siw

smtp

snort

solaris

soundcard

sox

spam

spamd

spf

spotify

sql

sqlite

squid

srs

ssh

ssh.com

ssl

su

subnet

subversion

sudo

sun

supermicro

switches

symbols

syslinux

syslog

systemd

systemrescuecd

t1

tcpip

tcpwrappers

telnet

terminal

testdisk

tftp

thttpd

thunderbird

timezone

ting

tls

tools

tr

trac

tuning

tunnel

ubuntu

unbound

vi

vpn

wget

wiki

windows

windowsxp

wireless

wpa_supplicant

x

xauth

xfree86

xfs

xinearama

xmms

youtube

zdump

zeromq

zic

zlib