使用PHP创建jqplot图 [英] Creating jqplot graph using php

查看:154
本文介绍了使用PHP创建jqplot图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太会说英语.

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

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

php页面显示 [{"PER":"23"},{"PER":"47"},{"PER":"86"},{"PER":"25"},{"PER":"74"} ] 它来自mysql服务器.

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

表格只有一列,值分别为23、47、86、25、74

the table has one column and values are 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示例文件.

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

当我使用

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

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

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.

请特别帮助T.T

谢谢!

编辑

这是表.内容只有一张表.我想传递PER的值.

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

企业经营时期的每股股票价格

232 232 23 432 23

232 232 23 432 23

236 56 65 43 47

236 56 65 43 47

574 53 45 75 86

574 53 45 75 86

453 45 45 265 25

453 45 45 265 25

46 63 67 45 74

46 63 67 45 74

我只是暂时设定了一些值进行测试.

I just made values temporarily to test.

推荐答案

问题是json数据的格式,它来自(关联的)$ output数组.您不希望其中包含"PER"!尝试替换以下代码行:

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

带有这些:

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

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

这会将$ output从一个关联数组更改为标准的整数数组.

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

并且打印行在json数据周围添加了额外的方括号-jqplot要求.

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

这篇关于使用PHP创建jqplot图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆