【发布时间】:2019-05-26 09:09:34
【问题描述】:
我正在制作应用程序,它从文件中获取数据并将其显示在图表上。但是,当我添加一个附加文件时,我看到 3 个图表,而不是 2 个。
我正在读取 csv 文件,解析成双精度并添加系列。它必须是 2 个图表,但我看到了 3 个。
string[] tmpStrArr;
double x;
double y;
public Form1()
{
InitializeComponent();
chartGraphic.ChartAreas[0].AxisY.ScaleView.Zoom(-60, 15); // -15<= y <=15
chartGraphic.ChartAreas[0].AxisX.ScaleView.Zoom(-60, 2); // -15 <= x <= 2
chartGraphic.ChartAreas[0].CursorX.IsUserEnabled = true;
chartGraphic.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chartGraphic.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chartGraphic.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
string line = "";
ofd.Title = "Open File With Data";
ofd.Filter = "CSV File|*.csv|TXT File|*txt";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
textBox1.Text = ofd.FileName;
MessageBox.Show(ofd.FileName);
StreamReader sr = new StreamReader(ofd.FileName);
while (line != null)
{
//for (int i = -15; i < 2; i++)
//{
//}
line = sr.ReadLine();
if (line != null)
{
tmpStrArr = line.Split(',');
x = Double.Parse(tmpStrArr[0]);
y = Double.Parse(tmpStrArr[1]);
chartGraphic.Series[0].Points.AddXY(x,y);
listBox1.Items.Add(line);
}
}
chartGraphic.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
tmpStrArr = null;
x = 0;
y = 0;
sr.Close();
ofd.Dispose();
}
}
我希望输出来自 2 个文件的 2 个图表,而不是来自 2 个文件的 3 个图表。
【问题讨论】:
-
你能显示结果吗?这 3 个图表是否在同一个图表区域中?会不会是第二个文件的数据太多了?
-
2 文件相似。 x 有 30 个值,y 有 30 个值。我看到的第三张图与其他图不匹配。我不明白它在哪里取这个值。这里不知道怎么加图片(
-
在编辑模式下应该有添加图片的图标吧?
-
我添加了图片。谢谢)
-
使用图表,图像通常比文字或代码更能说明问题..
标签: c# mschart openfiledialog