柱状图改进版


public void BindBitmap()
        
{
            
//创建一个画布
            Bitmap bm=new Bitmap(350,355);
            
//在新建的画布上画一个图
            Graphics bp=Graphics.FromImage(bm);
            
//设置bp的背景色为White
            bp.Clear(Color.White);
            
//创建数据源,在此为一个数组,这里可以从数据集(DataSet)中获取数据.
            int [] a1={20,40,60,80,100,120,140,160};
            
int [] a2={40,60,80,100,120,140,160,180};
            
            
//画一个矩形,将柱形图圈起来
            bp.FillRectangle(new SolidBrush(Color.Beige),1,1,255,205);
            bp.DrawRectangle(Pens.Black,
1,1,255,205);
            
//定义一种样式
            StringFormat fontStyle=new System.Drawing.StringFormat(StringFormatFlags.DisplayFormatControl);
            
//通过循环画出柱状图
            
//柱图1    --Blue        
            for(int i=0;i<a1.Length;i++)
            
{
                
//填充图(着色,起点X,起点Y,宽,高)
                bp.FillRectangle(new SolidBrush(Color.Blue),(i*30)+25,200-a1[i],5,a1[i]+5);
                
//填充边框(着色,起点X,起点Y,宽,高)
                bp.DrawRectangle(Pens.Black,(i*30)+25,200-a1[i],5,a1[i]+5);
                
//输出月份    
                bp.DrawString((i+1).ToString()+"",new Font("宋体"10), Brushes.Black ,(i*30)+18,210,fontStyle);             
            }

            
//柱图2--Red
            for(int i=0;i<a2.Length;i++)
            
{
                bp.FillRectangle(
new SolidBrush(Color.Red),(i*30)+30,200-a2[i],5,a2[i]+5);
                bp.DrawRectangle(Pens.Black,(i
*30)+30,200-a2[i],5,a2[i]+5);
            }

            
            
//画两个例子图
            
//旁边的"产品A"
            bp.FillRectangle(new SolidBrush(Color.Blue),280,10,20,10);
            bp.DrawRectangle(Pens.Black,
280,10,20,10);
            bp.DrawString(
"产品A",new Font("宋体"10), Brushes.Black ,305,10,fontStyle); 
            
//旁边的"产品B"
            bp.FillRectangle(new SolidBrush(Color.Red),280,30,20,10);
            bp.DrawRectangle(Pens.Black,
280,30,20,10);
            bp.DrawString(
"产品B",new Font("宋体"10), Brushes.Black ,305,30,fontStyle); 

            bp.DrawString(
"臭虫公司销虫量",new Font("宋体"12), Brushes.Black ,45,230,fontStyle); 
            
//以gif图片格式把图输出到IE上
            bm.Save(Response.OutputStream,ImageFormat.Gif);
        }

posted on 2004-09-14 09:50  CoolBug  阅读(2964)  评论(3编辑  收藏  举报