asking for an example code for x-y plotting in visual studio using c#


  • good day everyone. 

    I am not a programmer, but in my On the job training I am told to create a GUI which prompts an

    input and then the code will then process the input and create a graph as an output where the inputs

    are treated as x and y component. I just studied visual studio 2012 and c# for a week and I am now capable

    of using basic codes and window forms. In my task I searched in the net and found out that I can use the chart control.

    But I am running out of time and I really haven't found a program. I wish anyone can help me. I created a program but it 

    doesn't work so I am asking for your codes instead. PLEASE help me.

    Friday, May 10, 2013 3:09 AM

All replies

  • Hi,

    Did you already read this article?


    Failure is not the worst thing in the world. The very worst is not to try. Email Address :

    Friday, May 10, 2013 4:24 AM
  • Yes Eric I already read that article. I was able to create my own program but there is a problem in  my program and i  dont know how to solve it since I am not exposed to programming. The problem is when I input the coordinates, the graph runs only to the right direction . For example, my first input in the textbox is x=5 and y=40, so the graph will show the coordinates with the corresponding plot, but when I input x=2 and y=10, instead the line is suppose to go to the left direction, it still goes to the right. here is my code. please help me. I think i lack the code because I dont know how to do it.

    using System;

    using System.Collection. . . . 

    namespace Graph


    public partial class Form1 : Form


    public Form1()




    public void button_click1(object sender, EventArgs e)

    chart1.Series["Series1"].Points.AddXY(textBox1.Text, textBox2.Text);



    can you help me? thank you so much for your reply.

    Friday, May 10, 2013 5:29 AM
  • Greetings.

    I seriously doubt that chart is the right tool for this job, but you can probably get something like your desired outcome by inserting into the points list instead of adding. Something like so...

          private void button1_Click(object sender, EventArgs e)
             // Get the x value as a number.
             double xval = Convert.ToDouble(textBox1.Text);
             for (int i = 0; i < chart1.Series["Series1"].Points.Count; i++ )
                // If the x value is less than an existing point, insert it, then return.
                if (xval < Convert.ToDouble(chart1.Series["Series1"].Points[i].AxisLabel))
                   chart1.Series["Series1"].Points.InsertXY(i, textBox1.Text, textBox2.Text);
             // If we get here, the new point's x is greater than any existing point, so add it at the end.
             chart1.Series["Series1"].Points.AddXY(textBox1.Text, textBox2.Text);

    The above code is unpolished. For example, it will crash if contents of the textboxes is not numeric. I'll leave those finer details up to you.

    Note that this won't scale the x-axis (which is why I doubt that it's the right tool). If your x locations are, for example, 2, 4, 8, then they will still be evenly spaced instead of the latter two being twice as far apart as the first two.
    Friday, May 10, 2013 7:05 AM
  • Thank you so much for your reply Sir. I really appreciate it. But there is something wrong with
    output. The last input is not being connected to the previous input, instead, it is being inserted between the other previous values, so the output graph is incorrect Sir. But still thank you for your help.

    Friday, May 10, 2013 8:07 AM
  • if this is not the right tool to use for this program Sir, then what do you suggest me to do?

    I really am not a programmer but I am trying my best to learn quickly and to solve this problem 

    through the use of visual studio.

    Friday, May 10, 2013 8:13 AM
  • So it is not a chart that you want.

    Let me summarize, you just want to locate the points given by the text box values (x and y), right? and you don't want to connect them?

    Because normally, when you talked about graph, it's normal that it represents the new value to the right. Graphs is where you can track the progress/update between inputted data from time to time.


    Failure is not the worst thing in the world. The very worst is not to try. Email Address :

    Friday, May 10, 2013 8:37 AM
  • No Mr. Eric. I want to show the output of a certain group of coordinates. Just like in analytic geometry, when we want to plot coordinates we form a certain figure for example a triangle, or a irregular polygon. So as a summary, I want to take the inputs as x and y components and then connect these to the next pair of coordinates. So if I will be given (x1,y1) and (x2,y2) then I can generate a line, but then it should not be always on the right side because it is an x-y coordinate, a lesser number should be in the left side of the larger number. thank you so much for the time.  I Will try to clarify things and send a message tomorrow on what GUI application my office head really wants me to create :) . God bless

    Friday, May 10, 2013 9:13 AM
  • Hi Mr. Meridian,

    I have found your post useful in solving my problem. At least it provides a method to order the labels on the axises. However, my project needs to plot points on a bitmap-like graph with scaled x and y axis. Can you suggest a way to achieve this? Thank you!


    Monday, August 17, 2015 9:37 AM
  • Greetings Bachelor.

    I know a bit more about charts now than I did back in 2013.

    The original poster's problem could probably have been solved (and your problem could be solved) by adding numbers instead of text. It appears that charts are smart enough to order and scale numbers, but they just order text from left to right at equal spacing (which I didn't know two years ago. Sorry 11earlz14 if you're reading this).

    So you can probably get your project to work something like this.

          private void button1_Click(object sender, EventArgs e)
             double xval;
             double yval;
             bool xvalid = double.TryParse(textBox1.Text, out xval);
             bool yvalid = double.TryParse(textBox2.Text, out yval);
             if (xvalid && yvalid)
                chart1.Series["Series1"].Points.AddXY(xval, yval);
                MessageBox.Show("Invalid input");

    Note that it uses AddXY(), not Add().

    To get a "bitmap-like graph", you probably need to change the chart type. There are several possibilities, but my guess is that this is the one you're after.

          public Form1()
             chart1.Series["Series1"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastPoint;
             // And so on.

    • Edited by Ante Meridian Tuesday, August 18, 2015 1:05 AM Added apology.
    Tuesday, August 18, 2015 1:00 AM