C#绘制柱状图

本文介绍了一种使用C#绘制包含两个数据系列的定制化柱状图的方法。该方法能够根据输入的数据调整柱状图的高度,并在图表上添加必要的标签和注释,确保图表的清晰易读。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public Image GetBarChart(string title,int width,int height,string ToolNo,int RateLife,int RealLife)    //title标题, width绘图区宽 ,height绘图区长, ToolNo柱状图横轴意义,RateLife柱状图1高, RealLife柱状图2高
        {
            Bitmap bm = new Bitmap(width, height);  //设置绘图区域
            Graphics g = Graphics.FromImage(bm);
            g.Clear(Color.White);
            const int top  = 30;                    //设置纵轴最上方点的位置
            const int left = 35;
            if (width < left * 2 || height < top * 2)  //判断绘图区域大小
            {
               g.DrawString("绘图区域太小", new Font("Tahoma", 8),Brushes.Blue, new PointF(0, 0));
               return bm;
            }
            try
            {
                //绘制图的标题
                g.DrawString(title, new Font("Tahoma", 12), Brushes.Black, new PointF(-4, 2));
                //绘制小矩形及注释
                Font font = new System.Drawing.Font("Arial", 10, FontStyle.Regular); //设置注释样式
                g.FillRectangle(Brushes.Red, 270, 20, 20, 10);                       //绘制小矩形1
                g.DrawString("额定生产时间", font, Brushes.Red, 292, 18);            //小矩形的注释1
                g.FillRectangle(Brushes.Blue,270, 40, 20, 10);                       //绘制小矩形2
                g.DrawString("实际生产时间", font, Brushes.Blue, 292, 38);           //小矩形的注释2
                //定义柱状图的宽度及高度 
                float barWidth = (width - left) / (1 * 3 + 1);                
                PointF  barOrigin = new PointF(left + barWidth, 0);
                float   barHeight = 0;
                //顶部字体大小设置
                float topFontSize = (barWidth / RateLife.ToString().Length); 
                if (topFontSize > 2 * top / 3)
                {
                   topFontSize = 2 * top / 3;
                }
                if (topFontSize < 5)
                {
                   topFontSize = 5;
                }
                //底部字体大小设置
                float bottomFontSize = (2 * barWidth / ToolNo.Length) + 2;  
                if (bottomFontSize > 2 * top / 3)
                {
                  bottomFontSize = 2 * top / 3;
                }
                //画柱状图1
                barHeight = Convert.ToSingle(RateLife) * (height - 2 * top) / RateLife * 1;
                barOrigin.Y = height - barHeight - top;
                g.FillRectangle(new SolidBrush(Color.Red), barOrigin.X, barOrigin.Y, barWidth, barHeight);
                //柱状图1顶部加注释
                g.DrawString(RateLife.ToString(), new Font("Tahoma", topFontSize), Brushes.Red, new PointF(barOrigin.X+10 , barOrigin.Y - 3 * topFontSize / 2));
                //画柱状图2
                barOrigin.X = barOrigin.X + barWidth;
                barHeight = Convert.ToSingle(RealLife) * (height - 2 * top) / RateLife * 1;
                barOrigin.Y = height - barHeight - top;
                g.FillRectangle(new SolidBrush(Color.Blue), barOrigin.X, barOrigin.Y, barWidth, barHeight);
                //柱状图2顶部加注释
                g.DrawString(RealLife.ToString(), new Font("Tahoma", topFontSize), Brushes.Blue, new PointF(barOrigin.X + 10, barOrigin.Y - 3 * topFontSize / 2));
                //柱状图底部加注释
                g.DrawString(ToolNo, new Font("Tahoma", bottomFontSize), Brushes.Black, new PointF(barOrigin.X-80,height - top));
                //画XY轴线
                g.DrawLine(new Pen(Color.Blue, 2), new Point(left, top), new Point(left, height - top));
                g.DrawLine(new Pen(Color.Blue, 2), new Point(left, height - top), new Point(left + width, height - top));
                //给XY轴线加注释
                Pen pen = new Pen(Color.Black, 1);
                g.DrawString("轧辊编号", new Font("宋体 ", 10f), Brushes.Black, new Point(width-left-25, height - top+2));
                g.DrawString("时间(小时)", new Font("宋体 ", 10f), Brushes.Black, new Point(left, top));
                //返回所绘图形
                return bm;
            }
            catch
            {
                return bm;
            }
            finally
            {
                g.Dispose();
            }

        }
 DrawPic dp = new DrawPic();
 pictureBox1.Image = dp.GetBarChart("轧辊生产时间统计", 452, 268,  cmbZGNo.Text.Trim(), ZGRateTime, ZGRealTime);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮特大熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值