Creating jqplot graph using php

限于喜欢 提交于 2019-12-12 09:58:55

问题


I'm not really good at English sorry.

I don't know how to pass values to jqplot application.

the php webpage shows [{"PER":"23"},{"PER":"47"},{"PER":"86"},{"PER":"25"},{"PER":"74"}] which came from mysql server.

the table has one column and values are 23, 47, 86, 25, 74

this is the php code.

<?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();

?>

this is html sample file.

       $(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'
            }  
          });
    });

when I used

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

jsondata.txt included [[ 23, 47, 86, 25, 74 ]].

but when I used the other one it doesn't. I want to get values from server. I guess there are problems passing values.

please help specifically T.T

thanks!

EDIT

this is the table. the contents have only one table. I want to pass PER's values.

ENTERPRISE PERIOD EPS STOCKPRICE 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

I just made values temporarily to test.


回答1:


The problem is the format of your json data, which is coming from the (associative) $output array. you don't want those '"PER"'s in it! try replacing these lines of code:

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

print(json_encode($output)); 

with these:

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

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

this will change $output from an associative array, to a standard array of integers.

and the print line adds additional square brackets around the json data - which the jqplot requires.



来源:https://stackoverflow.com/questions/12708029/creating-jqplot-graph-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!