1

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;

コントローラー:

    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 に答える