I am reading date/time and data from a csv file and store this in a line chart. My date/time string is 1-1-2014 21:55:42
or 18-02-2014 00:00:00
which is actually the first entry and i have for a couple of hours data.
First i'm setting the chartArea X axis lablestyle to the proper format: "d-M-yyyy HH:mm:ss"
.
Then i parse my actual date string to a DateTime format using the same format as above: d-M-yyyy HH:mm:ss
. And add the data to the chart.
I ensure you my date is correct:
And my code:
private void button2_Click_1(object sender, EventArgs e) { string line; char[] delimiters = { ';', ',', '|' };
chart1.Series["Series1"].XValueType = ChartValueType.Time; chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "d-M-yyyy HH:mm:ss"; chart1.Series["Series1"].Points.Clear(); using (System.IO.StreamReader sr = new System.IO.StreamReader(filename)) { while (!sr.EndOfStream) { line = sr.ReadLine(); DateTime newDateTime = new DateTime(); string[] part = line.Split(delimiters); Console.WriteLine(part[0]); newDateTime = DateTime.ParseExact( part[0], "d-M-yyyy HH:mm:ss", CultureInfo.InvariantCulture ); chart1.Series["Series1"].Points.AddXY(newDateTime, part[5]); } } chart1.Refresh(); }