简体   繁体   中英

Display HH:mm:ss in DateTime x axis in Chart C#

I have series of data (temperature vs time) and I want to plot it using Chart object of .NET 4.0. The problem is that if I choose DateTime as the XValueType for the series it is displayed like dd.mm.yyyy but I want to display it as HH:mm:ss .

How can I do that?

Thanks for your help

Solution 1:

Try This:

DateTime myDateValue = DateTime.Now;
String XValueType  = myDateValue.ToString("HH:mm:ss");

Solution 2:

if you are using windows chart control then

Try This:

chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "HH:mm:ss";

Solution 3: as you said in your below comments if you are showing only DateTime as a string it would be difficult while comparing.

Yes it would be difficult if you only show the Time part (HH:mm:ss) as string. but if you display the Date and Time then you can again convert back the string to DateTime and perform conversion.

Try This:

DateTime date=DateTime.Now;
axisLabel=date.ToString("dd.MM.yyyy HH:mm:ss");

you can convert the datetime string back to DateTime as below:

DateTime date = DateTime.ParseExact(axisLabel,"dd.MM.yyyy 
                     HH:mm:ss",CultureInfo.InvariantCulture);

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