C#利⽤开源NPlot实现K线图(蜡烛图)
NPlot是.Net平台下开源的图表控件,包括K线图、柱状图、饼图、曲线图、散点图等。这⾥主要讲讲怎么利⽤NPlot实现股票K线图。
NPlot中PlotSurface2D对象是整个NPlot的图表的容器,所有的图表都需要添加到PlotSurface2D中才能进⾏显⽰。在WinForm程序中
卓文君白头吟我们引⽤的是Windows.PlotSurface2D类,此类集成⾃Control。这⾥利⽤的K线图数据来⾃OKEX⽐特币交易的⽇线数据。主要包括时
间、开盘价、最⾼价、最低价、收盘价等数据。数据存储数据库中的,Nplot提供数据源绑定功能,通过数据库查询查出的DataTable
对象可以直接绑定到蜡烛对象上。这⾥利⽤了NPlot中的CandlePlot蜡烛图对象。K线图效果如下:
整个代码分为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 = fal ;
976年
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 时间+价格
word海报>广东叉烧包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<int, GridStrategyEntity> dicGridStrategyEntity, DataTable backTe {
string sql;
dicAutoGridStrategy = dicGridStrategyEntity;
this.backTestResult = backTestResult;
backTestStartTime = startTime;
backTestEndTime = endTime;
// dealedCount = 0;
sql = "lect * 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.CloData = "clo";
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();
}