简体   繁体   中英

How to scale and draw a plot in C#

I am trying to use the CSV below for drawing a plot:

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

What I did so far is that I tried to make a Class to do this! this is the part when I try to draw the curve:

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;
        }

What I get at the end is jsut a stright line! that I think has been drawn on X{0,0} and Y{0,0} to X{0, 400} and Y{0,0}

Can you help me correct the mistakes please?

PS: http://itools.subhashbose.com/grapher/index.php this site can draw the plot I need pretty good from the CSV data I have (if you need to check).

Thanks!

This seems to be your problem:

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

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

i is always zero and you are never assigning Y . The "second" assignment isn't even using i , but the 1 index.

Quick fix without error checking:

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

  i++;
}

Your assigning the x both times.

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

And as @LarsTech points out you need to fix the counter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM