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

本文介绍如何使用NPlot在.NET平台上绘制股票K线图。文章详细解释了PlotSurface2D对象的配置方法,以及如何从数据库获取K线数据,并将其绑定到CandlePlot对象上以实现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 = 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<int, GridStrategyEntity> 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();            
        }

html5和c++开源 K线图工具, Create interactive charts easily for your web projects. Used by tens of thousands of developers and 61 out of the world's 100 largest companies, Highcharts is the simplest yet most flexible charting API on the market. Read more » Download » TemperatureRainfallTokyo climateSunshine hoursJanFebMarAprMayJunJulAugSepOctNovDec5°C10°C15°C20°C25°C30°C0 mm50 mm100 mm150 mm200 mm250 mmHighcharts.com Highstock Highstock lets you create stock or general timeline charts in pure JavaScript. Including sophisticated navigation options like a small navigator series, preset date ranges, date picker, scrolling and panning. Read more » Download » ZoomUSD to EUR14. Feb21. Feb28. Feb7. Mar14. Mar21. Mar28. Mar4. Apr11. Apr18. Apr25. Apr2. May9. May20042006200820100.70.650.751m3m6mYTD1yAllHighcharts.com Highmaps Interactive map charts with drilldown and touch support. Build interactive maps to display sales, election results or any other information linked to geography. Perfect for standalone use or in dashboards in combination with Highcharts! Read more » Download » Highcharts © Natural Earth Highcharts Cloud Online charts for non-techies. Create smashing, interactive diagrams for your news site or blog, or for sharing with your friends on social media. Read more » Highcharts Cloud 1 2 3 4 Free for non-commercial Do you want to use Highcharts for a personal website, a school site or a non-profit organisation? Then you don't need our permission, just go on! HTML 5 Based on native browser technologies, no plugins needed Open Fork us on GitHub and participate in tech discussions Compatible With modern browsers including mobile, tablets and old IE back to IE6 Dynamic Add, remove and modify series and points or modify axes Any chart you'd like Highcharts supports line, spline, area, areaspline, column, bar, pie, scatter, angular gauges, arearange, areasplinerang
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凡梦_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值