1.添加Office Web Components 引用
2.test.aspx:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
3.
//连接数据库并获取特定字符串
string strSeriesName = "图例1";
Test test = new Test();
DataTable dt = test.GetAll();//测试方法
string[] MonNum = new string[5];
string[] MonCount = new string[5];
//为数组赋值
for (int i = 0; i < dt.Rows.Count; i++)
{
MonNum[i] = dt.Rows[i]["UserName"].ToString();
MonCount[i] = dt.Rows[i]["Pwd"].ToString();
}
//为x轴指定特定字符串,以便显示数据
string strXdata = String.Empty;
foreach (string strData in MonNum)
{
strXdata += strData + "\t";
}
string strYdata = String.Empty;
//为y轴指定特定的字符串,以便与x轴相对应
foreach (string strValue in MonCount)
{
strYdata += strValue + "\t";
}
//创建ChartSpace对象来放置图表
//ChartSpace laySpace = new ChartSpaceClass();
ChartSpace laySpace = new ChartSpace();
//在ChartSpace对象中添加图表
ChChart InsertChart = laySpace.Charts.Add(0);
//指定绘制图表的类型。类型可以通过OWC.ChartChartTypeEnum枚举值得到
//InsertChart.Type = ChartChartTypeEnum.chChartTypeLine;//折线图
//InsertChart.Type = ChartChartTypeEnum.chChartTypeArea;//面积图
//InsertChart.Type = ChartChartTypeEnum.chChartTypeBarClustered;//条形图
InsertChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;//柱形图
//指定图表是否需要图例标注
InsertChart.HasLegend = true;//图例1
InsertChart.HasTitle = true;//为图表添加标题
InsertChart.Title.Caption = "Test";//标题名称
//为x,y轴添加图示说明
InsertChart.Axes[0].HasTitle = true;
InsertChart.Axes[0].Title.Caption = "用户名";
InsertChart.Axes[1].HasTitle = true;
InsertChart.Axes[1].Scaling.SplitMinimum = 50;
InsertChart.Axes[1].Title.Caption = "密码";
//添加一个series系列
InsertChart.SeriesCollection.Add(0);
//给定series系列的名字
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);
//给定分类
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);
//给定值
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYdata);
//输出文件.
string strAbsolutePath = (Server.MapPath(".")) + "\\Test\\ShowData.gif";
laySpace.ExportPicture(strAbsolutePath, "GIF", 400, 250);
//创建GIF文件的相对路径.
string strRelativePath = "Test/ShowData.gif";
//把图片添加到placeholder中,并在页面上显示
string strImageTag = "<IMG SRC='" + strRelativePath + "'/>";
this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));