I am trying to create a pie char for my database table.
temp -> with columns(id, sent, pcount, ncount)
pcount
and ncount
are int numbers. I want to create a pie chart for this two values.
I am trying to load this file.
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart()
{
var jsonData = $.ajax({
url: "graphData.php",
dataType:"json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
var options = {'title':'Ticket Sales',
'width':500,
'height':400};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data,options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
graphData.php content is the following.
<?php
$con = mysqli_connect('127.0.0.1:3306', 'root', 'root', 'test');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: ".mysqli_connect_error();
}
$sql = "SELECT pcount,count(*) AS count from temp";
if ($result=mysqli_query($con,$sql))
{
$rownum=mysqli_num_rows($result);
printf("Result set has %d rows.\n",$rownum);
mysqli_free_result($result);
}
//start the json data in the format Google Chart js/API expects to receieve it
$data = array('cols' => array(array('label' => 'pcount', 'type' => 'int'),
array('label' => 'mcount', 'type' => 'int')),
'rows' => array());
while($row = mysqli_fetch_row($result)) {
$data['rows'][] = array('c' => array(array('v' => $row[0]), array('v' => $row[1])));
}
echo json_encode($data);
?>
I have taken this code from web and modified as per my need. When I load my first PHP page, it shows nothing. What am I doing wrong?
Here is the code to create PIE chart(Just change function name for other charts) from my sql table in PHP.
Important thing to remember is
ind_type and sum are the column in my table, first var should be string here.
You obviously have modified the script wrongly and not to your needs. Otherwise you wouldn't be asking what you're doing wrong.
As asking "What am I doing wrong?" implies you do not understand the code incl. your modifications, the first thing you need to do is to step back to the last working version of the code.
So commit your changes now and then diff your script to the last working commit. This will show you your changes and it's often much more easier to spot the part where you introduced the error.