using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using ZedGraph;namespace vs_word_excel
{
    public partial class FrmZedGraph : DockContent
    {
        int tickStart = 0;
        int tickStart2 = 0;
        int zQ1 = 0;
        int zQ2 = 0;
        int fZ1 = 0;
        int fZ2 = 0;
        int pY1 = 0;
        int pY2 = 0;        public FrmZedGraph()
        {
            InitializeComponent();
             this.ControlBox = false;            
        }        public void createPane(ZedGraphControl zgc)
        {            GraphPane myPane = zgc.GraphPane;            //设置图标标题和x、y轴标题            myPane.Title.Text = "机票波动情况";            myPane.XAxis.Title.Text = "波动日期";            myPane.YAxis.Title.Text = "机票价格";            //更改标题的字体            FontSpec myFont = new FontSpec("Arial", 20, Color.Green, false, false, false);            myPane.Title.FontSpec = myFont;            myPane.XAxis.Title.FontSpec = myFont;            myPane.YAxis.Title.FontSpec = myFont;            // 造一些数据,PointPairList里有数据对x,y的数组            Random y = new Random();            PointPairList list1 = new PointPairList();            PointPairList list2 = new PointPairList();            for (int i = 0; i < 36; i++)
            {                double x = i;                double y1 = y.NextDouble() * 1000;                double y2 = y.NextDouble() * 500;                list1.Add(x, y1); //添加一组数据                list2.Add(x, y2);
            }            LineItem myCurve = myPane.AddCurve("line1", list1, Color.Green, SymbolType.Star);
            myPane.AddCurve("line2", list2, Color.Red, SymbolType.Star);            //填充图表颜色            myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);            //以上生成的图标X轴为数字,下面将转换为日期的文本            string[] labels = new string[36];            for (int i = 0; i < 36; i++)
            {                labels[i] = System.DateTime.Now.AddDays(i).ToShortDateString();            }            myPane.XAxis.Scale.TextLabels = labels; //X轴文本取值            myPane.XAxis.Type = AxisType.Text;   //X轴类型            //画到zedGraphControl1控件中,此句必加            zgc.AxisChange();//在数据变化时绘图            //更新图表            zedGraphControl1.Invalidate();            //重绘控件            Refresh();        }        private void updateGraph()
        {
            Random y = new Random();            PointPairList list1 = new PointPairList();            PointPairList list2 = new PointPairList();
            GraphPane myPane = zedGraphControl1.GraphPane;            for (int i = 0; i < 36; i++)
            {                double x = i;                double y1 = y.NextDouble() * 1000;                double y2 = y.NextDouble() * 500;                list1.Add(x, y1); //添加一组数据                list2.Add(x, y2);
            }            myPane.AddCurve("line1", list1, Color.Green, SymbolType.Star);            myPane.AddCurve("line2", list2, Color.Red, SymbolType.Star);            //填充图表颜色
            myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);            //画到zedGraphControl1控件中,此句必加            zedGraphControl1.AxisChange();//在数据变化时绘图            //更新图表            zedGraphControl1.Invalidate();            //重绘控件            Refresh();
        }        private void FrmZedGraph_Load(object sender, EventArgs e)
        {
            createPane(zedGraphControl1);            timer1Draw.Interval = 500;
            timer1Draw.Start();
        }        private void timer1Draw_Tick(object sender, EventArgs e)
        {
            updateGraph();
        }    }
}不知道怎样清空所有点,点 越描越多

解决方案 »

  1.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using WeifenLuo.WinFormsUI.Docking;
    using ZedGraph;namespace vs_word_excel
    {
        public partial class FrmZedGraph : DockContent
        {
            int tickStart = 0;
            int tickStart2 = 0;
            int zQ1 = 0;
            int zQ2 = 0;
            int fZ1 = 0;
            int fZ2 = 0;
            int pY1 = 0;
            int pY2 = 0;        public FrmZedGraph()
            {
                InitializeComponent();
                 this.ControlBox = false;            
            }        public void createPane(ZedGraphControl zgc)
            {
                //获取引用
                GraphPane myPane = zedGraphControl1.GraphPane;
                //设置标题
                myPane.Title.Text = "实时曲线";
                //设置X轴说明文字
                myPane.XAxis.Title.Text = "时间";
                //设置Y轴说明文字
                myPane.YAxis.Title.Text = "温度";            //设置1200个点,假设每50毫秒更新一次,刚好检测1分钟,一旦构造后将不能更改这个值
                RollingPointPairList list = new RollingPointPairList(1200);            //开始,增加的线是没有数据点的(也就是list为空)
                //增加一条名称:Voltage,颜色Color.Bule,无符号,无数据的空线条
                LineItem curve = myPane.AddCurve("温度", list, Color.Blue, SymbolType.None);            timer1Draw.Interval = 50; //设置timer控件的间隔为50毫秒
                timer1Draw.Enabled = true; //timer可用
                timer1Draw.Start(); //开始            myPane.XAxis.Scale.Min = 0; //X轴最小值0
                myPane.XAxis.Scale.Max = 30; //X轴最大30
                myPane.XAxis.Scale.MinorStep = 1;//X轴小步长1,也就是小间隔
                myPane.XAxis.Scale.MajorStep = 5;//X轴大步长为5,也就是显示文字的大间隔            //改变轴的刻度
                zedGraphControl1.AxisChange();            //保存开始时间
                tickStart = Environment.TickCount;
            }
            private UInt32 direct = 0;
            private void updateGraph()
            {
                //确保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 (list == null)
                {
                    return;
                }            // 时间用秒表示
                double time = (Environment.TickCount - tickStart) / 1000.0;
                
                list.Add(time, Math.Sin(2.0 * Math.PI * time/10 )+direct*1);
                Console.WriteLine(time.ToString());            Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;
                //if (time > xScale.Max - xScale.MajorStep)
                //{
                //    xScale.Max = time + xScale.MajorStep;
                //    xScale.Min = xScale.Max - 30.0;
                //}            if (time > xScale.Max )
                {
                    xScale.Max = time ;
                    xScale.Min = xScale.Max - 30.0;
                }            //第三步:调用ZedGraphControl.AxisChange()方法更新X和Y轴的范围
                zedGraphControl1.AxisChange();            //第四步:调用Form.Invalidate()方法更新图表
                zedGraphControl1.Invalidate();
            }        private void FrmZedGraph_Load(object sender, EventArgs e)
            {
                createPane(zedGraphControl1);            //timer1Draw.Interval = 500;
                //timer1Draw.Start();
            }        private void timer1Draw_Tick(object sender, EventArgs e)
            {
                updateGraph();
            }        private void button1_Click(object sender, EventArgs e)
            {
                direct++;
            }    }
    }
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using WeifenLuo.WinFormsUI.Docking;
    using ZedGraph;namespace vs_word_excel
    {
        public partial class FrmZedGraph : DockContent
        {
            int tickStart = 0;        public FrmZedGraph()
            {
                InitializeComponent(); 
            }        /// <summary>
            /// zedGraph直流分量,测试自动缩放功能使用
            /// </summary>
            private int m_dcValue = 0;
            
            /// <summary>
            /// x 轴范围
            /// </summary>
            private int m_xScale = 400;
            /// <summary>
            /// 坐标点数
            /// </summary>
            private UInt32 m_xTick = 0;        /// <summary>
            /// 初始化zedGraph图形控件
            /// </summary>
            /// <param name="zed"></param>
            public void initZedGraph(Timer tm, ZedGraphControl zed)
            {
                //获取引用
                GraphPane myPane = zed.GraphPane;
                //设置标题
                myPane.Title.Text = "实时曲线";
                //设置X轴说明文字
                myPane.XAxis.Title.Text = "时间";
                //设置Y轴说明文字
                myPane.YAxis.Title.Text = "温度";            //设置1200个点,假设每50毫秒更新一次,刚好检测1分钟,一旦构造后将不能更改这个值
                RollingPointPairList list1 = new RollingPointPairList(m_xScale);
                RollingPointPairList list2 = new RollingPointPairList(m_xScale);            //开始,增加的线是没有数据点的(也就是list为空)            //增加第一条曲线,名称 温度1,颜色Color.Bule,无符号,无数据的空线条
                LineItem curve1 = myPane.AddCurve("温度1", list1, Color.Blue, SymbolType.None);
                //增加第二条曲线,名称 温度2
                LineItem curve2 = myPane.AddCurve("温度2", list2, Color.HotPink, SymbolType.None);            tm.Interval = 10; //设置timer控件的间隔为50毫秒
                tm.Enabled = true; //timer可用
                tm.Start(); //开始            myPane.XAxis.Scale.Min = 0; //X轴最小值0
                myPane.XAxis.Scale.Max = m_xScale; //X轴最大30
                myPane.XAxis.Scale.MinorStep = 1;//X轴小步长1(小刻度)
                myPane.XAxis.Scale.MajorStep = 50;//X轴大步长为5,大刻度,也就是显示文字的大间隔            //改变轴的刻度
                zed.AxisChange();            //保存开始时间
                tickStart = Environment.TickCount;
            }
            /// <summary>
            /// 刷新zedGraph曲线
            /// </summary>
            /// <param name="zed"></param>
           
            private void updateZedGraph(ZedGraphControl zed)
            {
                //确保CurveList不为空
                if (zed.GraphPane.CurveList.Count <= 0)
                {
                    return;
                }            //取Graph第一个曲线,也就是第一步:在GraphPane.CurveList集合中查找CurveItem
                LineItem curve1 = zed.GraphPane.CurveList[0] as LineItem;            //取Graph第二个曲线,也就是第一步:在GraphPane.CurveList集合中查找CurveItem
                LineItem curve2 = zed.GraphPane.CurveList[1] as LineItem;            if (curve1 == null || curve2 == null )
                {
                    return;
                }            //第二步:在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据
                IPointListEdit list1 = curve1.Points as IPointListEdit;            IPointListEdit list2 = curve2.Points as IPointListEdit;            if (list1 == null || list2 == null)
                {
                    return;
                }            list1.Add(m_xTick, Math.Sin(2.0 * Math.PI * m_xTick / 100) + m_dcValue * 1);
                list2.Add(m_xTick, Math.Cos(2.0 * Math.PI * m_xTick / 100) + m_dcValue * 1);            m_xTick++;            Scale xScale = zed.GraphPane.XAxis.Scale;
                //if (tick > xScale.Max - xScale.MajorStep)
                //{
                //    xScale.Max = tick + xScale.MajorStep;
                //    xScale.Min = xScale.Max - 30.0;
                //}
                if (m_xTick > xScale.Max)
                {
                    xScale.Max = m_xTick;
                    xScale.Min = xScale.Max - m_xScale;
                }            //第三步:调用ZedGraphControl.AxisChange()方法更新X和Y轴的范围
                zed.AxisChange();            //第四步:调用Form.Invalidate()方法更新图表
                zed.Invalidate();
            }        private void FrmZedGraph_Load(object sender, EventArgs e)
            {
                initZedGraph(timer1Draw, zedGraphControl1);
            }        private void timer1Draw_Tick(object sender, EventArgs e)
            {
                updateZedGraph(zedGraphControl1);
            }        private void button1_Click(object sender, EventArgs e)
            {
                m_dcValue++;
            }    }
    }
    自动缩放,ok了