This article describes how to draw common graphs such as bar graphs, line graphs, column graphs, and area graphs.
Effect picture:
Hands-on tutorial:
Principle: OWC is the abbreviation of Office Web Compent, that is, Microsoft's Office Web component, which provides a flexible and basic mechanism for drawing graphics on the Web. In an intranet environment, if it can be assumed that there are specific browsers and some powerful software (such as IE6 and Office 2000/XP/2003) on the client, then it is possible to use Office Web components to provide an interactive graphics development environment. In this mode, the client workstation will share a large proportion of the entire task. In theory, all the graphs that Excel can do can be drawn through OWC.
Step 1:
Right-click the website root directory reference. as the picture shows:
Step 2:
Click "Add Reference" and a pop-up window will pop up to add a reference to OWC. as the picture shows:
Click "OK".
Step 3:
Quote Microsoft.Office.Interop.Owc11 in the 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 operation reference
using Microsoft.Office.Interop.Owc11;//Add Office component reference
public partial class OWCdrawing : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Connect to the database and get a specific string
string strSeriesName = "图例1";
string ConnectString = "Server=(local);DataBase=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);
//Store month
string[] MonNum = new string[12];
//Store data
string[] MonCount = new string[12];
//Assign a value to the array
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 the chart
ChartSpace laySpace = new ChartSpaceClass();
//Add a chart to the ChartSpace object
ChChart InsertChart = laySpace.Charts.Add(0);
//Specify the type of chart to be drawn. The type can be obtained through the OWC.ChartChartTypeEnum enumeration value
//InsertChart.Type = ChartChartTypeEnum.chChartTypeLine;//Line chart//
InsertChart.Type = ChartChartTypeEnum.chChartTypeArea;//Area chart//
InsertChart.Type = ChartChartTypeEnum.chChart/TypeBarClustered Bar chart
InsertChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;//Column chart
//Specify whether the chart needs legend annotation
InsertChart.HasLegend = false;
InsertChart.HasTitle = true;//Add a title to the chart
InsertChart.Title.Caption = "Monthly Expenditure Account of Qingqing Yueer in 2006";//Title Name
//Add icon descriptions for x and 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
InsertChart.SeriesCollection.Add(0);
//The name of the given series
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);
// Given category
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);
// Given value
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYdata);
//Output file.
string strAbsolutePath = (Server.MapPath(".")) + "// ShowData.gif";
laySpace.ExportPicture(strAbsolutePath, "GIF", 400, 250);
//Create the relative path of the GIF file.
string strRelativePath = "./ShowData.gif";
//Add the picture to the placeholder and display
string on the page strImageTag = "<IMG SRC='" + strRelativePath + "'/>";
this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));
}
}
前台代码:
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>清清月儿http://blog.csdn.net/21aspnet</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: left">
<table style="width: 600px">
<tr>
<td colspan="3" style="height: 20px">
<strong>怎么样在ASP.NET2.0中使用OWC组件画图</strong></td>
</tr>
<tr>
<td colspan="3" rowspan="2" style="height: 21px">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</td>
</tr>
<tr>
</tr>
</table>
</div>
</form>
</body>
</html>
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 table is created in the database, you have to manually assume that there are 12 pieces of data by yourself, and manually add them. The final result is similar to the following figure:
Background program description: The
most important thing is InsertChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;
You can click other methods after ChartChartTypeEnum. as the picture shows:
Listed below are other types of diagrams:
line chart:
Area chart:
Bar graph:
OWC can draw any graphics, and can also draw three-dimensional, please try it yourself.
You can refer to the OWC manual, the specific location:
C:/Program Files/Common Files/Microsoft Shared/Web Components/11/2052/OWCVBA11.CHM
Original address: http://blog.csdn.net/21aspnet/archive/2007/03/27/1543320.aspx