NavChart.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Windows.Forms.DataVisualization.Charting;
  11. using System.Windows.Markup;
  12. using System.Xaml.Schema;
  13. namespace DataManager
  14. {
  15. public partial class NavChart : Form
  16. {
  17. private string fundId;
  18. private DataTable nav;
  19. private DataTable indexNav;
  20. private DateTime oldestPriceDate = new DateTime(2000,1,1);
  21. private DateTime latestPriceDate = DateTime.Today;
  22. public NavChart(string fundId, DataTable nav)
  23. {
  24. InitializeComponent();
  25. this.fundId = fundId;
  26. this.nav = nav;
  27. InitializeData();
  28. }
  29. private void InitializeData()
  30. {
  31. dtpStartDate.Checked = true;
  32. if (nav != null && nav.Rows.Count > 0)
  33. {
  34. oldestPriceDate = DateTime.Parse(nav.Select("", "price_date")[0]["price_date"].ToString());
  35. if (oldestPriceDate <= new DateTime(1990, 1, 1)) oldestPriceDate = new DateTime(1990, 1, 1);
  36. latestPriceDate = DateTime.Parse(nav.Select("", "price_date")[nav.Rows.Count - 1]["price_date"].ToString());
  37. dtpStartDate.Value = oldestPriceDate;
  38. }
  39. dtpEndDate.Checked = true;
  40. dtpEndDate.Value = DateTime.Today;
  41. cmbTimePeriod.SelectedIndex = 0;
  42. if (!string.IsNullOrEmpty(fundId))
  43. {
  44. cmbBenchmark.DataSource = DataAccess.Get_dm_fund_benchmark(fundId);
  45. }
  46. else
  47. {
  48. DataTable dt = new DataTable();
  49. dt.Columns.Add("index_id", typeof(string));
  50. dt.Columns.Add("index_short_name", typeof(string));
  51. dt.Rows.Add("IN00000008", "沪深300");
  52. cmbBenchmark.DataSource = dt;
  53. }
  54. cmbBenchmark.DisplayMember = "index_short_name";
  55. cmbBenchmark.ValueMember = "index_id";
  56. cmbBenchmark.SelectedIndex = 0;
  57. chtNav.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.LightGray;
  58. chtNav.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Gray;
  59. chtNav.ChartAreas[0].AxisY.MinorGrid.Enabled = true;
  60. chtNav.ChartAreas[0].AxisY.MinorGrid.LineColor = Color.LightGray;
  61. chtNav.ChartAreas[0].AxisY.MinorGrid.LineDashStyle = ChartDashStyle.Dot;
  62. chtNav.ChartAreas[0].AxisY2.MinorGrid.Enabled = false;
  63. chtNav.ChartAreas[0].AxisY2.MajorGrid.LineColor = Color.LightGray;
  64. // Y 轴最大最小值自适应
  65. chtNav.ChartAreas[0].AxisY.IsStartedFromZero = false;
  66. chtNav.ChartAreas[0].AxisY2.IsStartedFromZero = false;
  67. Series seriesFund = chtNav.Series[0];
  68. seriesFund.ChartType = SeriesChartType.Line;
  69. seriesFund.MarkerStyle = MarkerStyle.Circle;
  70. seriesFund.LegendText = "分红再投净值";
  71. BindChartData(nav, seriesFund);
  72. Series seriesIndex = chtNav.Series[1];
  73. seriesIndex.ChartType = SeriesChartType.Line;
  74. seriesIndex.YAxisType = AxisType.Secondary;
  75. seriesIndex.LegendText = cmbBenchmark.GetItemText(cmbBenchmark.SelectedItem);
  76. string indexId = cmbBenchmark.SelectedValue.ToString();
  77. indexNav = DataAccess.Get_Nav(indexId, oldestPriceDate, latestPriceDate);
  78. BindChartData(indexNav, seriesIndex);
  79. }
  80. private DataTable FilterDate(DataTable data, DateTime startDate, DateTime endDate)
  81. {
  82. DataTable dt = null;
  83. if (startDate > endDate)
  84. {
  85. MessageBox.Show("起始日不能大于截止日哦");
  86. return null;
  87. }
  88. if (data != null && data.Rows.Count > 0)
  89. {
  90. string expression = "price_date <= #" + endDate.ToString() + "# and price_date >= #" + startDate.ToString() + "#";
  91. DataRow[] rows = data.Select(expression, "price_date");
  92. if (rows != null && rows.Length > 0)
  93. dt = rows.CopyToDataTable();
  94. }
  95. return dt;
  96. }
  97. private void BindChartData(DataTable data, Series series)
  98. {
  99. if (data != null && data.Rows.Count > 0)
  100. series.Points.DataBind(data.AsEnumerable(), "price_date", "cumulative_nav", "");
  101. }
  102. // 这个事件不好用,但Value_Changed会深度死机
  103. private void dtpEndDate_Leave(object sender, EventArgs e)
  104. {
  105. }
  106. private void btnDateChanged_Click(object sender, EventArgs e)
  107. {
  108. DateTime startDate = dtpStartDate.Value;
  109. if (dtpStartDate.Checked == false) startDate = DateTime.MinValue;
  110. DateTime endDate = dtpEndDate.Value;
  111. if (dtpEndDate.Checked == false) endDate = DateTime.MaxValue;
  112. BindChartData(FilterDate(nav, startDate, endDate), chtNav.Series["fund"]);
  113. BindChartData(FilterDate(indexNav, startDate, endDate), chtNav.Series["index"]);
  114. }
  115. private void cmbTimePeriod_SelectedIndexChanged(object sender, EventArgs e)
  116. {
  117. DateTime startDate = new DateTime();
  118. DateTime endDate = DateTime.Today;
  119. switch (cmbTimePeriod.SelectedIndex)
  120. {
  121. case 0:
  122. startDate = oldestPriceDate;
  123. break;
  124. case 1:
  125. startDate = endDate.AddMonths(-3);
  126. break;
  127. case 2:
  128. startDate = endDate.AddYears(-1);
  129. break;
  130. case 3:
  131. startDate = endDate.AddYears(-2);
  132. break;
  133. case 4:
  134. startDate = endDate.AddYears(-3);
  135. break;
  136. case 5:
  137. startDate = endDate.AddYears(-5);
  138. break;
  139. case 6:
  140. startDate = endDate.AddYears(-10);
  141. break;
  142. default:
  143. startDate = oldestPriceDate;
  144. break;
  145. }
  146. dtpStartDate.Value = startDate;
  147. dtpEndDate.Value = endDate;
  148. BindChartData(FilterDate(nav, startDate, endDate), chtNav.Series["fund"]);
  149. BindChartData(FilterDate(indexNav, startDate, endDate), chtNav.Series["index"]);
  150. }
  151. private void cmbBenchmark_SelectedIndexChanged(object sender, EventArgs e)
  152. {
  153. string indexId = cmbBenchmark.SelectedValue.ToString();
  154. if(indexId.Length != 10) return;
  155. indexNav = DataAccess.Get_Nav(indexId, oldestPriceDate, latestPriceDate);
  156. DateTime startDate = DateTime.Parse(dtpStartDate.Text);
  157. DateTime endDate = DateTime.Parse(dtpEndDate.Text);
  158. chtNav.Series["index"].LegendText = cmbBenchmark.GetItemText(cmbBenchmark.SelectedItem); ;
  159. BindChartData(FilterDate(indexNav, startDate, endDate), chtNav.Series["index"]);
  160. }
  161. }
  162. }