参考文献:http://www.cnblogs.com/ynyhn/articles/504023.html
英文文献:http://www.codeproject.com/KB/graphics/zedgraph.aspx 更全面
序言
文档(ZedGraph.chm) 包括了完整的类组件源代码。涉及更多的详细资料——ZedGraph有很多在说明文档中没有说明的配置选项。这些内容在网上可以找到。
使用代码
在简单的图形中,一个图表是通过一些简单的步骤创建的。你可以通过一个类库,一个窗体控件,或者一个ASP.NET网页控件,这些方式中的任何一种来使用ZedGraph。本文将主要介绍类库的使用。当然,你可以使用GraphPane 或者MasterPane等工具来完成与ZedGragh相同的功能
。
作为Web控件使用ZedGraph
ZedGraph现在具有一个可以应用到ASPX的Web控件类。提供下载演示项目示范了这个功能。要使用Web控件,你的页面必须包含以下图片:
<img src="graph1.aspx" />
在这个例子中,graph1.aspx是一个声名了这个控件的文件,这个声明要包含一个叫做“graph1.aspx.cs ”的后台代码文件,这个文件实际上负责绘制图形。所以,ZedGraph.dll文件必须未于和graph1.aspx同级的"bin"目录下。
作为用户控件使用ZedGraph
可以在Visual Studio .NET的工具箱中添加ZedGraph控件。首先,打开Visual Studio .NET,新建一个Windows项目,打开窗体设计器显示当前窗口。要查看工具箱,使用主菜单的视图-工具箱命令。
右键单击工具箱上的“我的用户控件”或“组件”栏,然后选择“添加/删除项”选项。点击“浏览”,选取“ZedGraph.dll ”文件。一旦文件添加了,你可以看到一个ZedGraphControl选项在工具栏中。将它拖到窗体设计器中,拉伸到合适大小。这样就在你的窗体中创建了一个ZedGraph控件。这个控件具备了ZedGraph控件的所有功能。一个ZedGraph控件就这样简单的创建了,他带有一个初始的图形窗格(又想不到词...)。ZedGraph.dll文件可以作为用户控件或组件。一些示例程序示范了这个用户控件不同编程语言(Visual Basic, Visual C#, and Visual C++)的用法。本文只关注组件的用法。
注:在C#2008中添加的过程是这样的:选项卡上右键,选择“选择项”菜单,点击浏览,
选取“ZedGraph.dll ”文件
。
作为组件使用ZedGraph
在你的项目中添加组件,步骤如下:
1.在项目中,选择项目菜单下的“添加”选项。通过浏览按扭找到ZedGraph.dll,点击OK。这将使你的项目包含了ZedGraph的所有功能。
2.在主窗体代码中添加使用ZedGraph的代码. using ZedGraph;
3.用如下的声明在窗体类定义代码中添加窗格(这个词总不好翻译,意思就是说ZedGraph画出来的那个表图形):
4.在你的窗体Load方法(如:Form1_Load() )中添加下列代码:
GraphPane myPane = zedGraphControl1.GraphPane; // get a reference to the GraphPane // Set the Titles myPane.Title= "My Test Graph\n(For CodeProject Sample)"; myPane.XAxis.Title = "My X Axis"; myPane.YAxis.Title = "My Y Axis"; // Make up some data arrays based on the Sine function double x, y1, y2; PointPairList list1 = new PointPairList(); PointPairList list2 = new PointPairList(); for (int i = 0; i < 36; i++) { x = (double)i + 5; y1 = 1.5 + Math.Sin((double)i * 0.2); 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 "Porsche" in the legend LineItem myCurve = myPane.AddCurve("Porsche",list1, Color.Red, SymbolType.Diamond); // Generate a blue curve with circle // symbols, and "Piper" in the legend LineItem myCurve2 = myPane.AddCurve("Piper",list2, Color.Blue, SymbolType.Circle); // Tell ZedGraph to refigure the // axes since the data have changed // zedGraphControl1.GraphPane.AddCurve(" ", b, d, Color.Green, SymbolType.Triangle); zedGraphControl1.AxisChange(); zedGraphControl1.Invalidate();
5.为了确保图形被绘制,你可以添加一行代码到你的Form_Paint() 方法(Paint事件调用的方法):
myPane.Draw( e.Graphics );
以上的代码产生的输出如下: