admin管理员组

文章数量:1327661

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 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\"}]}]}"}
Share Improve this question edited Sep 26, 2012 at 1:52 Deepak asked Sep 26, 2012 at 1:39 DeepakDeepak 6,81218 gold badges70 silver badges121 bronze badges 2
  • Are you using google.setOnLoadCallback? – McGarnagle Commented Sep 26, 2012 at 1:47
  • yes i am using that to call the ajax and the ajax in turn calls the drawBarChartAll() – Deepak Commented Sep 26, 2012 at 1:50
Add a ment  | 

1 Answer 1

Reset to default 9

I guess you missed the 'new' :

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

本文标签: phpJSON to Google ChartsStack Overflow