Winform中實現ZedGraph滾輪縮放後自動從新加載數據

場景

Winforn中設置ZedGraph曲線圖的屬性、座標軸屬性、刻度屬性:編程

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100112573this

需求是在曲線圖上進行滾輪或者鼠標劃區域縮放時從新加載數據。spa

效果以下.net

 

 

注:pwa

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公衆號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。線程

實現

在包含ZedGraph曲線圖的窗體中的load方法中進行圖形的初始化。code

//初始化ZedGraph
Common.DataChart.DataChartHelper.InitGraphPane(this.zedGraphControl1);

在初始化方法中進行滾輪縮放事件的綁定blog

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.最終將過濾後的數據從新保存,並以此去刷新曲線圖。

相關文章
相關標籤/搜索