动态生成折线图

public   class  PrintTable : System.Web.UI.Page
    {
        
private   void  Page_Load( object  sender, System.EventArgs e)
        {
            
if (Session[ " chuzulv " ] != null )
            {
                
// 数据源
                Table table = (Table)Session[ " Chuzulv " ];

                
// 可修改属性
                 int  Height = 300 ;                                 // 图像高度
                 int  Width = 500 ;                                 // 图像宽度
                 string  TableName = " 物资出租率 " ;                 // 表头
                Font TableHeaderFont = new  Font( " 宋体 " , 12 );     // 表头字体
                 float  High = 100 ;                                 // 默认最大标尺
                 float  Low = 0 ;                                 // 默认最小标尺
                Color BGColor = Color.Black;                     // 背景色
                Color HLineColor = Color.DarkCyan;             // 横向表格线颜色
                Color LineColor = Color.WhiteSmoke;             // 曲线颜色

                
// 临时属性不可修改
                Bitmap bm  =   new  Bitmap(Width,Height);         // 图像对象
                Graphics g  =  Graphics.FromImage(bm);         // 绘图对象
                StringFormat drawFormat  =   new  StringFormat(); // 纵向文字输出格式
                drawFormat.FormatFlags  =  StringFormatFlags.DirectionVertical;

                SolidBrush br
= new  SolidBrush(Color.Red);     // Brush,红色
                Pen p  = new  Pen(Color.Red, 1 );                 // Pen,红色,宽度1
                g.Clear(BGColor);                             // 绘制背景色
                g.DrawString(TableName,TableHeaderFont,br, new  Point( 0 , 0 ));  // 绘制表头
                 int  HStep  =  (Width  -   30 /  (table.Rows.Count  -   1 );                 // 横向步长(每日期)
                 int  VStep;                                     // 纵向步长(每百分点)
                 int  index = 0 ;                                 // 临时标示记录索引
                 foreach (TableRow r  in  table.Rows)
                {
                    index
++ ;
                    
if  (index  ==   1 )                             // 过滤数据源第一行Header
                         continue ;
                    
if  (r.Cells[ 5 ].Text  !=   ""   &&   float .Parse(r.Cells[ 5 ].Text)  >  High)     // 重新获取纵座标上下限
                        High  =   float .Parse(r.Cells[ 5 ].Text);
                    
if  (r.Cells[ 5 ].Text  !=   ""   &&   float .Parse(r.Cells[ 5 ].Text)  <  Low)
                        Low 
=   float .Parse(r.Cells[ 5 ].Text);
                }
                High 
+=  (High  %   10   ==   0 ?   0  :  10   -  High  %   10 ; // 上下限对齐
                Low  -=  (Low  %   10   ==   0 ?   0  :  10   +  Low  %   10 ;
                VStep 
=  (Height  -   50 /  ( int )(High  -  Low);     // 获取纵向步长

                Point lineStartPoint;                        
// 曲线起始点
                Point lineEndPoint;                             // 曲线终止点
                 int  BaseVHeight = 0 ;                             // 0点纵坐标
                 int  Vertical = 30 ;                             // 横向参考线最高点
                 for ( float  HLine  =  High ; HLine  >=  Low ;HLine  -=   10 )         // 绘制横向辅助线
                {
                    g.DrawLine(
new  Pen(Color.DarkCyan, 1 ), new  Point( 20 ,Vertical), new  Point(Width,Vertical));
                    g.DrawString(HLine.ToString()
+ " % " , new  Font( " 宋体 " , 8 ),br, new  Point( 5 ,Vertical  -  VStep ));
                    
if (HLine == 0 )                            
                        BaseVHeight
= Vertical;
                    Vertical 
+=  VStep  *   10 ;
                }
                Point startPoint
= new  Point( 0 ,Height - 50 );
                startPoint.X 
+=   10  ;
                
                lineStartPoint
= new  Point(startPoint.X   +  HStep  /   2 ,BaseVHeight);

                index 
=   0 ;
                
float  PerCent = 0 ;
                
foreach (TableRow r  in  table.Rows)
                {
                    index
++ ;
                    g.DrawString(r.Cells[
0 ].Text, new  Font( " 宋体 " , 8 ),br,startPoint,drawFormat);
                    
if  (index  ==   1 )
                        startPoint.X 
+=   20 ;
                    
else
                        startPoint.X 
+=  HStep;
                    
if  (r.Cells[ 5 ].Text  !=   "" )
                        
if  (index  ==   1 )
                            
continue ;
                        
else  
                            PerCent 
=   float .Parse(r.Cells[ 5 ].Text);
                    
else
                        PerCent 
=   0 ;
                    
if  (index  ==   2 )
                        lineStartPoint
= new  Point(startPoint.X  -  HStep ,BaseVHeight  -  ( int )PerCent  *  VStep);
                        
                    lineEndPoint 
=   new  Point(startPoint.X  -  HStep,BaseVHeight  -  ( int )PerCent  *  VStep);
                    g.DrawLine(
new  Pen(Color.WhiteSmoke, 1 ),lineStartPoint,lineEndPoint);
                    
                    lineStartPoint 
=  lineEndPoint;
                }
                
// 输出
                bm.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
                
// 清除对象
                ((Table)Session[ " chuzulv " ]).Dispose();
                Session[
" chuzulv " ] = null ;
            }
        }
补充一下Session[]==null;无法清除Session对象
还要用Session.Remove(*)来清除

因为报表不需要交互,所以一直用Table,

前一个页统计出Table之后存入Session["chuzulv"],然后置一Image控件,ImageUrl属性指向此页面

效果如图,上传时压缩了品质,动态生成的Gif效果很不错

table.JPG

转载于:https://www.cnblogs.com/pblee/archive/2005/11/15/276549.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值