Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
179 views
Welcome To Ask or Share your Answers For Others

1 Answer

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..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...