Skip to content

Commit

Permalink
Redo scaling to center initial point and push high/low water marks as…
Browse files Browse the repository at this point in the history
… needed
  • Loading branch information
jschementi committed May 1, 2011
1 parent 6f11545 commit b1edcc6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Schementi.Controls.Demos.Sparkline/MainWindow.xaml.cs
Expand Up @@ -12,9 +12,10 @@ public partial class MainWindow {

var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.5) };
var random = new Random();
var x = 20.0;
var x = 100.0;
timer.Tick += (s, e) => {
foreach (var sparkline in sparklines) {
//x = random.Next(100, 200);
x = x + (random.NextDouble() * 10 * random.NextDouble() * (random.NextDouble() < 0.5 ? -1 : 1));
if (x < 1) x = Math.Abs(x) + 1;
Console.WriteLine(x);
Expand Down
32 changes: 26 additions & 6 deletions Schementi.Controls.Sparkline/Sparkline.xaml.cs
Expand Up @@ -240,6 +240,7 @@ private void OnShowWatermarksPropertyChanged()
}

private void ResetTimeSeries() {
_both = _lower = _higher = false;
Canvas.Children.Clear();
Canvas.Children.Add(_polyline);
Canvas.Children.Add(_lowwatermark);
Expand Down Expand Up @@ -301,14 +302,33 @@ private void OnShowWatermarksPropertyChanged()
else if (y < LowWaterMark) LowWaterMark = y;
}

private double _lowMargin;
private double _height;
private bool _lower;
private bool _higher;
private bool _both;

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));
if (TimeSeries.Count < 2) {
Canvas.Height = _height = y + MinYRange;
_lowMargin = -y + MinYRange;
Canvas.Margin = new Thickness(0, 0, 0, _lowMargin);
return;
}
if (!_both && LowWaterMark != null && LowWaterMark < _lowMargin * -1) {
_lower = true;
_lowMargin = -LowWaterMark.Value;
Canvas.Margin = new Thickness(0, 0, 0, _lowMargin);
_both = _lower && _higher;
}
if (!_both && HighWaterMark != null && HighWaterMark > _height) {
_higher = true;
Canvas.Height = HighWaterMark.Value;
_both = _lower && _higher;
}
if (!_both) return;
Canvas.Height = double.NaN;
Canvas.Margin = new Thickness(0, 0, 0, -(LowWaterMark ?? 0));
}
#endregion
}
Expand Down

0 comments on commit b1edcc6

Please sign in to comment.