本例是在本人匯率案子中使用OWC生成報表的實例代碼﹐參照了几篇文章﹐共享一下﹐希望對大家有點幫助。
不過在本例中有個地方一直不能實現﹐就是能多曲線顯示的功能
﹐如知道的朋友請告知一下。源代碼如下﹕
頁面文件:ExchRateChart.aspx

<%
@ Page language="c#" Codebehind="ExchangeRateChart.aspx.cs" AutoEventWireup="false" Inherits="ExchRate.ExchRateWeb.ExchangeRateChart" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>匯率分析</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="css/ExchRate.css" rel="stylesheet">
<script language="javascript" src="css/buttons.js"></script>
</HEAD>
<body class="mainBody" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div align="center">
<table width="90%" height="90%">
<tr>
<td align="center"><asp:label id="lblTitle" Runat="server" Font-Bold="True" Font-Size="Large">匯率分析</asp:label></td>
</tr>
<tr>
<td class="tbDivider" noWrap align="center"><asp:label id="lblExchangeRateCategory" Runat="server">匯率類別</asp:label><asp:dropdownlist id=drpExchangeRateCategory DataTextField="ErcName" DataValueField="ErcNo" DataSource="<%#dtExchangeRateCategory%>" Runat="server">
</asp:dropdownlist><asp:label id="lblMoneyCategory" Runat="server">幣別</asp:label><asp:dropdownlist id=drpMoneyCategory DataTextField="McName" DataValueField="McNo" DataSource="<%#dtMoneyCategory%>" Runat="server">
</asp:dropdownlist><asp:label id="lblStartErDate" Runat="server">起止日期</asp:label><asp:textbox id="txtStartErDate" Runat="server" MaxLength="50" ReadOnly="True"></asp:textbox><asp:button id="btnStartErDate" Runat="server" Text="
"></asp:button><asp:label id="lblEndErDate" Runat="server">結束日期</asp:label><asp:textbox id="txtEndErDate" Runat="server" MaxLength="50" ReadOnly="True"></asp:textbox><asp:button id="btnEndErDate" Runat="server" Text="
"></asp:button><asp:button id="btnQuery" Runat="server" Text="查詢"></asp:button></td>
</tr>
<tr>
<td><hr size="1">
</td>
</tr>
<tr>
<td bgcolor="inactivecaptiontext" align="center" width="100%" height="100%">
<asp:Image id="imgChart" runat="server" ></asp:Image>
</td>
</tr>
</table>
</div>
</form>
</body>
</HTML>
代碼文件﹕ExchRateChart..aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using ExchRate.Common;
using ExchRate.Common.Data;
using ExchRate.DataAccess;
using OWC11;
namespace ExchRate.ExchRateWeb

{
/**////
///
///
public class ExchangeRateChart : System.Web.UI.Page
{

private ExchangeRateBodies exchangeRateBodies;
protected System.Web.UI.WebControls.DropDownList drpExchangeRateCategory;
protected System.Web.UI.WebControls.DropDownList drpMoneyCategory;
protected System.Web.UI.WebControls.Label lblStartErDate;
protected System.Web.UI.WebControls.TextBox txtStartErDate;
protected System.Web.UI.WebControls.Label lblEndErDate;
protected System.Web.UI.WebControls.TextBox txtEndErDate;
protected System.Web.UI.WebControls.Button btnStartErDate;
protected System.Web.UI.WebControls.Button btnEndErDate;
protected System.Web.UI.WebControls.Label lblExchangeRateCategory;
protected System.Web.UI.WebControls.Label lblMoneyCategory;
protected System.Web.UI.WebControls.Button btnQuery;
private ExchangeRateBodyData exchangeRateBodyData;
private ExchangeRateCategories exchangeRateCategories;
protected System.Web.UI.WebControls.Image imgChart;
protected System.Web.UI.WebControls.Label lblTitle;
private MoneyCategories moneyCategories;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
this.drpExchangeRateCategory.DataBind();
this.drpMoneyCategory.DataBind();
this.txtStartErDate.Text = DateTime.Now.AddDays(-6).ToString("yyyy/MM/dd");
this.txtEndErDate.Text = DateTime.Now.ToString("yyyy/MM/dd");
this.imgChart.Visible = false;
//設置時間選擇按鈕
this.btnStartErDate.Attributes.Add("OnClick","OpenModalDialog(txtStartErDate,'date.aspx','','330px','250px')");
this.btnEndErDate.Attributes.Add("OnClick","OpenModalDialog(txtEndErDate,'date.aspx','','330px','250px')");;
}

}

取得匯率類型#region 取得匯率類型
public DataTable dtExchangeRateCategory
{
get
{
exchangeRateCategories = new ExchangeRateCategories();
return exchangeRateCategories.GetExchangeRateCategory().Tables[ExchangeRateCategoryData.EXCHANGERATECATEGORY_TABLENAME];
}
}
#endregion

取得所有貨幣幣種#region 取得所有貨幣幣種
public DataTable dtMoneyCategory
{
get
{
moneyCategories = new MoneyCategories();
return moneyCategories.GetMoneyCategory().Tables[MoneyCategoryData.MONEYCATEGORY_TABLENAME];
}
}
#endregion
private void MakeLineChart(string chartTitle, string seriesTitle ,string ercNo, string mcNo, string startErDate, string endErDate)
{
exchangeRateBodies = new ExchangeRateBodies();
exchangeRateBodyData = exchangeRateBodies.GetExchangeRateBodyToChart(ercNo, mcNo, startErDate, endErDate);
StringWriter sw = new StringWriter();
XmlDocument xDoc = new XmlDocument();
exchangeRateBodyData.WriteXml(sw);
xDoc.LoadXml(sw.ToString());
sw.Close(); 
System.Xml.XmlNodeList nodes;
nodes = xDoc.ChildNodes.Item(0).ChildNodes;
int nCount = nodes.Count;
//string[] aNames = new string[nCount];
string[] aCategories = new string[nCount];
string[] aValues = new string[nCount];
string names=String.Empty;
string values =String.Empty;
string categories = string.Empty;
for(int i=0;i<nCount;i++)
{
//aNames[i]= nodes.Item(i).ChildNodes.Item(0).InnerText;
aCategories[i] = nodes.Item(i).ChildNodes.Item(0).InnerText;
aValues[i]= nodes.Item(i).ChildNodes.Item(1).InnerText;
}

//names= String.Join("\t", aNames);
categories =String.Join(" \t",aCategories);
values= String.Join("\t", aValues);
//創建ChartSpace對象來放置圖表
ChartSpace chartSpace = new ChartSpaceClass();
//在ChartSpace對象中添加圖表,Add方法返回Chart對象
ChChart chart = chartSpace.Charts.Add(0);

確定曲線類型﹐并確定區別不同的曲線名稱#region 確定曲線類型﹐并確定區別不同的曲線名稱
//提定圖表的類型為線性圖
chart.Type = ChartChartTypeEnum.chChartTypeSmoothLine;
//提定圖表是否需要圖例
chart.HasLegend = true;
//給定標題
chart.HasTitle = true;
chart.Title.Caption = chartTitle;
//chart.SetData(ChartDimensionsEnum.chDimSeriesNames, (int)ChartSpecialDataSourcesEnum.chDataLiteral, names);
chart.SetData(ChartDimensionsEnum.chDimSeriesNames, (int)ChartSpecialDataSourcesEnum.chDataLiteral, seriesTitle);
#endregion

確定分類(X)軸標簽與數值(Y)軸標簽所對應的字段#region 確定分類(X)軸標簽與數值(Y)軸標簽所對應的字段
foreach(ChSeries series in chart.SeriesCollection)
{
//設置顯示值屬性
ChDataLabels dataLabels = series.DataLabelsCollection.Add();
dataLabels.HasValue = true;
dataLabels.Font.Size = 9;
dataLabels.Font.Bold = true;
dataLabels.Font.Color = "red";
dataLabels.Position = ChartDataLabelPositionEnum.chLabelPositionCenter;
//給定分類
series.SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, categories);
//給定值
series.SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, values);
}
#endregion

對坐標軸的屬性進行設置#region 對坐標軸的屬性進行設置
//遍歷坐標軸集合
foreach(ChAxis axis in chart.Axes)
{
//顯示軸標題
axis.HasTitle = true;
//對分類(x)軸進行設置
string s = axis.Type.ToString();
if(axis.Type == ChartAxisTypeEnum.chTimescaleAxis)
{
//顯示x軸刻度標簽
axis.HasTickLabels = true;
//標簽顯示位置
axis.Position = ChartAxisPositionEnum.chAxisPositionBottom;
//標簦標題文字顏色
axis.Title.Font.Color = "blue";
//標簽標題文字大小
axis.Title.Font.Size = 9;
axis.Title.Caption = "日期";
}
else
//對數值(y)軸進行設置
{
//y軸主要網絡線的顏色
axis.MajorGridlines.Line.Color = "silver";
//不顯示y軸主要刻度標記
axis.MajorTickMarks = ChartTickMarkEnum.chTickMarkNone;
//顯示y軸刻度標簽
axis.HasTickLabels = true;
//y軸標題的文字顏色
axis.Title.Font.Color = "blue";
//標題文字大小
axis.Title.Font.Size = 9;
//標簽標題
axis.Title.Caption = "匯率";
}
}
#endregion

存儲為文件#region 存儲為文件
// //輸出成GIF文件
// string strAbsolutePath = (Server.MapPath(".")) + @"\chart\tempFile.gif";
// chartSpace.ExportPicture(strAbsolutePath, "GIF", 800, 600);
// //創建GIF文件的相對路徑
// string strRelativePath = "./chart/tempFile.gif";
// //生成顯示圖片的
// string strImageTag = "
// //把圖片添加到PlaceHolder
// placeHolder.Controls.Add(new LiteralControl(strImageTag));
#endregion
# region 不存儲為文件﹐一種更好的方式
byte[] byteArr = (byte[])chartSpace.GetPicture("png", 800, 600);
HttpContext ctx = HttpContext.Current;
string chartID = Guid.NewGuid().ToString();
ctx.Session[chartID] = byteArr;
imgChart.ImageUrl = string.Concat("chart.ashx?", chartID);
imgChart.Visible = true;
#endregion
}


Web Form 設計工具產生的程式碼#region Web Form 設計工具產生的程式碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 此為 ASP.NET Web Form 設計工具所需的呼叫。
//
InitializeComponent();
base.OnInit(e);
}

/**////
/// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
/// 這個方法的內容。
///
private void InitializeComponent()
{
this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnQuery_Click(object sender, System.EventArgs e)
{
string chartTitle = this.drpExchangeRateCategory.SelectedItem.Text.Trim() + "分析圖";
string seriesTitle = this.drpMoneyCategory.SelectedItem.Text.Trim();
string ercNo = this.drpExchangeRateCategory.SelectedItem.Value.Trim();
string mcNo = this.drpMoneyCategory.SelectedItem.Value.Trim();
string startErDate = this.txtStartErDate.Text.Trim();
string endErDate = this.txtEndErDate.Text.Trim();
MakeLineChart(chartTitle, seriesTitle, ercNo, mcNo, startErDate, endErDate );
}

}
}

<%
@ WebHandler language="c#" class="ExchRate.ExchRateWeb.ChartHandler"Codebehind="Chart.ashx.cs" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.IO;
namespace ExchRate.ExchRateWeb

{
/**//// <summary>
/// chart 的摘要描述。
/// </summary>
public class ChartHandler : IHttpHandler, IReadOnlySessionState
{

public void ProcessRequest(HttpContext context)
{
string chartID = context.Request.QueryString[0];
Array arr = (Array)context.Session[chartID];
context.ClearError();
context.Response.Expires = 0;
context.Response.Buffer = true;
context.Response.Clear();
MemoryStream memStream = new MemoryStream((byte[])arr);
memStream.WriteTo(context.Response.OutputStream);
memStream.Close();
context.Response.ContentType = "image/png";
context.Response.StatusCode = 200;
context.Response.End();
}
public bool IsReusable
{
get
{
return true;
}
}

}
}