1

I currently have a chart on my C# Windows Form Application (in Visual Studio 2013) that gradually draws a line onto it using a timer. I have tried to set the minimum and maximum values for the x- and y-axes and although the y-axis values are being set correctly and appearing as expected on the chart, the x-axis range is not being set correctly and stops at a certain point (around 17.9). Here is the code for the chart and the timer that I currently have:

private void btnPlotGraph_Click(object sender, EventArgs e)
{
    chart1.ChartAreas[0].AxisX.Minimum = 0;
    chart1.ChartAreas[0].AxisX.Maximum = double.Parse(txtTotalHorizontalDistance.Text);
    chart1.ChartAreas[0].AxisY.Minimum = 0 - double.Parse(txtInitialHeight.Text);
    chart1.ChartAreas[0].AxisY.Maximum = double.Parse(txtTotalVerticalDistance.Text);
    timer1.Tick += timer1_Tick;
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    string[] xCoordinates = File.ReadAllLines("H:\\Computing Coursework\\Code\\FormPrototype\\testX.txt");
    string[] yCoordinates = File.ReadAllLines("H:\\Computing Coursework\\Code\\FormPrototype\\testY.txt");

    chart1.Series["Projectile1"].Points.AddXY(xCoordinates[i], yCoordinates[i]);

    if (i >= xCoordinates.Length - 1)
    {
        timer1.Stop();
    }
    else
    {
        i++;
    }
}

Also, here is a screenshot of the form once it is run to show the problem with the x-axis maximum value (which should be 81.08 as shown in the text box):

enter image description here

4
  • Your fault is in the x-values. As you add them as string their values all are 0 so you can't do anything with them except displaying them in the default labels. No formatting, no ranges.. - Make sure to convert them to a number!! - Note: If the strings contain valid numbers the y-values do get converted but the x-values don't.. Commented Apr 29, 2016 at 9:39
  • Btw: Do you really want to read the files all over again? Or are they changing..??? Commented Apr 29, 2016 at 9:48
  • @TaW The contents of the files change each time the program is run so I need to re-read them each time. I'm not sure that it's a problem with the values being strings because even if I replace the line that reads the text boxes with an integer value, i.e. 'chart1.ChartAreas[0].AxisX.Maximum = 82;' the program still does not work correctly. Commented May 4, 2016 at 10:42
  • Well I am sure, but I am talking about the X-Values, not the Min&Max ranges. Commented May 4, 2016 at 11:15

1 Answer 1

0

Your fault is in the x-values.

As you add them as strings their values all are 0 so you can't do anything with them except displaying them in the default labels. No formatting, no ranges..

Make sure to convert them to a number, maybe like this:

 string[] xStringCoordinates = File.ReadAllLines(yourFileName);
 double[] xCoordinates = xStringCoordinates.Select(x => Convert.ToDouble(x)).ToArray();

Note: If the strings contain valid numbers the y-values do get converted by the system but the x-values don't..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.