owchart证券图形控件学习:如何添加K线?

owchart证券图形控件是矿洞程序员陶德原创,我偶然发现了这个控件,感觉非常不错,所以记录一下学习过程。

下面是作者提供的几个控件下载地址:

百度网盘: https://pan.baidu.com/s/1xSzQacCF_ufXmRrVUHpaYQ 密码1111

git: https://github.com/FaceCat007/owchart1.git

gitee: https://gitee.com/facecat007/owchart1.git

CSDN:https://download.csdn.net/download/owchart/12887272

作者提供的教程非常好,基本上按原教程来学习就好了。本文只是记录一下练习用的代码,以备后续查阅。

打开教程的方法

1、首先运行owchart_net.exe程序;

2、在鼠标右键菜单,选择使用教程即可,如下图所示:

 

 

 完整代码如下:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Chart chart = new Chart();  //创建K线
            chart.Dock = DockStyle.Fill;  //设置填充          
            Controls.Add(chart);   //添加控件

            //设置显示左轴的宽度
            chart.LeftYScaleWidth = 100; 
            //设置K线的间隔   
            chart.XScalePixel = 50;  
            //添加第一个层,占100%   
            ChartDiv div1 = chart.AddChartDiv(100); 

            //开盘价=0,最高价=1,最低价=2,收盘价=3   
            int openField = 0; 
            int highField = 1; 
            int lowField = 2; 
            int closeField = 3;

            //添加K线   
            CandleShape candleShape = chart.AddCandle("K线", openField, highField, lowField, closeField, div1);

            //获取数据源 
            CTableEx dataSource = chart.DataSource;

            //candleShape.UpColor = Color.FromArgb(255, 80, 80);
            //candleShape.DownColor = Color.FromArgb(80, 255, 255);
            //candleShape.CandleStyle = CandleStyle.American;
            candleShape.StyleField = CTableEx.AutoField;
            candleShape.ColorField = CTableEx.AutoField;
            dataSource.AddColumn(candleShape.StyleField);
            dataSource.AddColumn(candleShape.ColorField);

            //添加第一条数据,获取第一个日期      
            double date1 = LbCommon.getDateNum(2010, 12, 20, 0, 0, 0, 0);  
            //插入高开低收的值
            dataSource.Set(date1, openField, 80);            
            dataSource.Set(date1, highField, 100);            
            dataSource.Set(date1, lowField, 70);            
            dataSource.Set(date1, closeField, 90);

            //添加第二条数据,获取第二个日期  
            double date2 = LbCommon.getDateNum(2010, 12, 21, 0, 0, 0, 0);
            dataSource.Set(date2, openField, 90);
            dataSource.Set(date2, highField, 110);
            dataSource.Set(date2, lowField, 80);
            dataSource.Set(date2, closeField, 105);
            
            //添加第三条数据,获取第三个日期
            double date3 = LbCommon.getDateNum(2010, 12, 22, 0, 0, 0, 0);
            dataSource.Set(date3, openField, 110);
            dataSource.Set(date3, highField, 100);
            dataSource.Set(date3, lowField, 60);
            dataSource.Set(date3, closeField, 65);

            //刷新图像  
            chart.RefreshGraph();
        }

    }
View Code

有2点需要注意:

1、原文中的“LbCommon.GetDateNum”函数是不存在的,应该是“LbCommon.getDateNum”;

2、按原文的代码直接运行会报错,需要增加下面语句才行:

            candleShape.StyleField = CTableEx.AutoField;
            candleShape.ColorField = CTableEx.AutoField;
            dataSource.AddColumn(candleShape.StyleField);
            dataSource.AddColumn(candleShape.ColorField);

成功运行后,界面如下:

LbCommon.GetDateNum
posted @ 2021-03-08 08:41  顺其自然,道法自然  阅读(910)  评论(0编辑  收藏  举报