千家信息网

如何使用C#实现运行状态堆叠柱状图

发表于:2024-12-03 作者:千家信息网编辑
千家信息网最后更新 2024年12月03日,小编给大家分享一下如何使用C#实现运行状态堆叠柱状图,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!具体内容如下需求通过柱状
千家信息网最后更新 2024年12月03日如何使用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 ListAllTime=new List();        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();        }        ///         ///  图表绘制        ///         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        }        ///         ///  数据绘制        ///         /// 数据名字        /// 数据        /// chart类型        /// 是否为主轴数据        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++;            }        }        ///         /// 坐标轴设置        ///         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;        }        ///         ///  标签设置        ///         private void LegendSet()        {            Legend legend = this.chart1.Legends.Add("Legend");            legend.Alignment = StringAlignment.Center;              // 标签居中            legend.Docking = Docking.Top;                           //     上方        }        ///         /// 初始化数据列,默认全部停止时间        ///         ///         ///         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("完成");        }    }

显示效果

以上是"如何使用C#实现运行状态堆叠柱状图"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0