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

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();
}边栏推荐
- [HCTF 2018]admin
- C语言:快速排序三种方法(递归)
- [网鼎杯 2020 青龙组]AreUSerialz
- MySQL storage engine
- 秒杀项目的总结及面试常见问题
- Art-template 中文文档[详细篇]
- Connect to Mysql in the cloud server Docker detailed graphic and text operations (full)
- TypeError The view function did not return a valid response. The function either returned None 的解决
- Misc-traffic analysis of CTF
- P3 元宝第六单元笔记
猜你喜欢
随机推荐
【Typescript】学习笔记(三)之接口与泛型的使用
Redis简单了解
node手写服务器实现访问index页面
P3 元宝第三天的笔记
php实现数据库的增删查改操作-教务管理系统
C语言指针(指针数组、数组指针、函数指针、传参、回调函数等)超详细
Qt对动态库(*.dll)的封装以及使用
php漏洞全解
【文献阅读】Age Progress/Regression by Conditional Adversarial Autoencoder 基于条件对抗自编码器(CAAE)的老化/去龄化方案
cJSON开源项目详细解剖
[PASECA2019]honey_shop
P3 元宝第五单元笔记
零基础C语言“函数”教程,有手就行
Misc of CTF-image steganography
猜数字游戏
npm run serve启动报错npm ERR Missing script “serve“
函数解剖——深挖printf()与scanf()
MongoDB快速入门与基本使用
Qt设置窗口可拖动
Connect to Mysql in the cloud server Docker detailed graphic and text operations (full)






