1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
|
---|
6 |
|
---|
7 | namespace Microsoft.Research.DynamicDataDisplay.Charts
|
---|
8 | {
|
---|
9 | internal abstract class TimeSpanTicksProviderBase : TimePeriodTicksProvider<TimeSpan>
|
---|
10 | {
|
---|
11 | protected sealed override bool Continue(TimeSpan current, TimeSpan end)
|
---|
12 | {
|
---|
13 | return current < end;
|
---|
14 | }
|
---|
15 |
|
---|
16 | protected sealed override TimeSpan RoundDown(TimeSpan start, TimeSpan end)
|
---|
17 | {
|
---|
18 | return RoundDown(start, Difference);
|
---|
19 | }
|
---|
20 |
|
---|
21 | protected sealed override TimeSpan RoundUp(TimeSpan start, TimeSpan end)
|
---|
22 | {
|
---|
23 | return RoundUp(end, Difference);
|
---|
24 | }
|
---|
25 |
|
---|
26 | protected static TimeSpan Shift(TimeSpan span, DifferenceIn diff)
|
---|
27 | {
|
---|
28 | TimeSpan res = span;
|
---|
29 |
|
---|
30 | TimeSpan shift = new TimeSpan();
|
---|
31 | switch (diff)
|
---|
32 | {
|
---|
33 | case DifferenceIn.Year:
|
---|
34 | case DifferenceIn.Month:
|
---|
35 | case DifferenceIn.Day:
|
---|
36 | shift = TimeSpan.FromDays(1);
|
---|
37 | break;
|
---|
38 | case DifferenceIn.Hour:
|
---|
39 | shift = TimeSpan.FromHours(1);
|
---|
40 | break;
|
---|
41 | case DifferenceIn.Minute:
|
---|
42 | shift = TimeSpan.FromMinutes(1);
|
---|
43 | break;
|
---|
44 | case DifferenceIn.Second:
|
---|
45 | shift = TimeSpan.FromSeconds(1);
|
---|
46 | break;
|
---|
47 | case DifferenceIn.Millisecond:
|
---|
48 | shift = TimeSpan.FromMilliseconds(1);
|
---|
49 | break;
|
---|
50 | default:
|
---|
51 | break;
|
---|
52 | }
|
---|
53 |
|
---|
54 | res = res.Add(shift);
|
---|
55 | return res;
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected sealed override TimeSpan RoundDown(TimeSpan timeSpan, DifferenceIn diff)
|
---|
59 | {
|
---|
60 | TimeSpan res = timeSpan;
|
---|
61 |
|
---|
62 | if (timeSpan.Ticks < 0)
|
---|
63 | {
|
---|
64 | res = RoundUp(timeSpan.Duration(), diff).Negate();
|
---|
65 | }
|
---|
66 | else
|
---|
67 | {
|
---|
68 | switch (diff)
|
---|
69 | {
|
---|
70 | case DifferenceIn.Year:
|
---|
71 | case DifferenceIn.Month:
|
---|
72 | case DifferenceIn.Day:
|
---|
73 | res = TimeSpan.FromDays(timeSpan.Days);
|
---|
74 | break;
|
---|
75 | case DifferenceIn.Hour:
|
---|
76 | res = TimeSpan.FromDays(timeSpan.Days).
|
---|
77 | Add(TimeSpan.FromHours(timeSpan.Hours));
|
---|
78 | break;
|
---|
79 | case DifferenceIn.Minute:
|
---|
80 | res = TimeSpan.FromDays(timeSpan.Days).
|
---|
81 | Add(TimeSpan.FromHours(timeSpan.Hours)).
|
---|
82 | Add(TimeSpan.FromMinutes(timeSpan.Minutes));
|
---|
83 | break;
|
---|
84 | case DifferenceIn.Second:
|
---|
85 | res = TimeSpan.FromDays(timeSpan.Days).
|
---|
86 | Add(TimeSpan.FromHours(timeSpan.Hours)).
|
---|
87 | Add(TimeSpan.FromMinutes(timeSpan.Minutes)).
|
---|
88 | Add(TimeSpan.FromSeconds(timeSpan.Seconds));
|
---|
89 | break;
|
---|
90 | case DifferenceIn.Millisecond:
|
---|
91 | res = timeSpan;
|
---|
92 | break;
|
---|
93 | default:
|
---|
94 | break;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | return res;
|
---|
99 | }
|
---|
100 |
|
---|
101 | protected sealed override TimeSpan RoundUp(TimeSpan dateTime, DifferenceIn diff)
|
---|
102 | {
|
---|
103 | TimeSpan res = RoundDown(dateTime, diff);
|
---|
104 | res = Shift(res, diff);
|
---|
105 |
|
---|
106 | return res;
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected override List<TimeSpan> Trim(List<TimeSpan> ticks, Range<TimeSpan> range)
|
---|
110 | {
|
---|
111 | int startIndex = 0;
|
---|
112 | for (int i = 0; i < ticks.Count - 1; i++)
|
---|
113 | {
|
---|
114 | if (ticks[i] <= range.Min && range.Min <= ticks[i + 1])
|
---|
115 | {
|
---|
116 | startIndex = i;
|
---|
117 | break;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | int endIndex = ticks.Count - 1;
|
---|
122 | for (int i = ticks.Count - 1; i >= 1; i--)
|
---|
123 | {
|
---|
124 | if (ticks[i] >= range.Max && range.Max > ticks[i - 1])
|
---|
125 | {
|
---|
126 | endIndex = i;
|
---|
127 | break;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | List<TimeSpan> res = new List<TimeSpan>(endIndex - startIndex + 1);
|
---|
132 | for (int i = startIndex; i <= endIndex; i++)
|
---|
133 | {
|
---|
134 | res.Add(ticks[i]);
|
---|
135 | }
|
---|
136 |
|
---|
137 | return res;
|
---|
138 | }
|
---|
139 |
|
---|
140 | protected sealed override bool IsMinDate(TimeSpan dt)
|
---|
141 | {
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | internal sealed class DayTimeSpanProvider : TimeSpanTicksProviderBase
|
---|
147 | {
|
---|
148 | protected override DifferenceIn GetDifferenceCore()
|
---|
149 | {
|
---|
150 | return DifferenceIn.Day;
|
---|
151 | }
|
---|
152 |
|
---|
153 | protected override int[] GetTickCountsCore()
|
---|
154 | {
|
---|
155 | return new int[] { 20, 10, 5, 2, 1 };
|
---|
156 | }
|
---|
157 |
|
---|
158 | protected override int GetSpecificValue(TimeSpan start, TimeSpan dt)
|
---|
159 | {
|
---|
160 | return (dt - start).Days;
|
---|
161 | }
|
---|
162 |
|
---|
163 | protected override TimeSpan GetStart(TimeSpan start, int value, int step)
|
---|
164 | {
|
---|
165 | double days = start.TotalDays;
|
---|
166 | double newDays = ((int)(days / step)) * step;
|
---|
167 | if (newDays > days) {
|
---|
168 | newDays -= step;
|
---|
169 | }
|
---|
170 | return TimeSpan.FromDays(newDays);
|
---|
171 | //return TimeSpan.FromDays(start.Days);
|
---|
172 | }
|
---|
173 |
|
---|
174 | protected override TimeSpan AddStep(TimeSpan dt, int step)
|
---|
175 | {
|
---|
176 | return dt.Add(TimeSpan.FromDays(step));
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | internal sealed class HourTimeSpanProvider : TimeSpanTicksProviderBase
|
---|
181 | {
|
---|
182 | protected override DifferenceIn GetDifferenceCore()
|
---|
183 | {
|
---|
184 | return DifferenceIn.Hour;
|
---|
185 | }
|
---|
186 |
|
---|
187 | protected override int[] GetTickCountsCore()
|
---|
188 | {
|
---|
189 | return new int[] { 24, 12, 6, 4, 3, 2, 1 };
|
---|
190 | }
|
---|
191 |
|
---|
192 | protected override int GetSpecificValue(TimeSpan start, TimeSpan dt)
|
---|
193 | {
|
---|
194 | return (int)(dt - start).TotalHours;
|
---|
195 | }
|
---|
196 |
|
---|
197 | protected override TimeSpan GetStart(TimeSpan start, int value, int step)
|
---|
198 | {
|
---|
199 | double hours = start.TotalHours;
|
---|
200 | double newHours = ((int)(hours / step)) * step;
|
---|
201 | if (newHours > hours)
|
---|
202 | {
|
---|
203 | newHours -= step;
|
---|
204 | }
|
---|
205 | return TimeSpan.FromHours(newHours);
|
---|
206 | //return TimeSpan.FromDays(start.Days);
|
---|
207 | }
|
---|
208 |
|
---|
209 | protected override TimeSpan AddStep(TimeSpan dt, int step)
|
---|
210 | {
|
---|
211 | return dt.Add(TimeSpan.FromHours(step));
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | internal sealed class MinuteTimeSpanProvider : TimeSpanTicksProviderBase
|
---|
216 | {
|
---|
217 | protected override DifferenceIn GetDifferenceCore()
|
---|
218 | {
|
---|
219 | return DifferenceIn.Minute;
|
---|
220 | }
|
---|
221 |
|
---|
222 | protected override int[] GetTickCountsCore()
|
---|
223 | {
|
---|
224 | return new int[] { 60, 30, 20, 15, 10, 5, 4, 3, 2 };
|
---|
225 | }
|
---|
226 |
|
---|
227 | protected override int GetSpecificValue(TimeSpan start, TimeSpan dt)
|
---|
228 | {
|
---|
229 | return (int)(dt - start).TotalMinutes;
|
---|
230 | }
|
---|
231 |
|
---|
232 | protected override TimeSpan GetStart(TimeSpan start, int value, int step)
|
---|
233 | {
|
---|
234 | double minutes = start.TotalMinutes;
|
---|
235 | double newMinutes = ((int)(minutes / step)) * step;
|
---|
236 | if (newMinutes > minutes)
|
---|
237 | {
|
---|
238 | newMinutes -= step;
|
---|
239 | }
|
---|
240 |
|
---|
241 | return TimeSpan.FromMinutes(newMinutes);
|
---|
242 | }
|
---|
243 |
|
---|
244 | protected override TimeSpan AddStep(TimeSpan dt, int step)
|
---|
245 | {
|
---|
246 | return dt.Add(TimeSpan.FromMinutes(step));
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | internal sealed class SecondTimeSpanProvider : TimeSpanTicksProviderBase
|
---|
251 | {
|
---|
252 | protected override DifferenceIn GetDifferenceCore()
|
---|
253 | {
|
---|
254 | return DifferenceIn.Second;
|
---|
255 | }
|
---|
256 |
|
---|
257 | protected override int[] GetTickCountsCore()
|
---|
258 | {
|
---|
259 | return new int[] { 60, 30, 20, 15, 10, 5, 4, 3, 2 };
|
---|
260 | }
|
---|
261 |
|
---|
262 | protected override int GetSpecificValue(TimeSpan start, TimeSpan dt)
|
---|
263 | {
|
---|
264 | return (int)(dt - start).TotalSeconds;
|
---|
265 | }
|
---|
266 |
|
---|
267 | protected override TimeSpan GetStart(TimeSpan start, int value, int step)
|
---|
268 | {
|
---|
269 | double seconds = start.TotalSeconds;
|
---|
270 | double newSeconds = ((int)(seconds / step)) * step;
|
---|
271 | if (newSeconds > seconds) {
|
---|
272 | newSeconds -= step;
|
---|
273 | }
|
---|
274 |
|
---|
275 | return TimeSpan.FromSeconds(newSeconds);
|
---|
276 | //return new TimeSpan(start.Days, start.Hours, start.Minutes, 0);
|
---|
277 | }
|
---|
278 |
|
---|
279 | protected override TimeSpan AddStep(TimeSpan dt, int step)
|
---|
280 | {
|
---|
281 | return dt.Add(TimeSpan.FromSeconds(step));
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | internal sealed class MillisecondTimeSpanProvider : TimeSpanTicksProviderBase
|
---|
286 | {
|
---|
287 | protected override DifferenceIn GetDifferenceCore()
|
---|
288 | {
|
---|
289 | return DifferenceIn.Millisecond;
|
---|
290 | }
|
---|
291 |
|
---|
292 | protected override int[] GetTickCountsCore()
|
---|
293 | {
|
---|
294 | return new int[] { 100, 50, 40, 25, 20, 10, 5, 4, 2 };
|
---|
295 | }
|
---|
296 |
|
---|
297 | protected override int GetSpecificValue(TimeSpan start, TimeSpan dt)
|
---|
298 | {
|
---|
299 | return (int)(dt - start).TotalMilliseconds;
|
---|
300 | }
|
---|
301 |
|
---|
302 | protected override TimeSpan GetStart(TimeSpan start, int value, int step)
|
---|
303 | {
|
---|
304 | double millis = start.TotalMilliseconds;
|
---|
305 | double newMillis = ((int)(millis / step)) * step;
|
---|
306 | if (newMillis > millis) {
|
---|
307 | newMillis -= step;
|
---|
308 | }
|
---|
309 |
|
---|
310 | return TimeSpan.FromMilliseconds(newMillis);
|
---|
311 | //return start;
|
---|
312 | }
|
---|
313 |
|
---|
314 | protected override TimeSpan AddStep(TimeSpan dt, int step)
|
---|
315 | {
|
---|
316 | return dt.Add(TimeSpan.FromMilliseconds(step));
|
---|
317 | }
|
---|
318 | }
|
---|
319 | }
|
---|