2013-05-14 146 views
0

我在画布元素上创建了一个坐标系。我为每一个得到的价值绘制一个红点,并将其与旧的连接起来。WPF坐标系统更新

在这里看到: enter image description here

我越来越每秒约10个值。

1值= 1个像素

红线代表的值,我发现了一个恒定的值只是用于测试。

我的目标是在绘图达到坐标系末端时更新绘图。 我想把我的图画推向左边并绘制下一个点。

我的目标是:

  • 我不想失去点在我的图,因为后来我想放大和缩小
  • 我不希望我的系统慢下来尽可能少...

这是我的代码,但不知道如何可以更新在结尾部分的图....

 static double xOld = 32; 
     static double yOld = 580; 
     static double t = 32; 
     System.Windows.Shapes.Path path; 
     static GeometryGroup lineGroupDrw1 = new GeometryGroup(); 
     .... 


    public void drawPoly(double value) 
    { 

      //increase point position 
      t++; 


      //generate 2 point for the connection 
      Point pOne = new Point(xOld, yOld); 
      Point pTwo = new Point(t, value); 

      //connect old point with new 
      GeometryGroup lineGroup = new GeometryGroup(); 
      LineGeometry connectorGeometry = new LineGeometry(); 
      connectorGeometry.StartPoint = pOne; 
      connectorGeometry.EndPoint = pTwo; 
      lineGroup.Children.Add(connectorGeometry); 
      path = new System.Windows.Shapes.Path(); 
      path.Data = lineGroup; 
      path.StrokeThickness = 1; 
      path.Stroke = path.Fill = Brushes.Red; 


      //fill the static linegroup with a new point 
      lineGroupDrw1.Children.Add(connectorGeometry); 

      if (coordinateSystem.Width > t) 
      { 
       // draw graph 
       coordinateSystem.Children.Add(path); 
      } 
      else 
      { 
       //To do : update drawing 
       updateDrawingEnd(); 
      } 

      //refresh values 
      xOld = t; 
      yOld = value; 

     } 
      .... 

      public void updateDrawingEnd() 
     { 
      path = new System.Windows.Shapes.Path(); 
      path.Data = lineGroupDrw1; 
      path.StrokeThickness = 1; 
      path.Stroke = path.Fill = Brushes.Yellow; 

      coordinateSystem.Children.Add(path); 
      t = 145; 
     } 
+0

发布您的完整代码和XAML。 – 2013-05-14 14:16:43

回答

0

只需将您的UI放入滚动查看器中即可。忘记试图“移动”周围的线路。

<Window x:Class="MiscSamples.SignalGraph" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="SignalGraph" Height="300" Width="300"> 
    <ScrollViewer VerticalScrollBarVisibility="Auto" 
        HorizontalScrollBarVisibility="Auto"> 
     <Grid x:Name="coordinateSystem"> 

     </Grid> 
    </ScrollViewer> 
</Window> 

代码隐藏(从代码中取出并提高一点点)

public partial class SignalGraph : Window 
    { 
     private System.Threading.Timer timer; 
     private Random random = new Random(); 

     public SignalGraph() 
     { 
      InitializeComponent(); 

      timer = new System.Threading.Timer(x => DrawRandomLine(), null, 0, 100); 
     } 

     private void DrawRandomLine() 
     { 
      Dispatcher.BeginInvoke((Action) (() => drawPoly(random.Next(0,100))), null); 
     } 

     static double xOld = 32; 
     static double yOld = 580; 
     static double t = 32; 
     Path path; 
     static GeometryGroup lineGroupDrw1 = new GeometryGroup(); 

     public void drawPoly(double value) 
     { 
      //increase point position 
      t = t+5; 


      //generate 2 point for the connection 
      var pOne = new Point(xOld, yOld); 
      var pTwo = new Point(t, value); 

      //connect old point with new 
      var lineGroup = new GeometryGroup(); 

      var connectorGeometry = new LineGeometry {StartPoint = pOne, EndPoint = pTwo}; 

      lineGroup.Children.Add(connectorGeometry); 
      path = new Path 
         { 
          Data = lineGroup, 
          StrokeThickness = 1, 
          Stroke = Brushes.Red, 
          Fill = Brushes.Red 
         }; 

      //fill the static linegroup with a new point 
      lineGroupDrw1.Children.Add(connectorGeometry); 

      //if (coordinateSystem.ActualWidth > t) 
      //{ 
       // draw graph 
       coordinateSystem.Children.Add(path); 
      //} 
      //else 
      //{ 
      // //To do : update drawing 
      // updateDrawingEnd(); 
      //} 

      //refresh values 
      xOld = t; 
      yOld = value; 

     } 
    } 

结果:

enter image description here

0

我会考虑开发一个特定的用户控件来显示图并为图表数据表示使用单独的类。您可以使用简单的列表或创建提供更多功能的特定类。通过这种方式,您可以将所有点的记录与UI关注点分开保存(例如,如何在记忆中保留不再显示以供进一步使用的点)。 您可以将您的图表对象绑定到您的用户控件,并在此控件中开发特定的逻辑来处理缩放,显示以前的点等等......