本節(jié)我們將通過gid+繪制圖形。便于實例模擬,我們采用數(shù)組數(shù)據(jù)來模擬公司業(yè)務數(shù)據(jù)。
新建一個web窗體,命名為gdi_sample2.aspx,邏輯代碼如下:
'-----code begin-----
imports system.drawing
imports system.drawing.bitmap
imports system.drawing.graphics
imports system.drawing.imaging
public class gdi_sample4
inherits system.web.ui.page
#region " web 窗體設計器生成的代碼 "
'此處省略窗體設計器生成的代碼
#end region
'定義公共數(shù)組變量,用來存放模擬公司銷售額數(shù)據(jù)
public data(5, 1) as string
private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
'在此處放置初始化頁的用戶代碼
dim i as int16
dim objbitmap as bitmap
objbitmap = new bitmap(400, 300)
'聲明graphics對象
dim objgraphics as graphics
'指定畫布
objgraphics = graphics.fromimage(objbitmap)
'設置背景色為白色
objgraphics.clear(color.white)
'畫出圖形邊框,注意留出劃線的空間,為一個象素
objgraphics.drawrectangle(pens.black, 1, 1, 398, 298)
'生成表頭文字
objgraphics.drawstring("本公司上半年營業(yè)額統(tǒng)計圖", new font("宋體", 16, fontstyle.bold), brushes.black, new pointf(60, 5))
'獲取模擬數(shù)據(jù)
getdata()
dim monthcolor as pointf = new pointf(260, 40)
dim fontinfor as pointf = new pointf(285, 36)
'畫出示意顏色圖例
for i = 0 to 5
'畫出填充矩形。
objgraphics.fillrectangle(new solidbrush(getcolor(i)), monthcolor.x, monthcolor.y, 20, 10)
'畫出矩形邊框。
objgraphics.drawrectangle(pens.black, monthcolor.x, monthcolor.y, 20, 10)
'畫出圖例說明文字--data(i, 0)
objgraphics.drawstring(data(i, 0), new font("宋體", 10), brushes.black, fontinfor)
'移動坐標位置,只移動y方向的值即可。
monthcolor.y += 15
fontinfor.y += 15
next i
'遍歷數(shù)據(jù)源的每一項數(shù)據(jù),并根據(jù)數(shù)據(jù)的大小畫出矩形圖(即柱形圖的柱)。
for i = 0 to 5
'畫出填充矩形。
objgraphics.fillrectangle(new solidbrush(getcolor(i)), (i * 25) + 35, 270 - cint(data(i, 1)), 15, cint(data(i, 1)))
'畫出矩形邊框線。
objgraphics.drawrectangle(pens.black, (i * 25) + 35, 270 - cint(data(i, 1)), 15, cint(data(i, 1)))
next
'畫出示意坐標
objgraphics.drawline(new pen(color.blue, 1), 10, 0, 10, 320)
objgraphics.drawline(new pen(color.blue, 1), 10, 270, 200, 270)
'在示意坐標上添加數(shù)值標志,注意坐標的計算
for i = 0 to 5
objgraphics.drawline(new pen(color.blue, 1), 10, i * 50 + 20, 20, i * 50 + 20)
objgraphics.drawstring((250 - i * 50).tostring, new font("宋體", 10), brushes.black, 12, i * 50 + 8)
next
' 統(tǒng)計總銷售額
dim scount as integer
for i = 0 to 5
scount += cint(data(i, 1))
next
'定義畫出扇形角度變量
dim scg as single = 0
dim stg as single = 0
for i = 0 to 5
'計算當前角度值:當月銷售額 / 總銷售額 * 360,得到餅圖中當月所占的角度大小。
scg = cint(data(i, 1)) / scount * 360
新聞熱點
疑難解答
圖片精選