使用php创建jqplot图

时间:2012-10-03 11:58:36

标签: php mysql json jqplot

我不太擅长英语抱歉。

我不知道如何将值传递给jqplot应用程序。

php网页显示 [{ “PER”: “23”},{ “PER”: “47”},{ “PER”: “86”},{ “PER”: “25”},{ “PER”: “74”} ] 它来自mysql服务器。

该表有一列,值为23,47,86,25,74

这是php代码。

<?php
mysql_connect("localhost","root","autoset");
mysql_select_db("test");

$q=mysql_query("SELECT PER FROM Evaluation");
while($e=mysql_fetch_assoc($q))
    $output[]=$e;

print(json_encode($output)); 

mysql_close();

?>

这是html示例文件。

       $(document).ready(function(){

        var ajaxDataRenderer = function(url, plot) {
            var ret = null;
            $.ajax({
                // have to use synchronous here, else returns before data is fetched
                async: false,
                url: url,
                dataType:'json',
                success: function(data) {
                    ret = data;
                }
            });
            return ret;
        };

        var jsonurl ="http://127.0.0.1/index.php"; //"../report/jsondata.txt";

        plot1 = $.jqplot("chart2", jsonurl, {
            title: 'AJAX JSON Data Renderer',
            dataRenderer: ajaxDataRenderer,

            animate: true,
            animateReplot: true,
            cursor: {
              show: true,
              zoom: true,
              looseZoom: true,
              showTooltip: false,

            },
            series:[   
              {
                label:'a',
                color: '#FF0000',
                rendererOptions: {
                  animation: {
                    speed: 2000
                  }
                }
              },
              {
                  label:'b',
                  color: '#0000FF',
                  rendererOptions: {
                    animation: {
                      speed: 2000
                    }
                  }
                }
            ],


            axesDefaults: {
              pad: 0
            },
            axes: {
              xaxis: {
                label: "Period",
                renderer: $.jqplot.CategoryAxisRenderer,
                labelRenderer: $.jqplot.CanvasAxisLabelRenderer,

              },
              yaxis: {
                label: "PER",
                tickOptions: {
                  formatString: "%d"
                },
                rendererOptions: {
                  //forceTickAt0: true
                }
              },
              y2axis: {
                tickOptions: {
                  formatString: "%d"
                },
                rendererOptions: {
                  alignTicks: true
                  //forceTickAt0: true
                }
              }
            },


            highlighter: {
              show: true,
              showLabel: true, 
              tooltipAxes: 'y',
              sizeAdjust: 7.5 , tooltipLocation : 'ne'
            },


            legend: {
              show: true,
              location: 'sw',
              placement: 'outside'
            }  
          });
    });

当我使用

var jsonurl ="../report/jsondata.txt"; it worked. 

jsondata.txt包括[[23,47,86,25,74]]。

但是当我使用另一个时却没有。我想从服务器获取值。 我想传递价值存在问题。

请特别帮助T.T

谢谢!

修改

这是表格。内容只有一个表。我想通过PER的值。

  

企业期间每股盈利

     

232 232 23 432 23

     

236 56 65 43 47

     

574 53 45 75 86

     

453 45 45 265 25

     

46 63 67 45 74

我只是临时制作值以进行测试。

1 个答案:

答案 0 :(得分:2)

问题是你的json数据的格式,它来自(associative)$ output数组。你不希望那些“PER”在里面!尝试替换这些代码:

while($e=mysql_fetch_assoc($q))
    $output[]=$e;

print(json_encode($output)); 

用这些:

while($e=mysql_fetch_assoc($q))
    $output[]=$e["PER"];

print ('['.json_encode($output).']'); 

这将把关联数组的$ output更改为标准的整数数组。

并且打印行在json数据周围添加了额外的方括号 - 这是jqplot所需要的。