Você está na página 1de 7

How to Use ZedGraph?

Data visualization is one of the most important requirement in many quantitative applications. There are various libraries for plotting graphs in
.NET. ZedGraph is an excellent open source library for plotting 2D graphs in C# applications. It is very flexible and easy to use. It has multiple
inbuilt features such as zooming, pan, save the graph as image etc.

In this section, we will give you a brief tutorial on how to use ZedGraph in your C# Windows Form application. Click Here to download the demo
or follow the steps below to make your own application.

Step 1: Download ZedGraph Library from the link below


http://sourceforge.net/projects/zedgraph/files/

Step 2: Add ZedGraph.dll to your application.


Unzip the file downloaded in Step 1.
Create a new project. Right click on 'References' and click 'Add Reference'.

'Add Reference' window opens. Select 'Browse' Tab. Navigate to the directory containing 'ZedGraph.dll', select it and click 'OK'
Step 3: Add 'ZedGraphControl' to your Toolbox
Select Toolbox, go to 'General' tab. Right Click in the region below 'General' tab and click 'Choose Items'
'Choose Toolbox Items' window opens. Click on 'Browse' button. Navigate to the directory containing ZedGraph.dll.
Select ZedGraph.dll and press OK.
Step 4: Now we are ready to use ZedGraph! In the General Tab of Toolbox you will have 'ZedGraphControl' object. Choose it and draw a
rectangle on Form1. The graph will be plotted in this region.
Step 5: Add an additional button to Form1. View the code. The last and the most important thing is to use the ZedGraph namespace in the
Form1.cs. Type the following code in the beginning of Form1.cs.

using ZedGraph;

Step 6: Copy the code below in your button1 call back function in Form1.cs. Run the application and press button1 ('Plot'). Click Here to
download the demo application.

private void button1_Click(object sender, EventArgs e)


{
// Lets generate sine and cosine wave
double[] x = new double[100];
double[] y = new double[100];
double[] z = new double[100];

for (int i = 0; i < x.Length; i++)


{
x[i] = i;
y[i] = Math.Sin(0.3 * x[i]);
z[i] = Math.Cos(0.3 * x[i]);
}

// This is to remove all plots


zedGraphControl1.GraphPane.CurveList.Clear();

// GraphPane object holds one or more Curve


objects (or plots)
GraphPane myPane = zedGraphControl1.GraphPane;

// PointPairList holds the data for plotting, X


and Y arrays
PointPairList spl1 = new PointPairList(x, y);
PointPairList spl2 = new PointPairList(x, z);

// Add cruves to myPane object


LineItem myCurve1 = myPane.AddCurve("Sine Wave",
spl1, Color.Blue,SymbolType.None);
LineItem myCurve2 = myPane.AddCurve("Cosine Wave",
spl2, Color.Red,SymbolType.None);

myCurve1.Line.Width = 3.0F;
myCurve2.Line.Width = 3.0F;
myPane.Title.Text = "My First Plot";

// I add all three functions just to be sure it


refeshes the plot.
zedGraphControl1.AxisChange();
zedGraphControl1.Invalidate();
zedGraphControl1.Refresh();
}

Final Look:

Você também pode gostar