g.FillRectangle(newSolidBrush(ChartUtil.GetChartItemColor(i)),barOrigin.X,barOrigin.Y,barWidth,barHeight);
g.DrawLine(newPen(Color.Black,2),newPoint(CHART_LEFT,CHART_TOP),newPoint(CHART_LEFT,CHART_TOP+CHART_HEIGHT));
g.DrawLine(newPen(Color.Black,2),newPoint(CHART_LEFT,CHART_TOP+CHART_HEIGHT),newPoint(CHART_LEFT+CHART_WIDTH,CHART_TOP+CHART_HEIGHT));
g.DrawString("图表说明",newFont("Tahoma",12,FontStyle.Bold),Brushes.Black,newPointF(200,300));
{
g.FillRectangle(newSolidBrush(ChartUtil.GetChartItemColor(i)),boxOrigin.X,boxOrigin.Y,20,10);
g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10);
g.DrawString(dt.Rows[i][1].ToString()+"-"+dt.Rows[i][0].ToString(),newFont("Tahoma",10),Brushes.Black,textOrigin);
boxOrigin.Y+=15;
textOrigin.Y+=15;
}
//输出图形
g.Dispose();
returnbm;
}
catch
{
returnbm;
}
}
}
publicclassChartUtil
{
publicChartUtil()
{
}
publicstaticColorGetChartItemColor(intitemIndex)
{
ColorselectedColor;
switch(itemIndex)
{
case0:
selectedColor=Color.Blue;
break;
case1:
selectedColor=Color.Red;
break;
case2:
selectedColor=Color.Yellow;
break;
case3:
selectedColor=Color.Purple;
break;
default:
selectedColor=Color.Green;
break;
}
returnselectedColor;
}
}
}
以上是一个完整的WinForm中制作饼状图和柱状图源程序,大家可以通过以上程序的更改,做出能满足自己程序的需要。
画饼状图的一个简单例子(2008-05-2813:
41:
30)
标签:
杂谈
分类:
C#
privatevoidCreateImage()
{
//获取男员工数目
intman=40;
//获取女员工数目
intwoman=60;
intSum=man+woman;
//创建画图对象
intwidth=400,height=450;
Bitmapbitmap=newBitmap(width,height);
Graphicsg=Graphics.FromImage(bitmap);
try
{
//清空背景色
g.Clear(Color.White);
Penpen1=newPen(Color.Red);
Brushbrush1=newSolidBrush(Color.YellowGreen);
Brushbrush2=newSolidBrush(Color.Blue);
Brushbrush3=newSolidBrush(Color.Brown);
Fontfont1=newFont("CourierNew",16,FontStyle.Bold);
Fontfont2=newFont("CourierNew",8);
g.FillRectangle(brush1,0,0,width,height); //绘制背景图
g.DrawString("公司男女比例饼形图",font1,brush2,newPoint(80,20)); //书写标题
intpiex=100,piey=100,piew=150,pieh=150;
//男员工在圆中分配的角度
floatangle1=Convert.ToSingle((360/Convert.ToSingle(Sum))*Convert.ToSingle(man));
//女员工在圆中分配的角度
floatangle2=Convert.ToSingle((360/Convert.ToSingle(Sum))*Convert.ToSingle(woman));
g.FillPie(brush2,piex,piey,piew,pieh,0,angle1); //绘制男员工所占比例
g.FillPie(brush3,piex,piey,piew,pieh,angle1,angle2); //绘制女员工所占比例
//绘制标识
g.DrawRectangle(pen1,50,300,300,100); //绘制范围框
g.FillRectangle(brush2,90,320,20,10); //绘制小矩形
g.DrawString("男员工占公司总人数比例:
"+Convert.ToSingle(man)*100/Convert.ToSingle(Sum)+"%",font2,brush2,120,320);
g.FillRectangle(brush3,90,360,20,10);
g.DrawString("女员工占公司总人数比例:
"+Convert.ToSingle(woman)*100/Convert.ToSingle(Sum)+"%",font2,brush3,120,360);
}
catch(Exceptionmd)
{
Response.Write(md.Message);
}
System.IO.MemoryStreamms=newSystem.IO.MemoryStream();
bitmap.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType="image/Gif";
Response.BinaryWrite(ms.ToArray());
}
使用ASP.NET画饼状图
2007年07月14日星期六下午11:
30
使用ASP.NET画饼状图
<%@Page ContentType = "image/gif"%>
<%@Import Namespace = "System.Drawing" %>
<%@Import Namespace = "System.Drawing.Imaging" %>
<%@ImportNamespace="System.Drawing.Text"%>
无标题页