views:

45

answers:

2

I'm looking to set the y-axis for a MSChart to be midnight to midnight, in either regular time format(i.e. - "1:30 AM") or military time. I figured out I can specify the y-axis format using ChartArea.AxisY.LabelStyle = new LabelStyle() { Format = "HH:mm" }, but cannot figure out what to set the minimum/maximum values to be.

Has anyone employed this format?

A: 

I found a workaround since I never could get the formatting to work natively with DateTime values.

I eventually changed my Y axis data to be in integer format, with ranges from 0 to 2400(2359 really) to represent military time. I then updated the LabelStyle.Format to be "00:00" which renders my integer values into military time.

Yay for me. Hope this helps someone else.

Marc
A: 

There are a few things you need to do to get this working properly.

MSChart accepts DateTime objects as Y values. You can emulate durations by doing this for each of your data points (assuming they are timespans or something convertible into a TimeSpan):

TimeSpan testSpan = TimeSpan.FromMinutes(5);

YourChart.Series(0).Points.AddY(new DateTime(testSpan.Ticks))

That will convert it into a datetime starting from the the beginning of CLR time (e.g. 1/1/0001 12:05:00 AM).

Then just use the label format "HH:mm" on the Y-axis.

<asp:ChartArea Name="VsChartArea">      
    <AxisY Minimum="0">
        <LabelStyle Format="HH:mm" />
    </AxisY>
</asp:ChartArea>

That should make it look like this:

Duration example

To setup a custom interval (5 minutes):

<AxisY Minimum="0" IntervalType="Minutes" Interval="5">

Hope this helps!

subkamran
This is one of the most straight-forward answers I've seen on this site, thanks! Although I've implemented a workaround, I'll definitely reference this answer in the future.
Marc