1
votes

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.

enter image description here

I ensure you my date is correct: enter image description here

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();
    }
1
What is newdatetime in your code? Is it the correct date? Maybe log that into a file or something.Henno

1 Answers

2
votes

Problem : You have set the Custom Format for X-Axis as d-M-yyyy HH:mm:ss but you are just providing the datetime without formatting it.

Replace This:

chart1.Series["Series1"].Points.AddXY(newDateTime, part[5]);

With This:

chart1.Series["Series1"].Points.AddXY(
    newDateTime.ToString("d-M-yyyy HH:mm:ss"), part[5]);