1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 |
|
---|
6 | namespace Microsoft.Research.DynamicDataDisplay.Charts
|
---|
7 | {
|
---|
8 | /// <summary>
|
---|
9 | /// Contains default axis value conversions.
|
---|
10 | /// </summary>
|
---|
11 | public static class DefaultAxisConversions
|
---|
12 | {
|
---|
13 | #region double
|
---|
14 |
|
---|
15 | private static readonly Func<double, double> doubleToDouble = d => d;
|
---|
16 | public static Func<double, double> DoubleToDouble
|
---|
17 | {
|
---|
18 | get { return DefaultAxisConversions.doubleToDouble; }
|
---|
19 | }
|
---|
20 |
|
---|
21 | private static readonly Func<double, double> doubleFromDouble = d => d;
|
---|
22 | public static Func<double, double> DoubleFromDouble
|
---|
23 | {
|
---|
24 | get { return DefaultAxisConversions.doubleFromDouble; }
|
---|
25 | }
|
---|
26 |
|
---|
27 | #endregion
|
---|
28 |
|
---|
29 | #region DateTime
|
---|
30 |
|
---|
31 | private static readonly long minDateTimeTicks = DateTime.MinValue.Ticks;
|
---|
32 | private static readonly long maxDateTimeTicks = DateTime.MaxValue.Ticks;
|
---|
33 | private static readonly Func<double, System.DateTime> dateTimeFromDouble = d =>
|
---|
34 | {
|
---|
35 | long ticks = (long)(d * 10000000000L);
|
---|
36 |
|
---|
37 | // todo should we throw an exception if number of ticks is too big or small?
|
---|
38 | if (ticks < minDateTimeTicks)
|
---|
39 | ticks = minDateTimeTicks;
|
---|
40 | else if (ticks > maxDateTimeTicks)
|
---|
41 | ticks = maxDateTimeTicks;
|
---|
42 |
|
---|
43 | return new DateTime(ticks);
|
---|
44 | };
|
---|
45 | public static Func<double, DateTime> DateTimeFromDouble
|
---|
46 | {
|
---|
47 | get { return dateTimeFromDouble; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | private static readonly Func<DateTime, double> dateTimeToDouble = dt => dt.Ticks / 10000000000.0;
|
---|
51 | public static Func<DateTime, double> DateTimeToDouble
|
---|
52 | {
|
---|
53 | get { return DefaultAxisConversions.dateTimeToDouble; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region TimeSpan
|
---|
59 |
|
---|
60 | private static readonly long minTimeSpanTicks = TimeSpan.MinValue.Ticks;
|
---|
61 | private static readonly long maxTimeSpanTicks = TimeSpan.MaxValue.Ticks;
|
---|
62 |
|
---|
63 | private static readonly Func<double, TimeSpan> timeSpanFromDouble = d =>
|
---|
64 | {
|
---|
65 | long ticks = (long)(d * 10000000000L);
|
---|
66 |
|
---|
67 | // todo should we throw an exception if number of ticks is too big or small?
|
---|
68 | if (ticks < minTimeSpanTicks)
|
---|
69 | ticks = minTimeSpanTicks;
|
---|
70 | else if (ticks > maxTimeSpanTicks)
|
---|
71 | ticks = maxTimeSpanTicks;
|
---|
72 |
|
---|
73 | return new TimeSpan(ticks);
|
---|
74 | };
|
---|
75 |
|
---|
76 | public static Func<double, TimeSpan> TimeSpanFromDouble
|
---|
77 | {
|
---|
78 | get { return DefaultAxisConversions.timeSpanFromDouble; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | private static readonly Func<TimeSpan, double> timeSpanToDouble = timeSpan =>
|
---|
82 | {
|
---|
83 | return timeSpan.Ticks / 10000000000.0;
|
---|
84 | };
|
---|
85 |
|
---|
86 | public static Func<TimeSpan, double> TimeSpanToDouble
|
---|
87 | {
|
---|
88 | get { return DefaultAxisConversions.timeSpanToDouble; }
|
---|
89 | }
|
---|
90 |
|
---|
91 | #endregion
|
---|
92 |
|
---|
93 | #region integer
|
---|
94 |
|
---|
95 | private readonly static Func<double, int> intFromDouble = d => (int)d;
|
---|
96 | public static Func<double, int> IntFromDouble
|
---|
97 | {
|
---|
98 | get { return DefaultAxisConversions.intFromDouble; }
|
---|
99 | }
|
---|
100 |
|
---|
101 | private readonly static Func<int, double> intToDouble = i => (double)i;
|
---|
102 | public static Func<int, double> IntToDouble
|
---|
103 | {
|
---|
104 | get { return DefaultAxisConversions.intToDouble; }
|
---|
105 | }
|
---|
106 |
|
---|
107 | #endregion
|
---|
108 | }
|
---|
109 | }
|
---|