如何在实时Flot图表中显示Json随机数? [英] How do I display a Json random number in a real-time Flot chart?

查看:50
本文介绍了如何在实时Flot图表中显示Json随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I made in my C# page a random number which is stored in a json object:

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页面上显示它:

and then I display it on my ASP page:

  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>

我的图表上没有得到随机数。我做错了什么? 之间的代码//开始:实时绘图 //结束:实时绘图我从这里开始: http://www.flotcharts.org/flot/examples/realtime/index。 html

I don't get the random number on my chart. What did I do wrong? The code between //start: plot in real time and //end: plot in real time I took from here: http://www.flotcharts.org/flot/examples/realtime/index.html

推荐答案

在客户端试试这个:

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

            }

这篇关于如何在实时Flot图表中显示Json随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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