ScottPlot5 WindowsForm 测试

ScottPlot5 WindowsForm

ScottPlot5 是.Net的一个图表控件库;支持WPF,WindowsForm,Avalonia, Eto, WinUI, Blazor(仅WebAssembly),本次测试为WindowsForm环境;
本文章源码

步骤

  1. 创建.Net8 WindowsForm工程
  2. 引用如下两个包
<PackageReference Include="ScottPlot.Avalonia" Version="5.0.20" />
<PackageReference Include="ScottPlot.WinForms" Version="5.0.20" />
  1. 主界面增加FormsPlot控件设置Dock为Fill,并设置窗体最大化
  2. 写代码
  3. 启动WinForm工程

主要代码

        private void Form1_Load(object? sender, EventArgs e)
        {
            if (first)
            {
                StartPlot();
            }
            first = false;
        }
        private void StartPlot()
        {
            InitLines();
            if (timer == null)
            {
                timer = new(); // 每隔1秒触发一次
                timer.Interval = 1000;
                timer.Tick += TimerElapsed;
                timer.Start();
            }
        }

        private void InitLines()
        {
            if (plot == null) return;
            // PlotFont = Fonts.Detect("实时数据");
            plot.Plot.Axes.Title.Label.Text = "RealTime Data:实时数据";
            plot.Plot.Axes.Title.Label.FontName = PlotFont;

            plot.Plot.Clear();
            Lines = new LineModel[LineCount];
            DateTime start = DateTime.Now;
            for (int i = 0; i < LineCount; i++)
            {
                Lines[i] = new LineModel();
                Lines[i].Init(i + 1, plot.Plot, start);
            }
            plot.Plot.Benchmark.IsVisible = true;
        }

        public void UpdateData(DateTime ts, double[] data)
        {
            int Min = Math.Min(data.Length, Lines.Length);
            if (Min <= 0) return;

            for (int i = 0; i < Min; i++)
            {
                UpdateData(i, ts, data[i]);
            }
            Render();
        }

        private void UpdateData(int i, DateTime ts, double latestValue)
        {
            var tmp = Lines[i];
            tmp.UpdateData(ts, latestValue);
        }

        public void Render()
        {
            plot.Plot.Axes.AutoScale();
            plot.Refresh();
        }

        private void TimerElapsed(object? sender, EventArgs e)
        {
            //InvokeAsync(() =>
            double[] data = new double[LineCount];
            for (int i = 0; i < LineCount; i++)
            {
                data[i] = Generate.RandomData.RandomNumber(100);
            }
            UpdateData(DateTime.Now, data);
        }

效果

刚启动时FPS有80,稳定后约50FPS
在这里插入图片描述

已知问题

  1. Title不显示,可能设计就是这样的,可另外添加
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文光山石

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值