当前位置: 首页 > news >正文

在C#中出现WinForm原控件Chart卡顿问题

原因:随之加入点的增加数据量也逐步增加

减少reflash()的次数

将原生的Chart控件更换为OxyPlot

using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using OxyPlot.WindowsForms;
using System.Threading.Tasks;namespace OxyChart
{public partial class Form1 : Form{public Form1(){InitializeComponent();Load += Form1_Load;}private async void Form1_Load(object? sender, EventArgs e){InitializeOptimizedRealTimeChart();//plotView1.Model =await CreateLineChartAsync();if (plotView1.Model?.Series.Count > 0){// 获取第一个系列(索引为0)var firstSeries = plotView1.Model.Series[0] as LineSeries;if (firstSeries != null){// 添加单个数据点//firstSeries.Points.Add(new DataPoint(1, 10));//firstSeries.Points.Add(new DataPoint(2, 20));//firstSeries.Points.Add(new DataPoint(3, 15));//firstSeries.Points.Add(new DataPoint(4, 25));//firstSeries.Points.Add(new DataPoint(5, 18));for (int i = 0; i < 38400; i++){try{//PTChart.Series[0].Points.AddXY(DateTime.Now, 100);firstSeries.Points.Add(new DataPoint(DateTime.Now.ToOADate(), i));//PTChart.Refresh();plotView1.InvalidatePlot(true);}catch (Exception){}await RelayAsync(1);}// 刷新图表显示}}}private void InitializeOptimizedRealTimeChart(){var model = new PlotModel{Title = "优化版实时数据图表",PlotMargins = new OxyThickness(50, 20, 20, 40)};LineSeries series = new LineSeries{Color = OxyColors.Blue,MarkerFill = OxyColors.Red,LineStyle = LineStyle.Solid,StrokeThickness = 1,MarkerType = MarkerType.Circle,Title = "数据系列"  // 添加标题以便识别};// 配置坐标轴//model.Axes.Add(new LinearAxis//{//    Position = AxisPosition.Bottom,//    Title = "时间",//    MajorGridlineStyle = LineStyle.Solid,//    MinorGridlineStyle = LineStyle.Dot,//    IsPanEnabled = true,//    IsZoomEnabled = true//});model.Axes.Add(new DateTimeAxis{Position = AxisPosition.Bottom,Title = "时间",StringFormat = "HH:mm:ss", // 时间格式MajorGridlineStyle = LineStyle.Solid,MinorGridlineStyle = LineStyle.Dot,IsPanEnabled = true,IsZoomEnabled = true});model.Axes.Add(new LinearAxis{Position = AxisPosition.Left,Title = "数值",MajorGridlineStyle = LineStyle.Solid,MinorGridlineStyle = LineStyle.Dot});model.Series.Add(series);plotView1.Model = model;}private async Task<PlotModel> CreateLineChartAsync(){var model = new PlotModel { Title = "折线图示例" };var series1 = new LineSeries{Title = "系列1",MarkerType = MarkerType.Circle};var series2 = new LineSeries{Title = "系列2",MarkerType = MarkerType.Square};// 添加数据点for (int i = 0; i < 100; i++){series1.Points.Add(new DataPoint(i, i));// series2.Points.Add(new DataPoint(i, Math.Cos(i)));await RelayAsync(1);}model.Series.Add(series1);//model.Series.Add(series2);return model;}private async Task RelayAsync(int milliseconds, CancellationToken cancellationToken = default){if (milliseconds < 0){milliseconds = 100;}try{await Task.Delay(milliseconds, cancellationToken);}catch (TaskCanceledException){// 取消操作}}private void button1_Click(object sender, EventArgs e){if (plotView1.Model == null) return;plotView1.Model.DefaultXAxis.Reset();plotView1.Model.DefaultYAxis.Reset();plotView1.InvalidatePlot(true);}}
}

此控件在性能上比原生的Chart控件响应速度快

http://www.dtcms.com/a/519610.html

相关文章:

  • Spring Boot 3零基础教程,WEB 开发 内嵌服务器底层源码分析 笔记48
  • 网站开发案例分析成都制作网页
  • 导入的 Google(Chrome)书签默认不会自动显示在「书签栏」,而是放在一个文件夹里。下面是详细步骤,帮你把 导入的全部书签添加到书签栏
  • 一小时内使用NVIDIA Nemotron创建你自己的Bash计算机使用智能体
  • Chrome开发者工具
  • 虚拟机 Ubuntu 中安装 Google Chrome 浏览器
  • Docker/K8s部署MySQL的创新实践与优化技巧大纲
  • 网站建设管理流程避免网站侵权
  • 如何在Visual Studio中配置C++环境?
  • 珠海翻译公司高效翻译服务 2025年10月
  • 网站后台管理系统怎么登陆鄂州网站建设与设计
  • 建设系统网站企业密信下载app下载官网
  • 算法面经常考题整理(1)机器学习
  • 使用java如何进行接口测试
  • 机器学习-方差与偏差
  • 甘肃省网站建设咨询seo最好的网站源码
  • 3.序列式容器-heap
  • Module JDK is not defined 警告解决
  • 柞水县住房和城乡建设局网站网站建设客户分析调查表文档
  • html`contenteditable`
  • 【语音识别】语音识别的发展历程
  • 【C++ 类与对象 (下)】:进阶特性与编译器优化的深度实战
  • 加速智能体开发:从 Serverless 运行时到 Serverless AI 运行时
  • 怎么在服务器建立网站wordpress getcategorylink
  • uniapp textarea标签 在ios真机上出现高度拉长问题
  • cpp language 语法
  • uni-app 入门学习教程,从入门到精通,uni-app 企业项目实战:鲁嗑瓜子项目开发知识点(9)
  • uni-app小程序往飞书多维表格写入内容(包含图片)
  • 【uniapp】App平台展示pdf文件
  • Jenkins Pipeline 中使用 GitLab Webhook 触发任务执行