当通过 OpenFileDialog 打开第二个文件时,图表绘制几条线
Chart drawing couple lines, when open second file via OpenFileDialog
我正在制作应用程序,它从文件中获取数据并将其显示在图表上。但是,当我添加一个额外的文件时,我看到了 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 个图表。
您所看到的 不是您所怀疑的 三个 'graphs' 但 两个!
但是由于 ChartType
是 Line
并且第一个数据部分的最后一个点在 x=30 处结束,而第二个文件的第一个点在 x=1 处开始,您会看到一个额外的行连接这两个数据点。
Line
是少数支持 x-values 前进和后退的类型之一。
可以换成ChartType
Point
进行测试。或者你可以为第二个文件使用 2nd Series
并且工件将消失..
如果您希望所有点都在同一系列中,您无法阻止连接线,但您可以隐藏它 :您可以用 Color.Transparent
开始每组数据点,因为 线条颜色由第二个点 ..:[=18=] 决定
int pt = chartGraphic.Series[0].Points.AddXY(x,y);
if (pt == 0) chartGraphic.Series[0].Points[pt].Color = Color.Transparent
我正在制作应用程序,它从文件中获取数据并将其显示在图表上。但是,当我添加一个额外的文件时,我看到了 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 个图表。
您所看到的 不是您所怀疑的 三个 'graphs' 但 两个!
但是由于 ChartType
是 Line
并且第一个数据部分的最后一个点在 x=30 处结束,而第二个文件的第一个点在 x=1 处开始,您会看到一个额外的行连接这两个数据点。
Line
是少数支持 x-values 前进和后退的类型之一。
可以换成ChartType
Point
进行测试。或者你可以为第二个文件使用 2nd Series
并且工件将消失..
如果您希望所有点都在同一系列中,您无法阻止连接线,但您可以隐藏它 :您可以用 Color.Transparent
开始每组数据点,因为 线条颜色由第二个点 ..:[=18=] 决定
int pt = chartGraphic.Series[0].Points.AddXY(x,y);
if (pt == 0) chartGraphic.Series[0].Points[pt].Color = Color.Transparent