C#利用开源NPlot实现K线图(蜡烛图)

NPlot是.Net平台下开源的图表控件,包括K线图、柱状图、饼图、曲线图、散点图等。这里主要讲讲怎么利用NPlot实现股票K线图。NPlot中PlotSurface2D对象是整个NPlot的图表的容器,所有的图表都需要添加到PlotSurface2D中才能进行显示。在WinForm程序中我们引用的是Windows.PlotSurface2D类,此类集成自Control。这里利用的K线图数据来自OKEX比特币交易的日线数据。主要包括时间、开盘价、最高价、最低价、收盘价等数据。数据存储数据库中的,Nplot提供数据源绑定功能,通过数据库查询查出的DataTable对象可以直接绑定到蜡烛对象上。这里利用了NPlot中的CandlePlot蜡烛图对象。K线图效果如下:

C#利用开源NPlot实现K线图(蜡烛图)_第1张图片

整个代码分为PlotSurface2D声明、初始化、查询K线数据、创建CandlePlot对象并绑定数据,最后把CandlePlot对象添加到PlotSurface2D容器中进行刷新显示。具体代码如下:

PlotSurface2D声明,这里为类对象:
private NPlot.Windows. PlotSurface2D KLinePS;

PlotSurface2D初始化:

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 = 10;
            //鼠标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);
           
        }
SetCandlePlot方法为类的外部调用方法,包括K线数据查询、CandlePlot对象创建、数据源绑定及把CandlePlot对象添加到PlotSurface2D中去,最后通过PlotSurface2D的Refresh刷新显示。
 
 public void SetCandlePlot(string klineTable, string startTime, string endTime, Dictionary dicGridStrategyEntity, DataTable backTestResult)
        {
            string sql;
            dicAutoGridStrategy = dicGridStrategyEntity;
            this.backTestResult = backTestResult;
            backTestStartTime = startTime;
            backTestEndTime = endTime;
           // dealedCount = 0;
            sql = "select * from " + klineTable + "kline where datetime>#" + startTime + "#" + " and datetime<#"
                + endTime + "# order by datetime asc";
            DataTable dt = oleDbHelper.queryDataForDataTable(sql);
            CandlePlot cp = new CandlePlot();
            cp.DataSource = dt;
            cp.AbscissaData = "datetime";
            cp.OpenData = "open";
            cp.LowData = "low";
            cp.HighData = "high";
            cp.CloseData = "close";
            cp.Label = "蜡烛图";
            //开跌色
            cp.BearishColor = System.Drawing.Color.FromArgb(255, 255, 0, 0);
            //看涨色
            cp.BullishColor = System.Drawing.Color.FromArgb(255, 0, 255, 0);
            cp.Style = CandlePlot.Styles.Filled;
           //K线宽度
            cp.StickWidth = 4;            
            this.KLinePS.Add(cp);            
            //隐藏日期刻度标示
            this.KLinePS.XAxis1.HideTickText = true ;
            this.KLinePS.Refresh();            
        }

你可能感兴趣的:(C#,K线图,蜡烛图,NPlot,开源图表控件,C#)