View Full Version : Creating pie chart by taking values from the database

07-11-2012, 12:45 PM

i want to create a pie chart in my website by taking values from database. I should display the top 10 most purchased product in the form of pie chart in my website. But i dont know how to do it. I have no clue how to start.

Please someone help me in this.

07-11-2012, 02:28 PM
Hi. I think your best bet would be to have a look at the Google Chart API, should be able to do this for you.


07-11-2012, 02:48 PM
Google chart is a good one, and there are a LARGE number of free softwares you can pick up, some straight PHP, others PHP/JS, others PHP/Flash.

07-11-2012, 05:38 PM
But i dont know how to use google API and please send me the link for other softwares please.

07-11-2012, 05:59 PM
10 seconds of googling either of those gets you the results you want. Google charts is well documented and very easy to use.

07-12-2012, 04:15 AM

Here is my code. I tried doing like this , but nothing is getting displayed. blank screen displays. I don't know where i am going wrong. Please help me

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
$i = 0;
$sql = "select * from product inner join product_description on product.product_id = product_description.product_id order by product.viewed desc limit 10";
$results1 = $this->db->query($sql);
//for($i=0; $i<10; ++$i)
$stringResult .= "data[" .$i. "] = { label:" .$results['name'].", data: ". $results['quantity'] ."}";
//start loop
//while or foreach
// foreach($data as $v){
// echo ",['{$name}'],{$count}]\r\n";
return $stringResult;

var options = {
title: 'My Daily Activities'

var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);