How to draw a chart with OWC in asp.net (2)

Source: Internet
Author: User
Tags command line query relative
Asp.net| Chart OWC.aspx.vb:

Imports System
Imports OWC
Imports System.Web.UI

Public Class OWC
Inherits System.Web.UI.Page
Protected WithEvents Chartholder as System.Web.UI.WebControls.PlaceHolder

#Region the code generated by the Web Forms Designer

' This call is required by the Web Forms Designer.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()

End Sub

Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Mybase.init
' CodeGen: This method call is required by the Web Forms Designer
' Do not modify it using the Code Editor.
InitializeComponent ()
End Sub

#End Region

Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
' Create a ChartSpace object to place the chart
Dim objcspace as ChartSpace = New Chartspaceclass ()
' Add a chart to the ChartSpace object, the Add method returns the chart object
Dim objchart as WCChart = OBJCSPACE.CHARTS.ADD (0)
' Specifies the type of chart. Type is obtained by Owc.chartcharttypeenum enumeration value
Objchart.type = chartcharttypeenum.chcharttypecolumnclustered

' Specify whether the chart needs a legend
Objchart.haslegend = True

' Given title
Objchart.hastitle = True
ObjChart.Title.Caption = "1-6 said data distribution map"

' Illustration of a given x,y axis
Objchart.axes (0). HasTitle = True
Objchart.axes (0). title.caption = "Y axis: Quantity"
Objchart.axes (1). HasTitle = True
Objchart.axes (1). Title.caption = "X-axis: Month"

' Compute data
' *categories and values can be represented by a tab-separated string.
Dim strseriesname as String = "Legend 1"
Dim strcategory as String = "1" + Controlchars.tab + "2" + Controlchars.tab _
+ "3" + Controlchars.tab + "4" + Controlchars.tab + "5" + Controlchars.tab _
+ "6" + Controlchars.tab
Dim strvalue as String = "9" + Controlchars.tab + "8" + Controlchars.tab _
+ "4" + Controlchars.tab + "Ten" + Controlchars.tab + "+" + controlchars.tab _
+ "6" + Controlchars.tab

' Add a series
OBJCHART.SERIESCOLLECTION.ADD (0)

' Given the name of series
Objchart.seriescollection (0). SetData (chartdimensionsenum.chdimseriesnames,_
Chartspecialdatasourcesenum.chdataliteral, Strseriesname)

' Given classification
Objchart.seriescollection (0). SetData (chartdimensionsenum.chdimcategories,_
Chartspecialdatasourcesenum.chdataliteral, Strcategory)

' Given value
Objchart.seriescollection (0). SetData (chartdimensionsenum.chdimvalues,_
Chartspecialdatasourcesenum.chdataliteral, strvalue)
' Output into a GIF file.
Dim Strabsolutepath as String = (Server.MapPath (".")) + "\images\test.gif"
Objcspace.exportpicture (Strabsolutepath, "GIF", 600, 350)

' Create a relative path to the GIF file.
Dim Strrelativepath as String = "Images/test.gif"
' Add the picture to the placeholder.
Dim Strimagetag as String = "
CHARTHOLDER.CONTROLS.ADD (New LiteralControl (Strimagetag))
End Sub

End Class

The following is a C # version of OWC.asp.cs

Ublic class Owc:System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder Chartholder;

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
Create a ChartSpace object to place a chart
OWC. ChartSpace objcspace = new OWC. Chartspaceclass ();

Add a chart to the ChartSpace object, and the Add method returns the chart object
OWC. WCChart objchart = objCSpace.Charts.Add (0);

Specifies the type of chart. Type is obtained by Owc.chartcharttypeenum enumeration value
Objchart.type = OWC. chartcharttypeenum.chcharttypecolumnclustered;

Specify whether a chart requires a legend
Objchart.haslegend = true;

Given title
Objchart.hastitle = true;
Objchart.title.caption= "first-half distribution map";

Illustrated illustration of a given x,y axis
Objchart.axes[0]. HasTitle = true;
Objchart.axes[0]. Title.caption = "Y: Quantity";
OBJCHART.AXES[1]. HasTitle = true;
OBJCHART.AXES[1]. Title.caption = "X: Month";

Calculate data
/*categories and values can be represented by a tab-separated string.
String strseriesname = "Legend 1";
String strcategory = "1" + ' t ' + "2" + ' t ' + "3" + ' \ T ' + "4" + ' \ T ' + "5" + ' t ' + "6" + ' \ t ';
string strvalue = "9" + ' t ' + "8" + ' \ T ' + "4" + ' \ T ' + "+" + ' t ' + "+" + ' t ' + "6" + ' \ t ';

Add a series
OBJCHART.SERIESCOLLECTION.ADD (0);

Given the name of the series
Objchart.seriescollection[0]. SetData (OWC. Chartdimensionsenum.chdimseriesnames,
+ (int) OWC. Chartspecialdatasourcesenum.chdataliteral, Strseriesname);

Given classification
Objchart.seriescollection[0]. SetData (OWC. Chartdimensionsenum.chdimcategories,
+ (int) OWC. Chartspecialdatasourcesenum.chdataliteral, strcategory);

Given value
Objchart.seriescollection[0]. SetData
(OWC. Chartdimensionsenum.chdimvalues,
(int) OWC. Chartspecialdatasourcesenum.chdataliteral, strvalue);
Output as a GIF file.
String Strabsolutepath = (Server.MapPath (".")) + "\\i\\test.gif";
Objcspace.exportpicture (Strabsolutepath, "GIF", 600, 350);

Creates a relative path to a GIF file.
String Strrelativepath = "./i/test.gif";

Add the picture to the placeholder.
String Strimagetag = "CHARTHOLDER.CONTROLS.ADD (New LiteralControl (Strimagetag));
}

#region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion
}


If you use a Ado.net DataSet object, you can generate a tab-separated string:

strvalue + = (nodes. Item (j). Childnodes.item (0). InnerText + ' t ');
Strcategory + = (nodes. Item (j). Childnodes.item (1). InnerText + ' t ');




The Microsoft.Office.Interop namespace points to Office XP Pia,pia should be installed on the Web server beforehand. DLL files for office XP PIA OWC are used when compiling the source code. If you compile with vs.net, just add a reference to a Microsoft.Office.Interop.Owc.dll file (located in the directory that unlocks the Office XP Pia file), and if you compile from the command line, you must use the/r: parameter in the following ways:

Vbc/t:library/out:bin\getchart.dll/r:system.dll/r:system.web.dll
/r:system.data.dll
/r:c:\oxppia\microsoft.office.interop.owc.dll Getchart.aspx.vb



The code above has a lot to mention. First, we assume that the data source is located in the MSSQL database Owcdemo, the database has a Owcdata table, and the Owcdata table has two numeric columns, x and y respectively. The goal of Getchart.aspx is to get records from a database and then describe the data with a scatter chart (XY).

The data points of the OWC chart cannot be obtained directly from the ASP.net dataset, so we first load the data of the database into an array, and then fill in the data points of the OWC chart with the data of the array. If you want to improve on this example, it's a good idea to develop a asp.net server control that gets data from an abstract data source, including a DataSet object, an XML file, or an array, and generates an XY (scatter) chart.

DataReader is faster and more efficient than a dataset, but first we have to determine the number of records in the database and resize the array based on the number of records. To do this, we first take a SQL SELECT COUNT (*) query to get the number of records, then define the array size, and finally get the database records with the second SQL select query.

If we want the points of a scatter chart to be connected by a polyline, the records must be sorted by the x axis, which is implemented by an SQL ORDER BY clause.

The OWC chart is built on the drawing space. A drawing space can contain one or more charts, and each chart can have one or more data series. When generating OWC charts, we first create a drawing space, add a chart to the drawing space, set the type of the chart, add the data series, and finally fill in the data series with the data from the array.

In addition, we can set (optional) Various layout parameters, such as color, coordinate title, chart title, legend, and so on. OWC provides hundreds of layout parameters, we can adjust the chart at will. Of course, for different chart types, the drawing pattern is slightly different, for example, the parameter setting method for pie and scatter charts is different. There are OWC help files in the OWC 10 installation package detailing the OWC chart model.

Finally, the Response.BinaryWrite parameter specifies the type of graphics (GIF) to output, and the width and height of the graphic, in pixels. Here, we can scale the graph generated from the OWC chart as needed.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.