Winform中实现ZedGraph滚轮缩放后自动重新加载数据
程序员文章站
2022-05-03 12:49:02
场景 Winforn中设置ZedGraph曲线图的属性、坐标轴属性、刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100112573 需求是在曲线图上进行滚轮或者鼠标划区域缩放时重新加载数据。 效果如下 注: 博客主页 ......
场景
winforn中设置zedgraph曲线图的属性、坐标轴属性、刻度属性:
https://blog.csdn.net/badao_liumang_qizhi/article/details/100112573
需求是在曲线图上进行滚轮或者鼠标划区域缩放时重新加载数据。
效果如下
注:
博客主页:
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
在包含zedgraph曲线图的窗体中的load方法中进行图形的初始化。
//初始化zedgraph common.datachart.datacharthelper.initgraphpane(this.zedgraphcontrol1);
在初始化方法中进行滚轮缩放事件的绑定
zgc.zoomevent -= zgc_zoomevent; //滚轮缩放事件 zgc.zoomevent += zgc_zoomevent; //滚轮缩放事件
然后在滚轮缩放事件中
private static void zgc_zoomevent(zedgraphcontrol sender, zoomstate oldstate, zoomstate newstate) { try { refillbeginindex = (int)sender.graphpane.xaxis.scale.min; //缩放后的开始索引点 refillendindex = (int)sender.graphpane.xaxis.scale.max; //缩放后的结束索引点 #region 重新填充数据 if (refillbeginindex < 0) refillbeginindex = 0; if (refillendindex >= global.instance.virtualdata.recorddatafilter.count) refillendindex = global.instance.virtualdata.recorddatafilter.count - 1; if (refillendindex < 0) refillendindex = 0; if (refillbeginindex > refillendindex) refillbeginindex = refillendindex; int begindatapoint = global.instance.virtualdata.recorddatafilter[refillbeginindex].datapoint; //保存开始记录的datapoint值 int enddatapoint = global.instance.virtualdata.recorddatafilter[refillendindex].datapoint; //保存结束记录的datapoint值 list<wongoing.entity.record> newfilterdata = new list<entity.record>(); //保存新过滤的数据 #region 开启一个后台线程,用于重新计算过滤数据,计算完毕刷新曲线 stopwatch sw = new stopwatch(); sw.restart(); using (system.componentmodel.backgroundworker bgworker = new system.componentmodel.backgroundworker()) { bgworker.workerreportsprogress = true; //允许报告进度 bgworker.dowork += delegate(object senderobj, system.componentmodel.doworkeventargs dwea) { ienumerable<entity.record> records = global.instance.virtualdata.recorddatalist.where(p => p.datapoint >= begindatapoint && p.datapoint <= enddatapoint); entity.record[] newrecords = records.toarray<entity.record>(); bgworker.reportprogress(10); if (records != null) { int count = newrecords.length; sw.stop(); sw.restart(); if (count > global.appconfig.showpointcount) { int progressvalue = 10; int interval = (count - (count % global.appconfig.showpointcount)) / global.appconfig.showpointcount; int w = 0; for (int k = 0; k < global.appconfig.showpointcount; k++) { newfilterdata.add(newrecords[w]); w += interval; if (progressvalue != 10 + (int)(k * 90 / global.appconfig.showpointcount)) { progressvalue = 10 + (int)(k * 90 / global.appconfig.showpointcount); bgworker.reportprogress(progressvalue); } } } else { newfilterdata.addrange(newrecords); } sw.stop(); } bgworker.reportprogress(100); }; //进度变化时改变进度对话框中的进度值 bgworker.progresschanged += delegate(object senderobj, system.componentmodel.progresschangedeventargs pcea) { if (pcea.progresspercentage >= 0 && pcea.progresspercentage <= 100) { dialog.frmprogressbar.instance.progressvalue = pcea.progresspercentage; } }; //后台重新计算过滤数据完成 bgworker.runworkercompleted += delegate(object senderobj, system.componentmodel.runworkercompletedeventargs rwcea) { dialog.frmprogressbar.instance.dispose(); //关闭进度对话框 refreshpane(sender, newfilterdata, null, null); //重新刷新曲线 }; bgworker.runworkerasync(); //启动后台计算过滤数据的线程 dialog.frmprogressbar.instance.showdialog(); //显示进度对话框 } #endregion #endregion } catch(exception ex) { icsharpcode.core.loggingservice<datacharthelper>.error("zgc_zoomevent is exception :" + ex.message, ex); } }
注:
1.首先通过refillbeginindex = (int)sender.graphpane.xaxis.scale.min和refillendindex = (int)sender.graphpane.xaxis.scale.max;获取
缩放后的开始与结束的索引。
2.然后判断开始索引是否小于0,小于0则赋值为0,结束索引是否大于数据的总个数,大于则为总个数减一。
3.然后根据缩放后的开始和结束的索引获取缩放后要填充的数据。
4.中间又夹杂着进度条的显示和是否大于设置的要显示的总个数。
5.最终将过滤后的数据重新保存,并以此去刷新曲线图。