NavChart.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. cmbBenchmark.DataSource = DataAccess.Get_dm_fund_benchmark(fundId);
  43. cmbBenchmark.DisplayMember = "index_short_name";
  44. cmbBenchmark.ValueMember = "index_id";
  45. cmbBenchmark.SelectedIndex = 0;
  46. chtNav.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.LightGray;
  47. chtNav.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Gray;
  48. chtNav.ChartAreas[0].AxisY.MinorGrid.Enabled = true;
  49. chtNav.ChartAreas[0].AxisY.MinorGrid.LineColor = Color.LightGray;
  50. chtNav.ChartAreas[0].AxisY.MinorGrid.LineDashStyle = ChartDashStyle.Dot;
  51. chtNav.ChartAreas[0].AxisY2.MinorGrid.Enabled = false;
  52. chtNav.ChartAreas[0].AxisY2.MajorGrid.LineColor = Color.LightGray;
  53. // Y 轴最大最小值自适应
  54. chtNav.ChartAreas[0].AxisY.IsStartedFromZero = false;
  55. chtNav.ChartAreas[0].AxisY2.IsStartedFromZero = false;
  56. Series seriesFund = chtNav.Series[0];
  57. seriesFund.ChartType = SeriesChartType.Line;
  58. seriesFund.MarkerStyle = MarkerStyle.Circle;
  59. seriesFund.LegendText = "分红再投净值";
  60. BindChartData(nav, seriesFund);
  61. Series seriesIndex = chtNav.Series[1];
  62. seriesIndex.ChartType = SeriesChartType.Line;
  63. seriesIndex.YAxisType = AxisType.Secondary;
  64. seriesIndex.LegendText = cmbBenchmark.GetItemText(cmbBenchmark.SelectedItem);
  65. string indexId = cmbBenchmark.SelectedValue.ToString();
  66. indexNav = DataAccess.Get_Nav(indexId, oldestPriceDate, latestPriceDate);
  67. BindChartData(indexNav, seriesIndex);
  68. }
  69. private DataTable FilterDate(DataTable data, DateTime startDate, DateTime endDate)
  70. {
  71. DataTable dt = null;
  72. if (startDate > endDate)
  73. {
  74. MessageBox.Show("起始日不能大于截止日哦");
  75. return null;
  76. }
  77. if (data != null && data.Rows.Count > 0)
  78. {
  79. string expression = "price_date <= #" + endDate.ToString() + "# and price_date >= #" + startDate.ToString() + "#";
  80. DataRow[] rows = data.Select(expression, "price_date");
  81. if (rows != null && rows.Length > 0)
  82. dt = rows.CopyToDataTable();
  83. }
  84. return dt;
  85. }
  86. private void BindChartData(DataTable data, Series series)
  87. {
  88. if (data != null && data.Rows.Count > 0)
  89. series.Points.DataBind(data.AsEnumerable(), "price_date", "cumulative_nav", "");
  90. }
  91. // 这个事件不好用,但Value_Changed会深度死机
  92. private void dtpEndDate_Leave(object sender, EventArgs e)
  93. {
  94. }
  95. private void btnDateChanged_Click(object sender, EventArgs e)
  96. {
  97. DateTime startDate = dtpStartDate.Value;
  98. if (dtpStartDate.Checked == false) startDate = DateTime.MinValue;
  99. DateTime endDate = dtpEndDate.Value;
  100. if (dtpEndDate.Checked == false) endDate = DateTime.MaxValue;
  101. BindChartData(FilterDate(nav, startDate, endDate), chtNav.Series["fund"]);
  102. BindChartData(FilterDate(indexNav, startDate, endDate), chtNav.Series["index"]);
  103. }
  104. private void cmbTimePeriod_SelectedIndexChanged(object sender, EventArgs e)
  105. {
  106. DateTime startDate = new DateTime();
  107. DateTime endDate = DateTime.Today;
  108. switch (cmbTimePeriod.SelectedIndex)
  109. {
  110. case 0:
  111. startDate = oldestPriceDate;
  112. break;
  113. case 1:
  114. startDate = endDate.AddMonths(-3);
  115. break;
  116. case 2:
  117. startDate = endDate.AddYears(-1);
  118. break;
  119. case 3:
  120. startDate = endDate.AddYears(-2);
  121. break;
  122. case 4:
  123. startDate = endDate.AddYears(-3);
  124. break;
  125. case 5:
  126. startDate = endDate.AddYears(-5);
  127. break;
  128. case 6:
  129. startDate = endDate.AddYears(-10);
  130. break;
  131. default:
  132. startDate = oldestPriceDate;
  133. break;
  134. }
  135. dtpStartDate.Value = startDate;
  136. dtpEndDate.Value = endDate;
  137. BindChartData(FilterDate(nav, startDate, endDate), chtNav.Series["fund"]);
  138. BindChartData(FilterDate(indexNav, startDate, endDate), chtNav.Series["index"]);
  139. }
  140. private void cmbBenchmark_SelectedIndexChanged(object sender, EventArgs e)
  141. {
  142. string indexId = cmbBenchmark.SelectedValue.ToString();
  143. if(indexId.Length != 10) return;
  144. indexNav = DataAccess.Get_Nav(indexId, oldestPriceDate, latestPriceDate);
  145. DateTime startDate = DateTime.Parse(dtpStartDate.Text);
  146. DateTime endDate = DateTime.Parse(dtpEndDate.Text);
  147. chtNav.Series["index"].LegendText = cmbBenchmark.GetItemText(cmbBenchmark.SelectedItem); ;
  148. BindChartData(FilterDate(indexNav, startDate, endDate), chtNav.Series["index"]);
  149. }
  150. }
  151. }