Link to home
Start Free TrialLog in
Avatar of jcaicedc
jcaicedc

asked on

Filling a chart with recordset data in asp

Hello Everybody

It's me again, but this time I have an ASP question for you

I've the following code provided by the msnd service

<%

Dim oChart, c, Categories(5), Vals(5), i, sCaption, nData, nOrg

' Get the input value
nData = Request.QueryString("sOrg")

'When the page loads the first time, set ndata to 5
if len(nData) = 0 then nData = 5

' Generate random categories and values for the chart
' These values can come from some existing data source
for i = 1 to 5
      Categories(i) = "Machine" & CStr(i)
      Vals(i) = nData * Rnd(100)
next

' Create a Chart Object
Set oChart = CreateObject("OWC.Chart")
Set c = oChart.Constants

' Set the different parameters for the ChartSpace
oChart.Border.Color = c.chColorNone

' Get Organization number and use it to set the Caption
nOrg = nData/5
sCaption = "Current Utilizations for Org"
sCaption = sCaption & CStr(nOrg)

' Add a chart and set parameters for the chart
oChart.Charts.Add
oChart.Charts(0).Type = oChart.Constants.chChartTypeColumnClustered
oChart.Charts(0).SeriesCollection.Add
oChart.Charts(0).SeriesCollection(0).Caption = sCaption
oChart.Charts(0).SeriesCollection(0).SetData c.chDimCategories, c.chDataLiteral, Categories
oChart.Charts(0).SeriesCollection(0).SetData c.chDimValues, c.chDataLiteral, Vals
oChart.Charts(0).HasLegend = True
oChart.Charts(0).HasTitle = True

' Get a temporary filename to save chart in that file
sFname = Session("FSO").GetTempName & session.SessionID & ".gif"

' Export the chart to the temporary file
oChart.ExportPicture server.MapPath(sFname), "gif", 600, 512

' Create a link to the generated file
Response.Write "<img src='" & sFname & "'>"

' Store the file with its path in the session object for cleanup
Session("sTempFile" & Session("n")) = Server.MapPath(sFname)

' Increment the number of files
Session("n") = Session("n") + 1

%>
The code uses Rnd function to generate random values that are shown in the chart, of course. Also, there is the possibility to fill the chart with data from a recordset or database, but i don't know how can it be. Hope someone knows about that, and can help me with this little issue.

Thanks in advanced

Jcaicedc
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

It should be something like this:

intCounter  = 0
do while not objRS.EOF
     Categories(intCounter) = objRS("Category")
     Vals(intCounter) =objRS("Value")
     intCounter  = intCounter  + 1
    objRS.MoveNext
next
The idea here is that you have a series of categories and values that need to be populated. The code above iterates through your recordset, grabs the values for each pair, and then populates the chart.

Fritz the Blank
ASKER CERTIFIED SOLUTION
Avatar of farzinm
farzinm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you can bind the data to the chart in the manner that  farzinm suggests, that would be a much better solution than mine.

Fritz the Blank
Avatar of jcaicedc
jcaicedc

ASKER

Ok

I tried both solutions. First one returned me an error due the size of array related to the index of array. The second one returned me the following error
 
Microsoft Office Chart 9.0 error '80070057'

The given ADO Recordset must be set to client cursor and must not be forward only reading.

/tesis/chart.asp, line 34

Could you tell me why?

Thanks in advance
Oops, I forgot to post the code. Here he goes

<%

Dim oChart, c, sCaption, nData, nOrg

' Get the input value
nData = Trim(Request.Form("partidos"))


' Generate random categories and values for the chart
' These values can come from some existing data source


' Create a Chart Object
Set oChart = CreateObject("OWC.Chart")
Set c = oChart.Constants

' Set the different parameters for the ChartSpace
oChart.Border.Color = c.chColorNone

' Get Organization number and use it to set the Caption
nOrg = nData
sCaption = "Current Utilizations for Org"
sCaption = sCaption & nOrg

' Add a chart and set parameters for the chart
set oChart = Server.CreateObject("OWC.Chart")
set objChart = oChart.Charts.Add()
Set oConn = Server.CreateObject ("ADODB.Connection")
oConn.open = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="& Server.MapPath("./tesis.mdb")
'strConnection is your connection string
set RS = Server.CreateObject("ADODB.Recordset")
sql="Select partido, votos from concejos88 where partido = '"& nData &"'"
RS.Open sql,oConn
set oChart.DataSource = RS
objChart.SetData c.chDimCategories, 0, "partido"
objChart.SetData c.chDimValues, 0, "votos"
'columnname1 is the column name in your sql query
'columnname2 is the column name in your sql query

' Get a temporary filename to save chart in that file
sFname = Session("FSO").GetTempName & session.SessionID & ".gif"

' Export the chart to the temporary file
oChart.ExportPicture server.MapPath(sFname), "gif", 600, 512

' Create a link to the generated file
Response.Write "<img src='" & sFname & "'>"

' Store the file with its path in the session object for cleanup
Session("sTempFile" & Session("n")) = Server.MapPath(sFname)

' Increment the number of files
Session("n") = Session("n") + 1

%>
Try:

RS.Open sql,oConn,3,3

Fritz the Blank
Or this:

RS.CursorLocation = 3
RS.Open sql,oConn,3,3


Fritz the Blank
yes forgot to mention this before you open your connection

objRS.CursorType = adOpenStatic
objRS.CursorLocation = 3
where adOpenStatic =3
Fritz thanks a lot, you were as helpful as farzinm.  Sorry 'bout the points but EE has no option to divide the points. Thanks, and thanks and thanks a lot. See you soon

Jcaicedc
Actually they do!

In this case, farzinm provided your solution so it is best that points go to him/her anyway.

Good luck with you project,

Fritz the Blank
jcaicedc
thanks for the points, as fritz_the_blank  mentioned there is a feature called split points.
http://oldlook.experts-exchange.com/help/closing.jsp#3