代碼下載
介紹:
GraphsAndCharts 是一個完全動態圖形和表格產生器。圖表的產生不需要任何代碼,只需要在一個<IMG>的SRC屬性。圖表可以在空閑時被重新整理並不需要上傳網頁。只需要添加代碼到程式中,圖表就可以產生。不需要自己寫任何代碼。
注意:這是開原始碼,可以為圖表對象增加更多的功能。
該代碼可以被用來建立:
各種風格的圖表
表格:餅型,條形等
圖形物體如線,橢圓,矩形,圓等。
GraphsAndCharts的功能:
GraphsAndCharts的目的是迅速建立2D表格和圖形而不使用代碼。
使用GraphsAndCharts:
資料的可視話
描述待用資料
描述精確資料
報告
使用代碼:
解壓後用ASP.NET 2.0開啟代碼,是一個網頁程式。運行 default.htm。
怎樣工作:
一個<IMG>語句的秘密是它可以定位非圖片。
在.NET中,一個用.ashx做副檔名的頁面用來建立圖片(這裡,是DrawIt.ashx)。
在.ashx 頁面中,產生圖片的資訊被翻譯。
.ashx調用DrawAll類來建立位元影像。
記憶體中的位元影像作為一個二進位流輸出到<IMG>。
例如用<IMG>建立一個餅型表:
<IMG SRC=ashx/DrawIt.ashx?width=250&height=250&xaxis=pushes
&yaxis=pulls&background=powderblue&piechart=20,30,10,10,5;
&titles=kings,queens,rooks,pawns,knights,castles;&>
該例子展示了DrawIt.ashx用到的參數:
width=250
height=250
xaxis=pushes
yaxis=pulls
background=powderblue
piechart=20,30,10,10,5;
titles=kings,queens,rooks,pawns,knights,castles;
解釋:
<IMG>調用DrawIt.ashx。
DrawIt.ashx讀取相關參數並翻譯。
通過這些參數,DrawIt.ashx調用DrawAll類來建立繪圖物件。
DrawAll.cs中的函數建立相關的繪圖物件並在記憶體中返回位元影像。
DrawIt.ashx輸出二進位流到<IMG>
程式碼片段:
以下是DrawAll類中的drawBarChart函數:
Collapsepublic Bitmap drawBarChart (Bitmap aoBmp, Rectangle aoRect, int aintWidth,
int aintHeight, string astrBackground, string astrCaption,
string astrXaxis, string astrYaxis, int[] aintV, string[] astrTitles)
{
Single[] sngY = new Single[aintV.Length];
float flTotal;
Single sngMax;
int ii, intUpb, intBarWidth, intLegendHeight,
intSpace, intTopOffset, intTop;
Graphics oGraphic;
Font oFont;
oGraphic = Graphics.FromImage(aoBmp);
oFont = new Font("Small Fonts", 7);
intTopOffset = (astrCaption == "") ? 4 : 20;
oGraphic.FillRectangle(brushGet(astrBackground), 0, 0, aoRect.Left +
aintWidth + aoRect.Width, aoRect.Top +
aintHeight + aoRect.Bottom);
//畫軸
//注意:Y軸開始於頂部
oGraphic.DrawLine(penGet("black", 1), new Point(aoRect.Left,
aoRect.Top + aintHeight - aoRect.Height),
new Point(aoRect.Left + aintWidth,
aoRect.Top + aintHeight - aoRect.Height));
oGraphic.DrawLine(penGet("black", 1), new Point(aoRect.Left,
aoRect.Top + aintHeight - aoRect.Height),
new Point(aoRect.Left, aoRect.Top + 0));
//寫頭
writeHeaders(aoRect, oGraphic, astrXaxis,
astrYaxis, astrCaption, aintWidth, aintHeight);
intUpb = aintV.Length;
intSpace = 10;
intBarWidth = (int) ((aintWidth/intUpb) - intSpace);
intLegendHeight = (int) ((aintHeight/intUpb) - 5);
// Set Upper Y value
//實質上部Y值
sngMax = 1;
flTotal = 0;
for (ii=0;ii<intUpb;ii++)
{
sngMax = (aintV[ii] > sngMax) ? aintV[ii] : sngMax;
flTotal += aintV[ii];
}
for (ii=0;ii<intUpb;ii++)
{sngY[ii] = (aintV[ii] / sngMax) * (aintHeight - 10);}
//設定Y值比例
//繪條
for (ii=0;ii<intUpb;ii++)
{oGraphic.FillRectangle(brushGet(colorName(ii + 7)),
aoRect.Left + intSpace +
(intSpace + intBarWidth) * ii,
aoRect.Top - intTopOffset +
aintHeight - sngY[ii], intBarWidth, sngY[ii]);}
//繪製圖例
writeLegend(aoRect, oGraphic, astrCaption,
astrTitles, aintV, aintWidth, flTotal);
return aoBmp;
}