动态生成折线图

来源:互联网 发布:淘宝贴牌服装怎么操作 编辑:程序博客网 时间:2024/04/28 23:02

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效果很不错

  

前一页面Image控件的ImageUrl="PrintTable.aspx"

源码就是这样了,主要流程就是-->获取数据-->根据数据生成坐标轴-->画线

因为这个东西在项目里面一带而过,所以没有把它的功能扩展封装。
起个头,有兴趣自己扩展吧。
 
原创粉丝点击