2013-01-02 83 views
0

我需要为我的项目绘制图形,所以我搜索了zedgraph库。我查看了这个示例代码来学习该库。但是我无法在屏幕上显示图形。请帮助我。这是代码。如何在屏幕上显示ZedGraph?

namespace gafikdeneme 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      GraphPane pane = new GraphPane(); 

     pane.CurveList.Clear(); 

     PointPairList list1 = new PointPairList(); 
     PointPairList list2 = new PointPairList(); 
     PointPairList list3 = new PointPairList(); 

     double xmin = -50; 
     double xmax = 50; 

     for (double x = xmin; x <= xmax; x += 0.01) 
     { 
      list1.Add (x, f1 (x)); 
      list2.Add (x, f2 (x)); 
      list3.Add (x, f3 (x)); 
     } 

     pane.YAxisList.Clear(); 

     int axis1 = pane.AddYAxis ("Axis 1"); 
     int axis2 = pane.AddYAxis ("Axis 2"); 
     int axis3 = pane.AddYAxis ("Axis 3");   


     LineItem myCurve1 = pane.AddCurve ("Curve 1", list1, Color.Blue, SymbolType.None); 
     LineItem myCurve2 = pane.AddCurve ("Curve 2", list2, Color.Black, SymbolType.None); 
     LineItem myCurve3 = pane.AddCurve ("Curve 3", list3, Color.Red, SymbolType.None); 


     myCurve1.YAxisIndex = axis1; 
     myCurve2.YAxisIndex = axis2; 
     myCurve3.YAxisIndex = axis3; 

     myCurve1.IsVisible = true; 
     myCurve2.IsVisible = true; 
     myCurve3.IsVisible = true; 

     pane.YAxisList[axis1].Title.FontSpec.FontColor = Color.Blue; 
     pane.YAxisList[axis2].Title.FontSpec.FontColor = Color.Black; 
     pane.YAxisList[axis3].Title.FontSpec.FontColor = Color.Red; 

     pane.AxisChange(); 
} 

    public static double f1 (double x) 
    { 
     if (x == 0) 
     { 
      return 1; 
     } 

     return Math.Sin (x)/x; 
    } 

    public static double f2(double x) 
    { 
     if (x == 0) 
     { 
      return 1; 
     } 

     return 10 * Math.Sin (x * 0.5)/x; 
    } 


    public static double f3(double x) 
    { 
     if (x == 0) 
     { 
      return 1; 
     } 

     return 0.1 * Math.Sin (x * 2)/x; 
    } 

     } 
    } 

回答

2

也许这将解决您的问题

//litle change here 
ZedGraphControl control = new ZedGraph.ZedGraphControl(); 
GraphPane pane = control.GraphPane; 
//-- 

pane.CurveList.Clear(); 

PointPairList list1 = new PointPairList(); 
PointPairList list2 = new PointPairList(); 
PointPairList list3 = new PointPairList(); 

double xmin = -50; 
double xmax = 50; 

for (double x = xmin; x <= xmax; x += 0.01) 
{ 
    list1.Add (x, f1 (x)); 
    list2.Add (x, f2 (x)); 
    list3.Add (x, f3 (x)); 
} 

pane.YAxisList.Clear(); 

int axis1 = pane.AddYAxis ("Axis 1"); 
int axis2 = pane.AddYAxis ("Axis 2"); 
int axis3 = pane.AddYAxis ("Axis 3");   


LineItem myCurve1 = pane.AddCurve ("Curve 1", list1, Color.Blue, SymbolType.None); 
LineItem myCurve2 = pane.AddCurve ("Curve 2", list2, Color.Black, SymbolType.None); 
LineItem myCurve3 = pane.AddCurve ("Curve 3", list3, Color.Red, SymbolType.None); 


myCurve1.YAxisIndex = axis1; 
myCurve2.YAxisIndex = axis2; 
myCurve3.YAxisIndex = axis3; 

myCurve1.IsVisible = true; 
myCurve2.IsVisible = true; 
myCurve3.IsVisible = true; 

pane.YAxisList[axis1].Title.FontSpec.FontColor = Color.Blue; 
pane.YAxisList[axis2].Title.FontSpec.FontColor = Color.Black; 
pane.YAxisList[axis3].Title.FontSpec.FontColor = Color.Red; 

pane.AxisChange(); 

//and here, we have panel(panel1) to show our graph 
this.panel1.Controls.Add(control); 
//-- 
+0

是 - ZedGraphControl实际上是你需要把窗体上的控制。实际上,每个ZedGraphControl都可以包含MANY GraphPanes。 –