简体   繁体   中英

Chart shows wrong dates at X axis

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: "dM-yyyy HH:mm:ss" .

Then i parse my actual date string to a DateTime format using the same format as above: dM-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 = "dM-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], "dM-yyyy HH:mm:ss", CultureInfo.InvariantCulture ); chart1.Series["Series1"].Points.AddXY(newDateTime, part[5]); } } chart1.Refresh(); } 

Problem : You have set the Custom Format for X-Axis as dM-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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM