BigBug
BigBug

Reputation: 6290

Plotting Points

For some reason my code will not plot the second point on the graph. I'm grabbing the x,y values from two different textboxes on a WindowsForm. I have a class which contains x,y coordinates. Every time a new value is entered into the textbox i create a new object, add the two new co-ordinates to the object and add the object to the list.

Lastly, i loop through the list of objects and attempt to plot each x,y co-ordinate. Why is it not displaying all the points?

Here is my code:

Form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PlotGraph
{
public partial class Form1 : Form
{
    private List<TheList> allValuesList = new List<TheList>();

    private int x = 240; // the position of the X axis
    private int y = 0; // the position of the Y axis
    public static Bitmap bmp = new Bitmap(360, 390);
    public static Graphics g = Graphics.FromImage(bmp);

    public Form1()
    {
        InitializeComponent();
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        g.DrawLine(new Pen(Color.Red, 2), 5, 5, 5, 250);
        g.DrawLine(new Pen(Color.Red, 2), 5, 250, 300, 250);
    }

    private void btnPlotGraph_Click(object sender, EventArgs e)
    {
        TheList latestCoordinate = new TheList();
        if (textBoxX != null)
        {
            latestCoordinate.xCoordinate = Int16.Parse(textBoxX.Text);

        }

        if (textBoxY != null)
        {
            latestCoordinate.yCoordinate = Int16.Parse(textBoxY.Text);
        }

        allValuesList.Add(latestCoordinate);
        plotTheValues(allValuesList); 
    }

    public void plotTheValues(List<TheList> allValuesList)
    {
        Int16 x1 = 0; 
        Int16 y1 = 0; 
        foreach (TheList val in allValuesList)
        {
            x1 = val.xCoordinate;
            y1 = val.yCoordinate;
            g.DrawString("X", new Font("Calibri", 12), new SolidBrush(Color.Black), y + y1, x - x1);

            PictureBox display = new PictureBox();
            display.Width = ClientRectangle.Width;
            display.Height = ClientRectangle.Height;
            this.Controls.Add(display);
            display.Image = bmp;
        }
    }
}

}

Class TheList

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlotGraph
{
    public class TheList
    {
        public Int16 xCoordinate = -1;
        public Int16 yCoordinate = -1; 
    }
}

Upvotes: 1

Views: 2787

Answers (2)

sq33G
sq33G

Reputation: 3360

I think you need to move some of the display logic to your constructor:

    private int x = 240; // the position of the X axis
    private int y = 0; // the position of the Y axis
    public Bitmap bmp;
    public Graphics g;
    PictureBox display = new PictureBox();

    public Form1()
    {
        InitializeComponent();
        this.SetStyle(ControlStyles.ResizeRedraw, true);

        bmp = new Bitmap(360, 390);
        g = Graphics.FromImage(bmp);

        g.DrawLine(new Pen(Color.Red, 2), 5, 5, 5, 250);
        g.DrawLine(new Pen(Color.Red, 2), 5, 250, 300, 250);

        display = new PictureBox(); 
        display.Width = ClientRectangle.Width;
        display.Height = ClientRectangle.Height;

        this.Controls.Add(display);
    }

Then plotTheValues looks like this:

    public void plotTheValues(List<Point> allValuesList)
    {
        foreach (Point val in allValuesList)
        {
            g.DrawString("X", new Font("Calibri", 12), new SolidBrush(Color.Black), x - val.X, y - val.Y);
        }

        display.Image = bmp;
    }

Upvotes: 1

Marco
Marco

Reputation: 57593

I don't understand why you redraw for every item in list the same image (bmp), at the same position...

public void plotTheValues(List<TheList> allValuesList)
{
    foreach (TheList val in allValuesList)
    {
        Int16 x1 = val.xCoordinate;
        Int16 y1 = val.yCoordinate;
        g.DrawString("X", 
                     new Font("Calibri", 12), 
                     new SolidBrush(Color.Black), 
                     y + y1, x - x1);
        g.DrawImage(bmp,...); // It's really faster and memory saving
        // Are you sure you don't need to plot the image bmp
        // at item coordinates instead of always at the same position?
    }
}

Upvotes: 2

Related Questions