php - JSON 到 Google 图表

标签 php javascript jquery json google-visualization

我正在尝试使用 ajax 响应表单 PHP 文件构建 google 图表。以下是我的 javascript

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

当我使用以下代码构建图表时,我收到错误数据表未定义。

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);
    }

我做错了什么?我使用以下 php 构建 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);

ajax 响应是

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

最佳答案

我猜你错过了"new":

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

关于php - JSON 到 Google 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12593412/

相关文章:

php - 将列添加到 WooCommerce 中的管理订单列表

javascript - 从 API 加载数据后,react-select 上的下拉列表仍然为空

jquery - 在鼠标悬停时在 div 上创建移动文本

javascript - 防止 HTML 标签 strip 化

Javascript 数组总是未定义

PHP将上传的图片显示到不同的页面

php - 电子邮件确认码的最佳做法

javascript - JavaScript、PHP 和 MySQL 之间的通信

javascript - C3 条形图,条数未知

javascript - 在构造函数angular 2中声明属性