(1)熟悉一下画柱状图和饼图类

      用.net画柱状图和饼图是比较简单的,下面我们来熟悉System.Drawing和System.Drawing.Imaging这两个类。通过下面两个小例子,熟悉一下,这两个类的应用。
1、例子一代码如下:
   protected   void  Page_Load( object  sender, EventArgs e)
        
{
            
//添加一个画布
            Bitmap bm = new Bitmap(378461);
            Graphics g;
            g 
= Graphics.FromImage(bm);
            g.Clear(Color.White);

            
//画标题
            g.DrawString("访问者地区(今日详情)"new Font("宋体"9), Brushes.Black, new Point(55));

            
//画列表项
            g.DrawString("共 57 项  访问量  比列"new Font("宋体"9), Brushes.Black, new Point(11030));


            Color link 
= Color.FromArgb(114125145);//标准线颜色
            g.DrawLine(new Pen(link), new Point(10348), new Point(25048));

            g.DrawString(
"广东     357    14.5%"new Font("宋体"9), Brushes.Black, new Point(11560));
            g.DrawString(
"福建     300      10%"new Font("宋体"9), Brushes.Black, new Point(11575));
            g.DrawString(
"上海     250      17%"new Font("宋体"9), Brushes.Black, new Point(11590));
            g.DrawString(
"福建     300      10%"new Font("宋体"9), Brushes.Black, new Point(115105));
            g.DrawString(
"上海     250      17%"new Font("宋体"9), Brushes.Black, new Point(115120));
            g.DrawString(
"福建     300      10%"new Font("宋体"9), Brushes.Black, new Point(115135));
            g.DrawString(
"上海     250      17%"new Font("宋体"9), Brushes.Black, new Point(115150));
            g.DrawString(
"福建     300      10%"new Font("宋体"9), Brushes.Black, new Point(115165));
            g.DrawString(
"福建     300      10%"new Font("宋体"9), Brushes.Black, new Point(115180));
            g.DrawString(
"上海     250      17%"new Font("宋体"9), Brushes.Black, new Point(115195));
            g.DrawString(
"福建     250      17%"new Font("宋体"9), Brushes.Black, new Point(115210));
            g.DrawString(
"上海     250      17%"new Font("宋体"9), Brushes.Black, new Point(115225));
            g.DrawString(
"福建     250      17%"new Font("宋体"9), Brushes.Black, new Point(115240));
            g.DrawString(
"福建     250      17%"new Font("宋体"9), Brushes.Black, new Point(115255));
            g.DrawString(
"上海      25      17%"new Font("宋体"9), Brushes.Black, new Point(115270));
            g.DrawString(
"福建      25     1.1%"new Font("宋体"9), Brushes.Black, new Point(115285));

            
//设置边框的颜色
            Color bzlink = Color.FromArgb(185185185);//标准线颜色

            
//矩形上边
            g.DrawLine(new Pen(bzlink), new Point(26050), new Point(34050));

            
//矩形左边
            g.DrawLine(new Pen(bzlink), new Point(26050), new Point(260300));

            
//矩形右边
            g.DrawLine(new Pen(bzlink), new Point(34050), new Point(340300));



            
//填充背景的颜色
            SolidBrush bBrush = new SolidBrush(Color.FromArgb(243,243,243));
            
//SolidBrush greebrush=new SolidBrush(Color.FromName("red"));

            
//填充第一个色块
            g.FillRectangle(bBrush, 2815119249);
        
            
//真充第二个色块
            g.FillRectangle(bBrush, 3215118249);


            
//画棒图
            SolidBrush barBrush = new SolidBrush(Color.FromArgb(957121));

            g.FillRectangle(barBrush, 
26161797);
            g.FillRectangle(barBrush, 
26176707);
            g.FillRectangle(barBrush, 
26191557);
            g.FillRectangle(barBrush, 
261106207);
            g.FillRectangle(barBrush, 
261121157);
            g.FillRectangle(barBrush, 
261136137);
            g.FillRectangle(barBrush, 
261151107);
            g.FillRectangle(barBrush, 
261166107);
            g.FillRectangle(barBrush, 
26118197);
            g.FillRectangle(barBrush, 
26119697);
            g.FillRectangle(barBrush, 
26121197);
            g.FillRectangle(barBrush, 
26122697);
            g.FillRectangle(barBrush, 
26124187);
            g.FillRectangle(barBrush, 
26125687);
            g.FillRectangle(barBrush, 
26127187);
            g.FillRectangle(barBrush, 
26128687);




            bm.Save(Response.OutputStream, ImageFormat.Gif);


           
        }
运行上面程序,显示结果如下:
       (1)熟悉一下画柱状图和饼图类
注意:Color.FromArgb()方法可以自定义颜色。也可以Color的属性获取FCL自带的颜色。 
 2、例子源码如下:
       // 添加一个画布
            Bitmap bm  =   new  Bitmap( 505 172 );
            Graphics g;
            g 
=  Graphics.FromImage(bm);
            g.Clear(Color.White);

            
// 画标题
            g.DrawString( " 流量信息(今日详情) " new  Font( " 宋体 " 9 ), Brushes.Black,  new  Point( 5 5 ));


            
// 画边框
            Color bzlink  =  Color.FromArgb( 185 185 185 ); // 标准线颜色
            Pen p  =   new  Pen( new  SolidBrush(bzlink));
            g.DrawRectangle( p, 
90 30 380 100 );


            g.DrawRectangle(p, 
150 155 10 10 ); // 单位(订阅)
            g.DrawRectangle(p,  270 155 10 10 ); // 单位(下载)

            g.DrawString(
"    0 " new  Font( " 宋体 " 9 ), Brushes.Black,  new  Point( 62 128 ));
            g.DrawString(
"  700 " new  Font( " 宋体 " 9 ), Brushes.Black,  new  Point( 62 103 ));
            g.DrawString(
" 1400 " new  Font( " 宋体 " 9 ), Brushes.Black,  new  Point( 62 78 ));
            g.DrawString(
" 2100 " new  Font( " 宋体 " 9 ), Brushes.Black,  new  Point( 62 53 ));
            g.DrawString(
" 2800 " new  Font( " 宋体 " 9 ), Brushes.Black,  new  Point( 62 28 ));


            
// 填充背景的颜色
            SolidBrush bBrush  =   new  SolidBrush(Color.FromArgb( 243 243 243 ));
            
// SolidBrush greebrush=new SolidBrush(Color.FromName("red"));

            
// 填充第一个色块
            g.FillRectangle(bBrush,  91 31 378 24 );

            
// 真充第二个色块
            g.FillRectangle(bBrush,  91 80 378 25 );


            
// 画棒图
            SolidBrush barBrushR  =   new  SolidBrush(Color.FromArgb( 9 57 121 ));
            SolidBrush barBrushD 
=   new  SolidBrush(Color.FromArgb( 182 202 249 ));


            g.FillRectangle(barBrushR, 
150 155 10 10 ); // 单位(订阅)
            g.DrawString( " 订阅量  2578 " new  Font( " 宋体 " 9 ), Brushes.Black,  new  Point( 165 155 ));

            g.FillRectangle(barBrushD, 
271 156 9 9 ); // 单位(订阅)
            g.DrawString( " 下载量  256871 " new  Font( " 宋体 " 9 ), Brushes.Black,  new  Point( 285 155 ));




            
棒图

            g.DrawString(
" 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 " new  Font( " 宋体 " 9 ), Brushes.Black,  new  Point( 100 135 ));

            bm.Save(Response.OutputStream, ImageFormat.Gif);
运行这段代码显示如下:
       (1)熟悉一下画柱状图和饼图类
源代码: 下载地址
通过这两个小程序,可以熟悉System.Drawing和System.Drawing.Imaging这两个类。接下来的教程会和数据库查询结合在一起。

你可能感兴趣的:(柱状图)