Dynamic | server | Charts dynamically generate charts using OWC server-side components
There used to be some articles about generating dynamic graphs in ASP, but some of the methods mentioned in those articles have some limitations, such as the need to install office2000 on the client or install OWC, and some may be used on the intranet. However, when you access over the Internet, you may experience a problem that is not working correctly (this is primarily related to SQL Server security settings). So how do we avoid these problems? A good way is to convert the diagram generated in ASP into a picture file saved on the server, so that the user will eventually see a picture in GIF format, I think any of the browsers now look at GIF pictures are not a problem. : Perhaps the only possible problem is how to deal with these generated temporary picture files, which we will discuss at the end of this question.
Here we will use a concrete example to illustrate this approach. The test environment for the program is:
NT4.0 (SP5) +iis4.0+sql server7.0+ie5.0, to run the following ASP program, save the following code copy to one of your virtual directories, Create a subdirectory TMP in this virtual directory (you can modify the path in your code to fit your path), set Read permission in IIS, and notice that you must set this TMP directory to the IUSR_servername user with at least Change permissions on NT. Create a DSN in pubs point to the Pubs database for SQL Server.
Here's the code:
Chart.asp
<%
Function Exportcharttogif (CSpace)
Dim FSO
Dim Sfilepath
Dim sFileName
' Generate the file name of the temporary file
Set fso = CreateObject ("Scripting.FileSystemObject")
Sfilepath = Request.ServerVariables ("path_translated")
Sfilepath = Left (Sfilepath, InStrRev (Sfilepath, "\"))
Sfilepath = Sfilepath & "Tmp\"
sFileName = fso. GetTempName ()
sFileName = sFileName & ". gif"
Set fso=nothing
' Convert chart to GIF file and save in temp directory
M_cspace. ExportPicture Sfilepath & sFileName, "GIF", 200, 150
' The generated temporary picture will be present in the session for easy deletion
Session ("TC:" & Sfilepath & sFileName) = Sfilepath & sFileName
Exportcharttogif = sFileName
End Function
Sub BINDCHARTTODSC (CSpace, DSC, Srsname, Scategories, Svalues)
Dim CHT
Dim ser
Set c = CSpace. Constants
CSpace. Clear
' Binding data source
Set CSpace. DataSource = DSC
CSpace. DataMember = Srsname
Set cht = CSpace. Charts.add ()
Cht. HasLegend = True
Cht. Type = C.chcharttypepie
Set ser = Cht. SeriesCollection.Add ()
Ser. SetData c.chdimcategories, 0, Scategories
Ser. SetData c.chdimvalues, 0, Svalues
Set DLS = Ser. Datalabelscollection.add ()
Dls. Haspercentage = True
Dls. HasValue = False
End Sub
%>
<meta name= "generator" content= "Microsoft FrontPage 4.0" >
<meta name= "ProgId" content= "FrontPage.Editor.Document" >
<title> dynamically generate charts using OWC </title>
<body>
<%
Set m_cspace = Server. CreateObject ("OWC.") Chart ")
Set DSC = Server. CreateObject ("OWC.") DataSourceControl ")
Dsc. ConnectionString = "Dsn=pubs;uid=sa"
Dsc. Recordsetdefs.addnew "SELECT DISTINCT state,num=count (*) from authors GROUP by state", DSC. Constants.dsccommandtext, "ChartData."
BINDCHARTTODSC M_cspace, DSC, "ChartData", "state", "num"
M_sfilepath = Exportcharttogif (m_cspace)
Set m_cspace=nothing
Set m_cht=nothing
%>
<br><br>
</BODY>
</HTML>
We'll find two useful functions: Exportcharttogif and BINDCHARTTODSC.
Exportcharttogif (cspace) is a function used to convert chart to GIF files
CSpace: An example of a Owc.chart
BINDCHARTTODSC (CSpace, DSC, Srsname, Scategories, svalues) is used to bind a chart to a data source
CSPACE:OWC. Examples of chart
DSC: Data Source
Srsname: Recordset Name
The name field of each series in the Scategories,svalues:chart and the field name of the corresponding value (it doesn't seem clear, but look at the code and you'll see)
When using server-side OWC, it is virtually indistinguishable from using other components, using Server.CreateObject to create an instance, and then invoking the component's methods and properties.
The OWC includes four components, and their ProgID are:
OWC. Chart: Chart Components
OWC. Spreadsheet: Spreadsheet Component
OWC. DataSourceControl: Data Source Component
OWC. PivotTable: Dynamic Report components
Finally, how to deal with temporary picture files
You can see the following code in the Exportcharttogif-han book:
Session ("TC:" & Sfilepath & sFileName) = Sfilepath & sFileName
We save the resulting picture file path in session, and then add the following code to the Session_OnEnd:
Set fsotemp = CreateObject ("Scripting.FileSystemObject")
For each imagefile in session.contents
If left (imagefile,3) = "TC:" Then
Fsotemp.deletefile Mid (imagefile,4), True
End If
Next
Set fsotemp=nothing
All right. Finally finished, the method of dealing with temporary files may not be very good, if you have a better way, please let me know. Hopefully this article will help all of you: