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

【WinForm UI控件系列】散点图/折线图控件 (支持数值型、时间型、字符串型)

前言:c# winform UI控件系列(Net6+),纯GDI绘图无依赖,虽然做不到最好,争取做好更好用!

一、效果图 (x轴三种类型:数值、时间、字符串)





  • 支持五种颜色风格。
  • 标题:位置支持(左中右布局)
  • x轴标题、y轴标题:支持旋转角度。图例支持位置定义(选择合适位置)
  • 是否显示连线,是否显示网格线,是否圆滑曲线

二、使用说明

ScatterPlot 散点图/折线图控件

控件简介

ScatterPlot 是一个功能强大的散点图和折线图控件,参考 ScottPlot 设计,支持平滑曲线、多种X轴类型(数值型、字符串型、日期时间型)、网格线、值提示和图例显示。

主要特性

  • 多种X轴类型:支持 Numeric(数值)、Category(字符串类别)、DateTime(日期时间)
  • 平滑曲线:支持 Smooth 平滑曲线显示,可调整张力
  • 网格线:支持 X/Y 轴网格线显示控制
  • 值提示:鼠标悬停显示数据点值,支持自动格式化
  • 图例位置控制:支持 7 种图例位置(None, TopLeft, TopCenter, TopRight, BottomLeft, BottomCenter, BottomRight)
  • 轴标签旋转:支持 X/Y 轴标签旋转角度设置
  • 自动日期时间格式:根据时间跨度自动选择最优显示格式
  • 属性变更通知:实现 INotifyPropertyChanged,属性值改变立即生效

基本使用

简单散点图

// 创建散点图控件varscatterPlot=newScatterPlot{Dock=DockStyle.Fill,ColorType=ColorType.Primary};// 添加数据系列varseries=newScatterSeries{Name="系列1",ShowLine=true,ShowMarkers=true,Smooth=true};// 添加数据点for(inti=0;i<10;i++){series.Points.Add(newScatterPoint(i,Math.Sin(i*0.5)*10+20));}scatterPlot.Series.Add(series);scatterPlot.RefreshPlot();this.Controls.Add(scatterPlot);

X轴类型设置

// 数值型(默认)scatterPlot.XAxisType=XAxisType.Numeric;// 字符串类别型scatterPlot.XAxisType=XAxisType.Category;// 日期时间型scatterPlot.XAxisType=XAxisType.DateTime;

日期时间型数据

当使用XAxisType.DateTime时,控件会自动根据时间跨度选择合适的显示格式:

时间跨度X轴显示格式Tooltip格式
≤24小时HH:mmHH:mm
≤72小时MM-dd HH:mmMM-dd HH:mm
≤30天MM-ddMM-dd
≤1年yyyy-MMyyyy-MM
>1年yyyy-MM-ddyyyy-MM-dd
// 日期时间型示例(24小时内数据)scatterPlot.XAxisType=XAxisType.DateTime;scatterPlot.Title="24小时温度变化";scatterPlot.XAxisLabel="时间";varseries=newScatterSeries{Name="今日温度",ShowLine=true,ShowMarkers=true};// 添加24小时内的数据点DateTimetoday=DateTime.Now.Date;series.Add(today.AddHours(0),18);series.Add(today.AddHours(6),20);series.Add(today.AddHours(12),28);series.Add(today.AddHours(18),24);series.Add(today.AddHours(24),19);scatterPlot.AddSeries(series);scatterPlot.RefreshPlot();

图例位置控制

scatterPlot.ShowLegend=true;scatterPlot.LegendPosition=LegendPosition.TopRight;// 右上角scatterPlot.LegendOrientation=LegendOrientation.Horizontal;// 水平排列

轴标签旋转

// X轴标签旋转 45 度scatterPlot.AxisLabelStyle.XAxisRotation=45;// Y轴标签旋转 -90 度scatterPlot.AxisLabelStyle.YAxisRotation=-90;

网格线控制

// 显示 X 轴网格线scatterPlot.GridLineStyle.ShowX=true;// 显示 Y 轴网格线scatterPlot.GridLineStyle.ShowY=true;// 设置网格线颜色scatterPlot.GridLineStyle.Color=Color.FromArgb(230,230,230);

属性说明

主要属性

属性名类型默认值说明
SeriesList-数据系列集合
XAxisTypeXAxisTypeNumericX轴类型
ColorTypeColorTypePrimary色彩类型
Titlestring“”图表标题
XAxisLabelstring“”X轴标题
YAxisLabelstring“”Y轴标题

图例属性

属性名类型默认值说明
ShowLegendbooltrue是否显示图例
LegendPositionLegendPositionBottomCenter图例位置
LegendOrientationLegendOrientationHorizontal图例排列方向

轴标签样式

属性名类型默认值说明
AxisLabelStyle.XAxisRotationint0X轴标签旋转角度
AxisLabelStyle.YAxisRotationint0Y轴标签旋转角度
AxisLabelStyle.FontSizefloat9标签字体大小
AxisLabelStyle.ColorColorGray标签颜色

网格线样式

属性名类型默认值说明
GridLineStyle.ShowXbooltrue是否显示X轴网格线
GridLineStyle.ShowYbooltrue是否显示Y轴网格线
GridLineStyle.ColorColorLightGray网格线颜色
GridLineStyle.Widthint1网格线宽度

事件

PointClicked

数据点点击事件,可用于显示详细信息:

scatterPlot.PointClicked+=(sender,args)=>{stringxValue;if(scatterPlot.XAxisType==XAxisType.DateTime){xValue=newDateTime((long)args.Point.X).ToString("MM-dd HH:mm");}else{xValue=args.Point.X.ToString();}MessageBox.Show($"系列:{args.Series.Name}\nX:{xValue}\nY:{args.Point.Y:F2}");};

完整示例

24小时温度变化

varscatterPlot=newScatterPlot{Dock=DockStyle.Fill,Title="24小时温度变化",XAxisLabel="时间",YAxisLabel="温度 (°C)",XAxisType=XAxisType.DateTime,ShowLegend=true,LegendPosition=LegendPosition.BottomCenter};// 今日温度vartodaySeries=newScatterSeries{Name="今日温度",LegendText="今日",ShowLine=true,ShowMarkers=true,Smooth=true,SmoothTension=0.4f,LineWidth=2};DateTimetoday=DateTime.Now.Date;todaySeries.Add(today.AddHours(0),18);todaySeries.Add(today.AddHours(2),17);todaySeries.Add(today.AddHours(4),16);todaySeries.Add(today.AddHours(6),17);todaySeries.Add(today.AddHours(8),20);todaySeries.Add(today.AddHours(10),24);todaySeries.Add(today.AddHours(12),28);todaySeries.Add(today.AddHours(14),30);todaySeries.Add(today.AddHours(16),29);todaySeries.Add(today.AddHours(18),26);todaySeries.Add(today.AddHours(20),23);todaySeries.Add(today.AddHours(22),20);todaySeries.Add(today.AddHours(24),19);scatterPlot.AddSeries(todaySeries);scatterPlot.RefreshPlot();this.Controls.Add(scatterPlot);

多日期温度对比

varscatterPlot=newScatterPlot{Dock=DockStyle.Fill,Title="温度趋势图",XAxisLabel="日期",YAxisLabel="温度 (°C)",XAxisType=XAxisType.DateTime,ShowLegend=true,LegendPosition=LegendPosition.TopRight};// 本周温度varseries1=newScatterSeries{Name="本周温度",LegendText="本周",ShowLine=true,ShowMarkers=true,Smooth=true,LineWidth=2};// 添加7天数据for(inti=-6;i<=0;i++){series1.Add(DateTime.Now.AddDays(i).Date,20+Math.Sin(i*0.5)*5);}scatterPlot.AddSeries(series1);scatterPlot.RefreshPlot();

注意事项

  1. 日期时间数据:使用DateTime.ToOADate()DateTime.Ticks转换为数值存储
  2. 刷新图表:修改数据后调用RefreshPlot()重新绘制
  3. 平滑曲线:设置Smooth = true并可通过SmoothTension调整平滑度(0-1之间)
  4. 图例绘制:图例始终在最上层显示,不会被网格线遮挡
  5. 轴标题:设置XAxisLabelYAxisLabel会自动预留空间,避免与轴标签重叠

三、后记

陆续补充完善中,敬请关注,如有需求,有好的建议,请留言(xue5zhijing)

http://www.jsqmd.com/news/699860/

相关文章:

  • 安卓虚拟摄像头终极指南:5分钟学会VCAM视频替换技巧
  • 别再用记事本了!手把手教你用Python+010 Editor高效解决CTF中的编码乱序问题(以GKCTF签到题为例)
  • 前端表格筛选卡顿?智表ZCELL毫秒级响应与全场景筛选方案揭秘
  • 告别钢网!手把手教你用热风枪和普通焊锡丝搞定QFN芯片焊接(附温度曲线详解)
  • 技术深度解析:AlDente电池健康管理系统的架构设计与实现机制
  • 临沂开锁电话,配汽车钥匙,开汽车锁,换锁,临沂指纹锁安装,临沂上门开锁(临沂靠谱商家推荐仟亿锁业) - 品牌企业推荐师(官方)
  • 阶跃 StepAudio 2.5 ASR 上线!500TPS 极速推理,30分钟语音“秒级转写”
  • 如何让旧iPhone/iPad重获新生?Legacy iOS Kit完全指南
  • 多智能体协作自动化编排与拆解SKILL
  • RP2040与MicroMod开发板的嵌入式快速原型设计实践
  • GoFr框架:加速微服务开发的Go语言利器
  • 最强生图模型GPT-image-2,一手深度测评,附教程
  • git 分支 实战
  • AI记忆系统核心架构解析:从向量检索到MemoryOS实践
  • 变频器为什么要加制动电阻?该怎么选型?
  • 招聘 Agent:JD 解析、简历筛选与面试题生成的可控方案
  • 警惕AI CRM的“监控”陷阱:从技术视角谈隐私保护与数据主权的设计边界
  • 2026年3月做得好的水果礼盒品牌推荐,香妃果礼盒/鸡心果礼盒/水果礼盒/小苹果礼盒/海棠果礼盒,水果礼盒实力厂家选哪家 - 品牌推荐师
  • SNK施努卡新能源电池盒下箱体错漏装CCD在线检测解决方案
  • 嵌入式C语言适配LLM推理引擎的5大反模式(ARM Cortex-M4实测崩溃现场还原+修复前后性能对比Δ=3.8×)
  • 超元力无限方舟:创新全感沉浸,重塑沉浸式娱乐体验
  • kohya _ss训练stable-diffusion-LoRA模型保姆级教程(详细)
  • GitHub 热门项目 | 2026年04月25日
  • 深度学习在计算机视觉中的核心优势与应用实践
  • Hermes Agent 整合 OpenCode CLI 的实战经验
  • Redisson 介绍
  • 朴素分类器概率评估与优化实战
  • D6.3 PriorityClass 常用实验(2个)
  • DeepSeek创始人专访:中国的AI不可能永远跟随,需要有人站到技术的前沿
  • AutoCAD字体缺失终结者:FontCenter插件完整使用指南