zedgraph控件使用

article/2025/10/6 22:56:56

最近做一个上位机的项目,要求实时显示温度曲线,开始用.net自带的 chart控做的,在动态显示那块,在删除一个数据点、加入一个新的数据点的时候,新的数据点显示不出来;纠结好久,解决不了这个问题。后来只好选择开源.net控件zedgraph。

ZedGraph 是用于创建任意数据的二维线型、棒型、饼型图表的一个类库,也可以作为 Windows 窗体用户控件和 ASP 网页访问控件。这个类库具有高度的灵活性,几乎所有式样的图表都能够被创建。关于zedgraph控件的使用具体。参考官网介绍:http://www.codeproject.com/Articles/5431/A-flexible-charting-library-for-NET

在项目中遇到的问题总结如下:

一、曲线图基本参数设置:

             //标题和x轴、y轴标签

             this.zedGraphControl1.GraphPane.Title.Text = "实时曲线图";
             this.zedGraphControl1.GraphPane.Title.FontSpec.FontColor = Color.Blue;
             this.zedGraphControl1.GraphPane.Title.FontSpec.Size = 30f;

             this.zedGraphControl1.GraphPane.XAxis.Title.Text = "时间";
             this.zedGraphControl1.GraphPane.XAxis.Title.FontSpec.FontColor = Color.Blue;
             this.zedGraphControl1.GraphPane.XAxis.Title.FontSpec.Size = 20f;

             this.zedGraphControl1.GraphPane.YAxis.Title.Text = "温度(℃)";
             this.zedGraphControl1.GraphPane.YAxis.Title.FontSpec.FontColor = Color.Blue;
             this.zedGraphControl1.GraphPane.YAxis.Title.FontSpec.Size = 20f;
             //刻度值字体大小、颜色
             this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 20f;
             this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.FontColor = Color.Black;

             this.zedGraphControl1.GraphPane.YAxis.Scale.FontSpec.Size = 20f;
             this.zedGraphControl1.GraphPane.YAxis.Scale.Min = 0;
             this.zedGraphControl1.GraphPane.YAxis.Scale.Max = 100;
             this.zedGraphControl1.GraphPane.YAxis.Scale.MinorStep = 1;//小步长
             //x轴数据类型
             this.zedGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.DateAsOrdinal;
             this.zedGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.Date;
             this.zedGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.Text;//显示文本
             this.zedGraphControl1.GraphPane.XAxis.Scale.Format = "HH:mm:ss";//时间格式
             //yy-mm-dd HH:mm:ss  其中HH是24小时制  hh是12小时制
             //显示网格线
             this.zedGraphControl1.GraphPane.YAxis.MajorGrid.IsVisible = true;
             this.zedGraphControl1.GraphPane.XAxis.MajorGrid.IsVisible = true;
             //legend图例
             this.zedGraphControl1.GraphPane.Legend.FontSpec.Size = 10f;
             this.zedGraphControl1.GraphPane.Legend.Position = LegendPos.Right;
             //面板填充颜色
             this.zedGraphControl1.GraphPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f);
            //在面板上添加文本
            TextObj text2 = new TextObj(
                "Zoom: left mouse & drag\nPan: middle mouse & drag\nContext Menu: right mouse",
                0.05f, 0.95f, CoordType.ChartFraction, AlignH.Left, AlignV.Bottom);
            text2.FontSpec.StringAlignment = StringAlignment.Near;
            this.zedGraphControl1.GraphPane.GraphObjList.Add(text2);
            //添加箭头图案
            ArrowObj myArrow = new ArrowObj(Color.Red, 12F, 230F, 70F, 280F, 55F);
            this.zedGraphControl1.GraphPane.GraphObjList.Add(myArrow);
        // get a reference to the GraphPane
        GraphPane myPane =zedGraphControl1.GraphPane;
        // Set the Titles
        myPane.Title.Text = "My Test Graph\n(For CodeProject Sample)";
        myPane.XAxis.Title.Text = "My X Axis";
        myPane.YAxis.Title.Text = "My Y Axis";

            //设置图表大小
            this.zedGraphControl1.Location = new Point(10, 10);
            this.zedGraphControl1.Size = new Size(ClientRectangle.Width - 20, ClientRectangle.Height - 20);
二、曲线的实时显示:            
  //显示曲线 
            list.Add(x,y);//添加数据点
            LineItem mycurve = zedGraphControl1.GraphPane.AddCurve
                 ("温度传感器1", list, Color.Red, SymbolType.Square);
            //LineItem mycurve = zedGraphControl1.GraphPane.AddCurve
            //               ("温度传感器1", null, Ty, Color.Red, SymbolType.Square);
            //this.zedGraphControl1.GraphPane.XAxis.Scale.TextLabels = Tx;
            mycurve.Symbol.Size = 5.0f;//节点图案大小
            mycurve.Line.Width = 2.0F;//线宽
            //在面板上添加文本
            TextObj text = new TextObj(y.ToString("") + "℃",
                x, y*1.02, CoordType.AxisXYScale, AlignH.Center, AlignV.Top);
            text.FontSpec.Size = 15f;
            text.FontSpec.FontColor = Color.Black;
            text.FontSpec.Border.IsVisible = false;
            text.FontSpec.Fill.IsVisible = true;
            text.ZOrder = ZOrder.A_InFront;
            text.FontSpec.Fill = new Fill(Color.FromArgb(240, Color.Snow));
            // Rotate the text to 90 degrees
            //text.FontSpec.Angle = 60;  //字体倾斜度
            this.zedGraphControl1.GraphPane.GraphObjList.Add(text);
            //更新坐标轴
            this.zedGraphControl1.AxisChange();
            //更新图像
            this.zedGraphControl1.Refresh();
            //同时清除该点的值!!注意索引   节点值刷新慢一拍
            if (list.Count >= 9) this.zedGraphControl1.GraphPane.GraphObjList.RemoveAt(1);//删除改点处的文本
            if (list.Count >= 10) list.RemoveAt(0);//大于10个点就删除第一个点

根据上面的设置就可以很容易定制自己的曲线图了。大笑大笑

三、程序参考:

<span style="font-size:14px;">        //标题、标签this.zedGraphControl1.GraphPane.Title.Text = "实时曲线图";this.zedGraphControl1.GraphPane.Title.FontSpec.FontColor = Color.Blue;this.zedGraphControl1.GraphPane.Title.FontSpec.Size = 30f;this.zedGraphControl1.GraphPane.XAxis.Title.Text = "时间";this.zedGraphControl1.GraphPane.XAxis.Title.FontSpec.FontColor = Color.Blue;this.zedGraphControl1.GraphPane.XAxis.Title.FontSpec.Size = 20f;this.zedGraphControl1.GraphPane.YAxis.Title.Text = "温度(℃)";this.zedGraphControl1.GraphPane.YAxis.Title.FontSpec.FontColor = Color.Blue;this.zedGraphControl1.GraphPane.YAxis.Title.FontSpec.Size = 20f;//this.zedGraphControl1.GraphPane.Chart.Border.Color  = Color .White  ;this.zedGraphControl1.GraphPane.Chart.IsRectAuto =true ;//刻度值字体大小this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 20f;this.zedGraphControl1.GraphPane.YAxis.Scale.FontSpec.Size = 20f;this.zedGraphControl1.GraphPane.YAxis.Scale.Min = 0;this.zedGraphControl1.GraphPane.YAxis.Scale.Max = 50;//x轴类型this.zedGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.Date;this.zedGraphControl1.GraphPane.XAxis.Scale.Format = "HH:mm:ss";//显示网格线this.zedGraphControl1.GraphPane.YAxis.MajorGrid.IsVisible = true;this.zedGraphControl1.GraphPane.XAxis.MajorGrid.IsVisible = true;//legendthis.zedGraphControl1.GraphPane.Legend.FontSpec.Size = 10f;this.zedGraphControl1.GraphPane.Legend.Position = LegendPos.InsideTopRight;//面板填充颜色this.zedGraphControl1.GraphPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f);//设置窗口大小SetSize();//更新画面this.zedGraphControl1.AxisChange();this.zedGraphControl1.Refresh();
</span>

定时显示程序:

<span style="font-size:14px;">         private void timer2_Tick(object sender, EventArgs e){this.zedGraphControl1.GraphPane.CurveList.Clear();//温度曲线1try{y = Convert.ToDouble(Convert.ToInt16(strTempureArry[4]) / 100.00); ;}catch{return;}//存储数据     StoreDataDelegate myStoreDataDelegate = new StoreDataDelegate(StoreData);//实例化委托myStoreDataDelegate(DateTimeNow, y);//调用委托list.Add(x, y);               LineItem mycurve = zedGraphControl1.GraphPane.AddCurve("温度传感器1", list, Color.Red, SymbolType.Circle);                          mycurve.Line.Width = 2.0F;//线宽TextObj textT1 = new TextObj(y.ToString("") + "℃",x, y*1.02, CoordType.AxisXYScale, AlignH.Center , AlignV.Bottom);textT1.FontSpec.Size = 10f;textT1.FontSpec.FontColor = Color.Red;textT1.FontSpec.Fill.IsVisible = false;textT1.FontSpec.Border.IsVisible = false;//textT1.ZOrder = ZOrder.A_InFront;//textT1.FontSpec.Fill = new Fill(Color.FromArgb(240, Color.Snow));this.zedGraphControl1.GraphPane.GraphObjList.Add(textT1);this.zedGraphControl1.AxisChange();this.zedGraphControl1.Refresh();//this.zedGraphControl1.Invalidate();if (list.Count >= 10){list.RemoveAt(0);//同时清除该点的值this.zedGraphControl1.GraphPane.GraphObjList.RemoveAt(0);}}
</span>

控件大小设置:

         private void SetSize(){zedGraphControl1.Location = new Point(10, 10);zedGraphControl1.Size = new Size(ClientRectangle.Width - 100, ClientRectangle.Height - 200);}


http://chatgpt.dhexx.cn/article/aLAl13pN.shtml

相关文章

ZedGraph控件常用方法和属性总结

最近在WPF使用ZedGraph控件&#xff0c;发现这个控件的功能很强大&#xff0c;据说采用了双缓冲机制来绘制图&#xff0c;只要控制好显示的帧速&#xff0c;能够显示速度较快的动态图。 参考&#xff1a;https://blog.csdn.net/qq_26093511/article/details/51329059 1、常用…

ZedGraph设置刻度轴的颜色、刻度文本颜色以及网格线的颜色

1 刻度轴的设置 本小节介绍 如何设置刻度的颜色、长度、生长方向以及将正上方的刻度隐藏掉&#xff0c;还有设置刻度文本的颜色Scale.FontSpec.FontColor。 Color axisColor Color.FromArgb(150, 150, 150);#region X轴//设置主刻度的长度this.zedGraph.GraphPane.XAxis.Majo…

ZedGraph 总论

ZedGraph 总论 ZedGraph 是一个开源的.NET图表类库&#xff0c; 并且全部代码都是用C#开发的。它可以利用任意的数据集合创建2D的线性和柱形图表。 ZedGraph的类库具有很高的灵活性。几乎图表的每个层面都可以被用户修改。同时&#xff0c;为了保证类库的易用性&#xff0c;所…

ZedGraph做统计

下载ZedGraph.dll,在工具箱添加 &#xff0c;然后将空间拖入界面&#xff0c;&#xff0c; ZedGraph.dll下载地址 &#xff1a; http://download.csdn.net/detail/happy09li/4276410 参考资料 点击打开链接 private void Form9_Load(object sender, EventArgs e){DataTable …

ZedGraph使用(一) 柱形图

ZedGraph使用&#xff08;一&#xff09; 柱形图 作者&#xff1a;Kevin 日期&#xff1a; 2008-12-04 QQ:475762235 关键字&#xff1a;ZedGraph、柱形图、统计图、C#、绘图、绘图控件、WebFrom 内容摘要&#xff1a; ZedGraph是一个开源的控件&#xff0c;提供了用户控件和…

ZedGraph绘图

一、下载及配置 下载ZedGraph 官网下载地址 http://sourceforge.net/projects/zedgraph/files/ 添加 ZedGraph.dll 和ZedGraph.Web.dll的引用 在控件库中添加ZedGraph控件 右键点击工具箱 - 选择项 - .Net Framework 组件 - 浏览 - 找到ZedGraph.dll 和ZedGraph.Web.d…

ZedGraph类库之基本教程篇

第一部分&#xff1a;基本教程篇 ZedGraphDemo中一共有9个基本教程的例子。其中大部分都类似&#xff0c;我会讲解其中一些比较典型的例子。把ZedGraph类库的使用逐步展现给大家。 第一节&#xff1a; InitialSampleDemo.cs这个文件 http://blog.csdn.net/tjvictor/archive/20…

ZedGraph使用经验

ZedGraph资源 ZedGraph来源&#xff1a;http://sourceforge.net/project/showfiles.php?group_id114675 ZedGraph 相关例子资源&#xff1a;http://zedgraph.org/wiki/index.php?titleSample_Graphs ZedGraph的特点&#xff1a; 第一&#xff0c;可以先生成图片后再显示&…

C#zegraph用法

一、引用using ZedGraph; 在资源管理器中点击引用&#xff0c;将ZedGraph.dll添加到工程项目中。 二、添加zedGraphControl控件 在工具箱的常规项目右击添加选择项&#xff0c;浏览zedgraph.dll 二、基本图、直方图、折线图、圆饼图 三、zedgraph相关属性 classDescrip…

ZedGraph 绘制动态曲线

文章目录 前言&#xff1a;开发环境&#xff1a;1 下载ZedGraph 控件并设置图形界面2 功能实现3 需求升级4 小结 话不多数&#xff0c;先上一个效果图&#xff1a; 前言&#xff1a; 需要采集一些设备的数据以图表的形式展示出来&#xff0c;研究数据的走向是否平稳&#xff0…

ZedGraph如何显示鼠标附近的曲线的点?介绍三种方法

使用ZedGraph绘制曲线图的时候&#xff0c;不仅仅是看曲线的走向&#xff0c;也需要查看曲线上某位位置处采集到的数据是多少。下面介绍三种方法&#xff0c;从简单到复杂。 文章目录 1、使用自带的功能显示点的坐标2、 多条曲线的坐标点同时显示3、 多条曲线的坐标点同时显示…

开源框架ZedGraph的使用

.Net的绘图控件ZedGraph的使用 ZedGraph 是一个开源的.NET图表类库&#xff0c; 全部代码都是用C#开发的。它可以利用任意的数据集合创建2D的线性和柱形图表 我们一般不直接使用ZedGraphControl对象&#xff0c;而是使用它的面板对象来进行操作&#xff0c;这里我们将它的面板…

redis解决高并发问题,如商品秒杀

redis真的是一个很好的技术&#xff0c;它可以很好的在一定程度上解决网站一瞬间的并发量&#xff0c;例如商品抢购秒杀等活动。。。 redis之所以能解决高并发的原因是它可以直接访问内存&#xff0c;而以往我们用的是数据库(硬盘),提高了访问效率,解决了数据库服务器压力。 …

Redis的高并发场景解决方案

1.Redis的信息 &#xff08;1&#xff09;单线程容易实现。 &#xff08;2&#xff09;效率高轻松处理每秒几十万数据&#xff08;因为是占内存的所以CPU不是瓶颈&#xff09;。 &#xff08;3&#xff09;支持数据类型多&#xff08;String&#xff0c;List&#xff0c;has…

java如何解决高并发问题_java怎么处理高并发?

java处理高并发的方法:1、优化代码,减少不必要的资源浪费;2、把图片与页面进行分离,将图片放到独立的图片服器;3、使用缓存,可以大量减少与数据库的交互,提高性能;4、使用数据库集群;5、进行DB优化;6、硬件上做到负载均衡等等。 java处理高并发的方法: 1、从最基础的…

flask 多进程/多线程 解决高并发问题

1、简介&#xff1a; Flask 默认是单进程&#xff0c;单线程阻塞的任务模式&#xff0c;在项目上线的时候可以通过nginxgunicorn 的方式部署flask任务。 app.run()中可以接受两个参数&#xff0c;分别是threaded和processes&#xff0c;用于开启线程支持和进程支持。 1.thre…

高并发超卖问题简要解决方案

1、传统通过数据库保证不超卖 事务行锁并不是解决超卖的方案&#xff0c;只是保障数据的统一性。传统通过回滚事务的方式防止某些用户多卖的情况。 采用新建一个防重表事务的方式防止超卖。同一事务中&#xff0c;采用如 用户ID商品ID 的方式作为防重表唯一索引字段的数值&…

python 中如何解决高并发问题

python 中小量高并发问题的解决 描述&#xff1a;在多个用户同时发起对同一个商品的下单请求时&#xff0c;先查询商品库存&#xff0c;再修改商品库存&#xff0c;会出现资源竞争问题&#xff0c;导致库存的最终结果出现异常。 例如&#xff1a;id为16的商品的库存为10&#…

高并发的解决方式

大型网站如何防止崩溃&#xff0c;解决高并发带来的问题 大型网站&#xff0c;比如门户网站&#xff0c;在面对大量用户访问、高并发请求方面带来的问题 1大并发&#xff1a;在同一个时间点&#xff0c;有大量的客户来访问我们的网站&#xff0c;如果访问量过大&#xff0c;就…

三个方法解决php并发问题

福利&#xff1a;[网络安全重磅福利&#xff1a;入门&进阶全套282G学习资源包免费分享 &#xff01;] 解决php并发问题的方法有很多&#xff0c;具体可以使用MySQL的行级锁、乐观锁和Redis的分布式锁等技术来解决。此外&#xff0c;还可以使用消息队列、多进程、多线程等技…