C#版K线图组件-StockChartX

这个C#版的K线图组建也是非常的老,零几年就出来了,其实它是一个图表组件,支持绘制K线图而已。

上图上代码

如何基于StockChartX显示K线

1、将ModulusFE.StockChartX.dll添加到项目引用中

如何C#编程显示K线图

 

2、定义K线数据类

public class BarData
    {
        public DateTime Date; 
        public double OpenPrice;
        public double HighPrice;
        public double LowPrice;
        public double ClosePrice;
        public double Volume;
    }

 

 

3、声明图表实例

private readonly StockChartX _stockChartX;

 

4、从文件中读取数据,装载入K线数据List

private void LoadData(string fileName)
        {
            Data.Clear();
            #region
            try
            {
                using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + fileName, FileMode.Open))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        sr.BaseStream.Seek(0, SeekOrigin.Begin);
                        while (sr.Peek() > -1)
                        {
                            string[] record = sr.ReadLine().Split(new[] { ',' });
                            string[] date = record[0].Split('/');
                            BarData bar = new BarData
                            {
                                Date = new DateTime(int.Parse(date[2]), int.Parse(date[0]), int.Parse(date[1])),
                                OpenPrice = Double.Parse(record[1], CultureInfo.InvariantCulture),
                                HighPrice = Double.Parse(record[2], CultureInfo.InvariantCulture),
                                LowPrice = Double.Parse(record[3], CultureInfo.InvariantCulture),
                                ClosePrice = Double.Parse(record[4], CultureInfo.InvariantCulture),
                                Volume = Double.Parse(record[5], CultureInfo.InvariantCulture) / 1000000
                            };
                            Data.Add(bar);
                        }
                    }
                }
            }
            catch
            {
                return;
            }
            #endregion
        }

 

5、刷新K线图

public void RefreshChart()
        {
            _currentBar = (int)(Data.Count * 0.75);
            for (Row = 0; Row < _currentBar; Row++)
            {
                BarData bd = Data[Row];
                _stockChartX.AppendOHLCValues(_stockChartX.Symbol, bd.Date, bd.OpenPrice, bd.HighPrice, bd.LowPrice, bd.ClosePrice);
                _stockChartX.AppendVolumue(_stockChartX.Symbol, bd.Date, bd.Volume);
            }
            _stockChartX.Update();
        }

 

6、运行预览

如何C#编程显示K线图

 

7、组件下载

分享到:

评论已关闭