【发布时间】:2016-04-29 08:43:07
【问题描述】:
我目前在我的 C# Windows 窗体应用程序(在 Visual Studio 2013 中)上有一个图表,它使用计时器逐渐在其上绘制一条线。我试图设置 x 轴和 y 轴的最小值和最大值,虽然 y 轴值设置正确并按预期显示在图表上,但 x 轴范围设置不正确并停止在某个点(大约 17.9)。这是我目前拥有的图表和计时器的代码:
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,如文本框所示)的问题:
【问题讨论】:
-
你的错在于 x 值。当您将它们添加为字符串时,它们的值 all are 0 所以除了在默认标签中显示它们之外,您无法对它们做任何事情。没有格式,没有范围.. - 确保将它们转换为数字!! - 注意:如果字符串包含有效数字,y 值会被转换,但 x 值不会..
-
顺便说一句:你真的要重新阅读这些文件吗?或者他们正在改变..???
-
@TaW 每次程序运行时文件的内容都会改变,所以我每次都需要重新读取它们。我不确定值是字符串是否存在问题,因为即使我用整数值替换读取文本框的行,即'chart1.ChartAreas[0].AxisX.Maximum = 82;'该程序仍然无法正常运行。
-
我确定,但我说的是 X 值,而不是最小值和最大值范围。
标签: c# charts windows-forms-designer