Google Charts - 使用谷歌图表绘制现有的PHP数组

时间:2013-11-14 01:53:21

标签: php google-visualization

我对谷歌图表有疑问,当我使用addRows命令时,我的php变量是否足以满足所需的格式?

我会把我正在使用的代码放在:

<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

        google.load("visualization", "1.0",{"packages":["corechart"]});

        google.setOnLoadCallback(drawChart);

        function drawChart(){
            //create the data table
            var data = new google.visualization.DataTable();
            data.addColumn("string","Fruits");
            data.addColumn("number","Amount");
            data.addRows([<?php echo "'$jsRows'";?>]);

            //set chart options
            var options = {"title":"Amount of different fruits",
                "width":400,
                "height":300};

            //instantiate and draw chart, passing in options
            var options = new google.visualization.PieChart(document.getElementById("chart_div"));
            chart.draw(data, options);
            } //end of drawchart function
            </script>

我写的时候:echo $jsRows;我得到:["0 as 6 h",0],["6 as 12h",0],["12 as 18h",8],["18 as 24h",0],这是谷歌图表可以处理的数据格式,但是使用此代码,我的图表不会出现。有谁知道为什么?提前谢谢!

1 个答案:

答案 0 :(得分:1)

您应该从$jsRows变量周围删除引号。使用引号将数组转换为字符串,这与#addRows方法不兼容。这样做:

data.addRows([<?php echo $jsRows;?>]);