问题描述
我目前在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值中。
Your fault is in the x-values.
当您将它们添加为字符串时,它们的值都为 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轴最大值未正确设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!