Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DateTimeAxis.cs « MonoDevelop.Components.Chart « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e1bc83998ec5c9830ad44636e4bc9b61270821ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// DateTimeAxis.cs
//
// Author:
//   Lluis Sanchez Gual
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;

namespace MonoDevelop.Components.Chart
{
	public class DateTimeAxis: Axis
	{
		public DateTimeAxis ()
		{
		}
		
		public DateTimeAxis (bool showLabels): base (showLabels)
		{
		}
		
		protected override TickEnumerator CreateTickEnumerator (double minTickStep)
		{
			long val = (long) minTickStep;
			int scale;
			
			if (val > TimeSpan.TicksPerDay * 30 * 365)
				return null;
			else if (val > TimeSpan.TicksPerDay * 30)
				scale = 7;
			else if (val > TimeSpan.TicksPerDay)
				scale = 6;
			else if (val > TimeSpan.TicksPerHour)
				scale = 5;
			else if (val > TimeSpan.TicksPerMinute * 15)
				scale = 4;
			else if (val > TimeSpan.TicksPerMinute)
				scale = 3;
			else if (val > TimeSpan.TicksPerSecond * 15)
				scale = 2;
			else if (val > TimeSpan.TicksPerSecond)
				scale = 1;
			else
				scale = 0;
			
			return new DateTimeTickEnumerator (scale);
		}
	}
	
	internal class DateTimeTickEnumerator: TickEnumerator
	{
		int scale;
		DateTime current;
		
		public DateTimeTickEnumerator (int scale)
		{
			this.scale = scale;
		}
		
		public override void Init (double startValue)
		{
			DateTime t = new DateTime ((long)startValue);
			DateTime nt;
			switch (scale) {
				case 0: nt = new DateTime (t.Year, t.Month, t.Day, t.Hour, t.Minute, t.Second); break;
				case 1: nt = new DateTime (t.Year, t.Month, t.Day, t.Hour, t.Minute, (t.Second / 15) * 15); break;
				case 2: nt = new DateTime (t.Year, t.Month, t.Day, t.Hour, t.Minute, 0); break;
				case 3: nt = new DateTime (t.Year, t.Month, t.Day, t.Hour, (t.Minute / 15) * 15, 0); break;
				case 4: nt = new DateTime (t.Year, t.Month, t.Day, t.Hour, 0, 0); break;
				case 5: nt = new DateTime (t.Year, t.Month, t.Day); break;
				case 6: nt = new DateTime (t.Year, t.Month, 1); break;
				default: nt = new DateTime (t.Year, 1, 1); break;
			}
			current = nt;
		}
		
		public override void MoveNext ()
		{
			switch (scale) {
				case 0: current = current.AddSeconds (1); break;
				case 1: current = current.AddSeconds (15); break;
				case 2: current = current.AddMinutes (1); break;
				case 3: current = current.AddMinutes (15); break;
				case 4: current = current.AddHours (1); break;
				case 5: current = current.AddDays (1); break;
				case 6: current = current.AddMonths (1); break;
				case 7: current = current.AddYears (1); break;
			}
		}
		
		public override void MovePrevious ()
		{
			switch (scale) {
				case 0: current = current.AddSeconds (-1); break;
				case 1: current = current.AddSeconds (-15); break;
				case 2: current = current.AddMinutes (-1); break;
				case 3: current = current.AddMinutes (-15); break;
				case 4: current = current.AddHours (-1); break;
				case 5: current = current.AddDays (-1); break;
				case 6: current = current.AddMonths (-1); break;
				case 7: current = current.AddYears (-1); break;
			}
		}
		
		public override double CurrentValue {
			get { return (double) current.Ticks; }
		}
		
		public override string CurrentLabel {
			get {
				switch (scale) {
					case 0: case 1: return string.Format ("{0}:{1:00}:{2:00}", current.Hour, current.Minute, current.Second);
					case 2: case 3: return string.Format ("{0}:{1:00}", current.Hour, current.Minute);
					case 4: return string.Format ("{0}:00", current.Hour);
					case 5: return current.ToShortDateString ();
					case 6: return string.Format ("{0}/{1}", current.Month, current.Year);
					default: return string.Format ("{0}", current.Year);
				}
			}
		}
	}
}