1

我的 PHP 代码 (budgetingMain.php) 中有一个名为“totals”的数组。我想将它用于我的数据以创建 Google 饼图。但是,我很难对其进行编码。

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

    var jsontotals = <?php echo json_encode($totals) ?>;

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Category');
        data.addColumn('number', 'Amount Spent on it');
        data.addRows([
          'jsontotals'
        ]);

        // Set chart options
        var options = {'title':'How Much Pizza I Ate Last Night',
                       'width':400,
                       'height':300};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chartcontainer'));
        chart.draw(data, options);
      }
    </script>

在当前的设置中,我收到以下错误:“Uncaught SyntaxError: Unexpected token <” for this line。

var jsontotals = <?php echo json_encode($totals) ?>;

我意识到这是嵌入的问题,但我找不到让它工作的方法。欢迎任何帮助!

编辑:这是总数的结构

$totals = array(
array("ClothingAndAccessories",0),
array("FlowersAndDecorations",0),
array("Ceremony",0),
array("Reception",0),
array("Photography",0),
array("Gifts/favours",0),
array("Stationary",0),
array("Entertainment",0),
array("Other",0)
);
4

0 回答 0