简体   繁体   中英

JSON to Google Charts

I am trying to construct google charts using a ajax response form PHP file. Following is the output of alert in my javascript

{"cols":[{"id":"5","label":"LISTING","type":"number"}],"rows":[{"c":[{"v":"1"}]}]}

When I used the following code to construct the graph i get the error Data table is not defined.

function drawBarChartAll(totalclientstatus) {
        alert(totalclientstatus);
        //eval ( 'var jsonobject = totalclientstatus;'); 
        //var json = google.visualization.DataTable.toJSON(totalclientstatus)
        var data = google.visualization.DataTable(totalclientstatus);

        var options = {
            'width':670,
            'height':310,

            hAxis: {title: 'Year',  titleTextStyle: {color: 'red'}}
            //chartArea:{left:20,top:20,width:"70%",height:"100%"}
        };


        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.ColumnChart(document.getElementById('barchartall'));
        chart.draw(data, options);
    }

What am i doing wrong ? I am using the following php to construct the JSON

while ($row = mysqli_fetch_array($agent_result, MYSQL_ASSOC))
    {
        $columns = array();
        $reports['cols'][] = array(id => "{$row['statusid']}"
            , label => "{$row['status']}"
            , type => "number");
        $columns['c'][] = array(v => $row['total']);
        $reports['rows'][] = $columns;
    }
 return json_encode($reports);

The ajax response is

{"totalclientstatus":"{\"cols\":[{\"id\":\"5\",\"label\":\"LISTING\",\"type\":\"number\"}],\"rows\":[{\"c\":[{\"v\":\"1\"}]}]}"}

I guess you missed the 'new' :

var data = new google.visualization.DataTable( ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM