Permalink
Browse files

Add Action<Point, Panel> callback to Sparkline.AddTimeValue

  • Loading branch information...
1 parent b6c38c1 commit 4d351056783846aa6577df1250a87f01309b4f52 @jschementi committed Jul 8, 2011
@@ -19,7 +19,7 @@
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<schementi:Sparkline Margin="0 1"
- Foreground="#FFEEEEEE" ShowLatestLevel="True" />
+ Foreground="#FFEEEEEE" />
<schementi:Sparkline Grid.Column="0" Grid.Row="2" Margin="0 2"
Foreground="#FFEEEEEE"
PointFill="White" PointRadius="1"/>
@@ -74,6 +74,9 @@
<Link>MainPage.xaml.cs</Link>
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
+ <Compile Include="..\Schementi.Controls.Demos.Sparkline\SimpleRNG.cs">
+ <Link>SimpleRNG.cs</Link>
+ </Compile>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
@@ -13,10 +13,17 @@
// limitations under the License.
using System;
+using System.Collections.Generic;
using System.Linq;
using System.Threading;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media;
+using System.Windows.Shapes;
+using TestSimpleRNG;
namespace Schementi.Controls.Demos.Sparkline {
+
public partial class MainPage {
static readonly object StopLock = new object();
public MainPage() {
@@ -27,33 +34,40 @@ public partial class MainPage {
private Controls.Sparkline[] _sparklines;
private Controls.Sparkline[] Sparklines {
get {
- return _sparklines ?? (_sparklines = LayoutRoot.Children.OfType<Controls.Sparkline>().ToArray());
+ return _sparklines ?? (_sparklines = FindVisualChildren<Controls.Sparkline>(LayoutRoot).ToArray());
}
}
private void StartSparkline() {
- var sparklinesCount = Sparklines.Length;
- const int tickTime = 1000/2;
+ const int tickTime = 1000 / 2;
var stop = false;
- WaitCallback tickLoop = wc => {
- while (true) {
- var x = 100.0;
- for (var i = 0; i < sparklinesCount; i++) {
- var random = new Random();
- //x = random.Next(100, 200);
- x = x + (random.NextDouble() * 10 * random.NextDouble() * (random.NextDouble() < random.NextDouble() ? -1 : 1));
+ int lineCount = 0;
+ Func<Controls.Sparkline, Action> nextTick = s => {
+ var currentLine = lineCount++;
+ var x = 100.0;
+ return () => {
+ var count = 0;
+ while (true) {
+ x = x + SimpleRNG.GetNormal()*2;
if (x < 1) x = Math.Abs(x) + 1;
Console.WriteLine(x);
- var i1 = i;
- var x1 = x;
- Dispatcher.BeginInvoke(new Action(() => Sparklines[i1].AddTimeValue(x1)));
+ Action eachTick = () =>
+ s.AddTimeValue(x, null, (point, panel) =>
+ DrawFlag(currentLine, s, point, panel, ref count));
+
+ Dispatcher.BeginInvoke(eachTick);
+
+ Thread.Sleep(tickTime);
+ lock (StopLock) if (stop) return;
}
- Thread.Sleep(tickTime);
- lock (StopLock) if (stop) return;
- }
+ };
};
- Action start = () => ThreadPool.QueueUserWorkItem(tickLoop);
+
+ var tickGenerators = Sparklines.Select(nextTick).ToArray();
+
+ Func<Action, WaitCallback> tickLoop = gen => wc => gen();
+ Action start = () => { foreach (var gen in tickGenerators) ThreadPool.QueueUserWorkItem(tickLoop(gen)); };
Loaded += (s, e) => start();
Unloaded += (s, e) => {
@@ -62,5 +76,64 @@ public partial class MainPage {
}
};
}
+
+ public void DrawFlag(int currentLine, Controls.Sparkline s, Point point, Panel panel, ref int count) {
+ if (currentLine > 0) return;
+ if (SimpleRNG.GetUniform() < 0.75) return;
+
+ var path = new Path();
+ var circle = new EllipseGeometry { Center = point, RadiusX = 0.5, RadiusY = 0.5 };
+ path.Fill = new SolidColorBrush(Colors.Red);
+ path.Data = circle;
+ panel.Children.Add(path);
+
+ var rect = new Grid {
+ HorizontalAlignment = HorizontalAlignment.Left,
+ Margin = new Thickness(point.X - 0.25, point.Y, 0, 0).Align(s.LineMargin),
+ };
+ var line = new Border { Background = new SolidColorBrush(Colors.Gray), Width = 0.4, HorizontalAlignment = System.Windows.HorizontalAlignment.Left, UseLayoutRounding = false };
+ var box = new Border {
+ Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x11, 0x11, 0x11)),
+ BorderBrush = new SolidColorBrush(Colors.Gray),
+ BorderThickness = new Thickness(0.4),
+ Padding = new Thickness(0.5, 0.5, 0.5, 0.5),
+ VerticalAlignment = VerticalAlignment.Bottom,
+ RenderTransform = new ScaleTransform { ScaleY = -1.0 },
+ RenderTransformOrigin = new Point(0.0, 0.0),
+ };
+ var text = new TextBlock {
+ Text = Convert.ToString(Math.Round(point.Y, 2)),
+ FontSize = 2,
+ Foreground = new SolidColorBrush(Colors.White),
+ };
+ box.Child = text;
+ rect.Children.Add(line);
+ rect.Children.Add(box);
+
+ panel.Children.Insert(count++, rect);
+ }
+
+ public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject {
+ if (depObj == null) yield break;
+
+ for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) {
+ var child = VisualTreeHelper.GetChild(depObj, i);
+ if (child != null && child is T)
+ yield return (T) child;
+
+ foreach (var childOfChild in FindVisualChildren<T>(child))
+ yield return childOfChild;
+ }
+ }
+ }
+
+ public static class ThicknessExtensions {
+ public static Thickness Align(this Thickness lhs, Thickness rhs) {
+ return new Thickness(
+ lhs.Left + rhs.Left,
+ lhs.Top - rhs.Top,
+ lhs.Right + rhs.Right,
+ lhs.Bottom - rhs.Bottom);
+ }
}
}
@@ -58,6 +58,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
+ <Compile Include="SimpleRNG.cs" />
<Page Include="MainPage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Oops, something went wrong.

0 comments on commit 4d35105

Please sign in to comment.