alielie
2025-07-04 0940f31b7eba1cf6b6a8c1fca72d97cf686e79f2
Plugin.TestResult/Chart.xaml.cs
@@ -1,4 +1,5 @@
using ScottPlot;
using Plugin.TestResult_WPF.Model;
using ScottPlot;
using ScottPlot.Plottables;
using ScottPlot.TickGenerators;
using System;
@@ -9,6 +10,9 @@
namespace Plugin.TestResult_WPF
{
    /// <summary>
    /// Chart.xaml 的交互逻辑
    /// </summary>
    /// <summary>
    /// Chart.xaml 的交互逻辑
    /// </summary>
@@ -30,6 +34,7 @@
        private double maxYValue = -1;
        private double minYValue = double.MaxValue;
        private float fontSize = -1;
        private Timer timer;
@@ -167,6 +172,70 @@
            timer.Elapsed += Timer_Elapsed;
        }
        public Chart(string title, bool multi, float titleSize)
        {
            BaseConfig config = new BaseConfig();
            this.title = title;
            this.multi = multi;
            this.maxSize = config.MaxSize;
            this.refreshInterval = config.RefreshInterval;
            fontSize = titleSize;
            InitializeComponent();
            if (this.multi)
            {
                xValues = new Dictionary<string, Queue<long>>();
                yValues = new Dictionary<string, Queue<double>>();
                //chart.Plot.ShowLegend(Alignment.UpperLeft, Orientation.Vertical);
                var legend = chart.Plot.ShowLegend(Edge.Top);
                legend.Alignment = Alignment.UpperLeft;
                legend.Padding = new PixelPadding(0);
                chart.Plot.Axes.Left.TickLabelStyle.FontSize = titleSize - 3;
                chart.Plot.Axes.Bottom.TickLabelStyle.FontSize = titleSize - 3;
                chart.Plot.Axes.Left.Label.FontSize = titleSize;
            }
            else
            {
                singleLogger = InitLogger();
                singleLogger.Color = Colors.Red;
                xData = new Queue<long>(maxSize);
                yData = new Queue<double>(maxSize);
                chart.Plot.Axes.Left.TickLabelStyle.FontSize = titleSize - 3;
                chart.Plot.Axes.Bottom.TickLabelStyle.FontSize = titleSize - 3;
                chart.Plot.Axes.Left.Label.FontSize = titleSize;
            }
            /// 禁用鼠标
            chart.UserInputProcessor.Disable();
            string CustomFormatter(double position)
            {
                if (position > 0)
                    return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddMilliseconds(position).ToString("HH:mm:ss");
                return "";
            }
            EvenlySpacedMinorTickGenerator minorTickGen = new EvenlySpacedMinorTickGenerator(10);
            NumericAutomatic myTickGenerator = new NumericAutomatic()
            {
                LabelFormatter = CustomFormatter,
                MinorTickGenerator = minorTickGen
            };
            chart.Plot.Axes.Bottom.TickGenerator = myTickGenerator;
            if (!string.IsNullOrEmpty(this.title))
            {
                chart.Plot.Axes.Left.Label.Text = this.title;
                chart.Plot.Axes.Left.Label.Bold = false;
                chart.Plot.Font.Automatic();
                //RefreshData();
            }
            timer = new Timer();
            timer.Interval = this.refreshInterval;
            timer.Elapsed += Timer_Elapsed;
        }
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (maxYValue > 0)
@@ -177,16 +246,25 @@
        public void RefreshData()
        {
            /// 限制y轴
            if (maxYValue > -1)
            bool flag = timer.Enabled;
            try
            {
                chart.Plot.Axes.SetLimitsY(minYValue > 5 ? minYValue - 5 : 0, maxYValue + 5);
                timer.Enabled = false;
                /// 限制y轴
                if (maxYValue > -1)
                {
                    chart.Plot.Axes.SetLimitsY(minYValue > 5 ? minYValue - 5 : 0, maxYValue + 5);
                }
                else
                {
                    chart.Plot.Axes.SetLimitsY(0, 100);
                }
                chart.Refresh();
            }
            else
            finally
            {
                chart.Plot.Axes.SetLimitsY(0, 100);
                timer.Enabled = flag;
            }
            chart.Refresh();
        }
        public void Clear()
@@ -234,6 +312,20 @@
                .ToList()
                .ForEach(m => chart.Plot.Remove(m));
            //RefreshData();
        }
        public List<double> GetSingleData()
        {
            bool flag = timer.Enabled;
            try
            {
                timer.Enabled = false;
                return yData?.ToList();
            }
            finally
            {
                timer.Enabled = flag;
            }
        }
        private void InsertData(Queue<long> xAxis, Queue<double> yAxis, long x, double y)
@@ -306,9 +398,18 @@
        private void UserControl_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
        {
            if (this.multi) return;
            chart.Plot.Axes.Left.Label.FontSize = (float)FontSizeConverter.CountFontSize(15, ActualHeight, 92);
            chart.Plot.Axes.Left.TickLabelStyle.FontSize = (float)FontSizeConverter.CountFontSize(10, ActualHeight, 92);
            chart.Plot.Axes.Bottom.TickLabelStyle.FontSize = (float)FontSizeConverter.CountFontSize(10, ActualHeight, 92);
            if (fontSize < 0)
            {
                chart.Plot.Axes.Left.Label.FontSize = (float)FontSizeConverter.CountFontSize(15, ActualHeight, 92);
                chart.Plot.Axes.Left.TickLabelStyle.FontSize = (float)FontSizeConverter.CountFontSize(10, ActualHeight, 92);
                chart.Plot.Axes.Bottom.TickLabelStyle.FontSize = (float)FontSizeConverter.CountFontSize(10, ActualHeight, 92);
            }
            else
            {
                chart.Plot.Axes.Left.Label.FontSize = fontSize - 3;
                chart.Plot.Axes.Left.TickLabelStyle.FontSize = fontSize - 3;
                chart.Plot.Axes.Bottom.TickLabelStyle.FontSize = fontSize;
            }
        }
    }
}