■ ChartControl 클래스를 사용해 캔들 차트를 만드는 방법을 보여준다.
▶ OHLCModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System; namespace TestProject { /// <summary> /// OHLC 모델 /// </summary> public class OHLCModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public string ID { get; set; } #endregion #region 일자 - DATE /// <summary> /// 일자 /// </summary> public DateTime DATE {get; set;} #endregion #region 시가 - OPEN /// <summary> /// 시가 /// </summary> public decimal OPEN { get; set; } #endregion #region 고가 - HIGH /// <summary> /// 고가 /// </summary> public decimal HIGH { get; set; } #endregion #region 저가 - LOW /// <summary> /// 저가 /// </summary> public decimal LOW { get; set; } #endregion #region 종가 - CLOSE /// <summary> /// 종가 /// </summary> public decimal CLOSE { get; set; } #endregion #region 거래량 - VOLUME /// <summary> /// 거래량 /// </summary> public int VOLUME { get; set; } #endregion } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using DevExpress.XtraCharts; using DevExpress.XtraEditors; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// OHLC 리스트 /// </summary> private List<OHLCModel> ohlcList; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { this.ohlcList = GetOHLCList(); this.idComboBoxEdit1.Properties.Items.AddRange(ohlcList.Select(olhc => olhc.ID).Distinct().ToArray()); this.idComboBoxEdit1.SelectedIndex = 1; } #endregion #region ID 콤보 박스 에디터 선택 인덱스 변경시 처리하기 - idComboBoxEdit_SelectedIndexChanged(sender, e) /// <summary> /// ID 콤보 박스 에디터 선택 인덱스 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void idComboBoxEdit_SelectedIndexChanged(object sender, EventArgs e) { List<OHLCModel> targetList = this.ohlcList.Where ( ohlc => ohlc.ID == (string)this.idComboBoxEdit1.SelectedItem ).OrderBy(ohlc => ohlc.DATE).Reverse().Take(150).ToList(); this.chartControl.DataSource = targetList; DateTime maximumDate = targetList.Max(ohlc => ohlc.DATE); XYDiagram diagram = this.chartControl.Diagram as XYDiagram; diagram.AxisX.VisualRange.SetMinMaxValues(maximumDate.AddDays(-60), maximumDate); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region OHLC 리스트 구하기 - GetOHLCList() /// <summary> /// OHLC 리스트 구하기 /// </summary> /// <returns>OHLC 리스트</returns> private List<OHLCModel> GetOHLCList() { IEnumerable<string[]> itemArrayEnumerable = File.ReadAllLines("Source.csv").Select(line => line.Split('\t')); IEnumerable<OHLCModel> ohlcEnumerable = from line in itemArrayEnumerable select new OHLCModel() { ID = line[6], DATE = Convert.ToDateTime(line[0]), OPEN = Convert.ToDecimal(line[1]), HIGH = Convert.ToDecimal(line[2]), LOW = Convert.ToDecimal(line[3]), CLOSE = Convert.ToDecimal(line[4]), VOLUME = Convert.ToInt32(line[5]) }; return ohlcEnumerable.ToList(); } #endregion } } |