下面给出了一个通用的函数及其用例,请参考。 <% REM 首先我们设计一个ShowCart函数,其中 REM aValues 是一个数值数组 REM aLables 是横坐标的标号 REM strTitle是这个统计图的标题 REM strXAxisLable X轴的标签 REM strYAxisLable Y轴的标签 Sub ShowChart(ByRef aValues, ByRef aLabels, ByRef strTitle, ByRef strXAxisLabel, ByRef strYAxisLabel) ' 你可以随便改变的图形常数 ' 单位是屏幕像素点 Const GRAPH_WIDTH = 450 ' 图形宽度 Const GRAPH_HEIGHT = 250 ' 图形 Const GRAPH_BORDER = 5 ' 坐标线宽度 Const GRAPH_SPACER = 2 ' 统计条之间的距离 Const TABLE_BORDER = 0 ' 变量声明 Dim I Dim iMaxValue Dim iBarWidth Dim iBarHeight ' 取得aValues最大值 iMaxValue = 0 For I = 0 To UBound(aValues) If iMaxValue < aValues(I) Then iMaxValue = aValues(I) Next 'I ' 计算每条图形的宽度 iBarWidth = (GRAPH_WIDTH \ (UBound(aValues) + 1)) - GRAPH_SPACER ' 开始绘图 %> <TABLE BORDER='<%= TABLE_BORDER %>' CELLSPACING='0' CELLPADDING='0'> <TR> <TD COLSPAN='3' ALIGN='center'><H2><%= strTitle %></H2></TD> </TR> <TR> <TD VALIGN='center'><B><%= strYAxisLabel %></B></TD> <TD VALIGN='top'> <TABLE BORDER='<%= TABLE_BORDER %>' CELLSPACING='0' CELLPADDING='0'> <TR> & nbsp; &n bsp; <TD ROWSPAN='2'><IMG SRC='http://www.okasp.com/techinfo/images/spacer.gif' BORDER='0' WIDTH='1' HEIGHT='<%= GRAPH_HEIGHT %>'></TD> <TD VALIGN='top' ALIGN='right'><%= iMaxValue %> </TD> & nbsp; </TR> <TR> <TD VALIGN='bottom' ALIGN='right'>0 </TD> </TR> </TABLE> </TD> <TD> <TABLE BORDER='<%= TABLE_BORDER %>' CELLSPACING='0' CELLPADDING='0'> <TR> <TD VALIGN='bottom'><IMG SRC='http://www.okasp.com/techinfo/images/spacer_black.gif' BORDER='0' WIDTH='<%= GRAPH_BORDER %>' HEIGHT='<%= GRAPH_HEIGHT %>'></TD> <% ' 对数组所有数值进行循环,绘制条形图! & nbsp; For I = 0 To UBound(aValues) iBarHeight = Int((aValues(I) / iMaxValue) * GRAPH_HEIGHT) & nbsp; &n bsp; ' 因为浏览器会忽略0高度的条形图,所以用1代替! & nbsp; If iBarHeight = 0 Then iBarHeight = 1 %> <TD VALIGN='bottom'><IMG SRC='http://www.okasp.com/techinfo/images/spacer.gif' BORDER='0' WIDTH='<%= GRAPH_SPACER %>' HEIGHT='1'></TD> <TD VALIGN='bottom'><IMG SRC='http://www.okasp.com/techinfo/images/spacer_red.gif' BORDER='0' WIDTH='<%= iBarWidth %>' HEIGHT='<%= iBarHeight %>' ALT='<%= aValues(I) %>'></A></TD> <% & nbsp; Next 'I %> </TR> <TR> <TD COLSPAN='<%= (2 * (UBound(aValues) + 1)) + 1 %>'><IMG SRC='http://www.okasp.com/techinfo/images/spacer_black.gif' BORDER='0' WIDTH='<%= GRAPH_BORDER + ((UBound(aValues) + 1) * (iBarWidth + GRAPH_SPACER)) %>' HEIGHT='<%= GRAPH_BORDER %>'></TD> </TR> <% ' 检查横坐标数组是否有效! %> <% If IsArray(aLabels) Then %> <TR> <TD><!-- 留点空间 --></TD> <% For I = 0 To UBound(aValues) %> <TD><!-- 留点空间 --></TD> <TD ALIGN='center'><FONT SIZE='1'><%= aLabels(I) %></FONT></TD> <% Next 'I %> </TR> <% End If %> </TABLE> </TD> </TR> <TR> <TD COLSPAN='2'><!-- 使下一个TD元素居中--></TD> <TD ALIGN='center'><BR><B><%= strXAxisLabel %></B></TD> </TR> </TABLE> <% End Sub %> <% ' 一个带有横坐标的条形图 ShowChart Array(6, 10, 12, 18, 23, 26, 27, 28, 30, 34, 37, 45, 55), _ Array("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", _ "P10", "P11", "P12", "P13"), "连续几个月的销售额", "X 轴", "Y 轴" ' 空行 Response.Write "<BR>" & vbCrLf Response.Write "<BR>" & vbCrLf Response.Write "<BR>" & vbCrLf ' 构造一个由随机数组成的数组 Dim I Dim aTemp(49) Randomize For I = 0 to 49 aTemp(I) = Int((50 + 1) * Rnd) Next 'I ' 由随机数组生成的条形图 ShowChart aTemp, "这不是一个数组!", "随机条形图", "序号 ", "随机数" %>
|