日期:2014-05-18  浏览次数:21224 次

C# winform中如何从数据库取数据绘制实时曲线图
如题,一般画实时曲线图,都是将数据放在Point[] ptlist数组中,我的数据是每隔1S存放在sql server中,用什么方法可以将数据库中的数据实时取出,然后作为实时显示曲线的数据呢?曲线的横坐标是时间,纵坐标就是数据库中的数值

------解决方案--------------------
有个思路:
比如你图中最多显示10期数据,那么就建个Queue<T>,内存中只存放10组数据,超过的话,就存入数据库,并且删除该条数据,载入吓一跳,;
如果你是全部显示,那就放在list<t>里面吧;反正都要用到的,
------解决方案--------------------
引用ZedGraph.dll
using ZedGraph;
C# code

GraphPane myPan = new GraphPane();
        PointPairList list = new PointPairList();
        Random ran = new Random();
        LineItem myCurve;


        DataSet ds = new DataSet();
        SqlConnection myConnection = null;
        SqlCommand myCommand = null;
        SqlDataAdapter adapt = null;
        SqlDataReader reader = null;
        public Form1()
        {
            InitializeComponent();
        }


        public string GetConnectString(string server, string database, string username, string password)
        {
            return "Server=" + server + ";Database=" + database + ";UID=" + username + ";PWD=" + password;
        }

        public void Exc()
        {
            double x, y;
            myConnection = new SqlConnection();
            myConnection.ConnectionString = GetConnectString("192.168.0.101", "AMR", "sa", "123");
            myCommand = new SqlCommand("select MSID,UpdateTime from Tab_BS_Dailyfile", myConnection);
            myCommand.CommandType = CommandType.Text;
            myConnection.Open();

            adapt = new SqlDataAdapter();

            try
            {
                //myCommand.ExecuteNonQuery();

                reader = myCommand.ExecuteReader();
                while (reader.Read())
                {
                    for (int i = 0; i < reader.FieldCount; )
                    {
                        y = double.Parse(reader[i++].ToString());
                        x = (double)new XDate(DateTime.Parse(reader[i++].ToString()));
                        list.Add(x, y);
                    }
                }

            }
            catch (SqlException ex)
            {
                MessageBox.Show("插入错误!" + ex.Message.ToString());
            }
            myCommand.Clone();
            myConnection.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.zedGraphControl1.GraphPane.Title.Text = "动态折线图";

            this.zedGraphControl1.GraphPane.XAxis.Title.Text = "时间";
            this.zedGraphControl1.GraphPane.X2Axis.Scale.FontSpec.Size = 28;



            this.zedGraphControl1.GraphPane.YAxis.Title.Text = "数量";
            this.zedGraphControl1.GraphPane.YAxis.Scale.FontSpec.Size = 8;


            myPan.XAxis.Scale.IsVisible = true;


            this.zedGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.DateAsOrdinal;


            Exc();

            DateTime dt = DateTime.Now;



            myCurve = zedGraphControl1.GraphPane.AddCurve("My Curve",

                    list, Color.DarkGreen, SymbolType.None);



            this.zedGraphControl1.AxisChange();

            this.zedGraphControl1.Refresh();

        }