1

我想通过 JSON 设置谷歌图表,但目前它不起作用。其说法的网页无法读取未定义的“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;

控制器 :

    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>

4

1 回答 1

1

您的 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;
于 2013-10-20T02:09:34.080 回答