We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Transfer vb array data to histogram chart in web page

Bball
Bball asked
on
Medium Priority
343 Views
Last Modified: 2008-02-01
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
Comment
Watch Question

CERTIFIED EXPERT

Commented:
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!
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.

Author

Commented:
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

Author

Commented:
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
CERTIFIED EXPERT
Commented:
Sorry for that, a minor typo from my part. bmp should of course be oNewImg. Hence the code should be like this (with some further cleaning, and yes, this one compiles).

--- Code start ---
Const iPictureHeight As Integer = 100
Const iPictureWidth As Integer = 100
Dim iColumn As Integer

Response.Clear()

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

' Loop the array and draw lines
For iColumn = 0 To iPictureWidth
   ' Subtract from picture height to draw from the bottom up
   gr.DrawLine(System.Drawing.Pens.Blue, iColumn, iPictureHeight, iColumn, iPictureHeight -  ProfitSet1(iColumn))
Next iColumn

' Write the image to the client in jpeg format
oNewImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
--- Code stop ---

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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


CERTIFIED EXPERT

Commented:
A quick serch turned up this:

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

I'm not sure if it's useful.

Author

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.