【问题标题】:Chart drawing couple lines, when open second file via OpenFileDialog通过 OpenFileDialog 打开第二个文件时,图表绘制几条线
【发布时间】: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


【解决方案1】:

您所看到的不是您怀疑的三个“图表”而是两个

但由于 ChartTypeLine 并且第一个数据部分的最后一个点以 x=30 结束,而第二个文件的第一个点从 x=1 开始,您会看到额外的一行 connecting 这两个数据点。

Line 是少数支持前后 x 值的类型之一。

你可以换成ChartTypePoint进行测试。或者您可以为第二个文件使用 2nd Series,这样工件就会消失..

如果您希望将所有点保留在同一个系列中,您无法阻止连接线,但您可以隐藏它:您可以开始每组Color.Transparent 的数据点,因为线条颜色由第二个点确定..:

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

【讨论】:

  • 非常感谢!现在我明白了。我可以在哪里阅读有关添加系列的更多信息?我不太明白如何将第二系列用于另一个文件
  • 您可以在文件对话框之后添加一个系列:Series s = chartGraphic.Series.Add(someSeriesName) { ChartType = SeriesChartType.Line };。使用一个好听的名称,可能源自文件名。名称将显示在他的 Legend 中,但您可以根据需要设置不同的 `LegendText`。如果您只想使用您添加的系列,请先删除默认系列:chartGraphic.Series.Clear();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多