简体   繁体   English

实时HighChart不显示数据

[英]Live HighChart not displaying data

I am trying the code of highstocks which is given below 我正在尝试下面给出的高股票代码

   <script type="text/javascript">


 $(function () {

                Highcharts.setOptions({
                    global: {
                        useUTC: false
                    }
                });

                // Create the chart
                $('#container').highcharts('StockChart', {
                    chart: {
                        events: {
                            load: function () {

                                // set up the updating of the chart each second
                                var series = this.series[0];
                                setInterval(function () {
                                    var x = (new Date()).getTime(), // current time
                                    y = <%=chartData%> 
                                    series.addPoint([x, y], true, true);
                                }, 1000);
                            }
                        }
                    },

                    rangeSelector: {
                        buttons: [{
                            count: 1,
                            type: 'minute',
                            text: '1M'
                        }, {
                            count: 5,
                            type: 'minute',
                            text: '5M'
                        }, {
                            type: 'all',
                            text: 'All'
                        }],
                        inputEnabled: false,
                        selected: 0
                    },

                    title: {
                        text: 'Sensor Data'
                    },

                    exporting: {
                        enabled: false
                    },

                    series: [{
                        name: 'Sensor Value',
                        data: (function () {
                             //generate an array of random data
                            var data = [], time = (new Date()).getTime(), i;

                            for (i = -999; i <= 0; i++) {
                                data.push([
                                    time + i * 1000,
                                  <%= chartData%>
                                ]);
                            }
                            return data;
                        })()
                    }]
                });

            });
        </script>

and displaying it in to the container 并显示在容器中

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto; width: 733px;"></div>

The code behind is 后面的代码是

 public string chartData
    {
        get;
        set;

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        GetData();
        List<int> _data = new List<int>();
        foreach (DataRow row in dt.Rows)
        {
            _data.Add((int)row["Id"]);
        }
        JavaScriptSerializer jss = new JavaScriptSerializer();
        chartData = jss.Serialize(_data); //this make your list in jSON format like [88,99,10]
        Response.Write(chartData);
    }
    private void GetData()
    {
        StringBuilder str = new StringBuilder();
        SqlConnection con = new SqlConnection("Data Source=INBDQ2WK2LBCD2S\\SQLEXPRESS;Initial Catalog=MCAS;Integrated Security=SSPI");
        SqlDataAdapter adp = new SqlDataAdapter("select top 1 Id from Monitoring order by Id desc ", con);
        adp.Fill(dt);
    }

Here I have created a JSON and I am transferring it to the javascript for highchart. 在这里,我创建了一个JSON,并将其传输到javascript的highchart。

The problem I am having is that it is showing only one point. 我遇到的问题是它只显示了一点。 The values which are also updated in the database it is not updating in the chart. 在数据库中也更新的值在图表中未更新。

What is wrong with it? 怎么了

In your code I can see this: 在您的代码中,我可以看到:

setInterval(function () {
    var x = (new Date()).getTime(), // current time
            y = <%=chartData%>; 
    series.addPoint([x, y], true, true);
}, 1000);

I guess, that chartData variable is where you have stored new point? 我猜想,那个chartData变量是您存储新点的地方? At least you expect to have there. 至少您期望在那里。 However your variable chartData is assigned in HTML once, after page is loaded from server. 但是,从服务器加载页面后,将以HTML一次分配变量chartData That you will update value on server, doesn't mean value get updated on client side. 您将在服务器上更新价值,并不意味着在客户端更新价值。

I advice to use AJAX calls, for example: http://www.highcharts.com/studies/live-server.htm (see sources for code example). 我建议使用AJAX调用,例如: http : //www.highcharts.com/studies/live-server.htm (请参阅源代码示例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM