C#下利用開源NPlot繪製股票十字交叉線
阿新 • • 發佈:2019-02-14
在數字貨幣或者股票看盤交易軟體中,為了方便準確定位K線圖中的時間和買賣點,都用到了用十字交叉線來定位買賣時間和買賣點。這裡利用.Net下開源的NPlot圖表外掛,實現了跟著滑鼠移動的十字定位交叉線,並能做到根據螢幕座標轉化為時間和買賣點座標。這裡通過在K線圖上雙擊建立交叉十字線,十字線跟著滑鼠移動而移動,並且實時獲取移動的位置。效果如下:
主要程式碼實現包括PlotSurface2D(PlotSurface2D為NPlot控制元件中的容器類)建立及初始化、PlotSurface2D滑鼠雙擊及移動的事件繫結及程式碼實現,HorizontalLine橫線、VerticalLine豎線的宣告和建立。詳細程式碼如下:PlotSurface2D的宣告及初始化:
主要程式碼實現包括PlotSurface2D(PlotSurface2D為NPlot控制元件中的容器類)建立及初始化、PlotSurface2D滑鼠雙擊及移動的事件繫結及程式碼實現,HorizontalLine橫線、VerticalLine豎線的宣告和建立。詳細程式碼如下:PlotSurface2D的宣告及初始化:
private NPlot.Windows.PlotSurface2D KLinePS;
private void InitKLinePS() { KLinePS = new NPlot.Windows.PlotSurface2D(); this.KLinePS.AutoScaleAutoGeneratedAxes = true; this.KLinePS.AutoScaleTitle = false; this.KLinePS.DateTimeToolTip = true; this.KLinePS.DateTimeToolTip = true; this.KLinePS.Legend = null; this.KLinePS.LegendZOrder = -1; this.KLinePS.Location = new System.Drawing.Point(0, 0); this.KLinePS.Name = "costPS"; this.KLinePS.RightMenu = null; this.KLinePS.Padding = 1; //滑鼠tooltips 時間+價格 this.KLinePS.ShowCoordinates = true; this.KLinePS.Size = new System.Drawing.Size(969, 595); this.KLinePS.Width = 1300; this.KLinePS.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None; this.KLinePS.TabIndex = 2; this.KLinePS.Title = "123"; this.KLinePS.TitleFont = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel); }
HorizontalLine橫線、VerticalLine豎線的宣告,這裡只是宣告,具體的建立包括在滑鼠雙擊事件中:
VerticalLine lineCrossX;// = new VerticalLine(10);
HorizontalLine lineCrossY;// = new HorizontalLine(10);
滑鼠事件的繫結:
KLinePS.MouseMove += new System.Windows.Forms.MouseEventHandler(KLinePS_MouseMove); KLinePS.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(KLinePS_MouseDoubleClick);
滑鼠雙擊事件的實現:
private void KLinePS_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e) { System.Drawing.Point here = new System.Drawing.Point(e.X, e.Y); //螢幕座標轉化為業務座標 double x = this.KLinePS.PhysicalXAxis1Cache.PhysicalToWorld(here, true); double y = this.KLinePS.PhysicalYAxis1Cache.PhysicalToWorld(here, true); DateTime dateTime = new DateTime((long)x); //水平線建立 lineCrossY = new HorizontalLine(y); lineCrossY.LengthScale = 0.89f; lineCrossY.OrdinateValue = y; lineCrossY.Pen = Pens.Green; //line.OrdinateValue = 2; this.KLinePS.Add(lineCrossY); //// ///////垂直線/////////// lineCrossX = new VerticalLine(x); lineCrossX.LengthScale = 0.89f; lineCrossX.Pen = Pens.Red; lineCrossX.AbscissaValue = x; this.KLinePS.Add(lineCrossX); this.KLinePS.Refresh(); }
滑鼠移動事件的實現:
private void KLinePS_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (this.KLinePS.PhysicalXAxis1Cache == null || this.KLinePS.PhysicalYAxis1Cache == null)
return;
System.Drawing.Point here = new System.Drawing.Point(e.X, e.Y);
double x = this.KLinePS.PhysicalXAxis1Cache.PhysicalToWorld(here, true);
double y = this.KLinePS.PhysicalYAxis1Cache.PhysicalToWorld(here, true);
if (lineCrossY != null && lineCrossX != null)
{
lineCrossY.OrdinateValue = y;
lineCrossX.AbscissaValue = x;
}
this.KLinePS.Refresh();
}