diff --git a/Cardiopulmonarybypasssystems/Converters/TrendPointCollectionConverter.cs b/Cardiopulmonarybypasssystems/Converters/TrendPointCollectionConverter.cs new file mode 100644 index 0000000..ec496a3 --- /dev/null +++ b/Cardiopulmonarybypasssystems/Converters/TrendPointCollectionConverter.cs @@ -0,0 +1,57 @@ +using System.Collections; +using System.Globalization; +using System.Windows.Data; +using System.Windows.Media; + +namespace Cardiopulmonarybypasssystems.Converters; + +public sealed class TrendPointCollectionConverter : IMultiValueConverter +{ + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) + { + if (values.Length < 4 + || values[0] is not IEnumerable enumerable + || values[1] is not double width + || values[2] is not double height + || values[3] is not double maxValue + || width <= 0 + || height <= 0 + || maxValue <= 0) + { + return new PointCollection(); + } + + var samples = enumerable.Cast() + .Select(item => item is double value ? value : System.Convert.ToDouble(item, culture)) + .ToList(); + + if (samples.Count == 0) + { + return new PointCollection(); + } + + if (samples.Count == 1) + { + return new PointCollection + { + new System.Windows.Point(0, height - samples[0] / maxValue * height) + }; + } + + var points = new PointCollection(samples.Count); + var xStep = width / Math.Max(samples.Count - 1, 1); + + for (var index = 0; index < samples.Count; index++) + { + var x = xStep * index; + var yRatio = Math.Clamp(samples[index] / maxValue, 0d, 1d); + var y = height - yRatio * height; + points.Add(new System.Windows.Point(x, y)); + } + + return points; + } + + public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => + throw new NotSupportedException(); +} diff --git a/Cardiopulmonarybypasssystems/MainWindow.xaml b/Cardiopulmonarybypasssystems/MainWindow.xaml index f466006..d93d937 100644 --- a/Cardiopulmonarybypasssystems/MainWindow.xaml +++ b/Cardiopulmonarybypasssystems/MainWindow.xaml @@ -4,11 +4,15 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:models="clr-namespace:Cardiopulmonarybypasssystems.Models" + xmlns:converters="clr-namespace:Cardiopulmonarybypasssystems.Converters" mc:Ignorable="d" Title="心肺转流检测" Width="1024" Height="800" WindowStartupLocation="CenterScreen"> + + + @@ -398,150 +402,380 @@ + + + + + + + + + + + +