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();
}
{
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);