private void Canvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (GraphType == 0) { //读取数据 try { Time = (int)(double.Parse(SampleTimeTextBox.Text) * 1000); Voltage = int.Parse(MaxVoltageTextBox.Text) * 1000; TimeInterval = int.Parse(SampleIntervalTextBox.Text); } catch (Exception) { MessageBox.Show("数据异常"); } if (SparrowChart != null) { SparrowChart.XAxis.MaxValue = $"{Time / 1000.0}"; SparrowChart.YAxis.MaxValue = $"{Voltage / 1000.0}"; SparrowChart.UpdateLayout(); MainWindowModel.SetData(new Dictionary <int, double>()); } Canvas canvas = sender as Canvas; //Voltages.Clear(); pointList.Clear(); canvas.Children.Clear(); canvas.Children.Add(BackgroudChart); canvas.Children.Add(coordLabel); Point point = e.GetPosition(canvas); coordLabel.SetValue(Canvas.LeftProperty, point.X + 5); coordLabel.SetValue(Canvas.TopProperty, point.Y - 15); if ((Voltage <= 0) || (Time <= 0)) { MessageBox.Show("未选择有效的XY轴范围"); return; } //容量 RecordVoltages.Clear(); int capacity = (Time / TimeInterval); for (int i = 0; i < capacity; i++) { RecordVoltages.Add(TimeInterval * i, 0); } } }
/// <summary> /// 图表类型 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void GraphTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (IsLoaded) { int index = (sender as ComboBox).SelectedIndex; if (index == 0) { CheckButton.IsEnabled = true; SinewaveStackPanel.IsEnabled = false; } else if ((index == 1) || (index == 2)) { CheckButton.IsEnabled = true; SinewaveStackPanel.IsEnabled = true; } else { CheckButton.IsEnabled = false; SinewaveStackPanel.IsEnabled = false; } GraphType = index; //清空图表 try { Time = int.Parse(SampleTimeTextBox.Text) * 1000; Voltage = int.Parse(MaxVoltageTextBox.Text) * 1000; TimeInterval = int.Parse(SampleIntervalTextBox.Text); } catch (Exception) { MessageBox.Show("数据异常"); } if (SparrowChart != null) { SparrowChart.XAxis.MaxValue = $"{Time / 1000.0}"; SparrowChart.YAxis.MaxValue = $"{Voltage / 1000.0}"; SparrowChart.UpdateLayout(); MainWindowModel.SetData(new Dictionary <int, double>()); } //Voltages.Clear(); pointList.Clear(); Canvas.Children.Clear(); Canvas.Children.Add(BackgroudChart); //Canvas.Children.Add(coordLabel); if ((Voltage <= 0) || (Time <= 0)) { MessageBox.Show("未选择有效的XY轴范围"); return; } //容量 RecordVoltages.Clear(); int capacity = (int)(Time / TimeInterval); for (int i = 0; i < capacity; i++) { RecordVoltages.Add(TimeInterval * i, 0); } } }
private void CheckButton_Click(object sender, RoutedEventArgs e) { if (GraphType == 0) { var points = pointList.Distinct().ToList(); var validPoints = new List <Point>(); if (points?.Count > 0) { var xLength = Canvas.ActualWidth - BasePoint.X; int pointCount = Time / TimeInterval; double xInterval = xLength / pointCount; double currentX = BasePoint.X; int currentTime = 0; RecordVoltages.Clear(); for (int i = 0; i < pointCount; i++) { RecordVoltages.Add(i * TimeInterval, 0); } for (int i = 1; i < points.Count; i++) { while (true) { if (currentX > points[i].X) { //到下一个点 break; } else if ((currentX > points[i - 1].X) && (currentX < points[i].X)) { //计算线公式 double a = (points[i].Y - points[i - 1].Y) / (points[i].X - points[i - 1].X); double b1 = points[i].Y - a * points[i].X; double b2 = points[i - 1].Y - a * points[i - 1].X; //计算当前点 double y = currentX * a + b1; Point point = new Point(currentX, y); validPoints.Add(point); double yRatio = (BasePoint.Y - y) / BasePoint.Y; RecordVoltages[currentTime] = Voltage * yRatio; //递进 currentX += xInterval; currentTime += TimeInterval; } else { //当前点小于有效点,递进 currentX += xInterval; currentTime += TimeInterval; } } } MainWindowModel.SetPulseData(RecordVoltages); } } else { //数据校验 try { Time = (int)(double.Parse(SampleTimeTextBox.Text) * 1000); Voltage = double.Parse(MaxVoltageTextBox.Text) * 1000; TimeInterval = int.Parse(SampleIntervalTextBox.Text); Frequency = int.Parse(FrequencyTextBox.Text); DutyRatio = int.Parse(DutyRatioTextBox.Text); if ((Time <= 0) || (Voltage <= 0) || (TimeInterval <= 0) || (Frequency <= 0)) { MessageBox.Show("数据必须大于0!"); return; } double sampleFrequency = 1000.0 / TimeInterval; if (sampleFrequency < (Frequency * 2)) { MessageBox.Show($"采样频率({sampleFrequency.ToString("F2")})必须大于输出频率的2倍({Frequency.ToString("F2")} * 2)"); return; } } catch (Exception) { MessageBox.Show("数据异常"); return; } if (SparrowChart != null) { SparrowChart.XAxis.MaxValue = $"{Time * 1.2 / 1000.0}"; SparrowChart.YAxis.MaxValue = $"{Voltage * 1.2 / 1000.0}"; SparrowChart.UpdateLayout(); MainWindowModel.SetData(new Dictionary <int, double>()); } //Voltages.Clear(); pointList.Clear(); Canvas.Children.Clear(); Canvas.Children.Add(BackgroudChart); //容量 RecordVoltages.Clear(); int capacity = (int)(Time / TimeInterval); for (int i = 0; i < capacity; i++) { RecordVoltages.Add(TimeInterval * i, 0); } if (GraphType == 1) { //(周期,单位MS) double cycleTime = 1 * 1000.0 / Frequency; double positiveTime = cycleTime * (DutyRatio * 0.01); for (int i = 0; i < RecordVoltages.Count; i++) { //当前在周期中的时间 double currentTime = (TimeInterval * i) % cycleTime; //判断是高电平还是低电平 if (currentTime < positiveTime) { RecordVoltages[TimeInterval * i] = Voltage; } else { RecordVoltages[TimeInterval * i] = 0; } } } MainWindowModel.SetPulseData(RecordVoltages); } }