ZEDGRAPH画图心得

  1  OleDbConnection odcConnection = new OleDbConnection(MyConnectionString); //打开连接 C#操作Access之按列读取mdb   
  2             odcConnection.Open();    
  3             OleDbCommand odCommandValue = odcConnection.CreateCommand();//value
  4             OleDbCommand odCommandYear = odcConnection.CreateCommand();//year
  5 
  6             odCommandValue.CommandText = "select 全年平均 from 洱海各月出流流量"; //读取全年平均项,建立读取 C#操作Access之按列读取mdb   
  7             odCommandYear.CommandText = "select 年 from 洱海各月出流流量"; //读取year
  8 
  9             OleDbDataReader odrReaderValue = odCommandValue.ExecuteReader();//建立SQL查询value
 10             OleDbDataReader odrReaderYear = odCommandYear.ExecuteReader();//建立SQL查询year
 11 
 12             //ArrayList ArrValue = new ArrayList();//定义动态数组ArrValue
 13             //ArrayList ArrYear = new ArrayList();//定义动态数组ArrYear
 14 
 15             //double []Yearavrg;//数组定义
 16             //Yearavrg = new double[100];//初始化大小,分配内存
 17             //double []Year;//数组定义
 18             //Year = new double[100];
 19 
 20             double[] ArrValue;//数组定义
 21             ArrValue = new double[100];//初始化大小,分配内存
 22             double[] ArrYear;//数组定义
 23             ArrYear = new double[100];
 24             int im = 0, jm = 0;
 25             while (odrReaderValue.Read())
 26             {
 27                //ArrValue.Add(odrReaderValue["全年平均"].ToString());//读取列的每一个
 28                 ArrValue[im] = Convert.ToDouble(odrReaderValue["全年平均"]);
 29                 im++;
 30             }
 31             while (odrReaderYear.Read())
 32             {
 33                 //ArrYear.Add(odrReaderYear["年"].ToString());//读取列的每一个
 34                 ArrYear[jm] = Convert.ToDouble(odrReaderYear[""]);
 35                 jm++;
 36             }
 37 
 38             odrReaderValue.Close();
 39             odrReaderYear.Close();
 40             odcConnection.Close();
 41             //test
 42             //int leng = ArrValue.Count;//计算数组长度
 43             int leng = ArrValue.Length;
 44 
 45            // MessageBox.Show(leng.ToString());//test
 46             GraphPane MyPane=zedGraphControl1.GraphPane;
 47             MyPane.Title.Text = "洱海年平均出流流量";
 48             MyPane.XAxis.Title.Text = "年份";
 49             MyPane.YAxis.Title.Text = "流量(立方米每秒)";
 50 
 51             //
 52             PointPairList pplist = new PointPairList();//显示点数组
 53             //test-OK
 54             foreach (double a in ArrValue)
 55             {
 56 
 57 
 58                 textBox1.Text += a;
 59                 //pplist.Add(0,b);
 60 
 61 
 62             }
 63 
 64             //test-OK
 65             //foreach (string a in ArrYear)
 66             //foreach (string a in ArrValue)
 67             //{
 68             //    //MessageBox.Show(a);
 69             //    double b = double.Parse(a);//类型转换
 70             //    textBox1.Text += b;//test
 71             //}
 72 
 73             //foreach (string a in ArrValue)
 74             //{
 75             //    //MessageBox.Show(a);
 76             //    int i = 0;
 77             //    Yearavrg[i] = double.Parse(a);//类型转换
 78             //    i++;
 79                 
 80             //}
 81 
 82             //foreach (string b in ArrYear)
 83             //{
 84             //    //MessageBox.Show(a);
 85             //    int i = 0;
 86             //    Year[i] = double.Parse(b);//类型转换
 87             //    i++;
 88             //    textBox1.Text+=Year[i].ToString();
 89 
 90             //}
 91 
 92 
 93             for (int i = 0; i < leng; i++)
 94             {
 95 
 96                 pplist.Add(ArrYear[i], ArrValue[i]);//xaile是年,yaile是年平均流量值
 97             }
 98 
 99 
100             LineItem myCurve = MyPane.AddCurve("流量",pplist,Color.Red,SymbolType.Diamond);//标注样式
101 
102             myCurve.Symbol.Fill = new Fill(Color.White);//背景填充白色
103             MyPane.XAxis.MajorGrid.IsVisible = true;//显示X网格
104             MyPane.YAxis.MajorGrid.IsZeroLine = true;//Y坐标不显示0坐标线
105 
106             // Align the Y axis labels so they are flush to the axis
107             MyPane.YAxis.Scale.Align = AlignP.Inside;
108            
109             //坐标范围划定
110             MyPane.YAxis.Scale.Min = -20;
111             MyPane.YAxis.Scale.Max = 70;
112 
113             MyPane.XAxis.Scale.Min = 1950;
114             MyPane.XAxis.Scale.Max = 2016;
115 
116             MyPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f);
117 
118 
119             // Add a text box with instructions
120             TextObj text = new TextObj(
121                 "左键拖拽放大
鼠标中键滚放缩
右键菜单",
122                 0.05f, 0.95f, CoordType.ChartFraction, AlignH.Left, AlignV.Bottom);
123             text.FontSpec.StringAlignment = StringAlignment.Near;
124             MyPane.GraphObjList.Add(text);
125 
126             zedGraphControl1.IsShowHScrollBar = true;
127             zedGraphControl1.IsShowVScrollBar = true;
128             zedGraphControl1.IsAutoScrollRange = true;
129 
130             zedGraphControl1.IsShowPointValues = true;
131             zedGraphControl1.PointValueEvent += new ZedGraphControl.PointValueHandler(MyPointValueHandler);
132             // OPTIONAL: Add a custom context menu item
133            // zedGraphControl1.ContextMenuBuilder += new ZedGraphControl.ContextMenuBuilderEventHandler(MyContextMenuBuilder);
134 
135             // OPTIONAL: Handle the Zoom Event
136            // zedGraphControl1.ZoomEvent += new ZedGraphControl.ZoomEventHandler(MyZoomEvent);
137 
138             // Tell ZedGraph to calculate the axis ranges
139             // Note that you MUST call this after enabling IsAutoScrollRange, since AxisChange() sets
140             // up the proper scrolling parameters
141             zedGraphControl1.AxisChange();
142             // Make sure the Graph gets redrawn
143             zedGraphControl1.Invalidate();
144         }
145         /// <summary>
146         /// Display customized tooltips when the mouse hovers over a point
147         /// </summary>
148         private string MyPointValueHandler(ZedGraphControl control, GraphPane pane,CurveItem curve, int iPt)
149         {
150             // Get the PointPair that is under the mouse
151             PointPair pt = curve[iPt];
152 
153            // return curve.Label.Text + " is " + pt.Y.ToString("f2") + " m3/s at " + pt.X.ToString("f1") + " 年";
154             //return pt.X.ToString("f1") + "年"+curve.Label.Text + "是" + pt.Y.ToString("f2") + " wm3/s";
155             return pt.X.ToString() + "" + curve.Label.Text + "" + pt.Y.ToString("f2") + " wm3/s";//OKZWJ
156         }
157 
158         /// <summary>
159         /// Customize the context menu by adding a new item to the end of the menu
160         /// </summary>
161         private void MyContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip,
162                         Point mousePt, ZedGraphControl.ContextMenuObjectState objState)
163         {
164             ToolStripMenuItem item = new ToolStripMenuItem();
165             item.Name = "add-beta";
166             item.Tag = "add-beta";
167             item.Text = "Add a new Beta Point";
168             item.Click += new System.EventHandler(AddBetaPoint);
169 
170             menuStrip.Items.Add(item);
171         }
172 
173         /// <summary>
174         /// Handle the "Add New Beta Point" context menu item.  This finds the curve with
175         /// the CurveItem.Label = "Beta", and adds a new point to it.
176         /// </summary>
177         private void AddBetaPoint(object sender, EventArgs args)
178         {
179             // Get a reference to the "Beta" curve IPointListEdit
180             IPointListEdit ip = zedGraphControl1.GraphPane.CurveList["Beta"].Points as IPointListEdit;
181             if (ip != null)
182             {
183                 double x = ip.Count * 5.0;
184                 double y = Math.Sin(ip.Count * Math.PI / 15.0) * 16.0 * 13.5;
185                 ip.Add(x, y);
186                 zedGraphControl1.AxisChange();
187                 zedGraphControl1.Refresh();
188             }
189         }
190 
191         // Respond to a Zoom Event
192         private void MyZoomEvent(ZedGraphControl control, ZoomState oldState,ZoomState newState)
193         {
194             // Here we get notification everytime the user zooms
195         }
196 
197 
198 
199 
200     }
View Code

调试蛮辛苦的,尝试了很多次,各种改变,最后终于可以了!!!

原文地址:https://www.cnblogs.com/yuhuameng/p/3657821.html