HistoryLine.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  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. namespace IntelligentControlForsx.MyControls
  12. {
  13. public partial class HistoryLine : UserControl
  14. {
  15. public HistoryLine()
  16. {
  17. InitializeComponent();
  18. this.chart1.Series[0].Points.AddXY(1, 3.5);
  19. this.chart1.Series[0].Points.AddXY(2, 4.2);
  20. this.chart1.Series[0].Points.AddXY(3, 6);
  21. this.chart1.Series[0].Points.AddXY(4, 5.5);
  22. this.chart1.Series[0].Points.AddXY(5, 5.2);
  23. this.chart1.Series[0].Points.AddXY(6, 5.8);
  24. this.chart1.Series[0].Points.AddXY(7, 5);
  25. this.chart1.Series[1].Points.AddXY(1, 30);
  26. this.chart1.Series[1].Points.AddXY(2, 80);
  27. this.chart1.Series[1].Points.AddXY(3, 60);
  28. this.chart1.Series[1].Points.AddXY(4, 700);
  29. this.chart1.Series[1].Points.AddXY(5, 60);
  30. this.chart1.Series[1].Points.AddXY(6, 850);
  31. this.chart1.Series[1].Points.AddXY(7, 444);
  32. }
  33. //Dictionary<DateTime, double> SpeedDic;
  34. public void BuildChart(DateTime[] dts,Double[] speeds,Double[] powers)
  35. {
  36. chart1.Series[0].Points.Clear();
  37. chart1.Series[1].Points.Clear();
  38. if (dts.Length == speeds.Length && dts.Length == powers.Length)
  39. {
  40. for (int i = 0; i < dts.Length; i++)
  41. {
  42. double thePower=0.0;
  43. bool powerIsNum = Double.TryParse(powers[i].ToString(), out thePower);
  44. double theSpeed = 0.0;
  45. bool speedIsNum = Double.TryParse(speeds[i].ToString(), out theSpeed);
  46. if (powerIsNum && speedIsNum)
  47. {
  48. try
  49. {
  50. int p = Convert.ToInt32(speeds[i]);
  51. int s = Convert.ToInt32(powers[i]);
  52. }
  53. catch (Exception)
  54. {
  55. continue;
  56. }
  57. this.chart1.Series[0].Points.AddXY(dts[i], speeds[i]);
  58. this.chart1.Series[1].Points.AddXY(dts[i], powers[i]);
  59. }
  60. }
  61. }
  62. chart1.Invalidate();
  63. }
  64. private void chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)
  65. {
  66. if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
  67. {
  68. int i = e.HitTestResult.PointIndex;
  69. //DataPoint dp = e.HitTestResult.Series.Points[i];
  70. DataPoint dp1 = this.chart1.Series[0].Points[i];
  71. DataPoint dp2 = this.chart1.Series[1].Points[i];
  72. //分别显示x轴和y轴的数值,其中{1:F3},表示显示的是float类型,精确到小数点后3位。
  73. e.Text = string.Format("时间:{0};\n风速:{1:F2};\n功率:{2:F2};", dp1.XValue, dp1.YValues[0], dp2.YValues[0]);
  74. }
  75. }
  76. }
  77. }