Y&的通过价值每秒更新样条图表

时间:2016-08-04 19:16:46

标签: javascript c# asp.net asp.net-mvc highcharts

我想从我的sql server数据库中获取数据,并将读取器值作为Y轴值传递给样条图表,每秒更新一次。下面是我尝试的代码,但它只获取一次值,并使用相同的获取值每秒更新样条图。我希望它每隔一秒从数据库中获取值并绘制。请帮帮我怎样才能解决我的问题。



        public string runquery()
        {
            /* local variable declaration */
            string result = string.Empty;
           
            string connectionString = "Server=tcp:xxxxx.database.windows.net,1433;Initial Catalog=xxxx;Persist Security Info=False;User ID=xxxx;Password=xxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            if (connection.State == System.Data.ConnectionState.Open)
            {
                //Console.WriteLine(DateTime.Now.ToString() + " - Connection Succeeded!");
                string selectfromtable = "Select  data_col From infobenchtable order by id_col desc";
                SqlCommand selectfromtableequery = new SqlCommand(selectfromtable, connection);
                SqlDataReader rd = selectfromtableequery.ExecuteReader();
                if (rd.Read() == true) { result =  rd.GetString(0); }
                connection.Close();
            }
            return result;
        }
        public ActionResult SplineUpdateEachSecond()
        {
           string stringtopass = string.Empty;
            //string methodresult = runquery();
            stringtopass = @"// set up the updating of the chart each second
                                       var series = this.series[0];
                                       setInterval(function() {
                                          var x = (new Date()).getTime(), // current time
                                             y =" + runquery() + @"; //To Edit for azure
                                          series.addPoint([x, y], true, true);
                                       }, 10000);";
            //System.Windows.Forms.Mess
            List<object> points = new List<object>(20);
            DateTime now = DateTime.Now;
            Random rand = new Random();
            for (int i = -19; i <= 0; i++)
                points.Add(new { X = now.AddSeconds(i), Y = rand.NextDouble() });
            Highcharts chart = new Highcharts("chart")
                
                .SetOptions(new GlobalOptions { Global = new Global { UseUTC = false } })
                .InitChart(new Chart
                           {
                               DefaultSeriesType = ChartTypes.Line,
                               MarginRight = 10,
                               Events = new ChartEvents
                                        {
                                            Load = "ChartEventsLoad"
                                        }
                           })
            .AddJavascripFunction("ChartEventsLoad",
                                              runquery()) 


                .SetTitle(new Title { Text = "Live random data" })
                .SetXAxis(new XAxis
                          {
                              Type = AxisTypes.DateTime,
                              TickPixelInterval = 150
                          })
                .SetYAxis(new YAxis
                          {
                              Title = new YAxisTitle { Text = "Value" },
                              PlotLines = new[]
                                          {
                                              new YAxisPlotLines
                                              {
                                                  Value = 0,
                                                  Width = 1,
                                                  Color = ColorTranslator.FromHtml("#808080")
                                              }
                                          }
                          })
                .SetTooltip(new Tooltip { Formatter = "TooltipFormatter" })
                .AddJavascripFunction("TooltipFormatter",
                                      @"return '<b>'+ this.series.name +'</b><br/>'+
                                       Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+ 
                                       Highcharts.numberFormat(this.y, 2);")
                .SetLegend(new Legend { Enabled = false })
                .SetExporting(new Exporting { Enabled = false })
                .SetSeries(new Series
                           {
                               Name = "Random data",
                               Data = new Data(points.ToArray())
                           });

            return View(chart);
        }
&#13;
&#13;
&#13;

0 个答案:

没有答案