-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.vb
291 lines (256 loc) · 11.6 KB
/
Form1.vb
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
Imports System
Imports System.Collections.Generic
Imports System.Globalization
Imports System.IO
Imports System.Windows.Forms
Imports System.Xml
Imports DevExpress.Utils
Imports DevExpress.XtraCharts
Imports DevExpress.XtraEditors
Namespace DateTimeAggregation
Partial Public Class Form1
Inherits Form
Private Const title As String = "Measure Unit: "
Private gridAlignmentItems As List(Of ComboBoxItem)
Protected ReadOnly Property Diagram() As XYDiagram
Get
Return TryCast(chartControl1.Diagram, XYDiagram)
End Get
End Property
Protected ReadOnly Property AxisX() As AxisX
Get
Return Diagram.AxisX
End Get
End Property
Private ReadOnly Property SeriesGBP() As Series
Get
Return chartControl1.Series(0)
End Get
End Property
Public Sub New()
InitializeComponent()
LoadSeries(SeriesGBP, "GbpUsdRate.xml", "GBPUSD", "GBP")
gridAlignmentItems = CreateGridAlignmentComboBoxItems()
FillScaleMode()
FillMeasureUnit()
FillFunctions()
cbMeasureUnit.SelectedIndex = 1
End Sub
Private Function CreateGridAlignmentComboBoxItems() As List(Of ComboBoxItem)
Return New List(Of ComboBoxItem)() From { _
New ComboBoxItem("Auto", AddressOf SetAutoGridAlignment), _
New ComboBoxItem("Year", AddressOf SetYearGridAlignment), _
New ComboBoxItem("Quarter", AddressOf SetQuarterGridAlignment), _
New ComboBoxItem("Month", AddressOf SetMonthGridAlignment), _
New ComboBoxItem("Week", AddressOf SetWeekGridAlignment), _
New ComboBoxItem("Day", AddressOf SetDayGridAlignment) _
}
End Function
Private Sub FillScaleMode()
Dim scaleModeItems As New List(Of ComboBoxItem)() From { _
New ComboBoxItem("Automatic", AddressOf SetAutomaticScaleMode), _
New ComboBoxItem("Manual", AddressOf SetManualScaleMode), _
New ComboBoxItem("Continuous", AddressOf SetContinuousScaleMode) _
}
cbScaleMode.Properties.Items.AddRange(scaleModeItems)
cbScaleMode.SelectedIndex = 1
End Sub
Private Sub FillMeasureUnit()
Dim measureUnitItems As New List(Of ComboBoxItem)() From { _
New ComboBoxItem("Year", AddressOf SetYearMeasureUnit), _
New ComboBoxItem("Quarter", AddressOf SetQuarterMeasureUnit), _
New ComboBoxItem("Month", AddressOf SetMonthMeasureUnit), _
New ComboBoxItem("Week", AddressOf SetWeekMeasureUnit), _
New ComboBoxItem("Day", AddressOf SetDayMeasureUnit) _
}
cbMeasureUnit.Properties.Items.AddRange(measureUnitItems)
End Sub
Private Sub FillFunctions()
Dim aggregateFunctionItems As New List(Of ComboBoxItem)() From { _
New ComboBoxItem("Average", AddressOf SetAverageFunctionItems), _
New ComboBoxItem("Minimum", AddressOf SetMinimumFunctionItems), _
New ComboBoxItem("Maximum", AddressOf SetMaximumFunctionItems), _
New ComboBoxItem("Sum", AddressOf SetSumFunctionItems), _
New ComboBoxItem("Count", AddressOf SetCountFunctionItems) _
}
cbAggregateFunction.Properties.Items.AddRange(aggregateFunctionItems)
cbAggregateFunction.SelectedIndex = 0
End Sub
Private Sub LoadSeries(ByVal series As Series, ByVal xmlFile As String, ByVal name As String, ByVal shortName As String)
LoadPoints(series, AssemblyHelper.GetEmbeddedResourceStream(GetType(ComboBoxItem).Assembly, xmlFile, False))
series.Name = name
End Sub
Private Sub LoadPoints(ByVal series As Series, ByVal xmlStream As Stream)
If series IsNot Nothing AndAlso xmlStream IsNot Nothing Then
Dim document As New XmlDocument()
document.Load(xmlStream)
series.Points.BeginUpdate()
series.Points.Clear()
If document IsNot Nothing Then
Dim elements As XmlNodeList = document.DocumentElement.GetElementsByTagName("CurrencyRate")
For Each element As XmlNode In elements
Dim [date] As Date = Date.Parse(element.ChildNodes(0).InnerText)
Dim rate As Double = Double.Parse(element.ChildNodes(1).InnerText, CultureInfo.InvariantCulture)
series.Points.Add(New SeriesPoint([date], rate))
Next element
End If
series.Points.EndUpdate()
End If
End Sub
Private Sub ResetAxisOptions()
AxisX.DateTimeScaleOptions.GridSpacing = 1
AxisX.Tickmarks.MinorVisible = False
AxisX.Label.Angle = 0
SetAxisXGridAuto()
End Sub
Private Sub SetAxisXGridAuto()
AxisX.DateTimeScaleOptions.AutoGrid = True
End Sub
Private Sub ScaleOptionConditions()
cbGridAlignment.Enabled = True
cbMeasureUnit.Enabled = True
cbAggregateFunction.Enabled = True
Select Case AxisX.DateTimeScaleOptions.ScaleMode
Case ScaleMode.Automatic
cbGridAlignment.Enabled = False
cbMeasureUnit.Enabled = False
Case ScaleMode.Manual
cbGridAlignment.Enabled = True
cbMeasureUnit.Enabled = True
cbAggregateFunction.Enabled = True
Exit Select
Case ScaleMode.Continuous
cbAggregateFunction.Enabled = False
cbMeasureUnit.Enabled = False
End Select
End Sub
Private Sub SetAxisXGridAlignment(ByVal gridAlignment As DateTimeGridAlignment)
AxisX.DateTimeScaleOptions.AutoGrid = False
AxisX.DateTimeScaleOptions.GridAlignment = gridAlignment
End Sub
Private Sub SetAxisXAggregateFunction(ByVal aggregateFunction As AggregateFunction)
AxisX.DateTimeScaleOptions.AggregateFunction = aggregateFunction
End Sub
Private Sub SetAutomaticScaleMode()
ResetAxisOptions()
AxisX.DateTimeScaleOptions.ScaleMode = ScaleMode.Automatic
UpdateGridAlignment(1)
End Sub
Private Sub SetManualScaleMode()
AxisX.DateTimeScaleOptions.ScaleMode = ScaleMode.Manual
UpdateGridAlignment(2)
End Sub
Private Sub SetContinuousScaleMode()
AxisX.DateTimeScaleOptions.ScaleMode = ScaleMode.Continuous
UpdateGridAlignment(3)
End Sub
Private Sub SetYearMeasureUnit()
AxisX.Label.TextPattern = "{A:yyyy}"
AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Year
UpdateGridAlignment(1)
End Sub
Private Sub SetQuarterMeasureUnit()
ResetAxisOptions()
AxisX.Label.TextPattern = "{A:q}"
AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Quarter
UpdateGridAlignment(2)
End Sub
Private Sub SetMonthMeasureUnit()
ResetAxisOptions()
AxisX.Label.TextPattern = "{A:MMMM}"
AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Month
UpdateGridAlignment(3)
End Sub
Private Sub SetWeekMeasureUnit()
ResetAxisOptions()
AxisX.Label.TextPattern = "{A:d MMMM yyyy}"
AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Week
UpdateGridAlignment(4)
End Sub
Private Sub SetDayMeasureUnit()
ResetAxisOptions()
AxisX.Label.TextPattern = "{A:d}"
AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Day
UpdateGridAlignment(5)
End Sub
Private Sub UpdateGridAlignment(ByVal itemsCount As Integer)
itemsCount = Math.Max(itemsCount, 1) + 1
Dim selectedIndex As Integer = Math.Max(cbGridAlignment.SelectedIndex, 0)
cbGridAlignment.Properties.Items.Clear()
cbGridAlignment.Properties.Items.AddRange(gridAlignmentItems.GetRange(0, itemsCount))
cbGridAlignment.SelectedIndex = Math.Min(itemsCount - 1, selectedIndex)
ScaleOptionConditions()
ExecuteSelectedItemAction(cbGridAlignment)
End Sub
Private Sub SetDayGridAlignment()
SetAxisXGridAlignment(DateTimeGridAlignment.Day)
End Sub
Private Sub SetWeekGridAlignment()
SetAxisXGridAlignment(DateTimeGridAlignment.Week)
End Sub
Private Sub SetMonthGridAlignment()
SetAxisXGridAlignment(DateTimeGridAlignment.Month)
End Sub
Private Sub SetQuarterGridAlignment()
SetAxisXGridAlignment(DateTimeGridAlignment.Quarter)
End Sub
Private Sub SetYearGridAlignment()
SetAxisXGridAlignment(DateTimeGridAlignment.Year)
End Sub
Private Sub SetAutoGridAlignment()
SetAxisXGridAuto()
End Sub
Private Sub SetCountFunctionItems()
SetAxisXAggregateFunction(AggregateFunction.Count)
End Sub
Private Sub SetSumFunctionItems()
SetAxisXAggregateFunction(AggregateFunction.Sum)
End Sub
Private Sub SetMaximumFunctionItems()
SetAxisXAggregateFunction(AggregateFunction.Maximum)
End Sub
Private Sub SetMinimumFunctionItems()
SetAxisXAggregateFunction(AggregateFunction.Minimum)
End Sub
Private Sub SetAverageFunctionItems()
SetAxisXAggregateFunction(AggregateFunction.Average)
End Sub
Private Sub ExecuteSelectedItemAction(ByVal comboBox As ComboBoxEdit)
Dim item As ComboBoxItem = TryCast(comboBox.SelectedItem, ComboBoxItem)
If item IsNot Nothing Then
Dim itemAction As Action = item.Action
If itemAction IsNot Nothing Then
itemAction()
End If
End If
End Sub
Private Sub ComboBoxSelectedValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbAggregateFunction.SelectedIndexChanged, cbGridAlignment.SelectedValueChanged, cbScaleMode.SelectedValueChanged, cbMeasureUnit.SelectedValueChanged
Dim comboBox As ComboBoxEdit = TryCast(sender, ComboBoxEdit)
If comboBox IsNot Nothing Then
ExecuteSelectedItemAction(comboBox)
End If
End Sub
Private Sub checkEdit1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles checkEdit1.CheckedChanged
AxisX.DateTimeScaleOptions.WorkdaysOnly = DirectCast(sender, CheckEdit).Checked
End Sub
End Class
Public Class ComboBoxItem
Private titleText As String
Private privateAction As Action
Public Property Action() As Action
Get
Return privateAction
End Get
Private Set(ByVal value As Action)
privateAction = value
End Set
End Property
Public Sub New(ByVal titleText As String, ByVal action As Action)
Me.titleText = titleText
Me.Action = action
End Sub
Public Overrides Function ToString() As String
Return titleText
End Function
End Class
End Namespace