首页 > 编程 > .NET > 正文

ASP.NET画柱状图和折线图

2024-07-10 13:07:37
字体:
来源:转载
供稿:网友
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.drawing.imaging;
using system.drawing.drawing2d;

namespace commonu
{
    
/**//// <summary>
    
/// webform2 的摘要说明。
    
/// </summary>

    public class webform2 : system.web.ui.page
    
{
    
        
private void page_load(object sender, system.eventargs e)
        
{

            
/**//*自写编码*/
            
int imgwidth=600;        //1.[总宽度]            ***图宽度   
            int imgheight=300;        //2.[总高度]            ***图高度        
            int itemnum=1;            //3.[项目数量]            ***图表划分的块            
            int childnum=6;            //4.[块数]                ***大块中划分的子项的数量
            float childrate=0.6f;    //5.[各块总占空间比率]
            
//int childspace=15;        //6.[各块间的间距]
            int chartleft=80;        //7.[图表左边距]        ***图表距图的左边距离
            int chartright=50;        //8.[图表右边距]        ***图表距图的右边距离
            int charttop=50;        //9.[图表顶边距]        ***图表距图顶边距离
            int chartbottom=50;        //10.[图表底边距]        ***图表距图底边距离
            int ymaxvalue=5000;        //11.[纵坐标标尺最大值]    ***纵坐标标尺的最大值
            int yitemnum=10;        //12.[纵坐标标尺段数]    ***纵坐标标尺的段数
            int ytop=15;            //13.[距纵轴顶端间隔]
            int ystrstart=35;        //14.[纵坐标标尺文字起始x坐标]
            int xright=15;            //15.[距横轴右端间隔]
            int xstrstart=20;        //16.[横坐标标尺文字起始y坐标]
            

            
//[图表总宽度]=[总宽度]-[图表左边距]-[图表右边距]-[距横轴右端间隔]
            int chartwidth=imgwidth-chartleft-chartright-xright;
            
//[项目宽度]=[图表总宽度]/[项目数量]
            int itemwidth=chartwidth/itemnum;
            
//[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
            int factwidth=convert.toint32(math.floor(itemwidth*childrate));
            
//[各块矩形宽度]=[各块总占空间比率的实际宽度]/[块数]
            int rectanglewidth=factwidth/childnum;
            
//[各块间的间距]=([项目宽度]-[各块总占空间比率的实际宽度])/([块数]+1)
            int childspace=convert.toint32(math.floor((itemwidth-factwidth)/(childnum+1)));

            graphics objgps;
//建立画板对象
            bitmap objbitmap = new bitmap(imgwidth,imgheight);//建立位图对象
            objgps = graphics.fromimage(objbitmap);//根据位图对象建立画板对象
            objgps.clear(color.white);//设置画板对象的背景色



            
int[] arrvalues={0,0,0,0,0,0};//数据数组
            
//arrvalues[0]=500;
            arrvalues[0]=convert.toint32(math.floor((imgheight-chartbottom-charttop-ytop)*500/ymaxvalue));    //处理显示数据,进行图表数值对应
            arrvalues[1]=convert.toint32(math.floor((imgheight-chartbottom-charttop-ytop)*700/ymaxvalue));
            arrvalues[
2]=convert.toint32(math.floor((imgheight-chartbottom-charttop-ytop)*900/ymaxvalue));
            arrvalues[
3]=convert.toint32(math.floor((imgheight-chartbottom-charttop-ytop)*1000/ymaxvalue));
            arrvalues[
4]=convert.toint32(math.floor((imgheight-chartbottom-charttop-ytop)*1400/ymaxvalue));
            arrvalues[
5]=convert.toint32(math.floor((imgheight-chartbottom-charttop-ytop)*2200/ymaxvalue));
            
string[] arrvaluenames={"0","0","0","0","0","0","0","0","0","0"};//月份

            arrvaluenames[
0= "一月";
            arrvaluenames[
1]="二月";
            arrvaluenames[
2= "三月";
            arrvaluenames[
3]="四月";
            arrvaluenames[
4]="五月";
            arrvaluenames[
5]="六月";
            arrvaluenames[
6= "七月";
            arrvaluenames[
7]="八月";
            arrvaluenames[
8]="九月";
            arrvaluenames[
9]="十月";

            
//得出矩形宽度,和画图x轴位置

            
//[项目宽度]=[总宽度]/[项目数量]
            
//======[各块总占空间比率]=([各块矩形宽度]+[各块间的间距])/[项目宽度]
            
//[各块总占空间比率的实际宽度]=[项目宽度]*[各块总占空间比率]
            
//[各块矩形宽度]=([各块总占空间比率的实际宽度]-[各块间的间距]*([块数]))/[块数]
            
//[一边空余空间宽度]=([项目宽度]-[各块所占空间比率的总宽度])/2  

            system.drawing.point[] pi
=new point[arrvalues.length];    //定义折线点的对象数组
            system.drawing.point[] pit=new point[3];    //定义坐标三角点的对象数组
            system.drawing.pen pe=new pen(new solidbrush(getcolor(7)),1f);    //定义画直线的对象
            
//画纵轴
            objgps.drawline(pe,new point(chartleft,imgheight-chartbottom),new point(chartleft,charttop));
            
//画纵轴终点箭头
            pit[0].x=imgwidth-chartright;    //确定三角形三点的位置
            pit[0].y=imgheight-chartbottom-4;
            pit[
1].x=imgwidth-chartright;
            pit[
1].y=imgheight-chartbottom+4;
            pit[
2].x=imgwidth-chartright+10;
            pit[
2].y=imgheight-chartbottom;
            objgps.fillpolygon(
new solidbrush(getcolor(7)),pit);
            
//画纵轴标尺和标尺描述
            for(int i=1;i<=yitemnum;i++)
            
{
                
//画标尺
                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));
                
//画描述
                objgps.drawstring(arrvaluenames[i-1].tostring(),new font("宋体",10),brushes.black,new point(ystrstart,imgheight-chartbottom-(imgheight-chartbottom-charttop-ytop)/yitemnum*i-5));
            }

            
//画横轴
            objgps.drawline(pe,new point(chartleft,imgheight-chartbottom),new point(imgwidth-chartright,imgheight-chartbottom));
            
//画横轴终点箭头
            pit[0].x=chartleft-4;    //确定三角形三点的位置
            pit[0].y=charttop;
            pit[
1].x=chartleft+4;
            pit[
1].y=charttop;
            pit[
2].x=chartleft;
            pit[
2].y=charttop-10;
            objgps.fillpolygon(
new solidbrush(getcolor(7)),pit);
            
//画横轴标尺和标尺描述
            for(int i=1;i<=itemnum;i++)
            
{
                objgps.drawline(pe,
new pointf(chartleft+itemwidth*i,imgheight-chartbottom),new pointf(chartleft+itemwidth*i,imgheight-chartbottom+5));
                objgps.drawstring(arrvaluenames[i
-1].tostring(),new font("宋体",10),brushes.black,new point(chartleft+childspace+itemwidth*(i-1),imgheight-chartbottom+xstrstart));
            }


            
for(int j = 0;j<arrvalues.length;j++)//画矩形图和折线图
            {
                objgps.fillrectangle(
new solidbrush(getcolor(j)),(j*(childspace+rectanglewidth))+childspace+chartleft,imgheight-chartbottom-arrvalues[j],rectanglewidth,arrvalues[j]);
                objgps.drawrectangle(pens.black,(j
*(childspace+rectanglewidth))+childspace+chartleft,imgheight-chartbottom-arrvalues[j],rectanglewidth,arrvalues[j]);
                pi[j].x
=(j*(childspace+rectanglewidth))+childspace+chartleft;
                pi[j].y
=imgheight-chartbottom-arrvalues[j];
                pe.setlinecap(system.drawing.drawing2d.linecap.roundanchor,system.drawing.drawing2d.linecap.roundanchor,system.drawing.drawing2d.dashcap.round);
                
if(j>0)
                
{
                    objgps.drawline(pe,pi[j
-1],pi[j]);
                }

            }


            objbitmap.save(response.outputstream,imageformat.gif);
//该位图对象以"gif"格式输出
        }

/**//// <param name="itemindex">系统定义的颜色,有效值0到7,分别为(blue,yellow,red,orange,purple,brown,pink,black)</param>
/// <returns></returns>

        public static color getcolor(int itemindex)
        
{
            color objcolor 
= new color();
            
switch(itemindex)
            
{
                
case 0:
                    objcolor 
= color.blue;
                    
break;
                
case 1:
                    objcolor 
= color.yellow;
                    
break;
                
case 2:
                    objcolor 
= color.red;
                    
break;
                
case 3:
                    objcolor 
= color.orange;
                    
break;
                
case 4:
                    objcolor 
= color.purple;
                    
break;
                
case 5:
                    objcolor 
= color.brown;
                    
break;
                
case 6:
                    objcolor 
= color.pink;
                    
break;
                
default:
                    objcolor 
= color.black;
                    
break;
            }


            
return objcolor;
        }


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

        public static color getcolor(int red,int green,int blue)
        
{
            color objcolor 
= new color();
            objcolor 
= color.fromargb(red,green,blue);
            
return objcolor;
        }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表