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

隐身守侯 提交于 2019-12-20 04:52:44

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/22101707/display-hhmmss-in-datetime-x-axis-in-chart-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!