━━━━━━━━━━━ 提 Outline: ━━━ 一 一━ 一 一 一 一 一 1. Overview II, set the chart engine three, OWC license problem four, OWC running mechanism five Install OWC 10 Sixth, OWC programming mode on the web server, ━━━━━━━━━ 正 正━ 一 一 一 一 一 一, 一 在 一, It is often encountered that the interactive chart must be provided. For example, you might develop a application that manages sales and product data, data saved on the SQL Server database, the application allows users to add data, update existing data, but in addition to these features, customers also require applications to be available Pie, cylindrical or xy scatter plot forms intuitively describe data. In Windows desktop applications, such requirements have never become a problem, which is too much to choose from the graphics library and drawing components. But for web applications, the problem is more complicated. To draw charts in web applications include: ■ Client: Use a variety of ActiveX components, Web browsers are fully possible to achieve the "rich" Windows client. The disadvantage is that the client's settings are complicated, requiring release client software, usually calculating license fees according to each client. In addition, clients with non-MS Windows / IE are generally difficult to run. ■ Server side: Dynamically generate charts using server-side code running on the web server, and then sent to the client as GIF or JPG graphics. The advantage of this approach is that the client only needs a standard browser. The disadvantage of the client technology is that the interaction of graphics (unless new requests are submitted to the server, otherwise, it cannot be zoomed and scrolled). Many map sites (such as mapquest.com) use this technology. Note that the map graphics are not saved on the web server, but the user makes a request to generate from the map database when the user issues a request. This paper mainly discusses how to use the server-end chart drawing technology to provide graphics function in the ASP.NET web page, specifically, this paper analyzes how to generate a scatter plot using the data saved in the MS SQL database. Second, set the chart engine If you want to draw a chart in the ASP.NET application, you must have a suitable chart engine. ASP.NET has a built-in graphics tool library, named the system.drawing namespace GDI , can be used to create simple pie charts, column charts, line charts, etc., but it belongs to the low API, not drawing the chart Engine, especially unairing complex charts. In the ASP.NET environment, there are many commercial charts to draw code libraries, you can find Google, you can find: Mycos Charts .NET Web Forms EditionDundas Chart for asp.net.NetChartingCharting Controls At the ASP.NET Control Gallery However, many The products are quite expensive, and compared with the most widely used chart drawing tools in the world, it is not exempt from strange feelings. MS Excel is a quite powerful chart engine that is supported in chart types and provides complete chart layout adjustment. Because of this, the protagonist to be discussed herein is OWC, ie Office Web Components, or "Office Web Components". According to Microsoft's definition, OWC is a "Microsoft Technology that will be extended to Web to Web". It can be used on clients, for example, when we save the Excel worksheet as a web page, you can use it to easily publish interactive spreadsheets and charts to the web page.
At the same time, OWC is also an excellent server-side chart engine with equally powerful charts as MS Excel. Third, OWC license issues If you have used the version earlier OWC, you may have encountered Microsoft license issues. The previous problem is quite annoying. Microsoft not only requires Office licenses on the server, but also every customer's PC is also available. In fact, this is equivalent to the limitations of OWC to intranet, and only intranet can ensure that the Office license is installed on the client PC. But now Microsoft's attitude is slow-server still to install Office license, but as long as the chart is "non-interactive" use, such as the server-side chart drawing of this article, the client does not have to install the Office license. In fact, even if the server side does not have to install a complete Office license, Excel 2002 or FrontPage 2002 licenses are sufficient, so that OWC has become a cheaper server-side chart engine. So, install MS Office on the server? No, there is no need. Although OWC should be part of Office from the license conditions, but from the technical, OWC is an independent product. You only need to install the OWC package on the web server without having to install the entire Office. OWC first appeared in Office 2000, ie OWC 9.0. In Office XP, OWC programming mode has been modified, which makes OWC XP (which is OWC 10) cannot be fully compatible with OWC 9.0. OWC 10 requires running in the ASP.NET environment, so the OWC 10 package must be installed on the ASP.NET server. Next, a natural question is: Where can I download the OWC 10 package? Surprisingly, it can download from Microsoft's website, address http://office.microsoft.com/downloads/2002/owc10.aspx, but to note that a certain Office must have installed on the web server. OWC 10 can be legally used in 2002. Fourth, OWC Operation Mechanism OWC is a set of Com (ActiveX) controls, covering features such as spreadsheets, charts, and data perspectives. It is often used as client technology, and the COM control is installed on the client PC. If you are using on the server, people are mainly interested in its chart drawing. With OWC, we can dynamically create a chart on the ASP.NET Web server, and then send the chart to the client as a GIF graphic. The client see is just a normal graphic file, but in "back", the graph file is actually generated when the ASP.NET is respond to the client request. Therefore, this technique does not have a special requirement for the client, as long as the GIF graphics can be displayed, even if Netscape and Opera do not have any problems. In this case, why is OWC this excellent Microsoft technology in the ASP.NET development field? Microsoft does not make market propaganda for OWC, plus confusing licenses, of course, makes many developers. Perhaps Microsoft believes that the product is not fully mature, the upcoming Office 2003 will bring OWC 11, and its programming mode will change. In addition, there is a wide range of use of Microsoft worry OWC technology to affect OFFICE. Furthermore, there are few programming instances for OWC. Microsoft Knowledge Base has several client examples and "traditional" ASP server-side examples, but cannot find examples of OWC 10 in the ASP.NET environment.
OWC newsgroup, microsoft.public.Office.developer.Web.components, the main discussion is also the application of the client. If you want to use OWC 10 in the ASP.NET environment, it is mainly to explore itself. It is for these reasons, so this article explores the practical application of this technology from a considerable broad perspective. 5. Installing OWC 10 on a web server To draw charts on an ASP.NET web server, first install the necessary software and modify some configurations. First, the web server must of course have an ASP.NET running environment. In addition to .NET Framework Redistributable, there must be a Gacutil program (which belongs to .NET Framework SDK) to configure the OWC control, that is, install the .NET Framework SDK tool. If the .NET Framework 1.1 Redistributable and SDK are installed to the default directory, the content of the PATH environment variable should include: c: / windows/microsoft.net/framework/v1.1.4322; c: / program files / microsoft.net / sdk / v1 .1 / bin. Next, install OWC 10 on the web server. OWC can download from Microsoft free download, as long as all default options are used when installing. Since OWC 10 is a COM technology, in order to allow .NET code to use OWC 10 components, you must also install Office XP Primary Interop Assembly (PIA), PIA can be downloaded from Microsoft website (http://msdn.microsoft.com/library /Default.asp?url=/library/en-us/dnoxpta/html/odc_oxppias.asp). The downloaded OXPPIA.exe is a compressed file, now extract it into a directory on the server, assumes that is C: / OXPPIA, then start a command window (note, make sure the path environment variable is properly set), go to C : / oxppia directory running register.bat. This command imports Office XP PIA into the global assembly buffer and modifies the registry settings. Note Observing the output of the register.bat command, confident that the gacutil command is active. If the PATH environment variable is set to be incorrect, PIA is not possible to import correctly. The readme document says the VS.NET command line environment should be used, but VS.NET may not be installed on the web server. At this time, it is necessary to modify the PATH environment variable (the effect is the same). Finally, add the following line code to the
Section, for .NET Framework 1.1, Machine.config files can be found in the C: /Windows / Microsoft.Net/framework/v1.1.4322/config directory:
6. OWC programming mode In order to display an OWC chart in the ASP.NET page, we will create a simple ASP.NET page that is the only use of the page to display charts. The name of the web page is getchart.aspx. To display a chart in the web page, you can use a standard
HTML tag, as shown below:
The GetChart.aspx page dynamically generates an OWC chart on the server, and then converts the chart to GIF graphics to the client. Therefore, at the client, getChart.aspx is equivalent to a GIF graphic. Let's analyze the getchart.aspx file. If you use the CodeBehind mechanism of the ASP.NET, getchart.aspx actually as a number ASP.NET directive:
<% @ Page language = "vb" autoeventwireup = "false"
Codebehind = "getchart.aspx.vb" inherits = "getchart"%>
<% @ Outputcache duration = "5" VarybyParam = "none"%>
Remember, the getchart.aspx file should return a GIF file to the client (ie, a binary data stream), so it does not contain any HTML, it is just a reference to the background file getchart.aspx.vb, getchart.aspx. The role of VB is to generate binary data streams of GIF graphics. The second line of code above determines the buffer survival time, here is 5 seconds. If the information in the database is frequently updated, such as weather data or stock prices, the buffer life must be reduced so that users can get the latest data each time you click the "Refresh" button. Let's take a look at the getchart.aspx.vb file, this is where all chart generation, graphical conversion operations are actually implemented!
Imports system
Imports system.data
Imports system.data.sqlclient
Imports system.Web
Imports System.Web.ui
Imports System.Web.ui.WebControls
Imports Microsoft.Office.Interop
Public class getchart
Inherits System.Web.ui.page
Protected Withevents Chartspace1 As Owc.Chartspace
Private sub page_load (byval sender as system.Object, _
BYVAL E as system.eventargs) Handles mybase.load
Response.buffer = TRUE
Response.contentType = "image / gif"
'SQL Server connection string
DIM Connectionstring As String = "Connecting Strings"
'Calculate SQL commands for data points
Dim counttext as string = "Select count (*) from owcdata
'Get the SQL command for data points
DIM Commandtext As string = "SELECT X, Y from Owcdata Order BY X"
'Define Database Connection Objects
DIM MyConnection As New SqlConnection (Connectionstring)
'Define a command object to calculate the number of data points
DIM MyCount As New SqlCommand (CountText, MyConnection) 'Defines a command object for getting data points.
DIM MyCommand As New Sqlcommand (CommandText, MyConnection)
'Define a DataReader object
Dim DataReader1 As SqldataReader
'i = index variable (used to fill in an array)
'Numpoints = Index indicating the number of data points in the database
'AX = array of x-value
'ay = array of y values
DIM I, NUMPOINTS, AX, AY
'Define chart objects, data series objects, OWC drawings must
Dim Chart1, Chart1_Series1
'Open database connection
MyConnection.Open ()
'Step 1: Calculate the number of data points, and save it
The 'Numpoints variable.
Numpoints = mycount.executescalar ()
'Debugging information
'Response.Write (Numpoints & "#")
'According to the number of data points, adjust the size of the array
Redim AX (Numpoints - 1)
Redim AY (Numpoints - 1)
'Step 2: Get the data point, return x and the AX array
'Y value
DataReader1 = mycommand.executeReader ()
i = 0
While DataReader1.Read
AX (i) = DataReader1.GetValue (0)
AY (i) = DataReader1.GetValue (1)
i = i 1
End while
DataReader1.close ()
'Debugging information
'For i = 0 to numpoints - 1
'Response.Write (AX (I) & "| & AY (i) &" # ")
'Next I
'Close database connection
MyConnection.Close ()
'Newly built a drawing space
Chartspace1 = new owc.chartspace ()
'New a chart in Chartspace1 drawing space
Chart1 = chartspace1.Charts.Add (0)
Add a data series in the chart1 chart
Chart1_series1 = chart1.seriescollection.Add (0)
'Define the Chart1_Series1 data series into XY scatter plots,
'Belt links and data points
Chart1_series1.type = _
Chartspace1.constants.chcharttypescatterLineMarkers
'Named Data Series (Name will be displayed in the legend)
Chart1_series1.Setdata (Owc.ChartdimensionSenum.chdimseriesnames, _
Owc.ChartSpecialDataSourcesenum.chdataral, "Series1")
'Implant data in array into chart
Chart1_series1.setdata (Owc.ChartdimensionSenum.chdiMxValues, _
Owc.chartspecialDataSourcesenum.chdataliteral, AX) Chart1_series1.SetData (Owc.ChartdimensionSenum.chdimyvalues, _
Owc.chartspecialDataSourcesenum.chdataral, AY)
'Setting the drawing space format
With chartspace1
'.Border.color = chartspace1.constants.chcolornone
End with
'Setting the chart format
With chart1
'.Seriescollection (0). Interior.color = "RosyBrown"
'.Plotarea.interior.color = "wheat"
.Haslegend = true
.Legend.position = _
Owc.ChartlegendPositionenum.chlegendPositionBottom
.Hastitle = true
.Title.caption = "xy scatter plot example"
.Axes (0) .haastitle = true
.Axes (0) .title.caption = "y axis"
.Axes (1) .haastitle = true
.Axes (1) .title.caption = "x-axis"
End with
Returns the chart in the form of GIF graphics
Response.binaryWrite (Chartspace1.GetPicture ("GIF", 500, 400))
Response.end
End Sub
END CLASS
Below is a running example:
Microsoft.Office.Interop namespace points to Office XP PIA, PIA should be installed on the web server in advance. Use the Office XP PIA OWC's DLL file when compiling the source code. If you are compiled with VS.NET, just join a reference to a Microsoft.Office.Interop.owc.dll file (located in the directory of the Office XP PIA file), if you are compiled from the command line, you must use / r in the following ways. parameter:
VBC / T: library /out:bin/getchart.dll /r :system.dll /r:system.web.dll
/R :system.data.dll
/R: / pPPIA/MICROSOFT.Office.Interop.owc.dll getchart.aspx.vb