WPF之動態加載曲線


首先說一下思路: 先創建一個控件(其實就是一個canvas),在canvas里面生成一條線,給這條線綁定一個PointCollection,在主界面中用一個定時器改變這個PointCollection的值就行了.

 

1.創建的控件

 public partial class BrokenLine : UserControl
    {                     
        public BrokenLine()
        {
            InitializeComponent();
            this.Loaded += BrokenLine_Loaded;
        }
        private Brush mBrushes;
        private PointCollection coordinatePoints = new PointCollection();
        Polyline curvePolyline = new Polyline();
        public BrokenLine(PointCollection pointCollection,Brush brushes)
        {
            InitializeComponent();
            this.Loaded += BrokenLine_Loaded;
            coordinatePoints = pointCollection;
            mBrushes = brushes;
        }
        private void BrokenLine_Loaded(object sender, RoutedEventArgs e)
        {
            curvePolyline = new Polyline();
            curvePolyline.Stroke = mBrushes;
            curvePolyline.StrokeThickness = 1;
            Canvas.SetLeft(curvePolyline, 5);
            Canvas.SetTop(curvePolyline, 5);
            curvePolyline.Points = coordinatePoints;
            chartCanvas.Children.Add(curvePolyline);
        }
    }
View Code

 

2.主界面的代碼

  BrokenLine xinlv_brokenLine = new BrokenLine(mXL.CoordinatePoints, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#35CD75")));
                    Canvas.SetTop(xinlv_brokenLine, 20);
                    canvas_TZ.Children.Add(xinlv_brokenLine);
實例化一個控件
   if (points!=null && points.Count>10)///points作為一個緩存集合
                {
                    if (points.Count >= 10)
                    {
                        for (int k = 0; k < 10; k++)
                        {
                            CoordinatePoints.Add(points[k]);
                            if (coordinatePoints.Count > mShowLength)//mShowLength控制最多能顯示的長度(點的個數)
                            {
                                AddCurvePoint(true, 0.8);//將要顯示的點全部向前移動一位
                            }
                        }
                        points.RemoveRange(0, 10);
                    }
                    else {
                        for (int k = 0; k < points.Count; k++)
                        {
                            CoordinatePoints.Add(points[k]);
                            if (coordinatePoints.Count > mShowLength)
                            {
                                AddCurvePoint(true, 0.8);
                            }
                        }
                        points.Clear();
                    }

                }
定時器代碼
       private void AddCurvePoint(Boolean isMeet, double length)
        {
            if (isMeet)
            {
                CoordinatePoints.RemoveAt(0);
                for (int i = 0; i < CoordinatePoints.Count-1; i++)
                {
                    CoordinatePoints[i] = new Point(CoordinatePoints[i].X - length, CoordinatePoints[i].Y);
                }
                CoordinatePoints[coordinatePoints.Count-1] = new Point(CoordinatePoints[coordinatePoints.Count-1].X - length*mX, CoordinatePoints[coordinatePoints.Count - 1].Y);
                mX++;
            }


        }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM