How do I display a Json random number in a real-time Flot chart?

与世无争的帅哥 提交于 2020-01-24 10:16:26

问题


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

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

and the HTML:

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

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


回答1:


Try this on your client side:

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

            }



回答2:


Flot needs its data as an array of data series and data points not just one number. The simplest solution would be to insert this before your $.plot() call:

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

If you want to build a chart over time like in the example, you have to start with an (empty) array and add each new number you get to it.

Edit:

For a complete line define a global variable for the chart data and a counter:

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

In your success callback, instead of the code from above insert this:

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

and change the $.plot() call to

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


来源:https://stackoverflow.com/questions/34222666/how-do-i-display-a-json-random-number-in-a-real-time-flot-chart

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