Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

c# - Chart x-axis maximum value not being set correctly

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...