Search code examples
c#mschartopenfiledialog

Chart drawing couple lines, when open second file via OpenFileDialog


I'm making app, which taking data from file and shows it on chart. But, when I'm adding an additional file I see 3 graphs, not 2.

I am reading csv file, parse into double and adding series. It has to be 2 graphs, but I see 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();

        }


    }

I expect the output of 2 graphs, from 2 files, not 3 graphs from 2 files.

enter image description here


Solution

  • What you are seeing are not as you suspect three 'graphs' but two!

    But as the ChartType is Line and the last point of the 1st data portion ends at x=30 and the first of the 2nd file start at x=1 you see an extra line connecting these two datapoints.

    Line is one of the few type that support x-values going forward and back.

    You can change to ChartType Point to test. Or you can use a 2nd Series for the 2nd file and the artifact will dissappear..

    If you'd rather keep all points in the same series, you can't prevent the connecting line but you can hide it : You can start each set of datapoints with Color.Transparent, as the line color is determined by the 2nd point..:

    int pt = chartGraphic.Series[0].Points.AddXY(x,y);
    if (pt == 0) chartGraphic.Series[0].Points[pt].Color = Color.Transparent