using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Microsoft.Research.DynamicDataDisplay.Charts
{
///
/// Contains default axis value conversions.
///
public static class DefaultAxisConversions
{
#region double
private static readonly Func doubleToDouble = d => d;
public static Func DoubleToDouble
{
get { return DefaultAxisConversions.doubleToDouble; }
}
private static readonly Func doubleFromDouble = d => d;
public static Func DoubleFromDouble
{
get { return DefaultAxisConversions.doubleFromDouble; }
}
#endregion
#region DateTime
private static readonly long minDateTimeTicks = DateTime.MinValue.Ticks;
private static readonly long maxDateTimeTicks = DateTime.MaxValue.Ticks;
private static readonly Func dateTimeFromDouble = d =>
{
long ticks = (long)(d * 10000000000L);
// todo should we throw an exception if number of ticks is too big or small?
if (ticks < minDateTimeTicks)
ticks = minDateTimeTicks;
else if (ticks > maxDateTimeTicks)
ticks = maxDateTimeTicks;
return new DateTime(ticks);
};
public static Func DateTimeFromDouble
{
get { return dateTimeFromDouble; }
}
private static readonly Func dateTimeToDouble = dt => dt.Ticks / 10000000000.0;
public static Func DateTimeToDouble
{
get { return DefaultAxisConversions.dateTimeToDouble; }
}
#endregion
#region TimeSpan
private static readonly long minTimeSpanTicks = TimeSpan.MinValue.Ticks;
private static readonly long maxTimeSpanTicks = TimeSpan.MaxValue.Ticks;
private static readonly Func timeSpanFromDouble = d =>
{
long ticks = (long)(d * 10000000000L);
// todo should we throw an exception if number of ticks is too big or small?
if (ticks < minTimeSpanTicks)
ticks = minTimeSpanTicks;
else if (ticks > maxTimeSpanTicks)
ticks = maxTimeSpanTicks;
return new TimeSpan(ticks);
};
public static Func TimeSpanFromDouble
{
get { return DefaultAxisConversions.timeSpanFromDouble; }
}
private static readonly Func timeSpanToDouble = timeSpan =>
{
return timeSpan.Ticks / 10000000000.0;
};
public static Func TimeSpanToDouble
{
get { return DefaultAxisConversions.timeSpanToDouble; }
}
#endregion
#region integer
private readonly static Func intFromDouble = d => (int)d;
public static Func IntFromDouble
{
get { return DefaultAxisConversions.intFromDouble; }
}
private readonly static Func intToDouble = i => (double)i;
public static Func IntToDouble
{
get { return DefaultAxisConversions.intToDouble; }
}
#endregion
}
}