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); 
        }

 


免責聲明!

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



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