前段时间项目需要,写了一个简单的图标统计类.决定以后写点东西到blog来.
using system;
using system.drawing;
using system.drawing.imaging;
using system.drawing.drawing2d;
namespace crystalreportsdemo
...{
/**//// <summary>
/// createreport绘制图形报表-柱图。
/// </summary>
public class createreport
...{
public createreport()
...{
//
// todo: 在此处添加构造函数逻辑
//
}
public bitmap createpicturereport(string[] x,int[] y,string reportname)
...{
if(x.length != y.length)
...{
throw new exception("x和y数目不一致");
}
int colnum=y.length;
int colscale=880/(colnum+1);
bitmap bmp = new bitmap(900,700,pixelformat.format24bpprgb);
graphics dc = graphics.fromimage(bmp);
dc.fillrectangle(new solidbrush(color.white),0,0,900,700);
pen scalepen = new pen(color.black,2);
pen gridpen = new pen(color.black,1);
dc.drawline(scalepen,20,680,880,680);
dc.drawline(scalepen,20,680,20,20);
dc.drawline(gridpen,20,20,880,20);
dc.drawline(gridpen,880,20,880,680);
point drawpoint;
int xpoint;
int yheight;
if(colnum==0)
...{
throw new exception("无数据值,colnum值为0作为除数,抛出异常");
}
int drawwidth=880/(4*colnum);
//int drawwidth=580/(4*colnum+1);//4*colnum+1 确保colnum不为0.否则580/(4*0);的话会异常.0不能作为除数
int maxnum=getmaxnum(y);
rectangle rct;
random rnd = new random();
int red,green,blue;
for(int i=20;i<680;i=i+96)
...{
dc.drawline(gridpen,20,i,880,i);
}
for(int i=0;i<colnum;i++)
...{
red=rnd.next(0,255);
green=rnd.next(0,255);
blue=rnd.next(0,255);
solidbrush brush=new solidbrush(color.fromargb(red,green,blue));
xpoint=20+((i+1)*colscale)-drawwidth;
yheight=y[i]*650/maxnum;
drawpoint=new point(xpoint,680-yheight);
rct=new rectangle(drawpoint,new size(drawwidth*2,yheight));
dc.fillrectangle(brush,rct);
dc.drawrectangle(gridpen,rct);
//绘制x数值
dc.drawstring(x[i],new font("宋体",9),new solidbrush(color.orangered),new pointf((float)(xpoint+drawwidth/2),680-yheight/2));
//绘制y数值
dc.drawstring(y[i].tostring(), new font("宋体", 9), new solidbrush(color.black), new pointf((float)(xpoint + drawwidth / 2), 680 - yheight-15));
}
dc.drawstring(reportname,new font("宋体",10),brushes.black,3,3);
return bmp;
}
private int getmaxnum(int[] arr)
...{
int max=arr[0];
for(int i=1;i<arr.length;i++)
...{
if(arr[i]>max)
...{
max=arr[i];
}
}
return max;
}
}
}
新闻热点
疑难解答
图片精选