110,569
社区成员
发帖
与我相关
我的任务
分享
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();
}
/// <summary>
/// 动起来
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
zedGraphControl1.GraphPane.XAxis.Scale.MaxAuto = true;
double x = (double)new XDate(DateTime.Now);
double y = ran.NextDouble();
list.Add(x, y);//改成你数据库的数据就可以了
list.RemoveAt(0);
this.zedGraphControl1.AxisChange();
this.zedGraphControl1.Refresh();
}
private void DrawCurves(Graphics grp, List<Point> pointList)
{
Point[] temps = new Point[pointList.Count];
for (int i = 0; i < pointList.Count; i++)
{
temps[i] = pointList[i];
}
grp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
grp.DrawCurve(new Pen(Color.Red, 2), temps);
grp.Dispose();
}