ZedGraph5.1.5源碼分析去掉鼠標懸浮內容閃爍問題(附源碼下載)

場景

在使用ZedGraph繪製曲線圖時,將鼠標懸浮時內容閃爍,且頻率很高。編程

 

 

找到其源碼,發現不論鼠標移動的範圍大小,甚至乎不論鼠標是否移動,都要刷新一次Tooltip。ide

注:優化

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

實現

首先來到ZedGraph的官網spa

https://sourceforge.net/projects/zedgraph/.net

而後點擊File下的zedgraph sourcerest

 

 

選擇對應版本,這裏是5.1.5code

 

 

下載成功後,將zip解壓orm

找到source下的工程文件,雙擊使用VS打開對象

 

 

而後找到ZedGraphControl.Events.cs

 

 

找到其ZedGraphControl_MouseMove方法此方法是鼠標移動時的事件處理

能夠看到其對兩個方法的處理,一個是HandlePointValues,這是對顯示線上的點的座標時的處理,一個是HandleCursorValues這是對獲取最近曲線上的點的座標時的處理。

 

 

這樣看你的ZedGraph是開啓的哪樣設置。

假如是設置通過線上點時才顯示

zgc.IsShowPointValues = true;

 

那麼就要修改

HandlePointValues( mousePt );

這個方法

首先聲明一個類變量

private object lastObj;

 

用來存儲上一次使用的對象,而後找到其判斷條件,添加當前是否與上一次是同一對象

 

 

而後在最後方法返回時將當前對象賦值給上一次對象。

lastObj = nearestObj;

 

完整參考代碼

private Point HandlePointValues( Point mousePt )
  {
   int iPt;
   GraphPane pane;
   object nearestObj;

   using ( Graphics g = this.CreateGraphics() )
   {

    if ( _masterPane.FindNearestPaneObject( mousePt,
     g, out pane, out nearestObj, out iPt ) )
    {
                    if (nearestObj is CurveItem && iPt >= 0 && !object.Equals(nearestObj, lastObj))
     {
      CurveItem curve = (CurveItem)nearestObj;
      // Provide Callback for User to customize the tooltips
      if ( this.PointValueEvent != null )
      {
       string label = this.PointValueEvent( this, pane, curve, iPt );
       if ( label != null && label.Length > 0 )
       {
        this.pointToolTip.SetToolTip( this, label );
        this.pointToolTip.Active = true;
       }
       else
        this.pointToolTip.Active = false;
      }
      else
      {

       if ( curve is PieItem )
       {
        this.pointToolTip.SetToolTip( this,
         ( (PieItem)curve ).Value.ToString( _pointValueFormat ) );
       }
       //       else if ( curve is OHLCBarItem || curve is JapaneseCandleStickItem )
       //       {
       //        StockPt spt = (StockPt)curve.Points[iPt];
       //        this.pointToolTip.SetToolTip( this, ( (XDate) spt.Date ).ToString( "MM/dd/yyyy" ) + "\nOpen: $" +
       //        spt.Open.ToString( "N2" ) +
       //        "\nHigh: $" +
       //        spt.High.ToString( "N2" ) + "\nLow: $" +
       //        spt.Low.ToString( "N2" ) + "\nClose: $" +
       //        spt.Close.ToString
       //        ( "N2" ) );
       //       }
       else
       {
        PointPair pt = curve.Points[iPt];

        if ( pt.Tag is string )
         this.pointToolTip.SetToolTip( this, (string)pt.Tag );
        else
        {
         double xVal, yVal, lowVal;
         ValueHandler valueHandler = new ValueHandler( pane, false );
         if ( ( curve is BarItem || curve is ErrorBarItem || curve is HiLowBarItem )
           && pane.BarSettings.Base != BarBase.X )
          valueHandler.GetValues( curve, iPt, out yVal, out lowVal, out xVal );
         else
          valueHandler.GetValues( curve, iPt, out xVal, out lowVal, out yVal );

         string xStr = MakeValueLabel( curve.GetXAxis( pane ), xVal, iPt,
          curve.IsOverrideOrdinal );
         string yStr = MakeValueLabel( curve.GetYAxis( pane ), yVal, iPt,
          curve.IsOverrideOrdinal );

         this.pointToolTip.SetToolTip( this, "( " + xStr + ", " + yStr + " )" );

         //this.pointToolTip.SetToolTip( this,
         // curve.Points[iPt].ToString( this.pointValueFormat ) );
        }
       }

       this.pointToolTip.Active = true;
      }
     }
     else
      this.pointToolTip.Active = false;
    }
    else
     this.pointToolTip.Active = false;

    //g.Dispose();
   }
            lastObj = nearestObj; 
   return mousePt;
  }

 

具體其餘優化與功能修改可自行發掘。

若是在ZedGraph中設置的是顯示最近曲線上的點的座標,即

zgc.IsShowCursorValues = true;

 

那麼就要修改源碼的HandleCursorValues方法

一樣聲明一個類變量存儲上次得到的點

private Point lastMovedPoint;

 

而後在方法中加上判斷並經過

this.pointToolTip.AutomaticDelay = 1000;

 

設置提示延遲1秒。最後再將當前點賦值給類變量。

lastMovedPoint = mousePt;

完整示例代碼

private Point HandleCursorValues( Point mousePt )
  {
            GraphPane pane = _masterPane.FindPane(mousePt);
            if (pane != null && pane.Chart._rect.Contains(mousePt) && !mousePt.Equals(lastMovedPoint))
            {
                // Provide Callback for User to customize the tooltips
                if (this.CursorValueEvent != null)
                {
                    string label = this.CursorValueEvent(this, pane, mousePt);
                    if (label != null && label.Length > 0)
                    {
                        this.pointToolTip.AutomaticDelay = 1000;
                        this.pointToolTip.SetToolTip(this, label);
                        this.pointToolTip.Active = true;
                    }
                    else
                    {
                        this.pointToolTip.Active = false;
                    }
                    lastMovedPoint = mousePt;
                }
                else
                {
                    double x, x2, y, y2;
                    pane.ReverseTransform(mousePt, out x, out x2, out y, out y2);
                    string xStr = MakeValueLabel(pane.XAxis, x, -1, true);
                    string yStr = MakeValueLabel(pane.YAxis, y, -1, true);
                    string y2Str = MakeValueLabel(pane.Y2Axis, y2, -1, true);
                    this.pointToolTip.AutomaticDelay = 1000;
                    this.pointToolTip.SetToolTip(this, "( " + xStr + ", " + yStr + ", " + y2Str + " )");
                    this.pointToolTip.Active = true;
                }
               
            }
            else
                this.pointToolTip.Active = false;

            return mousePt;
           
  }

 

注:

這裏只着重修改當用戶重寫此事件的狀況下,即this.CursorValueEvent != null時,具體狀況可跟據本身須要進行修改。

ZedGraph5.1.5源碼與修改版源碼下載

關注公衆號:

霸道的程序猿

回覆:

ZedGraph源碼修改

相關文章
相關標籤/搜索