如果自己开发行情软件,如何方便地呈现K线?
最麻烦的办法就是用GDI+等方法自己绘制,国内的行情软件(例如通达信、大智慧)大多是这样开发的。
但是这对于个人来说不论从开发周期上还是技术上都是不可取的。
最简单快捷的方法就是用第三方组件,例如MicrosoftChar,ZedGragh,StockChartX等。
下面我介绍如何基于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();
        }
自己开发的K线图呈现模块:
如何C#编程显示K线图
posted on 2012-11-20 15:04  懒懒的呐喊  阅读(3832)  评论(0编辑  收藏  举报