Ranjith Kumar K
Ranjith Kumar K

Reputation: 41

load mysql data to google chart in PHP

could you please help here. Below is the script that am using to get the MYSQL data and pass that to google chart

$result = DB_query($sql, '', '', False, False);
$table = array();
$table['cols'] = array(
    array('label' => 'col1', 'type' => 'string'),
    array('label' => 'col2', 'type' => 'int')
);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
    $temp = array();
    // the following line will be used to slice the Pie chart
    $temp[] = array('v' => (string) $r['col1']);
    // Values of each slice
    $temp[] = array('v' => (int) $r['col2']);
    $rows[] = array('c' => $temp);
}

Below is the js part

echo
'
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("visualization", "1", {"packages":["corechart"]});
        google.setOnLoadCallback(drawChart);

        function drawChart() {
            var data = new google.visualization.DataTable(',$jsonTable,');
            var options = {
                title: "My Weekly Plan",
                is3D: "true",
                width: 800,
                height: 600
            };
            var chart = new google.visualization.PieChart(document.getElementById("chart_div"));
            chart.draw(data, options);
          }
    </script>
    <div id="chart_div"></div>
';

the problem here is even though am getting the correct value in the JSON variable, while loading the same to google chart am getting below error and hence the chart is not getting displayed.

"Uncaught Error: Invalid type, int, for column "col2".
    at gvjs_R.gvjs_.Sda 

Could you please help in understanding this issue.

Upvotes: 3

Views: 201

Answers (1)

WhiteHat
WhiteHat

Reputation: 61275

change 'int' to 'number'

valid types for google data table...

type - A string with the data type of the values of the column. The type can be one of the following: 'string', 'number', 'boolean', 'date', 'datetime', and 'timeofday'

see DataTable addColumn

Upvotes: 1

Related Questions