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;
}新聞熱點
疑難解答
圖片精選