首页 > 代码库 > ZedGraph

ZedGraph

折线图实例:http://blog.csdn.net/dengta_snowwhite/article/details/6129222
牛人总结: http://blog.csdn.net/tjvictor/article/category/257827

 

 若要定时的向一个图表里添加数据,应按照如下顺序实现:

  1.在GraphPane.CurveList集合中查找CurveItem

  2.在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据

  3.调用ZedGraphControl.AxisChange()方法更新X和Y轴的范围

  4.调用Form.Invalidate()方法更新图表

   /*翻译的不准,大概意思就是找到数据集合后增加/修改数据->修改轴范围->更新图表*/

每个CurveItem里的数据点作为一个IPointList接口的引用存储在CurveItem.Points中.

  注意数据点列表能被任意一个实现了 IPointList的接口的类引用.当然,此类需要同时实现IPoiontListEdit(),IpointListEdit.Add()和 IPontListEdit.RemoveAt()方法.

代码示例为一个ZedGraph控件在WinForm中的实现,用Timer事件动态显示更新数据.

  以下是完整代码(没有VS设计资料).。

  此代码包括两个方法:

    Form_Load()方法用一条没有数据点的曲线初始化图表,

    Timer_Tick()方法响应timer事件增加数据点. 

 1 using System;  2 using System.Collections.Generic;  3 using System.ComponentModel;  4 using System.Data;  5 using System.Drawing;  6 using System.Text;  7 using System.Windows.Forms;  8 using ZedGraph;  9 namespace DynamicData 10 { 11     public partial class Form1 : Form12     { 13     // Starting time in milliseconds 14 15         int tickStart = 0; 16         public Form1() 17         { 18             InitializeComponent(); 19         } 20         private void Form1_Load( object sender, EventArgs e ) 21       { 22       //获取引用 23       GraphPane myPane = zedGraphControl1.GraphPane; 24 25 26         //设置标题 27       myPane.Title.Text = "Test of Dynamic Data Update with ZedGraph " + "(After 25 seconds the graph scrolls)"; 28         //设置X轴说明文字29        myPane.XAxis.Title.Text = "Time, Seconds"; 30         //设置Y轴文字 31       myPane.YAxis.Title.Text = "Sample Potential, Volts"; 32         // Save 1200 points. At 50 ms sample rate, this is one minute 33       // The RollingPointPairList is an efficient storage class that always 34       // keeps a rolling set of point data without needing to shift any data values 35       //设置1200个点,假设每50毫秒更新一次,刚好检测1分钟 36       //一旦构造后将不能更改这个值 37       RollingPointPairList list = new RollingPointPairList( 1200 ); 38     // Initially, a curve is added with no data points (list is empty) // Color is blue, and there will be no symbols 39     //开始,增加的线是没有数据点的(也就是list为空) 40     //增加一条名称:Voltage,颜色Color.Bule,无符号,无数据的空线条 41       LineItem curve = myPane.AddCurve( "Voltage", list, Color.Blue, SymbolType.None ); 42     // Sample at 50ms intervals 43     //设置timer控件的间隔为50毫秒 44       timer1.Interval = 50; 45     //timer可用 46       timer1.Enabled = true; 47     //开始 48       timer1.Start(); 49     // Just manually control the X axis range so it scrolls continuously 50     // instead of discrete step-sized jumps 51     //X轴最小值052        myPane.XAxis.Scale.Min = 0; 53     //X轴最大30 54       myPane.XAxis.Scale.Max = 30; 55     //X轴小步长1,也就是小间隔 myPane.XAxis.Scale.MinorStep = 1; 56     //X轴大步长为5,也就是显示文字的大间隔57        myPane.XAxis.Scale.MajorStep = 5; 58     // Scale the axes59      //改变轴的刻度 60       zedGraphControl1.AxisChange(); 61     // Save the beginning time for reference 62     //保存开始时间 63       tickStart = Environment.TickCount; } 64 65 66 67     private void timer1_Tick( object sender, EventArgs e ) 68     { 69         // Make sure that the curvelist has at least one curve 70         //确保CurveList不为空71       if ( zedGraphControl1.GraphPane.CurveList.Count <= 0 ) return; 
          // Get the first CurveItem in the graph 72         // 取Graph第一个曲线,也就是第一步:在GraphPane.CurveList集合中查找CurveItem LineItem curve = zedGraphControl1.GraphPane.CurveList[0]
  as LineItem; if ( curve == null ) return;
73         // Get the PointPairList 74         //第二步:在CurveItem中访问PointPairList(或者其它的IPointList),根据自己的需要增加新数据或修改已存在的数据 75       IPointListEdit list = curve.Points as IPointListEdit; 76         // If this is null, it means the reference at curve.Points does not77         // support IPointListEdit, so we won‘t be able to modify it if ( list == null ) return; 78         // Time is measured in seconds double time = (Environment.TickCount - tickStart) / 1000.0;
          // 3 seconds per cycle list.Add( time, Math.Sin( 2.0 * Math.PI * time / 3.0 ) ); 79         // Keep the X scale at a rolling 30 second interval, with one 80         // 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; }
          // Make sure the Y axis is rescaled to accommodate actual data 81         //第三步:调用ZedGraphControl.AxisChange()方法更新X和Y轴的范围 zedGraphControl1.AxisChange(); // Force a redraw 82         //第四步:调用Form.Invalidate()方法更新图表 zedGraphControl1.Invalidate();83     } 84     

      private void Form1_Resize( object sender, EventArgs e )85 86     {
        SetSize();
      }
87     // Set the size and location of the ZedGraphControl 88     
  
      private void SetSize() ...89     {90       // Control is always 10 pixels inset from the client rectangle of the form Rectangle formRect = this.ClientRectangle; 91        formRect.Inflate( -10, -10 ); 92        if(zedGraphControl1.Size != formRect.Size )93        { 94          zedGraphControl1.Location = formRect.Location;95         zedGraphControl1.Size = formRect.Size; 96        } 97     }
   }
  }
98

 

另外几个例程:

1、静态作图  

 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using ZedGraph; //添加的10 namespace zedgraph11 {12     public partial class Form1 : Form13     {14         public Form1()15         {16             InitializeComponent();17             CreatePane(zedGraphControl1);18         }19         public void CreatePane(ZedGraphControl zgc)20         {21             GraphPane myPane = zgc.GraphPane;22             myPane.Title.Text = "My Mirror";23             myPane.XAxis.Title.Text = "Time,Second";24             myPane.YAxis.Title.Text = "Sample,Volts";25             PointPairList list = new PointPairList();26             Random ran = new Random();27             for (int i = 0; i < 100; i++)28             {29                 double x = (double)new XDate(DateTime.Now.AddSeconds(-(100 - i)));30                 double y = ran.NextDouble();31                 list.Add(x, y);32             }33             DateTime dt = DateTime.Now;34             // Generate a red curve with diamond35             // symbols, and "Porsche" in the legend36             LineItem myCurve = myPane.AddCurve("Porsche", list, Color.Red, SymbolType.Diamond);37             zgc.AxisChange();38             //this.zedGraphControl1.AxisChange();39             Refresh();40         }41     }42 }

2、动态作图

http://wenku.baidu.com/view/6a66bd58ad02de80d4d84031.html
http://wenku.baidu.com/view/d2b81714f18583d049645990.html

ZedGraph