Your JSON format is not correct for the Visualization API, and it will 
cause IE to throw an error (due to an extra comma at the end of the array 
of objects).  Here's the correct format:

$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' => $row['num_count'])
        )
    )
}
echo json_encode($table, JSON_NUMERIC_CHECK);

You can put the echo statement in your javascript for drawing the chart:

function drawChart () {
    var data = new google.visualization.DataTable(<?php echo 
json_encode($table, JSON_NUMERIC_CHECK); ?>);
    var chart = new 
google.visualization.PieChart(document.querySelector('#chart_div'));
    chart.draw(data, {
        // put the chart options in here
        height: 400,
        width: 600
    });
}
google.load('visualization', '1', {packages: ['corechart'], callback: 
drawChart});

and in your HTML, you need a div like this to put the chart in:

<div id="chart_div"></div>

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.

Reply via email to