www.pudn.com > ZedGraph.zip > Form1.cs, change:2013-10-15,size:9522b


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using ZedGraph; 
 
namespace 显示实时曲线 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
        int tickStart = 0; 
        private PointPairList list1 = new PointPairList(); 
        private PointPairList list2 = new PointPairList(); 
 
        LineItem curve1; 
        LineItem curve2; 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            //获取引用 
            GraphPane myPane = zedGraphControl1.GraphPane; 
            curve1 = myPane.AddCurve("曲线1", list1, Color.Blue, SymbolType.None); 
            curve2 = myPane.AddCurve("曲线2", list2, Color.Green, SymbolType.None); 
            //设置标题 
            myPane.Title.Text = "实时曲线"; 
            //设置X轴说明文字 
            myPane.XAxis.Title.Text = "时间"; 
            //设置Y轴说明文字 
            myPane.YAxis.Title.Text = "温度"; 
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f); 
            //myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f); 
            //设置1200个点,假设每50毫秒更新一次,刚好检测1分钟,一旦构造后将不能更改这个值 
            //RollingPointPairList list1 = new RollingPointPairList(2400); 
            //RollingPointPairList list2 = new RollingPointPairList(2400); 
            //开始,增加的线是没有数据点的(也就是list为空) 
            //增加一条名称:Voltage,颜色Color.Bule,无符号,无数据的空线条 
 
            timer1.Interval = 1;	//设置timer控件的间隔为50毫秒 
            timer1.Enabled = true;	//timer可用 
            timer1.Start();			//开始 
            myPane.Y2Axis.IsVisible = true; 
            myPane.Y2Axis.Scale.Align = AlignP.Inside; 
            myPane.Y2Axis.MajorTic.IsOpposite = false; 
            myPane.Y2Axis.MinorTic.IsOpposite = false; 
            myPane.XAxis.Scale.Format = "dd  HH:mm:ss";   //DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")  
            myPane.XAxis.Type = ZedGraph.AxisType.DateAsOrdinal; 
            myPane.XAxis.Scale.Min = 0;		//X轴最小值0 
            myPane.XAxis.Scale.Max = 5;	//X轴最大30 
            //myPane.XAxis.Scale.MinorStep = 0.02;//X轴小步长1,也就是小间隔 
            //myPane.XAxis.Scale.MajorStep = 0.1;//X轴大步长为5,也就是显示文字的大间隔 
            myPane.XAxis.MajorGrid.IsVisible = true;//设置X虚线  
            myPane.YAxis.MajorGrid.IsVisible = true;//设置Y虚线 
            //改变轴的刻度 
            zedGraphControl1.AxisChange(); 
            // Show the x axis grid 
            //   myPane.XAxis.MajorGrid.IsVisible = true; 
            //    myPane.YAxis.MajorTic.IsOpposite = true; 
            //  myPane.YAxis.MinorTic.IsOpposite = true; 
            // Don't display the Y zero line 
            //保存开始时间 
            tickStart = Environment.TickCount; 
 
            zedGraphControl1.IsShowPointValues = true; 
            zedGraphControl1.PointValueEvent += new ZedGraphControl.PointValueHandler(MyPointValueHandler); 
            // OPTIONAL: Add a custom context menu item 
            //   zedGraphControl1.ContextMenuBuilder += new ZedGraphControl.ContextMenuBuilderEventHandler(MyContextMenuBuilder); 
 
            // OPTIONAL: Handle the Zoom Event 
            zedGraphControl1.ZoomEvent += new ZedGraphControl.ZoomEventHandler(MyZoomEvent); 
            zedGraphControl1.AxisChange(); 
            // Make sure the Graph gets redrawn 
            zedGraphControl1.Invalidate(); 
        } 
        private int timerDrawI = 0; 
        private void timer1_Tick(object sender, EventArgs e) 
        { 
            //确保CurveList不为空 
            if (zedGraphControl1.GraphPane.CurveList.Count <= 0) 
            { 
                return; 
            } 
 
            //取Graph第一个曲线,也就是第一步:在GraphPane.CurveList集合中查找CurveItem 
            LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem; 
            if (curve == null) 
            { 
                return; 
            } 
 
            //第二步:在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据 
            IPointListEdit list = curve.Points as IPointListEdit; 
            // If this is null, it means the reference at curve.Points does not 
            // support IPointListEdit, so we won't be able to modify it 
            if (list == null) 
            { 
                return; 
            } 
 
            // Time is measured in seconds 
            double x = (double)new XDate(DateTime.Now.AddSeconds(100.00)); 
 
            // 3 seconds per cycle 
            list1.Add(x, (double)Math.Sin(timerDrawI / 10) * 12); 
            list2.Add(x, (double)Math.Sin(timerDrawI / 10f)); 
            // list.Add(time, Math.Sin(2.0 * Math.PI * time / 5.0)); 
            timerDrawI++; 
            // Keep the X scale at a rolling 30 second interval, with one 
            // major step between the max X value and the end of the axis 
            Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale; 
            //if ( time > xScale.Max - xScale.MajorStep ) 
            //{ 
            //    xScale.Max = time + xScale.MajorStep; 
            //    xScale.Min = xScale.Max - 30.0; 
            //} 
 
            //第三步:调用ZedGraphControl.AxisChange()方法更新X和Y轴的范围 
            zedGraphControl1.AxisChange(); 
 
            //第四步:调用Form.Invalidate()方法更新图表 
            zedGraphControl1.Invalidate(); 
            if (list.Count >= 100) 
            { 
                list.RemoveAt(0); 
                list2.RemoveAt(0); 
            } 
        } 
 
        /// <summary> 
        /// Display customized tooltips when the mouse hovers over a point 
        /// </summary> 
        private string MyPointValueHandler(ZedGraphControl control, GraphPane pane, 
                        CurveItem curve, int iPt) 
        { 
            // Get the PointPair that is under the mouse 
            PointPair pt = curve[iPt]; 
 
            return curve.Label.Text + " is " + pt.Y.ToString("f2") + " units at " + pt.X.ToString("f1") + " days"; 
        } 
        /// <summary> 
        /// Customize the context menu by adding a new item to the end of the menu 
        /// </summary> 
        //private void MyContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip, 
        //                Point mousePt) 
        //{ 
        //    ToolStripMenuItem item = new ToolStripMenuItem(); 
        //    item.Name = "add-beta"; 
        //    item.Tag = "add-beta"; 
        //    item.Text = "Add a new Beta Point"; 
        //    item.Click += new System.EventHandler(AddBetaPoint); 
 
        //    menuStrip.Items.Add(item); 
        //} 
 
        /// <summary> 
        /// Handle the "Add New Beta Point" context menu item.  This finds the curve with 
        /// the CurveItem.Label = "Beta", and adds a new point to it. 
        /// </summary> 
        private void AddBetaPoint(object sender, EventArgs args) 
        { 
            // Get a reference to the "Beta" curve IPointListEdit 
            IPointListEdit ip = zedGraphControl1.GraphPane.CurveList["Beta"].Points as IPointListEdit; 
            if (ip != null) 
            { 
                double x = ip.Count * 5.0; 
                double y = Math.Sin(ip.Count * Math.PI / 15.0) * 16.0 * 13.5; 
                ip.Add(x, y); 
                zedGraphControl1.AxisChange(); 
                zedGraphControl1.Refresh(); 
            } 
        } 
 
        // Respond to a Zoom Event 
        private void MyZoomEvent(ZedGraphControl control, ZoomState oldState, 
                    ZoomState newState) 
        { 
            // Here we get notification everytime the user zooms 
        } 
 
 
        private void Form1_Resize(object sender, EventArgs e) 
        { 
            SetSize(); 
        } 
 
        // Set the size and location of the ZedGraphControl 
        private void SetSize() 
        { 
            // Control is always 10 pixels inset from the client rectangle of the form 
            Rectangle formRect = this.ClientRectangle; 
            formRect.Inflate(-10, -10); 
 
            if (zedGraphControl1.Size != formRect.Size) 
            { 
                zedGraphControl1.Location = formRect.Location; 
                zedGraphControl1.Size = formRect.Size; 
            } 
        } 
 
        private void checkBox1_CheckedChanged(object sender, EventArgs e) 
        { 
            if (checkBox1.Checked == true) 
            { 
 
                curve2.IsVisible = true; 
            } 
            else 
            { 
                //否则文本框不可以用 
                curve2.IsVisible = false; 
            } 
        } 
 
        private void checkBox2_CheckedChanged(object sender, EventArgs e) 
        { 
            if (checkBox2.Checked == true) 
            { 
 
                curve1.IsVisible = true; 
            } 
            else 
            { 
                //否则文本框不可以用 
                curve1.IsVisible = false; 
            } 
        } 
    } 
}