/
Sparkline.xaml.cs
325 lines (281 loc) · 11.9 KB
/
Sparkline.xaml.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Schementi.Controls {
public class TimeValue {
public DateTime Time;
public double Value;
}
public class TimeSeries : ObservableCollection<TimeValue> {
public void AddTimeValue(double value, DateTime? dateTime = null) {
if (dateTime == null) dateTime = DateTime.Now;
Add(new TimeValue { Time = dateTime.Value, Value = value });
}
}
/// <summary>
/// Interaction logic for Sparkline.xaml
/// </summary>
public partial class Sparkline {
#region Dependency Properties
#region Points
public static DependencyProperty TimeSeriesProperty = DependencyProperty.Register(
"TimeSeries",
typeof(TimeSeries),
typeof(Sparkline),
new PropertyMetadata(new TimeSeries(), OnTimeSeriesPropertyChanged));
public TimeSeries TimeSeries {
get { return (TimeSeries)GetValue(TimeSeriesProperty); }
set { SetValue(TimeSeriesProperty, value); }
}
#endregion
#region StrokeThickness
public static DependencyProperty StrokeThicknessProperty = DependencyProperty.Register(
"StrokeThickness",
typeof(double),
typeof(Sparkline),
new PropertyMetadata(0.5));
public double StrokeThickness {
get { return (double)GetValue(StrokeThicknessProperty); }
set { SetValue(StrokeThicknessProperty, value); }
}
#endregion
#region PointFill
public static DependencyProperty PointFillProperty = DependencyProperty.Register(
"PointFill",
typeof(Brush),
typeof(Sparkline),
new PropertyMetadata(Brushes.Black));
public Brush PointFill {
get { return (Brush)GetValue(PointFillProperty); }
set { SetValue(PointFillProperty, value); }
}
#endregion
#region PointRadius
public static DependencyProperty PointRadiusProperty = DependencyProperty.Register(
"PointRadius",
typeof(double),
typeof(Sparkline),
new PropertyMetadata(0.0));
public double PointRadius {
get { return (double)GetValue(PointRadiusProperty); }
set { SetValue(PointRadiusProperty, value); }
}
#endregion
#region HighWaterMark
public static DependencyProperty HighWaterMarkProperty = DependencyProperty.Register(
"HighWaterMark",
typeof(double?),
typeof(Sparkline),
new PropertyMetadata(null));
public double? HighWaterMark {
get { return (double?)GetValue(HighWaterMarkProperty); }
set { SetValue(HighWaterMarkProperty, value); }
}
#endregion
#region LowWaterMark
public static DependencyProperty LowWaterMarkProperty = DependencyProperty.Register(
"LowWaterMark",
typeof(double?),
typeof(Sparkline),
new PropertyMetadata(null));
public double? LowWaterMark {
get { return (double?)GetValue(LowWaterMarkProperty); }
set { SetValue(LowWaterMarkProperty, value); }
}
#endregion
#region ShowWatermarks
public static DependencyProperty ShowWatermarksProperty = DependencyProperty.Register(
"ShowWatermarks",
typeof(bool),
typeof(Sparkline),
new PropertyMetadata(false, OnShowWatermarksPropertyChanged));
private static void OnShowWatermarksPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
((Sparkline)d).OnShowWatermarksPropertyChanged();
}
private void OnShowWatermarksPropertyChanged()
{
if (ShowWatermarks) {
_lowwatermark = new Rectangle {
Fill = Brushes.Red,
Opacity = 0.5,
Height = this.StrokeThickness,
VerticalAlignment = VerticalAlignment.Top
};
BindingOperations.SetBinding(_lowwatermark, MarginProperty,
new Binding("LowWaterMark") { Source = this, Converter = new YCoordinateToThicknessConverter() });
Canvas.Children.Insert(0, _lowwatermark);
_highwatermark = new Rectangle {
Fill = Brushes.Green,
Opacity = 0.5,
Height = this.StrokeThickness,
VerticalAlignment = VerticalAlignment.Top
};
BindingOperations.SetBinding(_highwatermark, MarginProperty,
new Binding("HighWaterMark") { Source = this, Converter = new YCoordinateToThicknessConverter() });
Canvas.Children.Insert(0, _highwatermark);
} else {
if (_lowwatermark != null) {
Canvas.Children.Remove(_lowwatermark);
_lowwatermark = null;
}
if (_highwatermark != null) {
Canvas.Children.Remove(_highwatermark);
_highwatermark = null;
}
}
}
public bool ShowWatermarks {
get { return (bool)GetValue(ShowWatermarksProperty); }
set { SetValue(ShowWatermarksProperty, value); }
}
#endregion
#region MinYRange
public static DependencyProperty MinYRangeProperty = DependencyProperty.Register(
"MinYRange",
typeof(double),
typeof(Sparkline),
new PropertyMetadata(25.0));
public double MinYRange {
get { return (double)GetValue(MinYRangeProperty); }
set { SetValue(MinYRangeProperty, value); }
}
#endregion
#endregion
#region Fields
private int _nextXValue;
private const int XWidth = 10;
private const int MinValue = 0;
private const int MaxValue = 100;
private Polyline _polyline;
private Rectangle _highwatermark;
private Rectangle _lowwatermark;
#endregion
#region Public API
public Sparkline() {
InitializeComponent();
TimeSeries = new TimeSeries();
InitializePolyline();
}
private void InitializePolyline() {
_polyline = new Polyline();
BindingOperations.SetBinding(_polyline, Shape.StrokeProperty,
new Binding("Foreground") { Mode = BindingMode.TwoWay, Source = this });
BindingOperations.SetBinding(_polyline, Shape.StrokeThicknessProperty,
new Binding("StrokeThickness") { Mode = BindingMode.TwoWay, Source = this });
Canvas.Children.Add(_polyline);
}
public void AddTimeValue(double value, DateTime? time = null) {
TimeSeries.AddTimeValue(value, time);
}
#endregion
#region Implementation
private static void OnTimeSeriesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
((Sparkline)d).OnTimeSeriesPropertyChanged(e);
}
private void OnTimeSeriesPropertyChanged(DependencyPropertyChangedEventArgs e) {
if (e.OldValue != null) ((TimeSeries)e.OldValue).CollectionChanged -= TimeSeriesCollectionChanged;
if (e.NewValue != null) ((TimeSeries)e.NewValue).CollectionChanged += TimeSeriesCollectionChanged;
}
private void TimeSeriesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
switch (e.Action) {
case NotifyCollectionChangedAction.Add:
foreach (var timeValue in e.NewItems.OfType<TimeValue>()) DrawTimeValue(timeValue);
break;
case NotifyCollectionChangedAction.Move:
ResetTimeSeries();
break;
case NotifyCollectionChangedAction.Remove:
ResetTimeSeries();
break;
case NotifyCollectionChangedAction.Replace:
ResetTimeSeries();
break;
case NotifyCollectionChangedAction.Reset:
ResetTimeSeries();
break;
default:
break;
}
}
private void ResetTimeSeries() {
Canvas.Children.Clear();
Canvas.Children.Add(_polyline);
Canvas.Children.Add(_lowwatermark);
Canvas.Children.Add(_highwatermark);
Canvas.Height = double.NaN;
_nextXValue = 0;
foreach (var timeValue in TimeSeries) {
DrawTimeValue(timeValue);
}
}
private void DrawTimeValue(TimeValue newTimeValue) {
var point = GetPoint(newTimeValue);
AddPoint(point);
ScrollViewer.ScrollToRightEnd();
}
private void AddPoint(Point point) {
_polyline.Points.Add(point);
SetWatermarks(point.Y);
SetCanvasHeight(point.Y);
if (PointRadius > 0.0)
Canvas.Children.Add(DrawDot(point));
//#if DEBUG
// var textbox = new TextBlock {
// Text = Convert.ToString(Math.Round(point.Y, 2)),
// FontSize = 3,
// Foreground = this.Foreground,
// Opacity = 0.25,
// Padding = new Thickness(2.0, 0, 0, 0),
// Margin = new Thickness(point.X, point.Y + 1, 0, 0),
// RenderTransform = new ScaleTransform { ScaleY = -1.0 },
// RenderTransformOrigin = new Point(0.0, 0.0),
// };
// Canvas.Children.Add(textbox);
//#endif
_nextXValue++;
}
private Path DrawDot(Point center) {
var path = new Path();
var circle = new EllipseGeometry(center, StrokeThickness, StrokeThickness);
path.Fill = PointFill;
path.Data = circle;
return path;
}
private Point GetPoint(TimeValue timeValue) {
return new Point(_nextXValue * XWidth, timeValue.Value);
}
private void SetWatermarks(double y) {
if (LowWaterMark == null)
LowWaterMark = y;
if (HighWaterMark == null)
HighWaterMark = y;
if (y > HighWaterMark) HighWaterMark = y;
else if (y < LowWaterMark) LowWaterMark = y;
}
private void SetCanvasHeight(double y) {
var range = HighWaterMark - LowWaterMark;
if (range < MinYRange) {
Canvas.Height = (LowWaterMark ?? 0) + MinYRange;
} else {
Canvas.Height = double.NaN;
Canvas.Margin = new Thickness(0, 0, 0, -(LowWaterMark ?? 0));
}
}
#endregion
}
public class YCoordinateToThicknessConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
return new Thickness(0, (double?)value ?? 0, 0, 0);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotSupportedException();
}
}
}