2011-10-31 38 views
1

我試圖使用CSV下方繪製曲線畫出一個情節:如何擴展和C#

2.364258,3.005366 
2.723633,3.009784 
3.083008,3.012145 
3.442383,3.012705 
3.801758,3.010412 
4.160156,3.010703 
4.518555,3.011985 
4.876953,3.012547 
5.235352,3.009941 
5.592773,3.011252 
5.951172,3.010596 
6.30957,3.011951 
6.667969,3.010613 
7.026367,3.008634 
7.384766,3.009744 
7.743164,3.01062 
8.101563,3.00942 
8.459961,3.009438 
8.818359,3.009478 
9.177734,3.010827 

我做什麼到目前爲止,我試圖做一個類來做到這一點!這是我試圖繪製曲線時的部分:

class Plotter 
    { 
     #region Fields and variables 

     private Bitmap plot; 
     private Graphics g; 

     public string PlotType {get; set;}   

     private int iWidth; //Width of the box 
     private int iHeight; // 

     private float xMax; //maximum range on X axis 
     private float yMax; //maximum range on Y axis 

     private PointF[] points; 

     #endregion  

     #region Constructors 

     /// <summary> 
     /// Constructor of class 
     /// </summary> 
     /// <param name="iWidth">Width of image in pixels</param> 
     /// <param name="iHeight">Height of image in pixels</param> 
     /// <param name="xMax">Maximum value of the values on X</param> 
     /// <param name="yMax">Maximum value of the values on Y</param> 
     /// <param name="pairs">Pairs of data in an array of PointF[] this is raw data!!</param> 
     public Plotter(int iWidth, int iHeight, float xMax, float yMax, PointF[] points) 
     { 
      this.iWidth = iWidth; 
      this.iHeight = iHeight; 
      this.xMax = xMax; 
      this.yMax = yMax; 

      this.points = points; 

      plot = new Bitmap(iWidth, iHeight); 
     } 

     public Bitmap DrawPlot() 
     { 
      Pen blackPen = new Pen(Color.Black, 1); 
      g = Graphics.FromImage(plot); 

      PointF[] p = new PointF[points.GetLength(0)]; 

      //Try to scale input data to pixel coordinates 
      foreach (PointF point in points) 
      { 
       int i = 0; 

       p[i].X = point.X * iWidth; 
       p[1].X = point.Y * iHeight; 

      } 

      g.DrawCurve(blackPen, p, 0); 

      return plot; 
     } 

我在最後得到的是一條直線!我認爲已經在X {0,0}和Y {0,0}上繪製爲X {0,400}和Y {0,0}

您能幫我糾正錯誤嗎?

P.S:http://itools.subhashbose.com/grapher/index.php本網站可以繪製出我需要的CSV數據(如果需要檢查的話)。

謝謝!

回答

3

這似乎是你的問題:

foreach (PointF point in points) 
{ 
    int i = 0; 

    p[i].X = point.X * iWidth; 
    p[1].X = point.Y * iHeight; 
} 

i永遠是零,你永遠不會分配Y。 「第二」作業甚至不使用i,而是1指數。

沒有錯誤檢查快速修復:

int i = 0; 
foreach (PointF point in points) 
{ 
    p[i].X = point.X * iWidth; 
    p[i].Y = point.Y * iHeight; 

    i++; 
} 
+0

作爲澳大利亞人說:好眼力,夥計。 – MusiGenesis

1

您分配x兩次。

p[i].X = point.X * iWidth; 
    p[1].X = point.Y * iHeight; 

正如@LarsTech指出你需要修復的櫃檯