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
jcaicedcAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fritz_the_blankCommented:
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
fritz_the_blankCommented:
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
farzinmCommented:
set oChart = Server.CreateObject("OWC.Chart")
set objChart = oChart.Charts.Add()
Set objConn = Server.CreateObject ("ADODB.Connection")
objConn.Open strConnection
'strConnection is your connection string
set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open sqlQuery,objConn
set oChart.DataSource = objRS
objChart.SetData c.chDimCategories, 0, "columnname1"
objChart.SetData c.chDimValues, 0, "columnname2"
'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

HTH



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

%>
fritz_the_blankCommented:
Try:

RS.Open sql,oConn,3,3

Fritz the Blank
fritz_the_blankCommented:
Or this:

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


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

objRS.CursorType = adOpenStatic
objRS.CursorLocation = 3
farzinmCommented:
where adOpenStatic =3
jcaicedcAuthor Commented:
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
fritz_the_blankCommented:
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
farzinmCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.