如何在实时Flot图表中显示Json随机数?

时间:2015-12-11 11:36:45

标签: javascript c# jquery ajax flot

我在C#页面中创建了一个存储在json对象中的随机数:

if (method == "rnd")
{
    //Random number
    this.Page.Response.ContentType = "application/json2";
    Random rnd = new Random();
    int nr = rnd.Next(1, 100); // creates a number between 1 and 99
    String str1 = nr.ToString();
    var json2 = new JavaScriptSerializer().Serialize(str1);
    this.Page.Response.Write(json2);
}

然后我在ASP页面上显示它:

  function test2() {
      $.ajax({
          type: 'GET',
          url: ('ajax.aspx?meth=') + "rnd",/
          contentType: 'application/json2; charset=utf-8',
          dataType: 'json',
          async: true,
          cache: false,
          global: false, 
          timeout: 120000,
          success: function (data, textStatus, jqXHR) { 
              $('#nr').html(data);    

              //start: plot in real time   
              var plot = $.plot("#placeholder", data, {
                  series: {
                      shadowSize: 0 // Drawing is faster without shadows
                  },
                  yaxis: {
                      min: 0,
                      max: 100
                  },
                  xaxis: {
                      show: false
                  }
              });
              //end: plot in real time
          },
          error: function (jqXHR, textStatus, errorThrown) {
              window.alert(errorThrown);
          }
      });
  }
  window.setInterval(test2, 1000);

和HTML:

<div id="nr"></div>
<div class="demo-container">
    <div id="placeholder" class="demo-placeholder"></div>
</div>

我的图表上没有随机数字。我做错了什么?我从//start: plot in real time

获取//end: plot in real timewith CTE as ( select e1.emp_id, e1.reports_to, e1.emp_name, [level]+1 from Emp e1 left outer join Emp e2 on e1.reports_to = e2.emp_id ) Select * from CTE 之间的代码:http://www.flotcharts.org/flot/examples/realtime/index.html

2 个答案:

答案 0 :(得分:1)

Flot需要将其数据作为数据序列和数据点的数组而不仅仅是一个数字。最简单的解决方案是在$.plot()电话前插入此内容:

data = [[[1, data]]];

如果你想像在示例中一样构建一个图表,你必须从一个(空)数组开始,并添加你得到的每个新数字。

修改

对于完整的行,定义图表数据的全局变量和计数器:

var completeData = [[]];
var dataCounter = 0;

success回调中,不是上面的代码,而是插入:

completeData[0].push([dataCounter++, data]);

并将$.plot()调用更改为

var plot = $.plot("#placeholder", completeData, {

答案 1 :(得分:1)

在您的客户端尝试此操作:

function test2() {
                $.ajax({
                    type: 'GET',
                    url: ('ajax.aspx?meth=') + "rnd",
                    contentType: 'application/json2; charset=utf-8',
                    dataType: 'json',
                    //async: true,
                    //cache: false,
                    //global: false,
                    //  timeout: 120000,
                    success: function (data, textStatus, jqXHR) {

                        var obj = jQuery.parseJSON(data);

                        $('#azi').html(obj.sec);
                        $('#nr').html(obj.val);
                        $('#nr1').html(obj.val1);

                        t = obj.val;
                        t1 = obj.val1;
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.alert(errorThrown);
                    }
                });

            }