C#从CSV文件中读取字符串并绘制线图

时间:2017-01-14 06:03:52

标签: c# winforms csv mschart

目前,我可以使用Windows窗体应用程序从多个CSV文件和绘图线图中读取数据。但是,现在我需要根据CSV文件的部分名称(csv文件的第3列)绘制折线图。

修改/新的CSV文件:(添加了部分名称列)

Values,Sector,Name
5.55,1024,red
5.37,1536,red
5.73,2048,blue
5.62,2560,.blue
5.12,3072,.yellow
...
  1. 根据部分名称列,我的线图需要相应地在单行中绘制,并且必须使用不同的颜色绘制不同的部分,包括显示的图例必须根据不同的部分名称显示图表的一侧。
  2. 1 csv file = 1 Series 。但是csv文件中有相同的部分名称(上面显示的csv文件示例,例如前20行的红色)。 相同的部分名称=相同的颜色。如果我打开2个或更多csv文件= 2系列。由于csv文件中的部分名称不同,每个系列都会有不同的颜色。
  3. 我对编程很新,非常感谢有人可以通过我的代码编辑来帮助我。

    谢谢。

2 个答案:

答案 0 :(得分:2)

您可以按部分列分隔数据,并使用部分名称作为Series集合的索引,而不是使用i

最好将部分名称用作Series.Name。我建议使用包含两个数字和字符串的数据类,并在List<Dataclass>中收集它们。然后为不同的部分创建Series。然后循环它们..

以下是一些代码示例:

为您的数据定义class

public class Data3
{
    public int N1 { get; set;}
    public double N2 { get; set;}
    public string S1 { get; set;}

    public Data3(double n2, int n1, string s1)
    {
        N1 = n1; N2 = n2; S1 = s1;
    }
}

选择你自己的名字!可选但始终建议:添加一个不错的ToString()重载!

声明一个类级别变量:

  List<Data3> data = new List<Data3>();

在阅读期间收集数据:

  data.Add(new Data3(Convert.ToDouble(pieces[1]), Convert.ToInt32(pieces[0]), pieces[2]));

要绘制图表,请先创建Series

 var sections= data.Select(x => x.S1).Distinct<string>();
 foreach (string s in series)
          chart.Series.Add(new Series(s) { ChartType = SeriesChartType.Line });

然后绘制数据;该系列可以通过Names

编入索引
 foreach (var d in data) chart.Series[d.S1].Points.AddXY(d.N1, d.N2);

我遗漏了将代码集成到您的应用程序中的细节;如果您遇到问题,请通过编辑问题来显示新代码!

一些注意事项:

  • 如果有疑问,请始终创建一个类来保存您的数据

  • 如有疑问,请始终选择结构上的课程

  • 如有疑问,请始终选择List<T> over arrays

  • 总是尝试将代码分解为有用名称的小块。

示例:要读取csv文件中的所有数据,请创建一个函数:

public void AppendCsvToDataList(string file, List<Data3> list)
{
    if (File.Exists(file))
    {
        var lines = File.ReadAllLines(file);
        for (int l = 1; l < lines.Length; l++)
        {
            var pieces = lines[l].Split(',');
            list.Add(new Data3(Convert.ToInt32(pieces[1]),
                               Convert.ToDouble(pieces[0]), pieces[2]));
        }
    }
}

答案 1 :(得分:1)

更新的代码:

GraphDemo(表格):

    List<Read> rrList = new List<Read>();

    void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog ff = new OpenFileDialog();
        Read rr;

        ff.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //"C:\\";
        ff.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
        ff.Multiselect = true;
        ff.FilterIndex = 1;
        ff.RestoreDirectory = true;

        if (ff.ShowDialog() == DialogResult.OK)
        {
            try
            {
                rrList.Clear();
                foreach (String file in ff.FileNames) //if ((myStream = ff.OpenFile()) != null)
                {
                    rr = new Read(file);
                    rrList.Add(rr); 
                }

                //Populate the ComboBoxes
                if (rrList.Count > 0)
                {
                    string[] header = rrList[0].header; //header of first file
                    xBox.DataSource = header; 
                    yBox.DataSource = header.Clone(); //without Clone the 2 comboboxes link together!
                }
                if (yBox.Items.Count > 1) yBox.SelectedIndex = 1; //select second item
            }
            catch (Exception err)
            {
                //Inform the user if we can't read the file
                MessageBox.Show(err.Message);
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Plot.Draw(rrList, xBox, yBox, chart);
    }

课程阅读:

public class Read
{
    public int nLines { get; private set; }
    public int nColumns { get; private set; }
    public string[] header { get; private set; }
    public float[,] data { get; private set; }
    public string fileName { get; set; }
    public string[] section { get; private set; }

    public Read(string file)
    {
        string[] pieces;

        fileName = Path.GetFileName(file);  
        string[] lines = File.ReadAllLines(file); // read all lines
        if (lines == null || lines.Length < 2) return; //no data in file
        header = lines[0].Split(','); //first line is header
        nLines = lines.Length - 1; //first line is header
        nColumns = header.Length;

        //read the numerical data and section name from the file
        data = new float[nLines, nColumns - 1]; // *** 1 less than nColumns as last col is sectionName
        section = new string[nLines]; // *** 
        for (int i = 0; i < nLines; i++) 
        {
            pieces = lines[i + 1].Split(','); // first line is header
            if (pieces.Length != nColumns) { MessageBox.Show("Invalid data at line " + (i + 2) + " of file " + fileName); return; }
            for (int j = 0; j < nColumns - 1; j++)
            {
                float.TryParse(pieces[j], out data[i, j]); //data[i, j] = float.Parse(pieces[j]);
            }
            section[i] = pieces[nColumns - 1]; //last item is section
        }
    }

}

类剧情:

public class Plot
{
    //public Plot() { } //no constructor required as we use a static class to be called

    public static void Draw(List<Read> rrList, ComboBox xBox, ComboBox yBox, Chart chart) //***
    {
        int indX = xBox.SelectedIndex;
        int indY = yBox.SelectedIndex;

        chart.Series.Clear(); //ensure that the chart is empty
        chart.Legends.Clear();
        Legend myLegend = chart.Legends.Add("myLegend");
        myLegend.Title = "myTitle";

        //define a set of colors to be used for sections
        Color[] colors = new Color[] { Color.Black, Color.Blue, Color.Red, Color.Green, Color.Magenta, Color.DarkCyan, Color.Chocolate, Color.DarkMagenta }; 

        //use a Dictionary to keep iColor of each section
        // key=sectionName, value=iColor (color index in our colors array)
        var sectionColors = new Dictionary<string, int>();

        int i = 0;
        int iColor = -1, maxColor = -1;
        foreach (Read rr in rrList)
        {
            float[,] data = rr.data;
            int nLines = rr.nLines;
            int nColumns = rr.nColumns;
            string[] header = rr.header;

            chart.Series.Add("Series" + i);
            chart.Series[i].ChartType = SeriesChartType.Line;

            //chart.Series[i].LegendText = rr.fileName;
            chart.Series[i].IsVisibleInLegend = false; //hide default item from legend

            chart.ChartAreas[0].AxisX.LabelStyle.Format = "{F2}";
            chart.ChartAreas[0].AxisX.Title = header[indX];
            chart.ChartAreas[0].AxisY.Title = header[indY];

            for (int j = 0; j < nLines; j++)
            {
                int k = chart.Series[i].Points.AddXY(data[j, indX], data[j, indY]);
                string curSection = rr.section[j];
                if (sectionColors.ContainsKey(curSection))
                {
                    iColor = sectionColors[curSection];
                }
                else
                {
                    maxColor++;
                    iColor = maxColor; sectionColors[curSection] = iColor;
                }
                chart.Series[i].Points[k].Color = colors[iColor];
            }

            i++; //series#

        } //end foreach rr

        //fill custom legends based on sections/colors
        foreach (var x in sectionColors)
        {
            string section = x.Key;
            iColor = x.Value;
            myLegend.CustomItems.Add(colors[iColor], section); //new LegendItem()
        }
    }

}