0

我正在尝试从中获取记录mysql并通过 呈现数据amchart,但我在这样做时遇到了困难。我编写了以下代码,但它不起作用。查询工作正常,但问题是用查询结果替换静态(占位符)数据。请建议。

{

$selectdata="SELECT member_lhp, COUNT( * ) AS  'land_pattern' FROM hh_basic_info GROUP BY member_lhp";
$resdata=mysql_query($selectdata);

<script type="text/javascript">
    var chart;
    var chartData = [
        <?php
            $count=0;
            while($rowdata = mysql_fetch_assoc($resdata))
            foreach($rowdata as $rows){
                $type= $rows['member_lhp'];
                $lp=$rows['land_pattern'];
                if($count++ > 0) echo ',';
        ?>
        {
            year: <?php echo $type;?>,
            income: <?php echo $lp;?>
        },
        <?php } ?>
    ];

    AmCharts.ready(function () {
        // SERIAL CHART
        chart = new AmCharts.AmSerialChart();
        chart.dataProvider = chartData;
        chart.categoryField = "year";
        // this single line makes the chart a bar chart, 
        // try to set it to false - your bars will turn to columns                
        chart.rotate = true;
        // the following two lines makes chart 3D
        chart.depth3D = 20;
        chart.angle = 30;

        // AXES
        // Category
        var categoryAxis = chart.categoryAxis;
        categoryAxis.gridPosition = "start";
        categoryAxis.axisColor = "#DADADA";
        categoryAxis.fillAlpha = 1;
        categoryAxis.gridAlpha = 0;
        categoryAxis.fillColor = "#FAFAFA";

        // value
        var valueAxis = new AmCharts.ValueAxis();
        valueAxis.axisColor = "#DADADA";
        valueAxis.title = "Villagers - Land holding pattern";
        valueAxis.gridAlpha = 0.1;
        chart.addValueAxis(valueAxis);

        // GRAPH
        var graph = new AmCharts.AmGraph();
        graph.title = "Income";
        graph.valueField = "income";
        graph.type = "column";
        graph.balloonText = "Land holding number in [[category]]:[[value]]";
        graph.lineAlpha = 0;
        graph.fillColors = "#bf1c25";
        graph.fillAlphas = 1;
        chart.addGraph(graph);

        // WRITE
        chart.write("chartdiv");
    });
</script>
}
4

2 回答 2

0

快速浏览一下,您的大括号后似乎可能包含了一个额外的逗号。如果您的计数大于 0,您已经通过 PHP 添加了一个逗号,因此这将在每组数据之间引入第二个不必要的逗号。

你有以下...

    {
      year: <?php echo $type;?>,
      income: <?php echo $lp;?>
    },

试着用这个代替......

    {
      year: <?php echo $type;?>,
      income: <?php echo $lp;?>
    }
于 2014-07-18T09:14:27.023 回答
0

好像忘记解析json格式了,直接写

var newchartData= JSON.parse(chartData);

现在分配newcharData给 DataProvider,它应该可以工作。

于 2016-03-25T08:37:58.213 回答