asp.net2.0, easy. Statistical charts

Source: Internet
Author: User
Tags foreach empty reference tostring
Asp.net| Statistics | charts

This article describes how to draw bar charts , line charts , column charts , area charts and other common graphics.

Effect Chart :

Hands-on Tutorials:

Rationale:OWC is the abbreviation for Office Web compent, Microsoft's Office Web Component, which provides a flexible and basic mechanism for drawing graphics on the Web. In an intranet environment, if you can assume that there are specific browsers and powerful software such as IE6 and office 2000/xp/2003 on a client, you have the ability to use the Office Web Component to provide an interactive graphical development environment. In this mode, the client workstation will share a significant proportion of the entire task. In theory, the drawings that Excel can make can be drawn by OWC.

First step:
Right-click the site root reference. As shown in the figure:

Step Two:
Click "Add Reference" to pop up a window and add a OWC reference. As shown in the figure:

Point "OK".

Step Three:
Reference Microsoft.Office.Interop.Owc11 in code.

All code
Background Code :
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;

Using System.Data.SqlClient; Add Data Action Reference
Using microsoft.office.interop.owc11;//Add Office component references

public partial class OWCdrawing:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{

Connecting to a database and getting a specific string
String strseriesname = "Legend 1";
String connectstring = "server= (local);D atabase=web; Uid=sa; Pwd=sa ";
String Sql = "Select Month,allcount from Chart";
SqlConnection myconn = new SqlConnection (ConnectString);
MyConn.Open ();
SqlDataAdapter Da = new SqlDataAdapter (SQL, myconn);
DataSet ds = new DataSet ();
Da.fill (DS);

Storage month
string[] Monnum = new STRING[12];
Storing data
string[] Moncount = new STRING[12];
assigning values to arrays
for (int i = 0; I < ds. Tables[0]. Rows.Count; i++)
{
Monnum[i] = ds. Tables[0]. Rows[i][0]. ToString ();
Moncount[i] = ds. Tables[0]. ROWS[I][1]. ToString ();
}
Specify a specific string for the X axis to display the data
string strxdata = String.Empty;
foreach (String strdata in Monnum)
{
Strxdata + = strdata + "T";
}
string strydata = String.Empty;
Specify a specific string for the Y axis to correspond to the X axis
foreach (String strvalue in Moncount)
{
Strydata + = strvalue + "T";
}

Create a ChartSpace object to place a chart
ChartSpace layspace = new Chartspaceclass ();

Add a chart to the ChartSpace object
ChChart Insertchart = laySpace.Charts.Add (0);

Specifies the type of chart to draw. Types can be obtained by Owc.chartcharttypeenum enumeration values
Insertchart.type = chartcharttypeenum.chcharttypeline;//Line chart
Insertchart.type = chartcharttypeenum.chcharttypearea;//Area Map
Insertchart.type = chartcharttypeenum.chcharttypebarclustered;//Bar chart
Insertchart.type = chartcharttypeenum.chcharttypecolumnclustered;//Column Chart

Specify whether the chart requires a legend callout
Insertchart.haslegend = false;


Insertchart.hastitle = true;//Add a caption to a chart
InsertChart.Title.Caption = "2006 qingqing monthly expenses per month chronological";//title Name

Add a diagram description for the x,y axis
Insertchart.axes[0]. HasTitle = true;
Insertchart.axes[0]. Title.caption = "";/month
INSERTCHART.AXES[1]. HasTitle = true;
INSERTCHART.AXES[1]. Scaling.splitminimum = 200;
INSERTCHART.AXES[1]. title.caption = "Quantity";

Add a Series Series
INSERTCHART.SERIESCOLLECTION.ADD (0);

Given the name of the series series
Insertchart.seriescollection[0]. SetData (chartdimensionsenum.chdimseriesnames, + (int) chartspecialdatasourcesenum.chdataliteral, strSeriesName);

Given classification
Insertchart.seriescollection[0]. SetData (chartdimensionsenum.chdimcategories, + (int) chartspecialdatasourcesenum.chdataliteral, strXdata);

Given value
Insertchart.seriescollection[0]. SetData (chartdimensionsenum.chdimvalues, (int) chartspecialdatasourcesenum.chdataliteral, strydata);
The output file.
String Strabsolutepath = (Server.MapPath (".")) + "\\ShowData.gif";
Layspace.exportpicture (Strabsolutepath, "GIF", 400, 250);

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

Add a picture to the placeholder and display it on the page
String Strimagetag = "This. PLACEHOLDER1.CONTROLS.ADD (New LiteralControl (Strimagetag));
}
}

Foreground Code :
<%@ Page language= "C #" autoeventwireup= "true" codefile= "OWCdrawing.aspx.cs" inherits= "owcdrawing"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title> Network Base http://www.xrss.cn</title>
<body>
<form id= "Form1" runat= "Server" >
<div style= "Text-align:left" >
<table style= "width:600px" >
<tr>
&LT;TD colspan= "3" style= "height:20px" >
<strong> How to use OWC component drawing </strong></td> in asp.net2.0
</tr>
<tr>
&LT;TD colspan= "3" rowspan= "2" style= "height:21px" >
<asp:placeholder id= "PlaceHolder1" runat= "Server" ></asp:PlaceHolder>
</td>
</tr>
<tr>
</tr>
</table>

</div>
</form>
</body>

Database SQL Script :
Use [web]
Go
/****** object: Table [dbo]. [Chart] Script Date: 03/27/2007 22:26:00 ******/
SET ANSI_NULLS on
Go
SET QUOTED_IDENTIFIER ON
Go
CREATE TABLE [dbo]. [Chart] (
[ID] [int] IDENTITY (1,1) not NULL,
[Month] [smallint] Null
[Allcount] [INT] Null
) on [PRIMARY]

After the database has built the table, you have to manually imagine that there are 12 data, manually added, the final result is similar to the following figure:

Background Program Description:
The key is insertchart.type = chartcharttypeenum.chcharttypecolumnclustered;

you can in ChartChartTypeEnum and point out other methods. As shown in the figure:

The other types of diagrams are listed below:

Line chart:


Area chart:

Bar chart:

OWC What graphics can be painted, but also to draw three-dimensional, please try your own.

can refer to OWC manual, specific location:
C:\Program Files\Common Files\Microsoft Shared\Web Components\11\2052\owcvba11. CHM



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.