TimeSpan on the Y-Axis

Oct 20, 2013 at 7:23 AM
Hello,

I am a bit new to programming in C#, I started learning about it a few days ago to complete an idea for a program that will help display data I have been gathering for my work. So please forgive me if this is a rather silly question.

I am trying to get the units on the Y-Axis to work similarly to how the DateTime scale works on the X-Axis. However, the value of the units on Y are not dates, but they are TimeSpans. I want it to work in a similar way, IE when you are zoomed out it will display hours or days, and as you zoom in it will change to minutes etc.

So far I have it as:
                var datesDataSource = new EnumerableDataSource<DateTime>(dates);
                datesDataSource.SetXMapping(x => dateAxis.ConvertToDouble(x));

                var timesDataSource = new EnumerableDataSource<TimeSpan>(times);
                timesDataSource.SetYMapping(y => y.TotalMinutes);
However, this method doesn't result in the Y axis scaling Days -> Hours -> Minutes etc. (I'm not expecting it to), however the plot is working.

Is there a simple way to do achieve this?

Thanks for your time,
Collin
Oct 20, 2013 at 7:32 AM
Edited Oct 20, 2013 at 7:33 AM
I feel that I should clarify:

On the X-Axis I want dates and times (this is working)
On the Y-Axis I want time (and I would like it to scale from days to hours to minutes to seconds depending on how far I am zoomed in or out on the graph).


Note that the Y-Axis doesn't have a date, only a time (the time it took to run the experiment for my work)

This way when the experiments take a longer time, it is easier to comprehend what it means. It a natural thing to understand 23 hours rather than 1380 minutes when the times are long and say 3 minutes versus .05 hours when the times are short.

I hope this makes sense, it is rather late.

Thanks,
Collin
Oct 21, 2013 at 6:15 AM
Hi

you try some thing like this.

Y axis will be having Custom formatter

yAxis.LabelProvider.CustomFormatter = (tickInfo)=>
{
return tickInfo.Tick.ToString(@"dd\.hh\:mm\:ss\"); 
};
Regards
Ravi
Oct 21, 2013 at 7:36 AM
I assume I need to change this?
                var timesDataSource = new EnumerableDataSource<TimeSpan>(times);
                timesDataSource.SetYMapping(y => y.TotalMinutes);
to
                var timesDataSource = new EnumerableDataSource<TimeSpan>(times);
                timesDataSource.SetYMapping(y => y.Ticks);
However, it doesn't seem to display any kind of label if I implement your code or not.

If I change y.TotalMinutes to y.Ticks and plot the results (without your CustomFormatter), I get nothing on the Y axis (the plot still looks right, has the same shape). If I add your CustomFormatter, I still get nothing?
                var timesDataSource = new EnumerableDataSource<TimeSpan>(times);
                timesDataSource.SetYMapping(y => y.Ticks);

                timeAxis.LabelProvider.CustomFormatter = (tickInfo) =>
                {
                    return tickInfo.Tick.ToString(@"dd\.hh\:mm\:ss");
                };
Oct 21, 2013 at 7:50 AM
Hi,

I thought your Y axis is TimeSpan axis, after seeing your code I understand your Y axis is numeric axis.

If you Y axis is numeric axis, then you do something like this
yAxis.LabelProvider.CustomFormatter = (tickInfo)=>
{
return tickInfo.Hours+":"+tickInfo.Minutes+":"+tickInfo.Minutes;
};
this may work. (I didn't test this code). Check this part of code is triggering or not.

Regards
Ravi.
Oct 21, 2013 at 7:53 AM
I am pretty sure that I now understand how the custom formatter works, and I don't think that is the problem. The ticks aren't displaying on the axis if I use it (the custom formatter) or not.

The largest tick in this particular data set is:
1965650000000

which is:

2.06.36.05

Oh and thank you very much for your response, and for the time you are spending to help me. I really do appreciate it.
Oct 21, 2013 at 7:55 AM
I just saw your reply, let me test it out.
Thanks!
Oct 21, 2013 at 8:04 AM
Edited Oct 21, 2013 at 8:26 AM
Based on the post you just posted, I got something to work:
                var timesDataSource = new EnumerableDataSource<TimeSpan>(times);
                timesDataSource.SetYMapping(y => Convert.ToInt64(y.TotalSeconds));

                timeAxis.LabelProvider.CustomFormatter = (tickInfo) =>
                {

                    int hours = (tickInfo.Tick / 3600);
                    int minutes = (tickInfo.Tick % 3600) / 60;
                    int seconds = (tickInfo.Tick % 60);

                    if ((minutes < 10) && (seconds < 10))
                        return hours + ":0" + minutes + ":0" + seconds;
                    else if ((minutes > 9) && (seconds < 10))
                        return hours + ":" + minutes + ":0" + seconds;
                    else if ((minutes < 10) && (seconds > 9))
                        return hours + ":0" + minutes + ":" + seconds;
                    else
                        return hours + ":" + minutes + ":" + seconds;

                    //return tickInfo.Tick.ToString(@"dd\.hh\:mm\:ss");
                };
Thanks a lot!