StationPowerLine.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. using System.Threading;
  12. using WisdomClient;
  13. using IntelligentControlForsx.Service;
  14. using WisdomClient.data;
  15. using log4net;
  16. using IntelligentControlForsx.Model;
  17. using IntelligentControlForsx.Common;
  18. namespace IntelligentControlForsx.MyControls
  19. {
  20. public partial class StationPowerLine : UserControl
  21. {
  22. private ILog logger = LogManager.GetLogger("AppInfoLog");
  23. private string stationId;
  24. [Browsable(true), Category("DATA")]
  25. public string StationId
  26. {
  27. get
  28. {
  29. return stationId;
  30. }
  31. set
  32. {
  33. stationId = value;
  34. }
  35. }
  36. //实际功率
  37. [Browsable(true), Category("DATA")]
  38. public string FactPowerPoint { get; set; }
  39. //上次更新时间
  40. private DateTime lastUpdateTime = DateTime.Now.AddHours(-8);
  41. //实际功率
  42. private List<TsData> factPowerData = new List<TsData>();
  43. public StationPowerLine()
  44. {
  45. InitializeComponent();
  46. SetStyle(ControlStyles.UserPaint, true);
  47. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  48. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲  
  49. this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  50. this.UpdateStyles();
  51. }
  52. private void StationPowerLine_Load(object sender, EventArgs e)
  53. {
  54. //X轴设置
  55. //chart1.ChartAreas[0].AxisX.Title = "时间";
  56. chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Near;
  57. chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;//不显示竖着的分割线
  58. /************************************************************************/
  59. /* 本文重点讲解时间格式的设置
  60. * 如果想显示原点第一个时间坐标,需要设置最小时间,时间间隔类型,时间间隔值等三个参数*/
  61. /************************************************************************/
  62. chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm"; //X轴显示的时间格式,HH为大写时是24小时制,hh小写时是12小时制
  63. chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;//如果是时间类型的数据,间隔方式可以是秒、分、时
  64. chart1.ChartAreas[0].AxisX.Interval = DateTime.Parse("01:00:00").Second;//间隔为15分钟
  65. //Y轴设置
  66. //chart1.ChartAreas[0].AxisY.Title = "功率";
  67. //chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Center;
  68. chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;//显示横着的分割线
  69. //chart1.ChartAreas[0].AxisY.Minimum = 0;
  70. //chart1.ChartAreas[0].AxisY.Maximum = 600;
  71. //chart1.ChartAreas[0].AxisY.Interval = 5;
  72. //Series绘制
  73. chart1.Series[0].LegendText = "实时功率";
  74. chart1.Series[0].ChartType = SeriesChartType.Line;
  75. chart1.Series[0].XValueType = ChartValueType.DateTime;
  76. //chart1.Series[0].IsValueShownAsLabel = true;//显示数据点的值
  77. chart1.Series[0].MarkerStyle = MarkerStyle.None;
  78. //this.timer1.Start();
  79. }
  80. private void UpdateChartAxisXTime()
  81. {
  82. DateTime dtEnd = DateTime.Now;
  83. DateTime dtStart = dtEnd.AddHours(-8);
  84. chart1.ChartAreas[0].AxisX.Minimum = dtStart.ToOADate();
  85. chart1.ChartAreas[0].AxisX.Maximum = dtEnd.ToOADate();
  86. }
  87. private void chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)
  88. {
  89. if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
  90. {
  91. int i = e.HitTestResult.PointIndex;
  92. //DataPoint dp = e.HitTestResult.Series.Points[i];
  93. DataPoint dp1 = this.chart1.Series[0].Points[i];
  94. //DataPoint dp2 = this.chart1.Series[1].Points[i];
  95. //分别显示x轴和y轴的数值,其中{1:F3},表示显示的是float类型,精确到小数点后3位。
  96. e.Text = string.Format("时间:{0};\n功率:{1:F2};", dp1.XValue, dp1.YValues[0]);
  97. }
  98. }
  99. public void DeActive()
  100. {
  101. timer1.Stop();
  102. }
  103. public void Active()
  104. {
  105. Thread ayscThread = new Thread(LoadData);
  106. ayscThread.IsBackground = true;
  107. ayscThread.Start();
  108. timer1.Start();
  109. }
  110. #region 异步加载数据
  111. private void timer1_Tick(object sender, EventArgs e)
  112. {
  113. if (isLoadding) return;
  114. Thread ayscThread = new Thread(LoadData);
  115. ayscThread.IsBackground = true;
  116. ayscThread.Start();
  117. //LoadData();
  118. }
  119. private bool isLoadding = false;
  120. private void LoadData()
  121. {
  122. if (isLoadding)
  123. return;
  124. isLoadding = true;
  125. try
  126. {
  127. UpdateCurve();
  128. }
  129. catch (Exception ex)
  130. {
  131. logger.Info("读取实时数据失败!ex=" + ex.Message);
  132. }
  133. finally
  134. {
  135. isLoadding = false;
  136. }
  137. }
  138. private void UpdateCurve()
  139. {
  140. DateTime now = DateTime.Now;
  141. long startTs = CommonMethod.ConvertDateTimeInt(lastUpdateTime);
  142. long endTs = CommonMethod.ConvertDateTimeInt(now);
  143. var lstFactPowerData = RestfulClient.findHistoryRawByTagName(FactPowerPoint, startTs, endTs);
  144. this.factPowerData.AddRange(lstFactPowerData);
  145. this.lastUpdateTime = now;
  146. //清除过时(超过8小时)数据
  147. long outTs = CommonMethod.ConvertDateTimeInt(now.AddHours(-8));
  148. for(int i=0;i< factPowerData.Count(); i++)
  149. {
  150. if (factPowerData[i].ts < outTs)
  151. {
  152. factPowerData.Remove(factPowerData[i]);
  153. i--;
  154. }
  155. }
  156. lock (this)
  157. {
  158. this.BeginInvoke(
  159. (Action)delegate
  160. {
  161. try
  162. {
  163. chart1.ChartAreas[0].AxisX.Minimum = now.AddHours(-8).ToOADate();
  164. chart1.ChartAreas[0].AxisX.Maximum = now.ToOADate();
  165. this.chart1.Series[0].Points.Clear();
  166. foreach (var td in factPowerData)
  167. {
  168. DateTime dt = CommonMethod.ConvertIntDateTime(td.ts);
  169. this.chart1.Series[0].Points.AddXY(dt, td.doubleValue.Value);
  170. }
  171. }
  172. catch(Exception ex)
  173. {
  174. logger.Error(ex);
  175. }
  176. });
  177. }
  178. }
  179. #endregion
  180. }
  181. }