/// <summary> /// 月累積量公式 /// </summary> private void Month_ElectricTotal() { TTime = DateTime.Now; ElectricTotals.Clear(); foreach (var ElectricConfigitem in ElectricConfigs) { var ElectricTotal = MysqlMethod.Search_ElectricTotal($"{TTime:yyyyMM01}", $"{TTime:yyyyMM31}", ElectricConfigitem.GatewayIndex, ElectricConfigitem.DeviceIndex); for (int i = 0; i < DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month); i++) { foreach (var item in ElectricTotal) { if (i >= 9) { if (item.ttime == DateTime.Now.ToString("yyyyMM") + $"{i + 1}") { ElectricTotals.Add(item); } else { ElectricTotal electricTotal = new ElectricTotal() { ttimen = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/") + $"{i + 1}" + " 00:00:00"), GatewayIndex = ElectricConfigitem.GatewayIndex, DeviceIndex = ElectricConfigitem.DeviceIndex, KwhTotal = 0 }; ElectricTotals.Add(electricTotal); } } else { if (item.ttime == DateTime.Now.ToString("yyyyMM") + $"0{i + 1}") { ElectricTotals.Add(item); } else { ElectricTotal electricTotal = new ElectricTotal() { ttimen = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/") + $"0{i + 1}" + " 00:00:00"), GatewayIndex = ElectricConfigitem.GatewayIndex, DeviceIndex = ElectricConfigitem.DeviceIndex, KwhTotal = 0 }; ElectricTotals.Add(electricTotal); } } } } } }
/// <summary> /// 查詢按鈕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ShearsimpleButton_Click(object sender, EventArgs e) { if (DeviceCheckedcomboBoxEdit.Text != "" && ValuecomboBoxEdit.Text != "" && StartdateEdit.Text != "" && EnddateEdit.Text != null && Convert.ToDateTime(StartdateEdit.EditValue) <= Convert.ToDateTime(EnddateEdit.EditValue)) { if (LinechartControl.Series.Count >= 0) { LinechartControl.Series.Clear(); } if (gridView1.Columns.Count > 0) { gridView1.Columns.Clear(); } string starttime = ((DateTime)StartdateEdit.EditValue).ToString("yyyyMMdd000000"); //起始時間 string endtime = ((DateTime)EnddateEdit.EditValue).ToString("yyyyMMdd235959"); //結束時間 string Tstarttime = ((DateTime)StartdateEdit.EditValue).ToString("yyyyMMdd"); //起始時間 string Tendtime = ((DateTime)EnddateEdit.EditValue).ToString("yyyyMMdd"); //結束時間 List <Series> Lineseries = new List <Series>(); //曲線圖用 ThreePhaseElectricMeter_Logs = new List <ThreePhaseElectricMeter_Log>(); //報表用 ElectricTotals = new List <ElectricTotal>(); ElectricSearchTypeEnum electricSearchTypeEnum = (ElectricSearchTypeEnum)ValuecomboBoxEdit.SelectedIndex; for (int i = 0; i < DeviceCheckedcomboBoxEdit.Properties.Items.Count; i++) { if (DeviceCheckedcomboBoxEdit.Properties.Items[i].CheckState == CheckState.Checked) { ElectricConfig = (ElectricConfig)DeviceCheckedcomboBoxEdit.Properties.Items[i].Tag; var data = MysqlMethod.Search_ThreePhaseElectricMeter_Log(starttime, endtime, ElectricConfig.GatewayIndex, ElectricConfig.DeviceIndex); var ElectricTotaldata = MysqlMethod.Search_ElectricTotal(Tstarttime, Tendtime, ElectricConfig.GatewayIndex, ElectricConfig.DeviceIndex); #region 報表資料整理 ThreePhaseElectricMeter_Logs.AddRange(data); ElectricTotals.AddRange(ElectricTotaldata); #endregion #region 曲線圖資料整理 switch (electricSearchTypeEnum) { case ElectricSearchTypeEnum.Voltage: { Series series1 = new Series($"{ElectricConfig.DeviceName} - R相電壓", viewType: ViewType.Line); series1.CrosshairLabelPattern = "{S}" + "\n" + "時間:{A:yyyy-MM-dd HH:mm:ss} " + "\n" + "數值:{V:0.00}V"; series1.LegendTextPattern = "{A}"; series1.ArgumentDataMember = "ttimen"; series1.LabelsVisibility = DefaultBoolean.False; series1.ValueDataMembers.AddRange(new string[] { "rsv" }); Lineseries.Add(series1); Series series2 = new Series($"{ElectricConfig.DeviceName} - S相電壓", viewType: ViewType.Line); series2.CrosshairLabelPattern = "{S}" + "\n" + "時間:{A:yyyy-MM-dd HH:mm:ss} " + "\n" + "數值:{V:0.00}V"; series2.LegendTextPattern = "{A}"; series2.ArgumentDataMember = "ttimen"; series2.LabelsVisibility = DefaultBoolean.False; series2.ValueDataMembers.AddRange(new string[] { "stv" }); Lineseries.Add(series2); Series series3 = new Series($"{ElectricConfig.DeviceName} - T相電壓", viewType: ViewType.Line); series3.CrosshairLabelPattern = "{S}" + "\n" + "時間:{A:yyyy-MM-dd HH:mm:ss} " + "\n" + "數值:{V:0.00}V"; series3.LegendTextPattern = "{A}"; series3.ArgumentDataMember = "ttimen"; series3.LabelsVisibility = DefaultBoolean.False; series3.ValueDataMembers.AddRange(new string[] { "trv" }); Lineseries.Add(series3); } break; case ElectricSearchTypeEnum.Current: { Series series1 = new Series($"{ElectricConfig.DeviceName}- R相電流", viewType: ViewType.Line); series1.CrosshairLabelPattern = "{S}" + "\n" + "時間:{A:yyyy-MM-dd HH:mm:ss} " + "\n" + "數值:{V:0.00}A"; series1.LegendTextPattern = "{A}"; series1.ArgumentDataMember = "ttimen"; series1.LabelsVisibility = DefaultBoolean.False; series1.ValueDataMembers.AddRange(new string[] { "ra" }); Lineseries.Add(series1); Series series2 = new Series($"{ElectricConfig.DeviceName} - S相電流", viewType: ViewType.Line); series2.CrosshairLabelPattern = "{S}" + "\n" + "時間:{A:yyyy-MM-dd HH:mm:ss} " + "\n" + "數值:{V:0.00}A"; series2.LegendTextPattern = "{A}"; series2.ArgumentDataMember = "ttimen"; series2.LabelsVisibility = DefaultBoolean.False; series2.ValueDataMembers.AddRange(new string[] { "sa" }); Lineseries.Add(series2); Series series3 = new Series($"{ElectricConfig.DeviceName} - T相電流", viewType: ViewType.Line); series3.CrosshairLabelPattern = "{S}" + "\n" + "時間:{A:yyyy-MM-dd HH:mm:ss} " + "\n" + "數值:{V:0.00}A"; series3.LegendTextPattern = "{A}"; series3.ArgumentDataMember = "ttimen"; series3.LabelsVisibility = DefaultBoolean.False; series3.ValueDataMembers.AddRange(new string[] { "ta" }); Lineseries.Add(series3); } break; case ElectricSearchTypeEnum.kW: { Series series1 = new Series($"{ElectricConfig.DeviceName} - 瞬間功率", viewType: ViewType.Line); series1.CrosshairLabelPattern = "{S}" + "\n" + "時間:{A:yyyy-MM-dd HH:mm:ss} " + "\n" + "數值:{V:0.00}kW"; series1.LegendTextPattern = "{A}"; series1.ArgumentDataMember = "ttimen"; series1.LabelsVisibility = DefaultBoolean.False; series1.ValueDataMembers.AddRange(new string[] { "kw" }); Lineseries.Add(series1); } break; case ElectricSearchTypeEnum.kVAR: { Series series1 = new Series($"{ElectricConfig.DeviceName} - 瞬間虛功率", viewType: ViewType.Line); series1.CrosshairLabelPattern = "{S}" + "\n" + "時間:{A:yyyy-MM-dd HH:mm:ss} " + "\n" + "數值:{V:0.00}kVAR"; series1.LegendTextPattern = "{A}"; series1.ArgumentDataMember = "ttimen"; series1.LabelsVisibility = DefaultBoolean.False; series1.ValueDataMembers.AddRange(new string[] { "kvar" }); Lineseries.Add(series1); } break; case ElectricSearchTypeEnum.PF: { Series series1 = new Series($"{ElectricConfig.DeviceName} - 功率因數", viewType: ViewType.Line); series1.CrosshairLabelPattern = "{S}" + "\n" + "時間:{A:yyyy-MM-dd HH:mm:ss} " + "\n" + "數值:{V:0.00}"; series1.LegendTextPattern = "{A}"; series1.ArgumentDataMember = "ttimen"; series1.LabelsVisibility = DefaultBoolean.False; series1.ValueDataMembers.AddRange(new string[] { "pfe" }); Lineseries.Add(series1); } break; case ElectricSearchTypeEnum.HZ: { Series series1 = new Series($"{ElectricConfig.DeviceName} - 頻率", viewType: ViewType.Line); series1.CrosshairLabelPattern = "{S}" + "\n" + "時間:{A:yyyy-MM-dd HH:mm:ss} " + "\n" + "數值:{V:0.00}HZ"; series1.LegendTextPattern = "{A}"; series1.ArgumentDataMember = "ttimen"; series1.LabelsVisibility = DefaultBoolean.False; series1.ValueDataMembers.AddRange(new string[] { "hz" }); Lineseries.Add(series1); } break; case ElectricSearchTypeEnum.kWh: { Series series1 = new Series($"{ElectricConfig.DeviceName} - 累積電量", viewType: ViewType.Bar); series1.DataSource = ElectricTotaldata; series1.CrosshairLabelPattern = "{S}" + "\n" + "時間:{A:yyyy-MM-dd} " + "\n" + "數值:{V:0.00}kWh"; series1.LegendTextPattern = "{A}"; series1.ArgumentDataMember = "ttimen"; series1.LabelsVisibility = DefaultBoolean.False; series1.ValueDataMembers.AddRange(new string[] { "KwhTotal" }); Lineseries.Add(series1); } break; } #endregion } } switch (electricSearchTypeEnum) { case ElectricSearchTypeEnum.Voltage: case ElectricSearchTypeEnum.Current: case ElectricSearchTypeEnum.kW: case ElectricSearchTypeEnum.kVAR: case ElectricSearchTypeEnum.PF: case ElectricSearchTypeEnum.HZ: { gridControl1.DataSource = ThreePhaseElectricMeter_Logs; LinechartControl.DataSource = ThreePhaseElectricMeter_Logs; } break; case ElectricSearchTypeEnum.kWh: { gridControl1.DataSource = ElectricTotals; //LinechartControl.DataSource = ElectricTotals; } break; } #region 報表 for (int i = 0; i < gridView1.Columns.Count; i++) { gridView1.Columns[i].Visible = false; } gridView1.OptionsBehavior.Editable = false; gridView1.OptionsView.ColumnAutoWidth = false; gridView1.OptionsSelection.EnableAppearanceFocusedCell = false; switch (electricSearchTypeEnum) { case ElectricSearchTypeEnum.Voltage: { gridView1.Columns["trv"].Visible = true; gridView1.Columns["trv"].Caption = "T相電壓"; gridView1.Columns["trv"].BestFit(); gridView1.Columns["stv"].Visible = true; gridView1.Columns["stv"].Caption = "S相電壓"; gridView1.Columns["stv"].BestFit(); gridView1.Columns["rsv"].Visible = true; gridView1.Columns["rsv"].Caption = "R相電壓"; gridView1.Columns["rsv"].BestFit(); } break; case ElectricSearchTypeEnum.Current: { gridView1.Columns["ta"].Visible = true; gridView1.Columns["ta"].Caption = "T相電流"; gridView1.Columns["ta"].BestFit(); gridView1.Columns["sa"].Visible = true; gridView1.Columns["sa"].Caption = "S相電流"; gridView1.Columns["sa"].BestFit(); gridView1.Columns["ra"].Visible = true; gridView1.Columns["ra"].Caption = "R相電流"; gridView1.Columns["ta"].BestFit(); } break; case ElectricSearchTypeEnum.kW: { gridView1.Columns["kw"].Visible = true; gridView1.Columns["kw"].Caption = "瞬間功率"; gridView1.Columns["kw"].BestFit(); } break; case ElectricSearchTypeEnum.kVAR: { gridView1.Columns["kvar"].Visible = true; gridView1.Columns["kvar"].Caption = "瞬間虛功率"; gridView1.Columns["kvar"].BestFit(); } break; case ElectricSearchTypeEnum.PF: { gridView1.Columns["pfe"].Visible = true; gridView1.Columns["pfe"].Caption = "功率因數"; gridView1.Columns["pfe"].BestFit(); } break; case ElectricSearchTypeEnum.HZ: { gridView1.Columns["hz"].Visible = true; gridView1.Columns["hz"].Caption = "頻率"; gridView1.Columns["hz"].BestFit(); } break; case ElectricSearchTypeEnum.kWh: { gridView1.Columns["KwhTotal"].Visible = true; gridView1.Columns["KwhTotal"].Caption = "累積電量"; gridView1.Columns["KwhTotal"].BestFit(); } break; } gridView1.Columns["ttimen"].Visible = true; gridView1.Columns["ttimen"].Caption = "時間"; switch (electricSearchTypeEnum) { case ElectricSearchTypeEnum.Voltage: case ElectricSearchTypeEnum.Current: case ElectricSearchTypeEnum.kW: case ElectricSearchTypeEnum.kVAR: case ElectricSearchTypeEnum.PF: case ElectricSearchTypeEnum.HZ: { gridView1.Columns["ttimen"].DisplayFormat.FormatString = "yyyy/MM/dd HH:mm:ss"; } break; case ElectricSearchTypeEnum.kWh: { gridView1.Columns["ttimen"].DisplayFormat.FormatString = "yyyy/MM/dd"; } break; } gridView1.Columns["ttimen"].BestFit(); gridView1.Columns["GatewayIndex"].Caption = "閘道器"; gridView1.Columns["DeviceIndex"].Caption = "設備"; gridView1.CustomColumnDisplayText += (s, ex) => { ColumnView view = s as ColumnView; if (ex.Column.FieldName == "DeviceIndex" && ex.ListSourceRowIndex != DevExpress.XtraGrid.GridControl.InvalidRowHandle) { int val = (int)ex.Value; for (int i = 0; i < ElectricConfigs.Count; i++) { int data = (int)view.GetListSourceRowCellValue(ex.ListSourceRowIndex, "GatewayIndex"); if (data == ElectricConfigs[i].GatewayIndex) { if (ElectricConfigs[i].DeviceIndex == val) { ex.DisplayText = ElectricConfigs[i].DeviceName; break; } } } } else if (ex.Column.FieldName == "GatewayIndex" && ex.ListSourceRowIndex != DevExpress.XtraGrid.GridControl.InvalidRowHandle) { int val = (int)ex.Value; for (int i = 0; i < GatewayConfigs.Count; i++) { ex.DisplayText = GatewayConfigs[i].GatewayName; } } }; gridView1.Columns["GatewayIndex"].Group(); gridView1.Columns["DeviceIndex"].Group(); #endregion #region 曲線圖 LinechartControl.Legend.Direction = LegendDirection.TopToBottom; //曲線圖線條說明的排序 LinechartControl.CrosshairOptions.CrosshairLabelMode = CrosshairLabelMode.ShowCommonForAllSeries; //顯示全部線條內容 LinechartControl.CrosshairOptions.LinesMode = CrosshairLinesMode.Auto; //自動獲取點上面的數值 LinechartControl.CrosshairOptions.GroupHeaderTextOptions.Font = new Font("微軟正黑體", 12); LinechartControl.CrosshairOptions.ShowArgumentLabels = true; //是否顯示Y軸垂直線 LinechartControl.SideBySideEqualBarWidth = false; //線條是否需要相等寬度 foreach (var item in Lineseries) { LinechartControl.Series.AddRange(item); } #region 最後曲線圖顯示刻度 if (LinechartControl.DataSource != null) { XYDiagram diagram = (XYDiagram)LinechartControl.Diagram; if (diagram != null) { diagram.EnableAxisXZooming = true; //放大縮小 diagram.EnableAxisXScrolling = true; //拖曳 switch (electricSearchTypeEnum) { case ElectricSearchTypeEnum.Voltage: case ElectricSearchTypeEnum.Current: case ElectricSearchTypeEnum.kW: case ElectricSearchTypeEnum.kVAR: case ElectricSearchTypeEnum.PF: case ElectricSearchTypeEnum.HZ: { diagram.AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Minute; // 顯示設定 diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Minute; // 刻度設定 diagram.AxisX.WholeRange.SideMarginsValue = 0; //不需要邊寬 diagram.AxisY.WholeRange.AlwaysShowZeroLevel = false; diagram.AxisX.Label.TextPattern = "{A:yyyy-MM-dd HH:mm}"; //X軸顯示 } break; case ElectricSearchTypeEnum.kWh: { diagram.AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Day; // 顯示設定 diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Day; // 刻度設定 diagram.AxisX.WholeRange.SideMarginsValue = 1; //不需要邊寬 diagram.AxisX.Label.TextPattern = "{A:yyyy-MM-dd}"; //X軸顯示 } break; } } } #endregion #endregion } else { FlyoutAction action = new FlyoutAction(); action.Caption = "電表資訊-查詢報表錯誤"; action.Description = "請選擇正確條件再進行查詢"; action.Commands.Add(FlyoutCommand.OK); FlyoutDialog.Show(FindForm(), action); } }