ASP.NET画柱状图和折线图

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Drawing.Imaging;
None.gif
using  System.Drawing.Drawing2D;
None.gif
None.gif
namespace  commonu
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm2 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm2 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//*自写编码*/
InBlock.gif            
int ImgWidth=600;        //1.[总宽度]            ***图宽度   
InBlock.gif
            int ImgHeight=300;        //2.[总高度]            ***图高度        
InBlock.gif
            int ItemNum=1;            //3.[项目数量]            ***图表划分的块            
InBlock.gif
            int ChildNum=6;            //4.[块数]                ***大块中划分的子项的数量
InBlock.gif
            float ChildRate=0.6f;    //5.[各块总占空间比率]
InBlock.gif            
//int ChildSpace=15;        //6.[各块间的间距]
InBlock.gif
            int ChartLeft=80;        //7.[图表左边距]        ***图表距图的左边距离
InBlock.gif
            int ChartRight=50;        //8.[图表右边距]        ***图表距图的右边距离
InBlock.gif
            int ChartTop=50;        //9.[图表顶边距]        ***图表距图顶边距离
InBlock.gif
            int ChartBottom=50;        //10.[图表底边距]        ***图表距图底边距离
InBlock.gif
            int YMaxValue=5000;        //11.[纵坐标标尺最大值]    ***纵坐标标尺的最大值
InBlock.gif
            int YItemNum=10;        //12.[纵坐标标尺段数]    ***纵坐标标尺的段数
InBlock.gif
            int YTop=15;            //13.[距纵轴顶端间隔]
InBlock.gif
            int YStrStart=35;        //14.[纵坐标标尺文字起始X坐标]
InBlock.gif
            int XRight=15;            //15.[距横轴右端间隔]
InBlock.gif
            int XStrStart=20;        //16.[横坐标标尺文字起始Y坐标]
InBlock.gif            
InBlock.gif
InBlock.gif            
//[图表总宽度]=[总宽度]-[图表左边距]-[图表右边距]-[距横轴右端间隔]
InBlock.gif
            int chartwidth=ImgWidth-ChartLeft-ChartRight-XRight;
InBlock.gif            
//[项目宽度]=[图表总宽度]/[项目数量]
InBlock.gif
            int itemwidth=chartwidth/ItemNum;
InBlock.gif            
//[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
InBlock.gif
            int factwidth=Convert.ToInt32(Math.Floor(itemwidth*ChildRate));
InBlock.gif            
//[各块矩形宽度]=[各块总占空间比率的实际宽度]/[块数]
InBlock.gif
            int rectanglewidth=factwidth/ChildNum;
InBlock.gif            
//[各块间的间距]=([项目宽度]-[各块总占空间比率的实际宽度])/([块数]+1)
InBlock.gif
            int childspace=Convert.ToInt32(Math.Floor((itemwidth-factwidth)/(ChildNum+1)));
InBlock.gif
InBlock.gif            Graphics objGps;
//建立画板对象
InBlock.gif
            Bitmap objBitMap = new Bitmap(ImgWidth,ImgHeight);//建立位图对象
InBlock.gif
            objGps = Graphics.FromImage(objBitMap);//根据位图对象建立画板对象
InBlock.gif
            objGps.Clear(Color.White);//设置画板对象的背景色
InBlock.gif

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
int[] arrValues=dot.gif{0,0,0,0,0,0};//数据数组
InBlock.gif            
//arrValues[0]=500;
InBlock.gif
            arrValues[0]=Convert.ToInt32(Math.Floor((ImgHeight-ChartBottom-ChartTop-YTop)*500/YMaxValue));    //处理显示数据,进行图表数值对应
InBlock.gif
            arrValues[1]=Convert.ToInt32(Math.Floor((ImgHeight-ChartBottom-ChartTop-YTop)*700/YMaxValue));
InBlock.gif            arrValues[
2]=Convert.ToInt32(Math.Floor((ImgHeight-ChartBottom-ChartTop-YTop)*900/YMaxValue));
InBlock.gif            arrValues[
3]=Convert.ToInt32(Math.Floor((ImgHeight-ChartBottom-ChartTop-YTop)*1000/YMaxValue));
InBlock.gif            arrValues[
4]=Convert.ToInt32(Math.Floor((ImgHeight-ChartBottom-ChartTop-YTop)*1400/YMaxValue));
InBlock.gif            arrValues[
5]=Convert.ToInt32(Math.Floor((ImgHeight-ChartBottom-ChartTop-YTop)*2200/YMaxValue));
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] arrValueNames=dot.gif{"0","0","0","0","0","0","0","0","0","0"};//月份
InBlock.gif

InBlock.gif            arrValueNames[
0= "一月";
InBlock.gif            arrValueNames[
1]="二月";
InBlock.gif            arrValueNames[
2= "三月";
InBlock.gif            arrValueNames[
3]="四月";
InBlock.gif            arrValueNames[
4]="五月";
InBlock.gif            arrValueNames[
5]="六月";
InBlock.gif            arrValueNames[
6= "七月";
InBlock.gif            arrValueNames[
7]="八月";
InBlock.gif            arrValueNames[
8]="九月";
InBlock.gif            arrValueNames[
9]="十月";
InBlock.gif
InBlock.gif            
//得出矩形宽度,和画图X轴位置
InBlock.gif
InBlock.gif            
//[项目宽度]=[总宽度]/[项目数量]
InBlock.gif            
//======[各块总占空间比率]=([各块矩形宽度]+[各块间的间距])/[项目宽度]
InBlock.gif            
//[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
InBlock.gif            
//[各块矩形宽度]=([各块总占空间比率的实际宽度]-[各块间的间距]*([块数]))/[块数]
InBlock.gif            
//[一边空余空间宽度]=([项目宽度]-[各块所占空间比率的总宽度])/2  
InBlock.gif

InBlock.gif            System.Drawing.Point[] pi
=new Point[arrValues.Length];    //定义折线点的对象数组
InBlock.gif
            System.Drawing.Point[] pit=new Point[3];    //定义坐标三角点的对象数组
InBlock.gif
            System.Drawing.Pen pe=new Pen(new SolidBrush(GetColor(7)),1f);    //定义画直线的对象
InBlock.gif            
//画纵轴
InBlock.gif
            objGps.DrawLine(pe,new Point(ChartLeft,ImgHeight-ChartBottom),new Point(ChartLeft,ChartTop));
InBlock.gif            
//画纵轴终点箭头
InBlock.gif
            pit[0].X=ImgWidth-ChartRight;    //确定三角形三点的位置
InBlock.gif
            pit[0].Y=ImgHeight-ChartBottom-4;
InBlock.gif            pit[
1].X=ImgWidth-ChartRight;
InBlock.gif            pit[
1].Y=ImgHeight-ChartBottom+4;
InBlock.gif            pit[
2].X=ImgWidth-ChartRight+10;
InBlock.gif            pit[
2].Y=ImgHeight-ChartBottom;
InBlock.gif            objGps.FillPolygon(
new SolidBrush(GetColor(7)),pit);
InBlock.gif            
//画纵轴标尺和标尺描述
InBlock.gif
            for(int i=1;i<=YItemNum;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//画标尺
InBlock.gif
                objGps.DrawLine(pe,new PointF(ChartLeft,ImgHeight-ChartBottom-(ImgHeight-ChartBottom-ChartTop-YTop)/YItemNum*i),new PointF(ChartLeft-5,ImgHeight-ChartBottom-(ImgHeight-ChartBottom-ChartTop-YTop)/YItemNum*i));
InBlock.gif                
//画描述
InBlock.gif
                objGps.DrawString(arrValueNames[i-1].ToString(),new Font("宋体",10),Brushes.Black,new Point(YStrStart,ImgHeight-ChartBottom-(ImgHeight-ChartBottom-ChartTop-YTop)/YItemNum*i-5));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//画横轴
InBlock.gif
            objGps.DrawLine(pe,new Point(ChartLeft,ImgHeight-ChartBottom),new Point(ImgWidth-ChartRight,ImgHeight-ChartBottom));
InBlock.gif            
//画横轴终点箭头
InBlock.gif
            pit[0].X=ChartLeft-4;    //确定三角形三点的位置
InBlock.gif
            pit[0].Y=ChartTop;
InBlock.gif            pit[
1].X=ChartLeft+4;
InBlock.gif            pit[
1].Y=ChartTop;
InBlock.gif            pit[
2].X=ChartLeft;
InBlock.gif            pit[
2].Y=ChartTop-10;
InBlock.gif            objGps.FillPolygon(
new SolidBrush(GetColor(7)),pit);
InBlock.gif            
//画横轴标尺和标尺描述
InBlock.gif
            for(int i=1;i<=ItemNum;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                objGps.DrawLine(pe,
new PointF(ChartLeft+itemwidth*i,ImgHeight-ChartBottom),new PointF(ChartLeft+itemwidth*i,ImgHeight-ChartBottom+5));
InBlock.gif                objGps.DrawString(arrValueNames[i
-1].ToString(),new Font("宋体",10),Brushes.Black,new Point(ChartLeft+childspace+itemwidth*(i-1),ImgHeight-ChartBottom+XStrStart));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
for(int j = 0;j<arrValues.Length;j++)//画矩形图和折线图
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                objGps.FillRectangle(
new SolidBrush(GetColor(j)),(j*(childspace+rectanglewidth))+childspace+ChartLeft,ImgHeight-ChartBottom-arrValues[j],rectanglewidth,arrValues[j]);
InBlock.gif                objGps.DrawRectangle(Pens.Black,(j
*(childspace+rectanglewidth))+childspace+ChartLeft,ImgHeight-ChartBottom-arrValues[j],rectanglewidth,arrValues[j]);
InBlock.gif                pi[j].X
=(j*(childspace+rectanglewidth))+childspace+ChartLeft;
InBlock.gif                pi[j].Y
=ImgHeight-ChartBottom-arrValues[j];
InBlock.gif                pe.SetLineCap(System.Drawing.Drawing2D.LineCap.RoundAnchor,System.Drawing.Drawing2D.LineCap.RoundAnchor,System.Drawing.Drawing2D.DashCap.Round);
InBlock.gif                
if(j>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    objGps.DrawLine(pe,pi[j
-1],pi[j]);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            objBitMap.Save(Response.OutputStream,ImageFormat.Gif);
//该位图对象以"GIF"格式输出
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <param name="itemIndex">系统定义的颜色,有效值0到7,分别为(Blue,Yellow,Red,Orange,Purple,Brown,Pink,Black)</param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gif        public static Color GetColor(int itemIndex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Color objColor 
= new Color();
InBlock.gif            
switch(itemIndex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case 0:
InBlock.gif                    objColor 
= Color.Blue;
InBlock.gif                    
break;
InBlock.gif                
case 1:
InBlock.gif                    objColor 
= Color.Yellow;
InBlock.gif                    
break;
InBlock.gif                
case 2:
InBlock.gif                    objColor 
= Color.Red;
InBlock.gif                    
break;
InBlock.gif                
case 3:
InBlock.gif                    objColor 
= Color.Orange;
InBlock.gif                    
break;
InBlock.gif                
case 4:
InBlock.gif                    objColor 
= Color.Purple;
InBlock.gif                    
break;
InBlock.gif                
case 5:
InBlock.gif                    objColor 
= Color.Brown;
InBlock.gif                    
break;
InBlock.gif                
case 6:
InBlock.gif                    objColor 
= Color.Pink;
InBlock.gif                    
break;
InBlock.gif                
default:
InBlock.gif                    objColor 
= Color.Black;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return objColor;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <param name="red">自定义颜色红色分量值,有效值0到255</param>
InBlock.gif
/// <param name="green">自定义颜色绿色分量值,有效值0到255</param>
InBlock.gif
/// <param name="blue">自定义颜色蓝色分量值,有效值0到255</param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gif        public static Color GetColor(int red,int green,int blue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Color objColor 
= new Color();
InBlock.gif            objColor 
= Color.FromArgb(red,green,blue);
InBlock.gif            
return objColor;
ExpandedSubBlockEnd.gif        }

InBlock.gif

我做的时候充分把能用变量的地方尽量有变量,
方便有兴趣的朋友把次改为组件,方便以后应用,我还会对此进行更新,做成一个组件。
下面是运行后的图例:
%E5%9B%BE%E4%BE%8B.gif

转载于:https://www.cnblogs.com/hanguoji/archive/2005/11/16/277444.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值