填充amchart动态从mysql中获取数据

时间:2013-07-15 06:01:21

标签: mysql dynamic amcharts

我正在尝试从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>
}

2 个答案:

答案 0 :(得分:0)

快速浏览一下,看起来你可能在你的花括号之后加了一个额外的逗号。如果你的计数大于0,你已经通过PHP添加了一个逗号,所以这会在每组数据之间引入第二个不必要的数字。

你有以下......

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

尝试这样做......

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

答案 1 :(得分:0)

好像你忘了以json格式解析, 只需写下

var newchartData= JSON.parse(chartData);

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