Permalink
Browse files

some cleanup

  • Loading branch information...
1 parent d7bb537 commit a5fffc42844692c6ce0542db8933ced407add265 @jschementi committed Apr 24, 2011
@@ -4,11 +4,7 @@
using System.Windows.Threading;
namespace Schementi.Controls.Demos.Sparkline {
-
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window {
+ public partial class MainWindow {
public MainWindow() {
InitializeComponent();
@@ -1,11 +1,9 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
-using System.ComponentModel;
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;
@@ -27,7 +25,7 @@ public class TimeSeries : ObservableCollection<TimeValue> {
/// <summary>
/// Interaction logic for Sparkline.xaml
/// </summary>
- public partial class Sparkline : UserControl {
+ public partial class Sparkline {
#region Dependency Properties
#region Points
public static DependencyProperty TimeSeriesProperty = DependencyProperty.Register(
@@ -115,10 +113,10 @@ public partial class Sparkline : UserControl {
new PropertyMetadata(false, OnShowWatermarksPropertyChanged));
private static void OnShowWatermarksPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
- ((Sparkline)d).OnShowWatermarksPropertyChanged(e);
+ ((Sparkline)d).OnShowWatermarksPropertyChanged();
}
- private void OnShowWatermarksPropertyChanged(DependencyPropertyChangedEventArgs e)
+ private void OnShowWatermarksPropertyChanged()
{
if (ShowWatermarks) {
_lowwatermark = new Rectangle {
@@ -127,19 +125,19 @@ private void OnShowWatermarksPropertyChanged(DependencyPropertyChangedEventArgs
Height = 1,
VerticalAlignment = VerticalAlignment.Top
};
- BindingOperations.SetBinding(_lowwatermark, FrameworkElement.MarginProperty,
+ BindingOperations.SetBinding(_lowwatermark, MarginProperty,
new Binding("LowWaterMark") { Source = this, Converter = new YCoordinateToThicknessConverter() });
- this.Canvas.Children.Add(_lowwatermark);
+ Canvas.Children.Add(_lowwatermark);
_highwatermark = new Rectangle {
Fill = Brushes.Green,
Opacity = 0.5,
Height = 1,
VerticalAlignment = VerticalAlignment.Top
};
- BindingOperations.SetBinding(_highwatermark, FrameworkElement.MarginProperty,
+ BindingOperations.SetBinding(_highwatermark, MarginProperty,
new Binding("HighWaterMark") { Source = this, Converter = new YCoordinateToThicknessConverter() });
- this.Canvas.Children.Add(_highwatermark);
+ Canvas.Children.Add(_highwatermark);
} else {
if (_lowwatermark != null) {
Canvas.Children.Remove(_lowwatermark);
@@ -161,11 +159,11 @@ private void OnShowWatermarksPropertyChanged(DependencyPropertyChangedEventArgs
#endregion
#region Fields
- private int _nextXValue = 0;
+ private int _nextXValue;
private const int XWidth = 10;
- private readonly Polyline _polyline;
+ private Polyline _polyline;
private Rectangle _highwatermark;
private Rectangle _lowwatermark;
@@ -175,14 +173,16 @@ private void OnShowWatermarksPropertyChanged(DependencyPropertyChangedEventArgs
public Sparkline() {
InitializeComponent();
TimeSeries = new TimeSeries();
+ InitializePolyline();
+ }
- _polyline = new Polyline();
+ private void InitializePolyline() {
+ _polyline = new Polyline();
BindingOperations.SetBinding(_polyline, Shape.StrokeProperty,
- new Binding("Foreground") {Mode = BindingMode.TwoWay, Source = this});
+ new Binding("Foreground") { Mode = BindingMode.TwoWay, Source = this });
BindingOperations.SetBinding(_polyline, Shape.StrokeThicknessProperty,
new Binding("StrokeThickness") { Mode = BindingMode.TwoWay, Source = this });
- this.Canvas.Children.Add(_polyline);
- this.OnShowWatermarksPropertyChanged(new DependencyPropertyChangedEventArgs());
+ Canvas.Children.Add(_polyline);
}
public void AddTimeValue(double value, DateTime? time = null) {
@@ -235,13 +235,16 @@ private void OnShowWatermarksPropertyChanged(DependencyPropertyChangedEventArgs
}
private void DrawTimeValue(TimeValue newTimeValue) {
- AddPoint(GetPoint(newTimeValue));
+ var point = GetPoint(newTimeValue);
+ AddPoint(point);
ScrollViewer.ScrollToRightEnd();
}
private void AddPoint(Point point) {
_polyline.Points.Add(point);
- if (this.PointRadius > 0.0)
+ SetWatermarks(point.Y);
+ SetCanvasHeight();
+ if (PointRadius > 0.0)
Canvas.Children.Add(DrawDot(point));
_nextXValue++;
}
@@ -255,19 +258,22 @@ private void OnShowWatermarksPropertyChanged(DependencyPropertyChangedEventArgs
}
private Point GetPoint(TimeValue timeValue) {
- var y = timeValue.Value;
+ 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;
+ }
- if (HighWaterMark > Canvas.ActualHeight)
+ private void SetCanvasHeight() {
+ if (HighWaterMark != null && HighWaterMark > Canvas.ActualHeight)
Canvas.Height = HighWaterMark.Value;
-
- return new Point(_nextXValue * XWidth, timeValue.Value);
}
#endregion
}

0 comments on commit a5fffc4

Please sign in to comment.