2013-03-20 34 views
0

A有問題。 Zedgraph爲我的公式繪製了錯誤的圖表。在c中使用ZedGraph#

我的代碼:

private void button1_Click(object sender, EventArgs e) 
{ 
    double a, b, c, dl; 
    a = float.Parse(textBox1.Text); 
    b = float.Parse(textBox2.Text); 
    c = float.Parse(textBox3.Text); 
    dl = (b * b) - (4 * a * c); 
    if (dl < 0) 
    { 
     MessageBox.Show("Error"); 
    } 
    else if (dl > 0) 
    { 
     x1 = (-b - Math.Sqrt(dl))/(2 * a); 
     x2 = (-b + Math.Sqrt(dl))/(2 * a); 
     textBox4.Text = x1.ToString(); 
     textBox5.Text = x2.ToString(); 
    } 
    else 
    { 
     string str = (b/(2 * a)).ToString(); 
     textBox4.Text = str; 
     textBox5.Text = str; 
    } 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    // 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 spl1 = new PointPairList(); 
    // Add cruves to myPane object 
    LineItem myCurve1 = myPane.AddCurve("Equation", spl1, Color.Blue, SymbolType.None); 
    myCurve1.Line.Width = 5.0F; 
    myPane.Title.Text = "Graph"; 
    for (int i = -5; i < 8; i++) 
    { 
     double y = (i - x1) * (i - x2); 
     spl1.Add(i, y); 
    } 

    //I add all three functions just to be sure it refeshes the plot. 
    zedGraphControl1.AxisChange(); 
    zedGraphControl1.Invalidate(); 
    zedGraphControl1.Refresh(); 
} 
+0

請編輯標記。 – abatishchev 2013-03-20 18:48:15

回答

0

試試這個:

private void button2_Click(object sender, EventArgs e) 
{ 
    // 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 spl1 = new PointPairList(); 

    LineItem lineItem = new LineItem("Equation"); 
    myCurve1.Line.Width = 5.0F; 
    myPane.Title.Text = "Graph"; 
    for (int i = -5; i < 8; i++) 
    { 
     double y = (i - x1) * (i - x2); 
     spl1.Add(i, y); 
    } 

    // Add cruves to myPane object 
    myCurve1 = myPane.AddCurve("Equation", spl1, Color.Blue, SymbolType.None); 


    //I add all three functions just to be sure it refeshes the plot. 
    zedGraphControl1.AxisChange(); 
    zedGraphControl1.Invalidate(); 
    zedGraphControl1.Refresh(); 

} 

我感動LineItem myCurve1 = myPane.AddCurve("Equation", spl1, Color.Blue, SymbolType.None);你建立你的spl1名單後進行。看看是否適合你

+0

這是錯誤的,因爲在聲明 – p3tos 2013-03-20 19:22:40

+0

之前使用局部變量'myCurve1'。我編輯了代碼。只需創建初始化LineItem,然後再使用該LineItem myCurve1 = new LineItem();' – 2013-03-20 20:53:25

+0

我添加時出錯:LineItem myCurve1 = new LineItem().. ZedGraph不包含已編輯的構造函數 – p3tos 2013-03-20 21:26:35