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