有回响的山谷 2023-12-01 18:09 采纳率: 53.3%
浏览 11
已结题

winform使用zedgraph绘制实时曲线卡顿

我需要使用winfrom绘制实时曲线,使用的控件是zedgraph
但是曲线出来后,一顿一顿的,想请问有什么办法使曲线流畅些?
无法上传视频,有遇到相同的情况的吗

img

  • 写回答

1条回答 默认 最新

  • 码农-白兰度 2023-12-02 09:35
    关注

    可能是由于 UI 线程被绘图操作阻塞导致的,在后台线程中更新数据: 实时曲线的数据更新最好放在一个独立的后台线程中进行,以避免阻塞 UI 线程。在后台线程中计算好数据后,再将数据传递给 UI 线程进行绘制。

    using System;
    using System.Threading;
    using System.Windows.Forms;
    using ZedGraph;
    
    namespace RealTimeGraph
    {
        public partial class MainForm : Form
        {
            private ZedGraphControl zedGraphControl1;
            private RollingPointPairList rollingPointPairList;
            private LineItem curve;
            private Random random = new Random();
    
            public MainForm()
            {
                InitializeComponent();
                InitializeGraph();
                StartUpdatingData();
            }
    
            private void InitializeGraph()
            {
                zedGraphControl1 = new ZedGraphControl();
                rollingPointPairList = new RollingPointPairList(1000);
                curve = zedGraphControl1.GraphPane.AddCurve("Real-time Data", rollingPointPairList, Color.Blue, SymbolType.None);
                Controls.Add(zedGraphControl1);
            }
    
            private void StartUpdatingData()
            {
                Thread dataUpdateThread = new Thread(UpdateDataLoop);
                dataUpdateThread.IsBackground = true;
                dataUpdateThread.Start();
            }
    
            private void UpdateDataLoop()
            {
                while (true)
                {
                    double x = rollingPointPairList.Count;
                    double y = random.NextDouble() * 100; // Replace this with your real data source
    
                    // Update data in the rolling list
                    rollingPointPairList.Add(x, y);
    
                    // Update the graph on the UI thread
                    zedGraphControl1.BeginInvoke(new Action(() =>
                    {
                        UpdateGraph();
                    }));
    
                    Thread.Sleep(100); // Adjust the sleep duration based on your data update rate
                }
            }
    
            private void UpdateGraph()
            {
                // Redraw the graph
                zedGraphControl1.AxisChange();
                zedGraphControl1.Invalidate();
            }
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月4日
  • 已采纳回答 12月27日
  • 创建了问题 12月1日