Window_sync_graph.xaml.cs 9.59 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Flyad7_WPF
{
    /// <summary>
    /// Window_sync_graph.xaml 的交互逻辑
    /// </summary>
    public partial class Window_sync_graph : Window
    {
        private FlyADBase.IFlyADClient mflyad;
        System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
        public Window_sync_graph()
        {
            InitializeComponent();
            InitializeComponent3();

            timer.Interval = TimeSpan.FromMilliseconds(60);
            timer.Tick += new EventHandler(timer_Tick);
        }
        
        void InitializeComponent3()
        {
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Series series_pos = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Series series_speed = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            

            // 
            // chart1
            // 
            #region chartArea
            chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.BackColor = System.Drawing.Color.OldLace;
            //chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            //chartArea1.BackSecondaryColor = System.Drawing.Color.White;
            chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            chartArea1.Name = "Default";
            chartArea1.ShadowColor = System.Drawing.Color.Transparent;
            //对数坐标
            //chartArea1.AxisY.IsLogarithmic = true;
            //chartArea1.AxisY.LogarithmBase = Math.E;
            this.chart1.ChartAreas.Add(chartArea1);
            #endregion

            #region legend
            legend1.Alignment = System.Drawing.StringAlignment.Near;
            legend1.IsTextAutoFit = false;
            legend1.BackColor = System.Drawing.Color.Transparent;
            legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            legend1.LegendStyle = System.Windows.Forms.DataVisualization.Charting.LegendStyle.Row;
            legend1.Name = "Default";
            legend1.DockedToChartArea = chartArea1.Name;
            legend1.Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Right;
            legend1.IsDockedInsideChartArea = true;
            legend1.Position.Auto = true;
            chart1.Legends.Add(legend1);
            #endregion



            #region series
            series_pos.BorderColor = System.Drawing.Color.LightBlue;
            series_pos.BorderWidth = 3;
            series_pos.ChartArea = chartArea1.Name;
            series_pos.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series_pos.Color = System.Drawing.Color.Blue;
            //series1.MarkerSize = 8;
            //series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
            series_pos.Name = "Position";
            series_pos.ShadowColor = System.Drawing.Color.Black;
            series_pos.ShadowOffset = 2;
            series_pos.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
            series_pos.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
            this.chart1.Series.Add(series_pos);


            series_speed.BorderColor = System.Drawing.Color.Pink;
            series_speed.BorderWidth = 3;
            series_speed.ChartArea = chartArea1.Name;
            series_speed.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series_speed.Color = System.Drawing.Color.Red;
            //series1.MarkerSize = 8;
            //series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
            series_speed.Name = "Speed";
            series_speed.ShadowColor = System.Drawing.Color.Black;
            series_speed.ShadowOffset = 2;
            series_speed.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
            series_speed.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
            series_speed.YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary;

            this.chart1.Series.Add(series_speed);

            #endregion
            this.chart1.Name = "chart1";
            this.chart1.BackColor = System.Drawing.Color.Transparent;
        }

        public void Init(FlyADBase.IFlyADClient flyad) 
        {
            mflyad = flyad;
            
        }

        void mflyad_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "Position") || (e.PropertyName == "Position2")) 
            {
                update();
            }
        }

        TimeSpan DataTime = TimeSpan.FromSeconds(60);
        int DataLen = 1000;//总数据量永远只有1000个
        void update()
        {
            if (chart1.Series["Position"].Points.Count() > 0) 
            {
                if (chart1.Series["Position"].Points.First().XValue > mflyad.Position2) 
                {
                    //清空全部数据
                    chart1.Series["Position"].Points.RemoveAt(0);
                    chart1.Series["Speed"].Points.RemoveAt(0);
                }
            }


            while (chart1.Series["Position"].Points.Count() > DataLen)
            {
                chart1.Series["Position"].Points.RemoveAt(0);
                chart1.Series["Speed"].Points.RemoveAt(0);
            }
            if (chart1.Series["Position"].Points.Count() > 0)
            {
                if (chart1.Series["Position"].Points.Last().XValue == mflyad.Position2)
                {
                    chart1.Series["Position"].Points.Last().SetValueY(mflyad.Position);
                    chart1.Series["Speed"].Points.Last().SetValueY(mflyad.Speed);
                    return;
                }
            }
            
            chart1.Series["Position"].Points.AddXY(mflyad.Position2, mflyad.Position);
            chart1.Series["Speed"].Points.AddXY(mflyad.Position2, mflyad.Speed);

            double min = chart1.Series["Position"].Points.First().XValue;
            double max = chart1.Series["Position"].Points.Last().XValue;
            if(max-min<10000)
            {
                max = min+10000;
            }


            chart1.ChartAreas[0].AxisX.Minimum = min;
            chart1.ChartAreas[0].AxisX.Maximum = max;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            textbox_datalen.Text = ((int)(DataTime.TotalSeconds)).ToString();
            setInterval();
            timer.Start();
            
        }
        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            timer.Stop();
        }
        void timer_Tick(object sender, EventArgs e)
        {
            update();
        }

        private void button_ok_Click(object sender, RoutedEventArgs e)
        {
            int datlen;
            if (!int.TryParse(textbox_datalen.Text, out datlen))
            {
                MessageBox.Show("格式出错", "异常", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (datlen < 5)
            {
                MessageBox.Show("小于 5s", "异常", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            DataTime = TimeSpan.FromSeconds(datlen);
            setInterval();
            MessageBox.Show("成功");
            return;
        }
        void setInterval() 
        {
            TimeSpan ts = TimeSpan.FromSeconds(DataTime.TotalSeconds / 1000);
            if (ts < TimeSpan.FromSeconds(0.1))
            {
                ts = TimeSpan.FromSeconds(0.1);
                DataLen = (int)(DataTime.TotalSeconds / ts.TotalSeconds);
            }
            else 
            {
                DataLen = 1000;
            }
            timer.Interval = ts;
        }

    }
}