图表 x 轴最大值未正确设置 [英] Chart x-axis maximum value not being set correctly

查看:28
本文介绍了图表 x 轴最大值未正确设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的 C# Windows 窗体应用程序(在 Visual Studio 2013 中)上有一个图表,它使用计时器逐渐在其上绘制一条线.我尝试设置 x 轴和 y 轴的最小值和最大值,尽管 y 轴值设置正确并在图表上按预期显示,但 x 轴范围未正确设置并停止在某个点(大约 17.9).这是我目前拥有的图表和计时器的代码:

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++;
    }
}

另外,这里是表格运行后的屏幕截图,以显示 x 轴最大值(如文本框中所示应为 81.08)的问题:

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):

推荐答案

你的错在于 x 值.

当您将它们添加为字符串时,它们的值都是 0,因此除了在默认标签中显示它们之外,您无法对它们做任何事情.没有格式,没有范围..

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();

注意:如果字符串包含有效数字,系统会转换 y 值,但不会转换 x 值.

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

这篇关于图表 x 轴最大值未正确设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆