ZedGraph绘图

一、下载及配置

下载ZedGraph 

官网下载地址   http://sourceforge.net/projects/zedgraph/files/ 

添加 ZedGraph.dll 和ZedGraph.Web.dll的引用 在控件库中添加ZedGraph控件 

右键点击工具箱 - 选择项 - .Net Framework 组件 - 浏览 - 找到ZedGraph.dll 和ZedGraph.Web.dll添加 zedGraphControl 控件就出现在工具箱中 

 

二、绘制折线图及添加标注

string sql = string.Format("select * from 成绩表”);

DataTable data =SqlProxy.GetData(sql).Tables[0]);

 

 // Get a reference to the GraphPane instance in the ZedGraphControl

 GraphPane myPane = zg1.GraphPane;

 

 // Set the titles and axis labels

 myPane.Title.Text = string.Format("张望的成绩变化曲线");

 myPane.XAxis.Title.Text = "测试次数";

 myPane.YAxis.Title.Text = "得分";

 // Make up some data points based on the Sine function

 string[] titles = new string[5] { "3000米跑", "单杠引体向上", "双杠杠端臂屈伸", "仰卧起坐", "5.5公斤

 Color[] colors = new System.Drawing.Color[] { Color.Red, Color.Black, Color.Blue, Color.Green, Color

 for (int i = 0; i < data.Columns.Count; i++)

 {

     PointPairList list = new PointPairList();

     for (int j = 0; j < data.Rows.Count; j++)

     {

         double x = j + 1;

         double y = double.Parse(data.Rows[j][i].ToString());

         list.Add(x, y);

     }

     LineItem myCurve = myPane.AddCurve(titles[i], list, colors[i], SymbolType.Diamond);

     for (int tt = 0; tt < data.Rows.Count; tt++)

     {//为每个点添加标注(标注信息为纵坐标值)

         PointPair pp = myCurve.Points[tt];

         TextObj text = new TextObj(pp.Y.ToString("f2"), pp.X, pp.Y, CoordType.AxisXYScale,

             AlignH.Left, AlignV.Center);

         text.ZOrder = ZOrder.A_InFront;

         text.FontSpec.Border.IsVisible = false;

         text.FontSpec.Fill.IsVisible = false;

         text.FontSpec.Angle = 60;//倾斜60度

         myPane.GraphObjList.Add(text);

     }

     // Fill the symbols with white

     myCurve.Symbol.Fill = new Fill(Color.White);

 }

 

// Make the Y axis scale red

 myPane.YAxis.Scale.FontSpec.FontColor = Color.Red;

 myPane.YAxis.Title.FontSpec.FontColor = Color.Red;

 // turn off the opposite tics so the Y tics don't show up on the Y2 axis

 myPane.YAxis.MajorTic.IsOpposite = false;

 myPane.YAxis.MinorTic.IsOpposite = false;

 // Don't display the Y zero line

 myPane.YAxis.MajorGrid.IsZeroLine = false;

 // Align the Y axis labels so they are flush to the axis

 myPane.YAxis.Scale.Align = AlignP.Inside;

 // Manually set the axis range

 myPane.YAxis.Scale.Min = 0;//纵轴显示最小值

 myPane.YAxis.Scale.Max = 30; //纵轴显示最大值

 myPane.YAxis.Scale.MajorStep = 1;//纵轴大刻度步长

 myPane.XAxis.Scale.Min = 1;//横轴最小值

 myPane.XAxis.Scale.Max = data.Rows.Count;//横轴最大值

 myPane.XAxis.Scale.MajorStep = 1;//横轴大刻度步长

 myPane.XAxis.MinorGrid.IsVisible = false;

 myPane.XAxis.MajorGrid.IsVisible = false;

 // Enable the Y2 axis display

 myPane.Y2Axis.IsVisible = false;

 

 myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f);

 

zg1.IsShowHScrollBar = false;//不显示水平滚动条

zg1.IsShowVScrollBar = false;//不显示垂直滚动条

zg1.IsAutoScrollRange = false;

zg1.IsShowPointValues = false;

SetSize();

 

zg1.AxisChange();//重新绘制坐标(当设置坐标轴后必须调用此函数)

zg1.Invalidate();

 

 

三、多个ZedGraphControl绘折线图

 

代码如下:

string sql = string.Format("select * from 数据表 where 工厂='A' and 时间>=100 and 时间<=200 and 数据类别='监测点数据' order by 时间 asc ");

 DataTable data = access.SelectData(sql).Tables[0];

string[] titles = new string[] {"Turbidity:0.5NTU","Oxidation Reduction Potential:664mV","pH:8.06",

 "Electrical Conductivity:858μS/cm","Temperature:29.1℃"};

 string[] yText = new string[] {"NTU","ORP","pH","EC","Temp" };

 Color[] colors = new System.Drawing.Color[] { Color.Red, Color.Black, Color.Blue, Color.Green, Color.HotPink };

for (int col_index = 0; col_index < N; col_index++)

 {//依次获取各个ZedGraphControl控件

     ZedGraphControl zgc = this.Controls.Find("zedGraphControl" + col_index, false)[0] as ZedGraphControl;

     zgc.GraphPane.CurveList.Clear();//清空已有图形(下面三行代码也是必不可少)

     zgc.GraphPane.GraphObjList.Clear();

     zgc.AxisChange();

     zgc.Refresh();

     GraphPane myPane = zgc.GraphPane;

     //zgc.Font = new Font("宋体",50);

     myPane.Legend.IsVisible = false;

     // Set the titles and axis labels

     myPane.Title.Text = titles[col_index];

     myPane.XAxis.Title.Text = "Time (s)";

     myPane.YAxis.Title.Text = yText[col_index];

   

     PointPairList list = new PointPairList();

     for (int j = 0; j < data.Rows.Count; j++)

     {

         double x =xMin+ j*X_STEP;

         double y = double.Parse(data.Rows[j][col_index].ToString());

         list.Add(x, y);

     }

     LineItem myCurve = myPane.AddCurve(titles[col_index], list, colors[col_index], SymbolType.Diamond);

    

     myCurve.Symbol.Fill = new Fill(Color.White);

     myPane.YAxis.Scale.FontSpec.FontColor = Color.Red;

     myPane.YAxis.Title.FontSpec.FontColor = Color.Red;

    

     myPane.YAxis.MajorTic.IsOpposite = false;

     myPane.YAxis.MinorTic.IsOpposite = false;

    

     myPane.YAxis.MajorGrid.IsZeroLine = false;

    

     myPane.YAxis.Scale.Align = AlignP.Inside;

 

     myPane.YAxis.Scale.Min = 100;

     myPane.YAxis.Scale.Max = 200;

   

     //myPane.YAxis.Scale.MinorStep = myPane.YAxis.Scale.MajorStep;

     myPane.YAxis.MajorGrid.IsVisible = true;

     

     myPane.XAxis.Scale.Min = 0;

     myPane.XAxis.Scale.Max = 100;

     myPane.XAxis.Scale.MajorStep = 12;

     myPane.XAxis.Scale.MinorStep = 12;

    

     myPane.XAxis.MajorGrid.IsVisible = true;

    

     myPane.Y2Axis.IsVisible = false;

     myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f);

     zgc.IsShowHScrollBar = false;

     zgc.IsShowVScrollBar = false;

     zgc.IsAutoScrollRange = false;

     zgc.IsShowPointValues = false;

     //SetSize();

     myPane.IsFontsScaled = false;//必须设置成false,否则设置字体无效

     myPane.Title.FontSpec.Size = 12;

     myPane.XAxis.Title.FontSpec.Size = 12;

     myPane.XAxis.Scale.FontSpec.Size = 12;

     myPane.YAxis.Scale.FontSpec.Size = 12;

     myPane.YAxis.Title.FontSpec.Size = 12;

     zgc.AxisChange();

     zgc.Invalidate();

    

 }

 

猜你喜欢

转载自blog.csdn.net/huzhizhewudi/article/details/84347877