2012-10-06 95 views
0

在Visual Studio 2010 IDE中使用C#窗體窗體上的圖表控件。無法在Windows窗體圖表控件上繪製線條

我正在創建繪製隨機點的圖表。一旦生成點,我爲圖表中的每個點運行一個循環,查看所有其他點座標。在該循環內,我計算從父點到它的鄰居的距離。如果距離是< =我指定的某個距離,我想繪製一條線顯示兩者之間的連接。我遇到的問題實際上是繪製該線。此外,我需要找到一種方法來走這條圖表中的最短路徑。

所以這真的是兩個問題: 1.如何繪製圖表上的線條? (目前的困境) 2.我如何走圖找到最短路徑?

這裏是我使用來完成此些碼的剪斷,與當前的誤差沿着:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

    } 

    public void createNodes(int x, int y, int Nodes, int dgNodes) 
    { 
     Random rdn = new Random(); 

     for (int i = 0; i < (Nodes - dgNodes); i++) 
     { 
      chtGraph.Series["Series1"].Points.AddXY 
         (rdn.Next(x), rdn.Next(y)); 
     } 

     for (int i = 0; i <= dgNodes - 1; i++) 
     { 
      chtGraph.Series["Series2"].Points.AddXY 
         (rdn.Next(x), rdn.Next(y)); 
     } 
    } 

    public void buildGraph(int x, int y, int Nodes, int dgNodes) 
    { 
     //set the min/max axis on the chart 
     chtGraph.ChartAreas["ChartArea1"].AxisX.Maximum = x; 
     chtGraph.ChartAreas["ChartArea1"].AxisX.Minimum = 0; 
     chtGraph.ChartAreas["ChartArea1"].AxisY.Maximum = y; 
     chtGraph.ChartAreas["ChartArea1"].AxisY.Minimum = 0; 
     chtGraph.ChartAreas["ChartArea1"].AxisX.Interval = x/10; 
     chtGraph.ChartAreas["ChartArea1"].AxisY.Interval = y/10; 

     chtGraph.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false; 
     chtGraph.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false; 

     //build all the nodes 
     createNodes(x, y, Nodes, dgNodes); 
    } 

    public void drawEdges(int intNumNodes, int intTransPower) 
    { 

     Pen pen = new Pen(Color.Black, 1); 
     Graphics g = chtGraph.CreateGraphics(); 

     Point[] pts = new Point[intNumNodes]; 
     int i = 0; 

     //Gather all the data generator data points into a point array 
     foreach (DataPoint p in chtGraph.Series[0].Points) 
     { 
      Point point = new Point((int)p.XValue, (int)p.YValues[0]); 
      pts[i] = point; 
      i++; 
     } 

     //Gather all the non data generator into the same point array 
     foreach (DataPoint p in chtGraph.Series[1].Points) 
     { 
      Point point = new Point((int)p.XValue, (int)p.YValues[0]); 
      pts[i] = point; 
      i++; 
     } 

     //loop through all the data points 
     foreach (Point p in pts) 
     { 
      //examine all the other data points for each data point visited 
      for (int j = 0; j < pts.Length; j++) 
      { 

       //if the distance from the parent node (p) to the neighbor node is less than the transmit power, then draw a line 
       if (Math.Sqrt(Math.Pow((p.X - pts[j].X), 2) + Math.Pow((p.Y - pts[j].Y), 2)) <= intTransPower) 
       { 
        //gr.DrawLine(pen, p, pts[j]); 
        //gr.Graphics.DrawLine(pen, p.X, p.Y, pts[j].X, pts[j].Y); 

       } 
      } 
     } 
    } 

    private void btnExecute_Click(object sender, EventArgs e) 
    { 
     if (txtDG.Text == "" || txtNodes.Text == "" || txtStorage.Text == "" || txtTransPower.Text == "" 
      || txtXAxis.Text == "" || txtXAxis.Text == "") 
     { 
      lblError.Text = "Please enter in all inputs!"; 
      lblError.Visible = true; 
      return; 
     } 
     //create variables for use through program 

     int intTransPower = Convert.ToInt32(txtTransPower.Text); 
     int intXAxis = Convert.ToInt32(txtXAxis.Text); 
     int intYAxis = Convert.ToInt32(txtYAxis.Text); 
     int intNum_DG = Convert.ToInt32(txtDG.Text); 
     int intNumNodes = Convert.ToInt32(txtNodes.Text); 
     int intStorage = Convert.ToInt32(txtStorage.Text); 

     lblError.Visible = false; 
     lblError.Text = ""; 

     if (txtDG.Text == "" || txtNodes.Text == "" || txtStorage.Text == "" || txtTransPower.Text == "" 
      || txtXAxis.Text == "" || txtXAxis.Text == "") 
     { 
     lblError.Text = "Please enter in all inputs!"; 
     lblError.Visible = true;} 

     chtGraph.Series["Series1"].Points.Clear(); 
     chtGraph.Series["Series2"].Points.Clear(); 

     buildGraph(intXAxis, intYAxis, intNumNodes, intNum_DG); 

     drawEdges(intNumNodes, intTransPower); 
    } 

} 

錯誤:錯誤1類型「System.Windows.Forms.DataVisualization.Charting.ChartGraphics '沒有構造函數定義

+0

這是圖表控件的工作。如果您想繪製任何額外的內容,請爲其PostPaint事件編寫一個事件處理程序。它e.ChartGraphics成員給你你正在尋找的圖形對象。 –

+0

謝謝...這就像一個魅力! – dvsoukup

回答

1

在問題的評論中描述了使用的事件。