Jeg har lige lidt slam-kode her som jeg ikke er voldsomt stolt af at vise frem. Det er ikke kønt, men det virker.
Jeg har en funktion (GetAgeArray) som returnerer en masse tal. F.eks. {4,6,12,5,23,12,34,45}. Der tælles så op og laves et histogram.
Der er næsten ingen kommentarer, men du må spørge. Det var lige noget jeg havde liggende. Du kan se det "in action" her:
http://www.hghardware.dk/faellesholdet/restricted/histogram.aspx----------------------histogram.aspx-------------------------
<%@ Page Language="vb" Debug="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.oleDB" %>
<%@ import namespace="system.drawing" %>
<%@ import namespace="system.drawing.imaging" %>
<%@ import namespace="system.drawing.drawing2d" %>
<%@ Import Namespace="FH.DataAccess" %>
<script language="vb" runat="server">
' initialise objects
Public height as Integer = 200
Public width as Integer = 400
public b as New system.drawing.bitmap(width, height, pixelformat.Format16bppRgb555)
Public zeroX As Double = 55.77375
Public zeroY As Double = 12.47425
Sub Page_load(obj as Object, E as EventArgs)
' Set the content type
response.contenttype="image/jpeg"
dim fhDB As new Visit()
Dim ages() As Integer = fhDB.GetAgeArray()
dim Xval(), Yval(),max,min As Integer
dim g as graphics = graphics.fromimage(b)
DoStat( ages, Xval, YVal, max )
DrawHistogram(Xval, Yval, max,400,200,g)
' send the image to the viewer
b.save(response.outputstream, imageformat.jpeg)
' tidy up
b.dispose()
End Sub
Sub DoStat(data() As Integer, byRef XVal() As Integer, byRef Yval() As Integer, byRef max as integer)
Dim min as integer
Dim i as integer
Max = data(0)
Min = data(0)
'Find max/min
For each i in data
If i>max then max=i
If i<min then min=i
Next
ReDim XVal(max-min)
ReDim YVal(max-min)
For I=0 to max-min
XVal(i) = Min + i
YVal(i) = 0
Next
'Generér histogramdata
For Each i In data
Yval(i-Min) += 1
Next
'Find max antal
Max = Yval(0)
For each i in Yval
If i>max Then max=i
Next
End Sub
Function DrawHistogram(dataX() As Integer, dataY() As Integer, max as integer, width as integer, height as integer, g as Graphics)
g.clear(color.white) ' blank the image
g.smoothingMode = smoothingMode.antiAlias ' antialias objects
dim borderpen as New pen(color.gray,1)
dim graphpen as New pen(color.gray,1)
dim I as Integer
dim borderLeft,borderRight,borderTop, borderBottom as Integer
dim chartWidth, chartHeight As Integer
borderLeft=20
borderRight=10
borderTop=20
borderBottom=20
Dim strHeadline As String = "Aldersfordeling"
dim intervalsY As Integer = Max 'Antal intervaller i vandrette gridlinier
If intervalsY>10 Then
intervalsY = CInt(Max/2)
If Max mod 2 = 1 Then max=max+1
End If
chartWidth = width-borderLeft-borderRight
chartHeight = height-borderTop-borderBottom
Dim rec As New Rectangle(borderleft,bordertop,chartwidth,chartheight) 'graf-område
g.drawRectangle(borderpen,0,0,width-1,height-1)
Dim lb as new LinearGradientBrush(rec, Color.SteelBlue,Color.White,LinearGradientMode.Vertical)
g.FillRectangle(lb,rec)
g.drawRectangle(borderpen,rec)
dim salign as New system.drawing.stringformat
salign.alignment = stringalignment.center
Dim blockbr as new LinearGradientBrush(rec, Color.Yellow,Color.Red,LinearGradientMode.Vertical)
Dim blockwidth As Integer = cInt(chartwidth / (dataX.Length))
Dim scale As Double = (chartheight/max)
Dim X as integer
Dim Y as integer
For I=0 to dataX.Length-1
X = cInt(borderleft+blockwidth/3+blockwidth*i)
Y = cInt(dataY(i)*scale)
Dim blockrec As New rectangle(X,bordertop+chartheight-Y, blockwidth/2 ,y)
g.FillRectangle(blockbr,blockrec)
g.drawString((dataX(i)-1).toString, New font("Verdana",9,fontstyle.regular),Brushes.black, X+blockwidth/4,height-20,salign)
Next
salign.alignment = stringalignment.Far
For i = 0 to intervalsY
Y = cInt(bordertop + i*(chartheight/intervalsY))
if i>0 and i<intervalsY then g.drawLine(graphpen,rec.Left,Y,Rec.Right,Y)
g.drawString((max-i*max/intervalsY).toString, New font("Verdana",9,fontstyle.regular),Brushes.black, borderleft,Y-7,salign)
Next
salign.alignment = stringalignment.Center
g.drawString(strHeadline, New font("Verdana",12,fontstyle.bold),Brushes.black, width/2,0,salign)
End Function
</script>