public Form1()
{
InitializeComponent();
CreateGraph(zedGraphControl2);// 1. 调用CreateGraph 方法
}
public void CreateGraph(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;//2.得到画板
myPane.Title.Text = "曲线图";
myPane.XAxis.Title.Text = "时间(S)";
myPane.YAxis.Title.Text = "电压(mV)";
PointPairList list1 = new PointPairList();//3.添加一条线
string sql = "select col1,col2 from testztq";
string sqlconn = "Server=(local);Database=myztq;User ID=sa;Password=123; ";
using (SqlConnection conn = new SqlConnection(sqlconn))
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
myPane.XAxis.Scale.Max = dt.Rows.Count;
foreach (DataRow dr in dt.Rows)
{
list1.Add(double.Parse(dr["col1"].ToString()), double.Parse(dr["col2"].ToString())); //4.给线加上点
}
}
conn.Close();
}
LineItem myCurve = myPane.AddCurve("", list1, Color.Red, SymbolType.XCross); //5.第一条曲线绘制
zgc.AxisChange(); //6.重新绘制轴范围
zedGraphControl2.Invalidate(); //7.更新图标
}
}