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 MatrixPopupLine : UserControl { private ILog logger = LogManager.GetLogger("AppInfoLog"); public MatrixPopupLine() { InitializeComponent(); } private void MatrixPopupLine_Load(object sender, EventArgs e) { //定义图表大小尺寸 //chart1.Width = Width - 100; //chart1.Height = Height - 100; //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.Minimum = DateTime.Parse("00:00:00").ToOADate(); chart1.ChartAreas[0].AxisX.Maximum = DateTime.Parse("23:45:00").ToOADate(); 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.Spline; chart1.Series[0].XValueType = ChartValueType.DateTime; //chart1.Series[0].IsValueShownAsLabel = true;//显示数据点的值 chart1.Series[0].MarkerStyle = MarkerStyle.None; chart1.Series[1].LegendText = "AGC有功设定"; chart1.Series[1].ChartType = SeriesChartType.Line; chart1.Series[1].XValueType = ChartValueType.DateTime; //chart1.Series[1].IsValueShownAsLabel = true;//显示数据点的值 chart1.Series[1].MarkerStyle = MarkerStyle.None; chart1.Series[2].LegendText = "实时功率"; chart1.Series[2].ChartType = SeriesChartType.Line; chart1.Series[2].XValueType = ChartValueType.DateTime; //chart1.Series[2].IsValueShownAsLabel = true;//显示数据点的值 chart1.Series[2].MarkerStyle = MarkerStyle.None; chart1.Series[3].LegendText = "理论功率"; chart1.Series[3].ChartType = SeriesChartType.Line; chart1.Series[3].XValueType = ChartValueType.DateTime; //chart1.Series[3].IsValueShownAsLabel = true;//显示数据点的值 chart1.Series[3].MarkerStyle = MarkerStyle.None; } private string stationId; public string StationId { get { return stationId; } set { stationId = value; Thread ayscThread = new Thread(LoadData); ayscThread.IsBackground = true; ayscThread.Start(); } } // private DateTime lastUpdateTime = DateTime.Parse public void BuildChart(DateTime[] dts, Double[] speeds, Double[] powers) { chart1.Series[0].Points.Clear(); chart1.Series[1].Points.Clear(); if (dts.Length == speeds.Length && dts.Length == powers.Length) { for (int i = 0; i < dts.Length; i++) { this.chart1.Series[0].Points.AddXY(dts[i], speeds[i]); this.chart1.Series[1].Points.AddXY(dts[i], powers[i]); } } chart1.Invalidate(); } 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};\n功率:{2:F2};", dp1.XValue, dp1.YValues[0], dp2.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() { var dict = CacheService.Instance.DictPowerCurveModel; if (dict == null) return; var pcm = dict[StationId]; Console.WriteLine("==================当前曲线刷新场站为" + StationId); if (pcm == null) return; if (pcm.LastUpdateTime < DateTime.Today) { //跨天清空缓存数据 pcm.LastUpdateTime = DateTime.Today; pcm.FactPowerData.Clear(); pcm.PredictPowerData.Clear(); //pcm.CDQPredictPowerData; pcm.TheoryPowerData.Clear(); pcm.AgcPowerData.Clear(); pcm.WindSpeedData.Clear(); } DateTime now = DateTime.Now; long startTs = CommonMethod.ConvertDateTimeInt(pcm.LastUpdateTime); long endTs = CommonMethod.ConvertDateTimeInt(now); var lstFactPowerData = RestfulClient.findHistorySnapByTagName(pcm.FactPowerPoint, startTs, endTs, 20); pcm.FactPowerData.AddRange(lstFactPowerData); if (StationId != "SBQ_FDC") { var lstAgcPowerData = RestfulClient.findHistorySnapByTagName(pcm.AgcPowerPoint, startTs, endTs, 20); pcm.AgcPowerData.AddRange(lstAgcPowerData); } else { var lstAgcPowerData1 = RestfulClient.findHistorySnapByTagName("SBQDQ.NX_GD_SBQF_DQ_P1_L1_001_AI0818", startTs, endTs, 20); var lstAgcPowerData2 = RestfulClient.findHistorySnapByTagName("SBQDQ.NX_GD_SBQF_DQ_P1_L1_001_AI0827", startTs, endTs, 20); List list = new List(); for (int i = 0; i < lstAgcPowerData1.Count; i++) { if (lstAgcPowerData1.Count == lstAgcPowerData2.Count) { TsData t = new TsData(); t.ts = lstAgcPowerData1[i].ts; t.doubleValue = lstAgcPowerData1[i].doubleValue + lstAgcPowerData2[i].doubleValue; list.Add(t); } } pcm.AgcPowerData.AddRange(list); } var lstTheoryPowerData = RestfulClient.findHistorySnapByTagName(pcm.TheoryPowerPoint, startTs, endTs, 20); pcm.TheoryPowerData.AddRange(lstTheoryPowerData); var lstPredictPowerData = RestfulClient.findHistorySnapByTagName(pcm.PredictPowerPoint, startTs, endTs, 20); pcm.PredictPowerData.AddRange(lstPredictPowerData); pcm.LastUpdateTime = now; var dict2 = CacheService.Instance.MatrixCDQPredictPointMap; if (dict2 != null) { var pmms = dict2.Values.Where(q => q.ThingType == PointType.Station && q.ThingId == StationId).ToArray(); var points = pmms.Select(q => q.PointId).ToArray(); Dictionary dictResult = RestfulClient.findLatestByTagNames(points); if (dictResult != null && dictResult.Count > 0) { for (int i = 0; i < pcm.CDQPredictPowerData.Length; i++) { TsData td = new TsData(); td.ts = CommonMethod.ConvertDateTimeInt(pcm.LastUpdateTime.AddMinutes(15 * i)); td.doubleValue = 0; pcm.CDQPredictPowerData[i] = td; } foreach (string pid in dictResult.Keys) { string id = pid.Substring(pid.Length - 2, 2); int index = Convert.ToInt32(id); pcm.CDQPredictPowerData[index - 1].doubleValue = dictResult[pid].doubleValue == null ? 0 : dictResult[pid].doubleValue.Value; } } } DateTime endTime = DateTime.Now; TimeSpan sp = endTime - now; Console.WriteLine("==============================曲线数据访问时间:" + sp.TotalMilliseconds); lock (this) { this.BeginInvoke( (Action)delegate { try { DateTime sTime = DateTime.Now; this.chart1.Series[0].Points.Clear(); foreach (var td in pcm.PredictPowerData) { DateTime dt = CommonMethod.ConvertIntDateTime(td.ts); this.chart1.Series[0].Points.AddXY(dt, td.doubleValue.Value); } foreach (var td in pcm.CDQPredictPowerData) { DateTime dt = CommonMethod.ConvertIntDateTime(td.ts); this.chart1.Series[0].Points.AddXY(dt, td.doubleValue.Value); } this.chart1.Series[1].Points.Clear(); foreach (var td in pcm.AgcPowerData) { DateTime dt = CommonMethod.ConvertIntDateTime(td.ts); this.chart1.Series[1].Points.AddXY(dt, td.doubleValue.Value); } this.chart1.Series[2].Points.Clear(); foreach (var td in pcm.FactPowerData) { DateTime dt = CommonMethod.ConvertIntDateTime(td.ts); this.chart1.Series[2].Points.AddXY(dt, td.doubleValue.Value); } this.chart1.Series[3].Points.Clear(); foreach (var td in pcm.TheoryPowerData) { DateTime dt = CommonMethod.ConvertIntDateTime(td.ts); this.chart1.Series[3].Points.AddXY(dt, td.doubleValue.Value / 1000); } DateTime eTime = DateTime.Now; TimeSpan sp1 = eTime - sTime; Console.WriteLine("==============================曲线数据绑定时间:" + sp1.TotalMilliseconds); } catch (Exception ex) { logger.Error(ex); } }); } } #endregion } }