using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; using System.Threading; using WisdomClient; using IntelligentControlForsx.Service; using WisdomClient.data; using log4net; using IntelligentControlForsx.Model; using IntelligentControlForsx.Common; namespace IntelligentControlForsx.MyControls { public partial class StationPowerLine : UserControl { private ILog logger = LogManager.GetLogger("AppInfoLog"); private string stationId; [Browsable(true), Category("DATA")] public string StationId { get { return stationId; } set { stationId = value; } } //实际功率 [Browsable(true), Category("DATA")] public string FactPowerPoint { get; set; } //上次更新时间 private DateTime lastUpdateTime = DateTime.Now.AddHours(-8); //实际功率 private List factPowerData = new List(); public StationPowerLine() { InitializeComponent(); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲   this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); this.UpdateStyles(); } private void StationPowerLine_Load(object sender, EventArgs e) { //X轴设置 //chart1.ChartAreas[0].AxisX.Title = "时间"; chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Near; chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;//不显示竖着的分割线 /************************************************************************/ /* 本文重点讲解时间格式的设置 * 如果想显示原点第一个时间坐标,需要设置最小时间,时间间隔类型,时间间隔值等三个参数*/ /************************************************************************/ chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm"; //X轴显示的时间格式,HH为大写时是24小时制,hh小写时是12小时制 chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;//如果是时间类型的数据,间隔方式可以是秒、分、时 chart1.ChartAreas[0].AxisX.Interval = DateTime.Parse("01:00:00").Second;//间隔为15分钟 //Y轴设置 //chart1.ChartAreas[0].AxisY.Title = "功率"; //chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Center; chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;//显示横着的分割线 //chart1.ChartAreas[0].AxisY.Minimum = 0; //chart1.ChartAreas[0].AxisY.Maximum = 600; //chart1.ChartAreas[0].AxisY.Interval = 5; //Series绘制 chart1.Series[0].LegendText = "实时功率"; chart1.Series[0].ChartType = SeriesChartType.Line; chart1.Series[0].XValueType = ChartValueType.DateTime; //chart1.Series[0].IsValueShownAsLabel = true;//显示数据点的值 chart1.Series[0].MarkerStyle = MarkerStyle.None; //this.timer1.Start(); } private void UpdateChartAxisXTime() { DateTime dtEnd = DateTime.Now; DateTime dtStart = dtEnd.AddHours(-8); chart1.ChartAreas[0].AxisX.Minimum = dtStart.ToOADate(); chart1.ChartAreas[0].AxisX.Maximum = dtEnd.ToOADate(); } private void chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e) { if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint) { int i = e.HitTestResult.PointIndex; //DataPoint dp = e.HitTestResult.Series.Points[i]; DataPoint dp1 = this.chart1.Series[0].Points[i]; //DataPoint dp2 = this.chart1.Series[1].Points[i]; //分别显示x轴和y轴的数值,其中{1:F3},表示显示的是float类型,精确到小数点后3位。 e.Text = string.Format("时间:{0};\n功率:{1:F2};", dp1.XValue, dp1.YValues[0]); } } public void DeActive() { timer1.Stop(); } public void Active() { Thread ayscThread = new Thread(LoadData); ayscThread.IsBackground = true; ayscThread.Start(); timer1.Start(); } #region 异步加载数据 private void timer1_Tick(object sender, EventArgs e) { if (isLoadding) return; Thread ayscThread = new Thread(LoadData); ayscThread.IsBackground = true; ayscThread.Start(); //LoadData(); } private bool isLoadding = false; private void LoadData() { if (isLoadding) return; isLoadding = true; try { UpdateCurve(); } catch (Exception ex) { logger.Info("读取实时数据失败!ex=" + ex.Message); } finally { isLoadding = false; } } private void UpdateCurve() { DateTime now = DateTime.Now; long startTs = CommonMethod.ConvertDateTimeInt(lastUpdateTime); long endTs = CommonMethod.ConvertDateTimeInt(now); var lstFactPowerData = RestfulClient.findHistoryRawByTagName(FactPowerPoint, startTs, endTs); this.factPowerData.AddRange(lstFactPowerData); this.lastUpdateTime = now; //清除过时(超过8小时)数据 long outTs = CommonMethod.ConvertDateTimeInt(now.AddHours(-8)); for(int i=0;i< factPowerData.Count(); i++) { if (factPowerData[i].ts < outTs) { factPowerData.Remove(factPowerData[i]); i--; } } lock (this) { this.BeginInvoke( (Action)delegate { try { chart1.ChartAreas[0].AxisX.Minimum = now.AddHours(-8).ToOADate(); chart1.ChartAreas[0].AxisX.Maximum = now.ToOADate(); this.chart1.Series[0].Points.Clear(); foreach (var td in factPowerData) { DateTime dt = CommonMethod.ConvertIntDateTime(td.ts); this.chart1.Series[0].Points.AddXY(dt, td.doubleValue.Value); } } catch(Exception ex) { logger.Error(ex); } }); } } #endregion } }