C#动态画折线图

1.前台

<Grid x:Name="速率表" Margin="284,30,37,217">
            <Image Stretch="Fill" Source="/Skins/Images/Gis/speedbg1.png" Margin="0,0,0,26"></Image>
            <Canvas x:Name="speeedCanvas" Margin="0,0,0,17.333">
                <Line x:Name="x_axis" Stroke="Red" StrokeThickness="2" X1="0" Y1="88" X2="289" Y2="88" StrokeStartLineCap="Round" Width="289"  />
                <Line x:Name="y_axis" Stroke="Red" StrokeThickness="2" X1="0" Y1="88" X2="0" Y2="0" StrokeStartLineCap="Round" Height="88"  />
            </Canvas> 

            <TextBlock x:Name="beforetime"   Text="{Binding Path=Time2, RelativeSource={RelativeSource AncestorType=UserControl}}" TextAlignment="Left"   Foreground="#FF0581C9" FontSize="14" FontFamily="Microsoft YaHei"     Margin="0,94,241,0"/>
            <TextBlock x:Name="nowtime"  Text="{Binding Path=Time1, RelativeSource={RelativeSource AncestorType=UserControl}}" TextAlignment="Right"  Foreground="#FF7B7E80" FontSize="12" FontFamily="Microsoft YaHei"      Margin="226,94,0,0"/>
            <TextBlock x:Name="速率" Foreground="#FFD3D1D1" FontFamily="Microsoft YaHei"   Opacity="0.302"  Text="速率"  Margin="0,0,258,98"/>
            <Path x:Name="矩形_9" Data="F1M2,2C2,2 7,2 7,2 7,2 7,9 7,9 7,9 2,9 2,9 2,9 2,2 2,2z" Fill="#FF5BCC0F"   Width="8" Height="12" Stretch="Fill" Margin="71,98,210,5"/>
            <TextBlock x:Name="a设备" Foreground="#FF8B8B8B" FontSize="14" FontFamily="Microsoft YaHei"    Text="a设备"   Margin="84,94,166,0"/> 
        </Grid>

2.后台

 //当前时间

        private string time1 = "00:00"; 
        public string Time1
        {
            get
            {
                return time1;
            }

            set
            {
                if (time1 == value)
                {
                    return;
                }

                time1 = value;
                NotifyPropertyChanged("Time1");
            }
        }


        //时间2 
        private string time2 = "00:00"; 
        public string Time2
        {
            get
            {
                return time2;
            }

            set
            {
                if (time2 == value)
                {
                    return;
                }

                time2 = value;
                NotifyPropertyChanged("Time2");
            }
        }



        //获取当前时间   每秒更新一次  
        //获得当前时间的速率,重新绘图
        private void UpdateTime()
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(1000);
            timer.Tick += timer_Tick;
            timer.Start();



        }

        void timer_Tick(object sender, EventArgs e)
        {
            Time1 = DateTime.Now.ToString("hh") + ":" + DateTime.Now.ToString("mm") + ":" + DateTime.Now.ToString("ss");

            if (System.DateTime.Now.Minute - 20 < 0)
            {
                if (DateTime.Now.Hour - 1 >= 0)
                {
                    Time2 = (24 - 1).ToString() + ":" + (60 - DateTime.Now.Minute).ToString();
                }
                else
                {
                    Time2 = (DateTime.Now.Hour - 1).ToString() + ":" + (60 - DateTime.Now.Minute).ToString();
                }

            }
            else
            {
                Time2 = DateTime.Now.Hour.ToString() + ":" + (DateTime.Now.Minute - 20).ToString();
            }

            //每一秒,增加一个新的点,21个点后,增加新点,删除第一个点
            AddCurvePoint1();
            AddCurvePoint2();
            DrawCurve(); 
        }
 //控件加载事件
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateTime(); 
        }
///////////////////////////////////////////////////////
     //点集合
        private PointCollection coordinatePoints1 = new PointCollection();

        //数据点的集合
        private List<Point> dataPoints1 = new List<Point>();

        Random rPoint = new Random();

        //增加新的点,删除第一个点,并且其他点向后移动   Point dataPoint
        private void AddCurvePoint1()
        {

            double x_point = 289;//横坐标(canvas长度289)
            double y_point = 88 - rPoint.NextDouble() * 88;//随机纵坐标

            if (dataPoints1.Count <= 0)
            {
                dataPoints1.Add(new Point(x_point, y_point));
                //将数据点在画布中的位置保存下来
                coordinatePoints1.Add(new Point(x_point, y_point));
            }

            else//当超出21个点的时候
            {
                if (dataPoints1.Count >= pointCount)
                {
                    dataPoints1.RemoveAt(0);//删除第一个点
                    coordinatePoints1.RemoveAt(0);
                }

                for (int i = 0; i < dataPoints1.Count; i++)
                {
                    //每一个点的X数据都要向左移动    252/20px
                    dataPoints1[i] = new Point(dataPoints1[i].X - (289.0 / pointCount), dataPoints1[i].Y);
                    coordinatePoints1[i] = new Point(dataPoints1[i].X, dataPoints1[i].Y);
                }

                dataPoints1.Add(new Point(x_point, y_point));
                //将数据点在画布中的位置保存下来
                coordinatePoints1.Add(new Point(x_point, y_point));
            }

        }
//画折线 a设备
        private void DrawCurve()
        {
            //先清空画板
            if (speeedCanvas.Children != null)
            {
                speeedCanvas.Children.Clear();
            }

            Polyline curvePolyline1 = new Polyline();
            curvePolyline1.Stroke = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF5BCC0F"));
            curvePolyline1.StrokeThickness = 2;
            curvePolyline1.Points = coordinatePoints1;
            speeedCanvas.Children.Add(curvePolyline1); 
        }

猜你喜欢

转载自www.cnblogs.com/LY-HeroesRebor/p/9069002.html
今日推荐