ZedGraph5.1.5源码分析去掉鼠标悬浮内容闪烁问题(附源码下载)
場景
在使用ZedGraph繪制曲線圖時(shí),將鼠標(biāo)懸浮時(shí)內(nèi)容閃爍,且頻率很高。
?
找到其源碼,發(fā)現(xiàn)不論鼠標(biāo)移動(dòng)的范圍大小,甚至乎不論鼠標(biāo)是否移動(dòng),都要刷新一次Tooltip。
注:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號(hào)
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費(fèi)下載。
實(shí)現(xiàn)
首先來到ZedGraph的官網(wǎng)
https://sourceforge.net/projects/zedgraph/
然后點(diǎn)擊File下的zedgraph source
?
選擇對(duì)應(yīng)版本,這里是5.1.5
?
下載成功后,將zip解壓
找到source下的工程文件,雙擊使用VS打開
?
然后找到ZedGraphControl.Events.cs
?
找到其ZedGraphControl_MouseMove方法此方法是鼠標(biāo)移動(dòng)時(shí)的事件處理
可以看到其對(duì)兩個(gè)方法的處理,一個(gè)是HandlePointValues,這是對(duì)顯示線上的點(diǎn)的坐標(biāo)時(shí)的處理,一個(gè)是HandleCursorValues這是對(duì)獲取最近曲線上的點(diǎn)的坐標(biāo)時(shí)的處理。
這樣看你的ZedGraph是開啟的哪樣設(shè)置。
假如是設(shè)置經(jīng)過線上點(diǎn)時(shí)才顯示
zgc.IsShowPointValues = true;那么就要修改
HandlePointValues( mousePt );
這個(gè)方法
首先聲明一個(gè)類變量
private object lastObj;用來存儲(chǔ)上一次使用的對(duì)象,然后找到其判斷條件,添加當(dāng)前是否與上一次是同一對(duì)象
然后在最后方法返回時(shí)將當(dāng)前對(duì)象賦值給上一次對(duì)象。
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 tooltipsif ( 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;}elsethis.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 );elsevalueHandler.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;}}elsethis.pointToolTip.Active = false;}elsethis.pointToolTip.Active = false;//g.Dispose();}lastObj = nearestObj;return mousePt;}具體其他優(yōu)化與功能修改可自行發(fā)掘。
如果在ZedGraph中設(shè)置的是顯示最近曲線上的點(diǎn)的坐標(biāo),即
zgc.IsShowCursorValues = true;那么就要修改源碼的HandleCursorValues方法
同樣聲明一個(gè)類變量存儲(chǔ)上次獲得的點(diǎn)
private Point lastMovedPoint;然后在方法中加上判斷并通過
this.pointToolTip.AutomaticDelay = 1000;設(shè)置提示延遲1秒。最后再將當(dāng)前點(diǎn)賦值給類變量。
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 tooltipsif (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;}}elsethis.pointToolTip.Active = false;return mousePt;}注:
這里只著重修改當(dāng)用戶重寫此事件的情況下,即this.CursorValueEvent != null時(shí),具體情況可跟據(jù)自己需要進(jìn)行修改。
ZedGraph5.1.5源碼與修改版源碼下載
關(guān)注公眾號(hào):
霸道的程序猿
回復(fù):
ZedGraph源碼修改
總結(jié)
以上是生活随笔為你收集整理的ZedGraph5.1.5源码分析去掉鼠标悬浮内容闪烁问题(附源码下载)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android中通过ImageSwitc
- 下一篇: 阿里巴巴矢量图标库iconfont的使用