Are you fetching the data via AJAX? If so, then you need to change the
DataTable constructor to use jsonData instead of the echo statement:
var data = new google.visualization.DataTable(jsonData);
Dan is correct - you only want one google.load call. Remove these
redundant lines:
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
Also, if you are getting an error when using
echo json_encode($table, JSON_NUMERIC_CHECK);
then your PHP version is old and out of date, I would recommend upgrading
to the latest version if that is an option for you. If you can't upgrade,
then you need to make one small adjustment in your PHP code to correct the
numbers: change this line:
array('v' => $row['num_count'])
to this:
array('v' => (int) $row['num_count'])
as your database is outputting the counts as strings instead of integers.
The Visualization API will throw an error when you present a number as a
string, so you have to convert them into proper numbers (int or float,
depending on the type of number) when creating the JSON string (which is
what JSON_NUMERIC_CHECK does automatically in recent versions of PHP).
On Saturday, November 16, 2013 9:04:45 PM UTC-5, Hugo Mendoza wrote:
>
> Hi there -
>
> Here is what I receive from my db (which I think is fine):
> {"cols":[{"label":"priority","type":"string"},{"label":"num_count","type":"number"}],"rows":[{"c":[{"v":"High"},{"v":"4"}]},{"c":[{"v":"Low"},{"v":"3"}]},{"c":[{"v":"Medium"},{"v":"7"}]},{"c":[{"v":"Urgent"},{"v":"2"}]}]}
>
> But when I run this (posted below), I get the following error message: *Table
> has no columns. *Could someone help me out? Thank you!
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
> http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
> <title>Sample Pie Chart</title>
> <script type="text/javascript" src="https://www.google.com/jsapi
> "></script>
> <script type="text/javascript" src="//
> ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
> <script type="text/javascript">
> google.load("visualization", "1", {packages:["corechart"]});
> google.setOnLoadCallback(drawChart);
> function drawChart() {
>
> var jsonData = $.ajax({
> url: "data-file.php",
> dataType:"json",
> async: false
> }).responseText;
>
> var data = new google.visualization.DataTable(<?php echo
> json_encode($table); ?>);
> var chart = new
> google.visualization.PieChart(document.querySelector('#chart_div'));
> chart.draw(data, {
> // put the chart options in here
> height: 800,
> width: 600
> });
>
> }
> google.load('visualization', '1', {packages: ['corechart'], callback:
> drawChart});
>
> </script>
>
> </head>
> <body>
>
> <p>Pie Chart </p>
> <div id="chart_div" style="width: 900px; height: 500px;"></div>
>
> </body>
> </html>
>
>
>
>
>
>
>
>
> On Saturday, November 16, 2013 5:00:15 PM UTC-5, Hugo Mendoza wrote:
>>
>> Hi there -
>>
>> I figured out how to put my data into a JSON format using PHP and MySQL,
>> see below. Now, could someone please help me create, say, a pie chart with
>> Google Charts. I have not done this before, and I could use some help
>> creating it.
>>
>> <?php
>> // Connect to MySQL
>>
>> include ("./mylibrary/login.php");
>> login();
>>
>> // Fetch the data
>> $query = "
>> SELECT priority, COUNT(*) AS num_count\n"
>> . " FROM issue\n"
>> . " GROUP BY priority ";
>>
>> $result = mysql_query( $query );
>>
>> // All good?
>> if ( !$result ) {
>> // Nope
>> $message = 'Invalid query: ' . mysql_error() . "\n";
>> $message .= 'Whole query: ' . $query;
>> die( $message );
>> }
>>
>> // Print out rows
>> $prefix = '';
>> echo "[\n";
>> while ( $row = mysql_fetch_assoc( $result ) ) {
>> echo $prefix . " {\n";
>> echo ' "priority": "' . $row['priority'] . '",' . "\n";
>> echo ' "num_count": ' . $row['num_count'] . "\n";
>> echo " }";
>> $prefix = ",\n";
>> }
>> echo "\n]";
>>
>> ?>
>>
>> Thank you,
>> Hugo
>>
>>
--
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.