C#實現運行狀態堆疊柱狀圖
本文實例為大傢分享瞭C#實現運行狀態堆疊柱狀圖的具體代碼,供大傢參考,具體內容如下
需求通過柱狀圖顯示設備運行時間停止時間,稼動率等
通過數據庫查詢記錄,按小時顯示顯示目標數據。
界面設計
添加用戶控件(窗體)
代碼如下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace TestRunChart { public partial class SysTimeBarChart : UserControl { public List<SysTimeCnt> ListAllTime=new List<SysTimeCnt>(); public SysTimeBarChart() { InitializeComponent(); this.chart1.Series.Clear(); } public DateTime timeStart { get{ DateTime Time1 = this.dateTimePickerLast.Value; DateTime Time2 = this.dateTimePickerStart.Value; return Time1 >= Time2? Time2 : Time1; } } public DateTime timeEnd { get { DateTime Time1 = this.dateTimePickerLast.Value; DateTime Time2 = this.dateTimePickerStart.Value; return Time1 > Time2 ? Time1 : Time2; } } public int Hours { get { TimeSpan ts1 = new TimeSpan(timeStart.Ticks); TimeSpan ts2 = new TimeSpan(timeEnd.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); return ts.Hours; } } public int Days { get { TimeSpan ts1 = new TimeSpan(timeStart.Ticks); TimeSpan ts2 = new TimeSpan(timeEnd.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); return ts.Days; } } private void btn_GetChart_Click(object sender, EventArgs e) { // 判斷時間設置 if (Hours == 0) MessageBox.Show("時間設置間隔小於1小時,請重新設置時間", "警告", MessageBoxButtons.OK); else if(Days != 0) MessageBox.Show("時間間隔大於24小時,請重新設置時間", "警告", MessageBoxButtons.OK); else Plot(); } private void btn_clear_Click(object sender, EventArgs e) { this.chart1.Series.Clear(); this.chart1.Legends.Clear(); this.chart1.ChartAreas.Clear(); ListAllTime.Clear(); } /// <summary> /// 圖表繪制 /// </summary> private void Plot() { this.chart1.Series.Clear(); // 清空圖表 this.chart1.Legends.Clear(); this.chart1.ChartAreas.Clear(); if (ListAllTime == null || ListAllTime.Count == 0) { MessageBox.Show("數據為空"); return; } // 輔助設置 AxiesSet(); LegendSet(); // 表格繪制 if (this.rbt_runTime.Checked) ChartPlot(TimeType.RunTime); else if (this.rbt_StopTime.Checked) ChartPlot(TimeType.StopTime); else if (this.rbt_almtime.Checked) ChartPlot(TimeType.AlmTime); else if (this.rbt_runrate.Checked) ChartPlot(TimeType.RunRate); else { ChartPlot(TimeType.RunTime); ChartPlot(TimeType.StopTime); ChartPlot(TimeType.AlmTime); ChartPlot(TimeType.RunRate); } } enum TimeType { RunTime, AlmTime, StopTime, RunRate } /// <summary> /// 數據繪制 /// </summary> /// <param name="seriesName">數據名字</param> /// <param name="data">數據</param> /// <param name="chartType">chart類型</param> /// <param name="isPrimary">是否為主軸數據</param> private void ChartPlot(TimeType type) { string seriesName = ""; SeriesChartType chartType = SeriesChartType.StackedColumn; bool isPrimary = true; switch (type) { case TimeType.RunTime: seriesName = "運行時間"; break; case TimeType.StopTime: seriesName = "待機時間"; break; case TimeType.AlmTime: seriesName = "報警時間"; break; case TimeType.RunRate: seriesName = "稼動率"; chartType = SeriesChartType.Line; isPrimary = false;//非主軸,右邊軸 break; default: break; } Series series = this.chart1.Series.Add(seriesName); series.ChartType = chartType; // 圖表類型 series.YAxisType = isPrimary ? AxisType.Primary : AxisType.Secondary; series.BorderWidth = 2; series.Label = isPrimary ? "#VAL" : "#VAL{P}"; int i = 0; foreach (var m in ListAllTime) { double mvalue = 0; switch (type) { case TimeType.RunTime: mvalue = m.RunTime; break; case TimeType.StopTime: mvalue = m.StopTime; break; case TimeType.AlmTime: mvalue = m.AlmTime; break; case TimeType.RunRate: mvalue = m.RunRate; break; default: break; } series.Points.AddXY(i, mvalue); i++; } } /// <summary> /// 坐標軸設置 /// </summary> private void AxiesSet() { ChartArea chartAreas = this.chart1.ChartAreas.Add("ChartAreas"); chartAreas.AxisX.MajorGrid.Enabled = false; // 坐標軸 chartAreas.AxisY.MajorGrid.Enabled = false; // Y軸主軸 chartAreas.AxisY.Maximum = 60; chartAreas.AxisY2.MajorGrid.Enabled = false; // Y軸次軸 chartAreas.AxisY2.Enabled = AxisEnabled.True; chartAreas.AxisY2.LabelStyle.Format = "0%"; chartAreas.AxisY2.Maximum = 1; } /// <summary> /// 標簽設置 /// </summary> private void LegendSet() { Legend legend = this.chart1.Legends.Add("Legend"); legend.Alignment = StringAlignment.Center; // 標簽居中 legend.Docking = Docking.Top; // 上方 } /// <summary> /// 初始化數據列,默認全部停止時間 /// </summary> /// <param name="brandom"></param> /// <returns></returns> public bool InitDataList(bool brandom=false) { if (Days != 0) { MessageBox.Show("時間設置異常"); return false; } var startTime = timeStart; ListAllTime.Clear(); Random rd = new Random(); for (int i = 0; i <= Hours; i++) { var num = rd.Next(0, 59); SysTimeCnt mCnt = new SysTimeCnt() { _InSertTime = string.Format("{0:yyyy-MM-dd-HH-mm}", startTime), RunTime = brandom? num:0, AlmTime = brandom ? rd.Next(0, 60 - num):0 }; ListAllTime.Add(mCnt); startTime.AddHours(1); } return true; } } public class SysTimeCnt { public string _InSertTime; public string InSertTime => _InSertTime; public void UpdateInSertTime() { _InSertTime = string.Format("{0:yyyy-MM-dd-HH-mm}", DateTime.Now); } public double RunTime; public double AlmTime; public double StopTime { get { return 60 - RunTime - AlmTime; } } public double RunRate { get { return Math.Round(Convert.ToDouble(this.RunTime / 60), 2); } } } }
主窗體代碼
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn_AddData_Click(object sender, EventArgs e) { var bSucessd = sysTimeBarChart1.InitDataList(true); if (bSucessd) MessageBox.Show("完成"); } }
顯示效果
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。