home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / bar_chart.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  4.9 KB  |  135 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. Sub ShowChart(ByRef aValues, ByRef aLabels, ByRef strTitle, ByRef strXAxisLabel, ByRef strYAxisLabel)
  15.     ' Some user changable graph defining constants
  16.     ' All units are in screen pixels
  17.     Const GRAPH_WIDTH  = 450  ' The width of the body of the graph
  18.     Const GRAPH_HEIGHT = 250  ' The heigth of the body of the graph
  19.     Const GRAPH_BORDER = 5    ' The size of the black border
  20.     Const GRAPH_SPACER = 2    ' The size of the space between the bars
  21.  
  22.     ' Debugging constant so I can eaasily switch on borders in case
  23.     ' the tables get messed up.  Should be left at zero unless you're
  24.     ' trying to figure out which table cells doing what.
  25.     Const TABLE_BORDER = 0
  26.     'Const TABLE_BORDER = 10
  27.  
  28.     ' Declare our variables
  29.     Dim I
  30.     Dim iMaxValue
  31.     Dim iBarWidth
  32.     Dim iBarHeight
  33.  
  34.     ' Get the maximum value in the data set
  35.     iMaxValue = 0
  36.     For I = 0 To UBound(aValues)
  37.         If iMaxValue < aValues(I) Then iMaxValue = aValues(I)
  38.     Next 'I
  39.     'Response.Write iMaxValue ' Debugging line
  40.  
  41.  
  42.     ' Calculate the width of the bars
  43.     ' Take the overall width and divide by number of items and round down.
  44.     ' I then reduce it by the size of the spacer so the end result
  45.     ' should be GRAPH_WIDTH or less!
  46.     iBarWidth = (GRAPH_WIDTH \ (UBound(aValues) + 1)) - GRAPH_SPACER
  47.     'Response.Write iBarWidth ' Debugging line
  48.  
  49.  
  50.     ' Start drawing the graph
  51.     %>
  52.     <TABLE BORDER="<%= TABLE_BORDER %>" CELLSPACING="0" CELLPADDING="0">
  53.         <TR>
  54.             <TD COLSPAN="3" ALIGN="center"><H2><%= strTitle %></H2></TD>
  55.         </TR>
  56.         <TR>
  57.             <TD VALIGN="center"><B><%= strYAxisLabel %></B></TD>
  58.             <TD VALIGN="top">
  59.                 <TABLE BORDER="<%= TABLE_BORDER %>" CELLSPACING="0" CELLPADDING="0">
  60.                     <TR>
  61.                         <TD ROWSPAN="2"><IMG SRC="./images/spacer.gif" BORDER="0" WIDTH="1" HEIGHT="<%= GRAPH_HEIGHT %>"></TD>
  62.                         <TD VALIGN="top" ALIGN="right"><%= iMaxValue %> </TD>
  63.                     </TR>
  64.                     <TR>
  65.                         <TD VALIGN="bottom" ALIGN="right">0 </TD>
  66.                     </TR>
  67.                 </TABLE>
  68.             </TD>
  69.             <TD>
  70.                 <TABLE BORDER="<%= TABLE_BORDER %>" CELLSPACING="0" CELLPADDING="0">
  71.                     <TR>
  72.                         <TD VALIGN="bottom"><IMG SRC="./images/spacer_black.gif" BORDER="0" WIDTH="<%= GRAPH_BORDER %>" HEIGHT="<%= GRAPH_HEIGHT %>"></TD>
  73.                     <%
  74.                     ' We're now in the body of the chart.  Loop through the data showing the bars!
  75.                     For I = 0 To UBound(aValues)
  76.                         iBarHeight = Int((aValues(I) / iMaxValue) * GRAPH_HEIGHT)
  77.  
  78.                         ' This is a hack since browsers ignore a 0 as an image dimension!
  79.                         If iBarHeight = 0 Then iBarHeight = 1
  80.                     %>
  81.                         <TD VALIGN="bottom"><IMG SRC="./images/spacer.gif" BORDER="0" WIDTH="<%= GRAPH_SPACER %>" HEIGHT="1"></TD>
  82.                         <TD VALIGN="bottom"><IMG SRC="./images/spacer_red.gif" BORDER="0" WIDTH="<%= iBarWidth %>" HEIGHT="<%= iBarHeight %>" ALT="<%= aValues(I) %>"></A></TD>
  83.                     <%
  84.                     Next 'I
  85.                     %>
  86.                     </TR>
  87.                     <!-- I was using GRAPH_BORDER + GRAPH_WIDTH but it was moving the last x axis label -->
  88.                     <TR>
  89.                         <TD COLSPAN="<%= (2 * (UBound(aValues) + 1)) + 1 %>"><IMG SRC="./images/spacer_black.gif" BORDER="0" WIDTH="<%= GRAPH_BORDER + ((UBound(aValues) + 1) * (iBarWidth + GRAPH_SPACER)) %>" HEIGHT="<%= GRAPH_BORDER %>"></TD>
  90.                     </TR>
  91.                 <% ' The label array is optional and is really only useful for small data sets with very short labels! %>
  92.                 <% If IsArray(aLabels) Then %>
  93.                     <TR>
  94.                         <TD><!-- Spacing for Left Border Column --></TD>
  95.                     <% For I = 0 To UBound(aValues)  %>
  96.                         <TD><!-- Spacing for Spacer Column --></TD>
  97.                         <TD ALIGN="center"><FONT SIZE="1"><%= aLabels(I) %></FONT></TD>
  98.                     <% Next 'I %>
  99.                     </TR>
  100.                 <% End If %>
  101.                 </TABLE>
  102.             </TD>
  103.         </TR>
  104.         <TR>
  105.             <TD COLSPAN="2"><!-- Place holder for X Axis label centering--></TD>
  106.             <TD ALIGN="center"><BR><B><%= strXAxisLabel %></B></TD>
  107.         </TR>
  108.     </TABLE>
  109.     <%
  110. End Sub
  111. %>
  112. <%
  113. ' Static Chart (with Bar Labels)
  114. 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"), "Chart Title", "X Label", "Y Label"
  115.  
  116.  
  117. ' Spacing
  118. Response.Write "<BR>" & vbCrLf
  119. Response.Write "<BR>" & vbCrLf
  120. Response.Write "<BR>" & vbCrLf
  121.  
  122.  
  123. ' Random number chart
  124. Dim I
  125. Dim aTemp(49)
  126.  
  127. Randomize
  128. For I = 0 to 49
  129.     aTemp(I) = Int((50 + 1) * Rnd)
  130. Next 'I
  131.  
  132. ' Chart made from random numbers (without Bar Labels)
  133. ShowChart aTemp, "Note that this isn't an Array!", "Chart of 50 Random Numbers", "Index", "Value"
  134. %>
  135.