TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (7) Thread Tools Search this Thread Display Modes
Old 12-10-2007, 12:16 PM   7 links from elsewhere to this Post. Click to view. #1 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default Creating a three dimensional pie chart using Google's Chart API

I ran across this great, easy but very useful API a few days ago. Now for the tutorial.. let's assume we make a website for the TalkPHP soccerteam. They've played 10 matches (6 won, 3 draw, 1 lost). We'll be making a nice three dimensional pie chart of those stats!
Let me point out one little disadvantage before we start: This API has a limit to 50,000 queries per 24 hours. But futhermore nothing fency!

Little introduction
The Google Chart API returns a PNG image in response to a URL. So you have to include an <img /> into your page, and the API will return the chart. You are able to create several charts; line, bar, pie (the one we're going to create in this tutorial) and others.

URL Format
The URL must be in the following format: http://chart.apis.google.com/chart?<parameter 1>&<parameter 2>&<parameter n>.

The parameters
The following parameters are required with every query:
  • Chart size (chs=<width in pixels>x<height in pixels>)
  • Chart data (chd=<encoding type>:<chart data string>)
  • Chart type (cht=<chart type>)

Other optional parameters we're going to use in this tutorial are:
  • Chart title (chtt=<chart title>)
  • Pie chart labels (chl=<label1|label2|label3>)
  • Chart colors (chco=<hexadecimal format>)

You can specify as many parameters as you like, in any order. Just make sure you separate all the parameters by an ampersand (&).

Inserting the parameters
Alright! Let's go and fill in some parameters.

The width and height
Let's give the image a width of 300 pixels, and a height of 200 pixels:
http://chart.apis.google.com/chart?chs=300x200

Set the chart type
We want it to be a three dimensional pie chart:
http://chart.apis.google.com/chart?chs=300x200&cht=p3

Add the labels
We have a total of three labels; won, draw and lost:
http://chart.apis.google.com/chart?chs=300x200&cht=p3&chl=Lost|Draw|Won

Add the data
TalkPHP's soccerteam has won 60% of their matches, 30% draw and 10% are lost. We can choose out of three encoding types to insert data.
  • Simple encoding
  • Text encoding
  • Extended encoding
I prefer Text encoding because you can use percents as numbers. 0.0 stands for 0%, 1.0 for 1%, 30.0 for 30% and so on. -1 stands for a missing value. Separate all values by a comma (,).
http://chart.apis.google.com/chart?chs=300x200&cht=p3&chl=Won|Draw|Lost&chd=t:6 0.0,30.0,10.0

Give it a title!
The title will be "Pie chart for TalkPHP's soccerteam <new line> By maZtah". Insert on the spaces a '+', and use a '|' for the <new line>:
http://chart.apis.google.com/chart?chs=300x200&cht=p3&chl=Won|Draw|Lost&chd=t:6 0.0,30.0,10.0&chtt=Pie+chart+for+TalkPHP's+soccert eam|By+maZtah

Last but not least: change the color!
As we do not like the default orange color, we are going to change it in trendish pink!
http://chart.apis.google.com/chart?chs=300x200&cht=p3&chl=Won|Draw|Lost&chd=t:6 0.0,30.0,10.0&chtt=Pie+chart+for+TalkPHP's+soccert eam|By+maZtah&chco=ff0099

The result
Let's see what it looks like, right now!



Beautiful, isn't it?
Alright! Let's get to our last point!

Including the image to our webpage.
This can be done easily by the following code:
HTML Code:
<img src="http://chart.apis.google.com/chart?chs=300x200&cht=p3&chl=Won|Draw|Lost&chd=t:60.0,30.0,10.0&chtt=Pie+chart+for+TalkPHP's+soccerteam|By+maZtah&chco=ff0099" width="300" height="200" alt="Pie chart for TalkPHP soccerteam<br />By maZtah" />
Extra: making it dynamic
You ofcourse are able to make a dynamic version of the above pie chart. This can be achieved as follows:
PHP Code:
<?php
$iWon 
6;
$iDraw 3;
$iLost 1;
?>
<img src="http://chart.apis.google.com/chart?chs=300x200&cht=p3&chl=Won|Draw|Lost&chd=t:<?php echo $iWon?>.0,<?php echo $iDraw?>.0,<?php echo $iLost?>.0&chtt=Pie+chart+for+TalkPHP's+soccerteam|By+maZtah&chco=ff0099" width="300" height="200" alt="Pie chart for TalkPHP soccerteam<br />By maZtah" />
Ofcourse you could write a more advanced function to draw such a chart, but that's beyond the goal of this tutorial.
Reference: http://code.google.com/apis/chart/.

Good luck everybody!
maZtah is offline  
Reply With Quote
The Following 8 Users Say Thank You to maZtah For This Useful Post:
aristoworks (12-10-2007), bdm (12-10-2007), danielneri (12-25-2007), Gurnk (12-11-2007), Haris (12-10-2007), Karl (12-10-2007), Matt83 (12-10-2007), Wildhoney (12-14-2007)
Old 12-10-2007, 02:49 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Nice tutorial MaZtah, you did a good job of explaining all the basics.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-10-2007, 03:14 PM   #3 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Good job and thank you.
__________________
Necessity is the mother of invention.

My blog
Haris is offline  
Reply With Quote
Old 12-10-2007, 07:21 PM   #4 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Very nice explanation.
bdm is offline  
Reply With Quote
Old 12-14-2007, 12:13 PM   #5 (permalink)
The Wanderer
 
bmicallef's Avatar
 
Join Date: Nov 2007
Posts: 18
Thanks: 3
bmicallef is on a distinguished road
Default

I have not used the Google Chart API yet, but after reading your tutorial, I will defiantly take a look!

The 50,000 requsts/24 hrs does concern me though ...

In the past I have used the FREE (Community Edition) PHP/SWF Charts at http://www.maani.us ... check out the gallery.

By clicking on any chart, the PHP code generating it is relieved ... nice documentation, built in animations, 100% customizable!

Brad

Last edited by bmicallef : 12-14-2007 at 12:14 PM. Reason: wrong url
Send a message via AIM to bmicallef
bmicallef is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/general/1695-creating-three-dimensional-pie-chart-using-googles-chart-api.html
Posted By For Type Date
Creating Charts - SitePoint Forums This thread Refback 01-13-2008 05:21 PM
PHP Creating a Three Dimensional Pie Chart using Googles Chart API Tutorial This thread Refback 01-11-2008 12:37 AM
PHP Image Manipulation Creating a Three Dimensional Pie Chart using Googles Chart API Tutorial This thread Refback 01-10-2008 02:18 PM
reddit.com: newest submissions This thread Refback 01-10-2008 03:19 AM
reddit.com: newest submissions This thread Refback 01-10-2008 03:09 AM
Creating Charts - SitePoint Forums This thread Refback 01-04-2008 12:40 AM
Creating a three dimensional pie chart using Google's Chart API - TalkPHP This thread Refback 12-29-2007 10:41 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:39 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design