Link to home
Create AccountLog in
Avatar of Bball
Bball

asked on

Transfer vb array data to histogram chart in web page

I have a working web application where the user works through various steps in a simulation providing input along the way. Random events occur to combine with the user's input to produce corresponding calculations and a final profit value. In the end, I have programmed a loop that cycles through the calculations 100 times with a fresh draw of the random events each time to produce 100 final profit values.

Here's the quesion: This application is written in .Net using vb coding behind the page and in various subroutines. The final profit values are stored in a vb array of length 100 titled ProfitSet1. I need to be able to take this data set and produce a histogram in a web page. What is the best way to go about doing that such that when the program is run the histogram can be graphed from this resulting array?

Bball
Avatar of TheMegaLoser
TheMegaLoser
Flag of Sweden image

You create an aspx page that returns a picture. Then you reference this script from where you want the histogram placed.

<IMG SRC="histogram.aspx">

In the histogram.aspx code-behind you create the actual picture.

' Clear the response
Response.Clear()

' Create a new bitmap
Dim oNewImg As New System.Drawing.Bitmap(100,100)
Dim gr As Graphics = System.Drawing.Graphics.FromImage(bmp)

' Loop the array and draw lines
for i = 0 to 100
   gr.Drawline(i, 0, i, array(i))
next i

oNewImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)

Note, code isn't compiled but should work. Hope ths helps!
Avatar of rangasuman
rangasuman

I would suggest you to use Microsoft Office Web Components for this purpose. You can download it from:
http://www.microsoft.com/downloads/details.aspx?familyid=7287252C-402E-4F72-97A5-E0FD290D4B76&displaylang=en
Some good tutorials and source code is available at:
http://www.west-wind.com/presentations/OWCCharting/OWCCharting.asp
http://www.4guysfromrolla.com/webtech/022101-1.shtml
Hope that helps.
Avatar of Bball

ASKER

TheMegaLoser,

I'm trying out your code and I am getting two errors:

From the line:
Dim gr As Graphics = System.Drawing.Graphics.FromImage(bmp)
I get the error that 'bmp' is not declared. I'm assuming 'bmp' needs to be created on the page as a blank image and declared on the code behind page as System.Web.UI.WebControls.Image. Is that correct?

From the line:
            gr.DrawLine(i, 0, i, ProfitSet1(i))
I get the error 'Overload resolution failed because no accessible 'DrawLine' accepts this number of arguments.' Since this is all new to me, I'm not sure what to do about this one.

Thanks
Bball

Avatar of Bball

ASKER

TheMegaLoser,

O.K., I figured out that gr.DrawLine needs an argument for the pen so I changed that to the following:
                   gr.DrawLine(pen.blue,i, 0, i, ProfitSet1(i))
and error goes away.

I'm having more difficulty resolving the first error though. If I declare 'bmp' as either

    Protected bmp As System.Drawing.Image

or as

    Dim bmp As System.Drawing.Image

depending upon where I put it, I get an error when I debug. The error refers to the line

Dim gr As Graphics = System.Drawing.Graphics.FromImage(bmp)

and states

Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: image

I think I'm getting a feel for how this is suppose to work but want to make sure I'm headed in the right direction before I proceed.

Thanks for your help!
Bball
ASKER CERTIFIED SOLUTION
Avatar of TheMegaLoser
TheMegaLoser
Flag of Sweden image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Bball

ASKER

TheMegaLoser, thanks for the code. That seems to clear up the compilation issues. I need to pretty up the format of the graph and I think I'm good to go.

One question, the ProfitSet1 array is actually the raw data. I need to sort it into bins for the histogram. Do you know of any canned vb code for this? I can write my own but I thought if you knew of any out there, it would save some time. My searches haven't turned up anything yet in vb, only in javascript so far.

Thanks


A quick serch turned up this:

http://www25.brinkster.com/mferris/html/downloads.html

I'm not sure if it's useful.
Avatar of Bball

ASKER

I ended up writing my own code for sorting the data into bins. Not that difficult and now I have a working graph. Still much to do on making it look nicer but the solution approach to create a page that returns a picture and then calling that page from where I want it displayed was fantastic. Thank you to TheMegaLoser for providing it!

Bball