• 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
  • 相关阅读:
    OSS系列【springboot集成Minio】
    Linux系列【使用LVM对目录进行扩容】
    Linux系列【使用lvm对磁盘进行分区管理】
    Linux系列【根目录空间满了,如何快速扩容?】
    开发工具系列【finalshell连接时间长了,忘记密码,如何查看密码?】
    OSS系列【java调用Minio实现文件上传下载】
    Linux系列【解决vi命令不能用的问题】
    Linux系列【linux下boot目录满了怎么清理?】
    OSS系列【centos7单机搭建Minio】
    Windows系列【win10桌面的文件夹无法拖动,怎么办?】
  • 原文地址:https://www.cnblogs.com/lzyqq/p/11377258.html
Copyright © 2020-2023  润新知