Thank you for all the help.
I am still having some problems, but I think that I am almost there.
Here is the code to my data-file.php (see below), which returns this JSON
format:
{"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}]}]}
<?php
include ("./mylibrary/login.php");
login();
$query = "
SELECT priority, COUNT(*) AS num_count\n"
. " FROM issue\n"
. " GROUP BY priority ";
$result = mysql_query( $query );
if ( !$result ) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die( $message );
}
$table = array(
'cols' => array(
array('label' => 'priority', 'type' => 'string'),
array('label' => 'num_count', 'type' => 'number')
),
'rows' => array()
);
while ($row = mysql_fetch_assoc($result)) {
$table['rows'][] = array(
'c' => array(
array('v' => $row['priority']),
array('v' => (int) $row['num_count']) //Using this because PHP server with
host is out of date
)
);
}
echo json_encode($table);
?>
Here is my other file, but it does not display a chart and am not getting
any errors (I tried both html and php extensions with no luck):
<!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">
google.load("visualization", "1", {packages:["corechart"]});
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
});
}
</script>
</head>
<body>
<p>Pie Chart Sample</p>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
Thank you in advance for any help that you can provide.
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.