Hello

I currently have a program that collects data from a txt file and then works out the averages of each column.

What I am now trying to do is use this data to draw a line Graph for each individual column contained within the txt file.

This is the code I currently have that reads the data and works out the averages.

private void GetDataFromFile()
        {
            Dictionary<int, List<int>> dic = new Dictionary<int, List<int>>();
            string tag = "[HRData]";
            string path = "test.txt";
            //string path = (Chosen_File);
            using (StreamReader sr = new StreamReader(path))
            {
                string line;
                int counter = 1;
                while ((line = sr.ReadLine()) != null)
                {
                    
                    if (line != tag)
                    {
                        counter = 1;
                        string[] lineData = line.Split('\t');
                        foreach (string data in lineData)
                        {
                            int number = Convert.ToInt32(data);
                            if (!dic.ContainsKey(counter))
                                dic.Add(counter, new List<int> { number });
                            else
                                dic[counter].Add(number);
                            counter++;
                        }
                    }
                }
            }

              
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<int, List<int>> kvp in dic)
            {
                var sum = kvp.Value.Average();
                sb.AppendLine("Column " + kvp.Key.ToString() + ": " + sum.ToString());
            }
            richTextBox1.Text = ("Column Averages:\n" + sb.ToString());
        }

This is the contents of the file I am reading.

[HRData]
97	117	0	-117	0	50
97	117	0	-117	0	50
97	183	0	-117	0	50
94	183	55	-117	34	50
95	210	56	-117	84	4931
95	207	59	-117	113	5438
95	196	57	-117	125	5944
97	248	56	-117	125	5944
98	255	64	-117	141	5432
100	252	64	-117	141	5432
101	248	79	-117	147	5444
102	237	82	-117	146	6456
104	235	82	-117	142	6457
105	247	84	-117	134	6458
106	261	87	-117	146	7220
106	268	87	-117	146	7220
107	281	91	-117	152	7475
108	292	95	-117	161	7733
109	297	98	-117	173	8245
110	301	101	-117	179	7733
111	299	103	-117	186	6966
112	297	106	-117	193	6445
113	294	105	-117	200	6706
115	293	104	-117	190	6703
116	294	103	-117	191	5685
117	297	105	-117	193	5688
118	294	105	-117	193	5432
118	293	103	-117	206	4922
119	293	103	-117	207	4914
120	294	104	-117	201	5678
120	294	104	-117	211	6195
121	292	103	-117	192	5939
121	289	102	-117	198	6712

I have been trying to read the data from the sub routine and then use it to draw a line graph onto a bitmap without much success.

If anyone can provide some help or point me in the right direction it would be much appreciated.

Thank you

Thanks for the reply.

I've looked through and it has helped, I understand now how I would draw the line graph. The problem I've now come across is that I need to get each column of data from the file into individual arrays that I can then call to get the changing points to plot the graph for each column.

Have you got any other examples that I could have a look at that might help?

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.