ZedGraph控件属性及基础教程详解(2003) 下载本文

ZedGraph 属性及基础教程

public InitialSampleDemo() : base( \Project Initial Sample\, \Sample\DemoType.Tutorial ) 初始化基类的构造函数。基类重载了四个构造函数

public DemoBase( string description, string title, DemoType type ) {

ArrayList types = new ArrayList(); types.Add( type );

Init( description, title, types ); }

public DemoBase( string description, string title, DemoType type, DemoType type2 ) {

ArrayList types = new ArrayList(); types.Add( type ); types.Add( type2 );

Init( description, title, types ); }

public DemoBase( string description, string title, ICollection types ) {

Init( description, title, types ); }

private void Init( string description, string title, ICollection types ) {

this.description = description; this.title = title; this.types = types;

control = new ZedGraphControl(); }

函数中的变量含义如下: Description:对此结构的描述。

Title:在树形结构(TreeView)中显示的标题。

Types:要把此类显示在哪个树形结构的区域中。若有多个Types,则把此类分入不同的树形区域中。例如MasterPane Sample在Tutorial Sample和 Special Features两个区域都有。见图中的相应区域标注。

5 / 33

ZedGraph 属性及基础教程

myPane.Title = \Test Graph\\n(For CodeProject Sample)\ myPane.XAxis.Title = \X Axis\ myPane.YAxis.Title = \Y Axis\

分别指定这个Pane的title、XAxis和YAxis的标题。见上图。 PointPairList list1 = new PointPairList(); PointPairList list2 = new PointPairList(); for ( int i=0; i<36; i++ ) {

double x = (double) i + 5;

double y1 = 1.5 + Math.Sin( (double) i * 0.2 );

double y2 = 3.0 * ( 1.5 + Math.Sin( (double) i * 0.2 ) ); list1.Add( x, y1 ); list2.Add( x, y2 ); }

PointPairList类是一个集合类,继承自 System.Object

System.Collections.CollectionBase

ZedGraph.CollectionPlus

它是PointPair对象的集合,PointPair类是一个包含(X,Y)的坐标类。

6 / 33

ZedGraph 属性及基础教程

其中的for循环在为两个PointPairList复值。

LineItem myCurve = myPane.AddCurve( \list1, Color.Red, SymbolType.Diamond ); LineItem类是ZedGraph中的线条类.

myPane.AddCurve( \list1, Color.Red, SymbolType.Diamond );

的意思是将刚刚赋值的list以”Porsche”这个名字以红色和水晶形状画到Pane中,这个函数的返回值是一个LineItem。你可以通过myCurve这个变量来对它进行进一步的设定。其中SymbolType是个Enum,它枚举了12个可供使用的形状

最后一步就是刷新了。base.ZedGraphControl.AxisChange();

这样整个程序就完成了,简单吧,其实这是个简单的应该,以后会介绍更加复杂的用法和类库。

基本教程篇--第二节:ModInitialSampleDemo.cs介绍

为了讲解方便,我先附上源代码和效果图。

代码如下:

using System;

using System.Drawing; using System.Collections; using ZedGraph;

namespace ZedGraph.Demo

7 / 33

ZedGraph 属性及基础教程

{

///

/// Summary description for SimpleDemo. ///

public class ModInitialSampleDemo : DemoBase {

public ModInitialSampleDemo() : base( \Project Modified Initial Sample\ \Initial Sample\DemoType.Tutorial ) {

GraphPane myPane = base.GraphPane; // Set up the title and axis labels

myPane.Title = \Test Graph\\n(For CodeProject Sample)\ myPane.XAxis.Title = \X Axis\ myPane.YAxis.Title = \Y Axis\

// Make up some data arrays based on the Sine function PointPairList list1 = new PointPairList(); PointPairList list2 = new PointPairList(); for ( int i=0; i<36; i++ ) {

double x = (double) i + 5;

double y1 = 1.5 + Math.Sin( (double) i * 0.2 );

double y2 = 3.0 * ( 1.5 + Math.Sin( (double) i * 0.2 ) ); list1.Add( x, y1 ); list2.Add( x, y2 ); }

// Generate a red curve with diamond // symbols, and \in the legend

LineItem myCurve = myPane.AddCurve( \Color.Red, SymbolType.Diamond ); // Generate a blue curve with circle // symbols, and \in the legend

LineItem myCurve2 = myPane.AddCurve( \Color.Blue, SymbolType.Circle ); // Change the color of the title

8 / 33