javascript - 无法读取房地产谷歌图表

标签 javascript php codeigniter google-visualization

我想通过 JSON 设置 google 图表,但目前它不起作用。该网页说无法读取“1”未定义的属性。如何解决这个问题?预先感谢这是我的代码:

测试目的:(模型)

    $table = array();
    $table['cols'] = array(
        array('label' => 'Name', 'type' => 'string'),
        array('label' => 'Value', 'type' => 'number')
    );

    $PR = $CO = $IN = $SP = $SC = 5;


    $temp[] = array('v' => 'PR'); 
    $temp[] = array('v' => $PR);
    $temp[] = array('v' => 'CO'); 
    $temp[] = array('v' => $CO);
    $temp[] = array('v' => 'SCC'); 
    $temp[] = array('v' => $SC);
    $temp[] = array('v' => 'IN'); 
    $temp[] = array('v' => $IN);
    $temp[] = array('v' => 'SP'); 
    $temp[] = array('v' => $SP);

    $table['rows'] = $temp;

    return $table;

Controller :

    public function getProjects(){
    $this->load->model('mreport');
    $results = $this->mreport->getBilling();
    echo json_encode($results);

}

HTML:

   <?php ?>

<html>
<head>
    <script type="text/javascript" src="<?php echo base_url() . 'resources/js/jquery-1.9.1.js' ?>"></script>
    <script type="text/javascript" src="<?php echo base_url() . 'resources/js/jquery-ui-1.10.3.custom.js' ?>"></script>
    <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 jsonData = $.ajax({
                url: "<?php echo site_url('report/getProjects'); ?>",
                dataType: "json",
                async: false
            }).responseText;

            // Create our data table out of JSON data loaded from server.
            var data = new google.visualization.DataTable(jsonData);

            // Instantiate and draw our chart, passing in some options.
            var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
            chart.draw(data, {width: 400, height: 240});
        }

        var options = {
            title: 'Pie chart',
            is3D: true,
        };
    </script>
</head>
<body>
    <div id="chart_div"></div>
</body>

最佳答案

您的 JSON 字符串配置不正确。试试这个:

$table = array();
$table['cols'] = array(
    array('label' => 'Name', 'type' => 'string'),
    array('label' => 'Value', 'type' => 'number')
);

$PR = $CO = $IN = $SP = $SC = 5;

$table['rows'] = array();
$temp = array();
$temp[] = array('v' => 'PR'); 
$temp[] = array('v' => $PR);
$table['rows'][] = array('c' => $temp);

$temp = array();
$temp[] = array('v' => 'CO'); 
$temp[] = array('v' => $CO);
$table['rows'][] = array('c' => $temp);

$temp = array();
$temp[] = array('v' => 'SCC'); 
$temp[] = array('v' => $SC);
$table['rows'][] = array('c' => $temp);

$temp = array();
$temp[] = array('v' => 'IN'); 
$temp[] = array('v' => $IN);
$table['rows'][] = array('c' => $temp);

$temp = array();
$temp[] = array('v' => 'SP'); 
$temp[] = array('v' => $SP);
$table['rows'][] = array('c' => $temp);

return $table;

关于javascript - 无法读取房地产谷歌图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19471837/

相关文章:

java - 将 URLDecoder.decode(temp, "UTF-8") 转换为 php

javascript - 登录表单电子邮件 ID 将使用 ajax 成功功能显示在密码重置表单中,无需刷新页面

php - 如何通过codeigniter重定向带有数据的页面

javascript - 将 document.cookie 等字符串转换为对象

javascript - 是否可以使用 jQuery xml 处理程序解析 SOAP 响应?

javascript - Twitter 小部件并不总是加载

java - 无法在 Java 和 PHP 之间交换使用 AES-256 加密的数据

php - 使用 PHP OO 类获取行

php - 在 CodeIgniter 的 Active Record 中使用 'having'

javascript - 添加光标 : pointer to audio element