-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChartView.cs
258 lines (193 loc) · 5.36 KB
/
ChartView.cs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Herring
{
internal class ChartView : PictureBox
{
public class Bar
{
private int minutes;
private Bar(int minutes)
{
this.minutes = minutes;
}
public static Bar FromBar(int bar)
{
return new Bar(bar*5);
}
public static Bar FromMinutes(int minutes)
{
return new Bar(minutes);
}
public int Minutes
{
get => minutes;
set => minutes = value;
}
public int Number
{
get => Minutes / 5;
set => minutes = value * 5;
}
public TimeSpan Span => TimeSpan.FromMinutes(minutes);
}
private readonly Chart chart = new Chart();
private Bar hoveredBar;
private readonly int minutesInBar = 5;
private readonly int pixelsPerBar = 4;
// IN 5 min quants
private int selectionStart = -1;
private int selectionEnd = -1;
private int start = -1;
Log log;
private readonly int BarsInDay = 24 * 12;
private int ScreenToBar(int screen)
{
return screen / pixelsPerBar;
}
private int BarToScreen(int bar)
{
return bar * pixelsPerBar;
}
private int BarToMinutes(int bar)
{
return bar * minutesInBar;
}
/// <summary>
/// Informs about changed seletion (start or span).
/// </summary>
public event EventHandler SelectionChanged;
/// <summary>
/// Returns only minutes/hours
/// </summary>
public DateTime? SelectionStart
{
get
{
if (selectionStart == -1)
return null;
var span = new TimeSpan(0, ScreenToBar(selectionStart) * 5, 0);
var dateTime = new DateTime() + span;
return dateTime;
}
}
public TimeSpan? SelectionSpan
{
get
{
if (selectionStart == -1 || selectionEnd == -1)
return null;
var range = Math.Max(0, selectionEnd - selectionStart);
var span = new TimeSpan(0, ScreenToBar(range) * 5, 0);
return span;
}
}
[Browsable(false)]
public Bar SelectedBar
{
get => hoveredBar;
set
{
if (hoveredBar == value)
return;
hoveredBar = value;
RepaintLog();
}
}
public ChartView()
{
Init();
}
public void UpdateChart(ActivitySummary summary)
{
chart.UpdateChart(summary);
Invalidate();
}
public void RefreshChart(Log items)
{
log = items;
RepaintLog();
}
private void RepaintLog()
{
chart.CreateChart(log, hoveredBar?.Number, selectionStart, selectionEnd);
Image = chart.Bitmap;
}
private void Init()
{
BorderStyle = BorderStyle.Fixed3D;
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
var x = Math.Min(Math.Max(0, e.X), BarToScreen(BarsInDay));
start = (x / pixelsPerBar) * pixelsPerBar;
selectionStart = -1;
selectionEnd = -1;
OnSelectionChanged();
RepaintLog();
}
else
{
var newSelection = Bar.FromBar(ScreenToBar(e.X));
if (newSelection == hoveredBar)
return;
hoveredBar = newSelection;
RepaintLog();
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (Math.Abs(selectionStart - selectionEnd) < 5)
{
selectionStart = -1;
selectionEnd = -1;
start = -1;
OnSelectionChanged();
RepaintLog();
}
}
else
{
var newSelection = Bar.FromBar( Math.Max(BarsInDay, ScreenToBar(e.X)));
if (newSelection == hoveredBar)
return;
hoveredBar = newSelection;
RepaintLog();
}
base.OnMouseUp(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
var x = Math.Min(Math.Max(0, e.X), BarToScreen(BarsInDay));
var newSelection = Bar.FromBar(Math.Min(BarsInDay, ScreenToBar(e.X)));
if (e.Button == MouseButtons.Left && Math.Abs(start - x) >= 5) // Draging
{
selectionEnd = Math.Max(start, x);
selectionStart = Math.Min(start, x);
OnSelectionChanged();
hoveredBar = newSelection;
RepaintLog();
return;
}
if (e.Button == MouseButtons.None)
{
if (newSelection == hoveredBar)
return;
hoveredBar = newSelection;
RepaintLog();
}
base.OnMouseMove(e);
}
private void OnSelectionChanged()
{
SelectionChanged?.Invoke(this, EventArgs.Empty);
}
}
}