Establish a server chart using Office 2000 chart components

xiaoxiao2021-03-06  65

Some of you may have been required to write ASP pages that established charts for changing data. The usual solution is to buy a business server chart generation component. Ok, Offic 2000 contains a chart component that you use, and you don't need to install the entire Office or Excel on the load server. Moreover, as a dynamic link library (DLL), you don't have to change the metadata in order to allow component processing (the translation: data of data). This article will show you how to write ASP applications, its function is to receive stock markets from the XML file and store the data in the database and then establish a user-defined chart.

Install chart components

To install the server components, you need to run the Office2000 installer on the server and select Install Microsoft Office Web Componets. Note that your needs have a license agreement of Office2000, which is afraid to install other applications (Word, Excel).

build database

We have to build a data sheet to store our stock market. Create an Access database called quotes.mdb, including a table called quotes. Use the table shown below, or use the example provided herein.

Get and store the stock market in XML format

Stock market data comes from an XML file, which is included in the downloaded example. I will use Microsoft's XML format to store XML files, which allows direct load data to ADO records. If there are several different formats in the data source, we have to translate the XML file for this format or write some syntax parsing code to get relevant data. We use the loadXml.asp page to complete this feature:

DIM Stock, Last

File = server.mappath ("xmlquote.xml")

ObjRecordset.open "file: //" & 'file open recordset

ObjRecordset.movefirst

While Not ObjRecordset.eof

Stock = ObjRecordset ("stock")

Last = Replace (ObjRecordset ("Last"), ",", ".")

Data = DATE ()

Set objrecordset1 = objdataacon.execute ("INSERT INTO Quotes" VALUES _

('"& stock &", "& last &") ", limited", "& LAST &") ", LRECORDSFOUND)

ObjRecordset.movenext

Wend

ObjRecordset.close

This example is from a local file system to load an XML file, but more realistic situations will be loaded from an Internet server:

Objrecordset.open "http://www.webservername.com/stock/quotes/" & File

Once the loaded record is the same as the other record set. This code stacks all records and loads its values ​​in the database. The REPLACE function must be used on my computer, because the separator in my area setting is a comma is not a point. If the XML source file is also used as a digital separator, you don't have to use it.

In a real environment, this page should be in a schedule, and every time you call to get the sampling of the stock market. The accuracy of the chart is limited to the spacing of the sampling and its consistency.

The XML file is saved directly as an ADO record set is a new feature of ADO2.1 and is strengthened in ADO2.5. If you want to know more about the ADO and XML record sets, click on the reference in the relevant link. Let users define charts

The Just-in-Time will become more attractive, especially the user can define the parameters of the chart. This type of option is very much, I have selected three parameters for the user input:

During the day period (on the date), the size (wide, height) of the chart uses pixel to indicate stock (multiple choice)

More selects the types (LINE, BARS, ETC), model (2D, 3D) and is almost as much as in the Excel Chart Wizard. These values ​​are entered by the user in the DEFAULT.ASP page and is displayed in QUOTES.ASP below.

The date range is automatically adjusted according to the time span of the available stock market, and the stock list is dynamically generated from the market database.

Generate chart

Once we let users choose, we can generate the corresponding chart. The quotes.asp page reads the parameters and generates charts three steps:

Establish an object and set global properties

The chart object supports multiple pictures, each of which is an object. In this page, we only need one. Some global attributes are drawn objects, but most of them are used to set the chart itself. There are a lot of properties to use, just I choose:

'Create a chart Object

Set Ochart = Server.createObject ("OWC.CHART")

Set C = Ochart.Constants' Object with Charting Constants

'Set the Different Parameters for the Chartspace

Ochart.Border.color = C.chcolornone

'Add Chart Object and Define ITS Type (LINE)

SET Chart = Ochart.Charts.Add

Chart.Type = Ochart.Constants.chcharttypeline

Chart.haslegegend = TRUE

Chart.hastitle = false

Chart.legend.position = Ochart.Constants.chlegendPositionBottom

Load data value

Each chart has several data values, which behave in different line segments. Each stock is a value set and all value sets share a custom date range. This group value is loaded through an array that has been established. This array is generated from the database. Naturally, we don't know how much elements will contain in the future array, so they have to establish a dynamic array and change the size of the data read from the read database:

'Add a value set for the chart

Set serie = chart.seriescollection.add

Serie.caption = stock 'Parameter, stock name

'Loading x朼 xis Labels

Serie.SetData C.chdimcategories, C.chdataliteral, Categories

'Loading value

Serie.SetData C.chdimvalues, C.chdataliteral, VALS

Save the graph as a GIF file

Once all data is loaded, the chart is ready to be generated. The output must be added to the response page, in order to make a chart to be saved as a GIF file for a unique name. The response page should include a link for this image: 'Get a Temporary FileName to Save Chart in That File

'The FileSystem Object IS Already Open

SFNAME = fs.gettempname & session.SessionID & ".gif"

'Export the chart to the temporary file (Width and Height from User Parameters)

Ochart.exportPicture Server.Mappath (Sfname), "GIF", CINT (WCHART), CINT (HCHART)

'Create a Link to The Generated File

Response.write "

SET FS = Nothing

Set Ochart = Nothing

Set chart = Nothing

SET series = Nothing

SET C = Nothing

Temporary file deletion

You have an instant chart now, but it has also left a lot of temporary files. Each time the user creates a chart, a temporary file will remain on the hard disk and no longer used. One solution is to create another ASP page to delete these files, but this requires a scheduler to activate this page in a timely manner. My program is that case the drawing page itself. Each time a user requires a picture, the page first deletes more than one minute old file. This function is as follows:

Sub deleteoldCharts ()

'Delete Temporary Files Older Than ONE Minute

ON Error ResMe next

Dim FileSystem, File, File1, Filecoll, S

DIM CNT, GLOBALCNT, FNAME

DIM path, DIR

File = "charts.asp" 'use the page ion filename

PATH = Server.mappath (file)

DIR = Left (Path, Len (PATH) 杔 En (file))

Set FileSystem = Server.createObject ("scripting.filesystemObject")

Set file = filesystem.getfolder (DIR)

SET filecoll = file.files

CNT = 0: globalcnt = 0

For Each File1 in FileColl

FileDate = file1.datecreated

GLOBALCNT = Globalcnt 1

If TimeValue (FileDate)

FNAME = file1.name

IF INSTR (Ucase (FNAME), ".gif") THEN

FileSystem.deletefile Dir & Fname, True

CNT = CNT 1END IF

END IF

NEXT

Set filesystem = Nothing

Set file = Nothing

Set filecoll = Nothing

End Sub

One minute interval is enough to avoid deleting files being in use, but you can add time intervals as needed. This solution will leave some files on the hard disk, but effectively prevents an unlimited growth of hard disk space, so that it remains at an acceptable level. Possible improvements This example app is just a starting point. Everyone who knows the Excel chart knows that the Office2000's chart component is a complex object with many properties and methods, and browsing its documentation is worth it.

Do not set tags for each X-axis: If the sample value is larger than the size of the chart, the X axis will not be readable. The solution is to use the selected value to set the label and use a fixed interval. To define the range of Y-axis for multiple selection: The exemplary code of drawing the chart is a suitable interval for the Y-axis used for one stock chart. If you need a chart of multiple stocks, the y-axis range is set to the maximum from zero to this set of data. This seems to be a bug in the chart component. This solution is to set the smallest value and maximum value of the Y axis directly in the code.

Sample program

Included in this article, consisting of the following files:

Default.asp default page, user parameter entry form quotes.asp chart Generate page fuctions.inc shared function library, all ASP pages are transferred to this page Loadxml.asp to load the stock market to the XML format to the database quotes.mdb access2000 Data stock stock market XMLQUOTES.ASP an XML market file

To use this example, create a folder in any location in the root directory of the IIS and copy all files to this directory. No adjustment or setting ODBC DSN is required.

summary

I hope I can show how to use MS Office 2000 chart components to easily build an application of a powerful drawing chart. If you have a license agreement with Office 2000 or Excel, then it is useful as much as possible in other available chart components.

Translation, Production: Iven OICQ: 1917460 E-mail: ivice@china.com iCestudio © 2000

转载请注明原文地址:https://www.9cbs.com/read-86403.html

New Post(0)