使用owc组件生成图表的设计思路
引用owc组件-->创建图表控件-->创建图表对象-->设置图表属性-->获取图表数据-->生成图片-->在web页面中显示图片
owc下载https://www.microsoft.com/zh-cn/download/details.aspx?id=22276
添加引用,选择“COM”页,选择“Microsoft Office Web Components 11.0”,点击“确定”按钮
using System.Data.Sql;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Owc11;
首先,创建数据库,表里两个字段,name,jiguan,然后我们创建一个temp.jpg图片,然后把它放在根目录
在每个.aspx页面加入以下代码(excel.aspx除外)
<table>
<tr>
<td><img alt="" src="temp.jpg" style="width:600px;height:450px"/></td>
</tr>
</table>
下面是
柱形图
public partial class histogram : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//创建图表控件
ChartSpace myspace = new ChartSpace();
//添加一个图表对象
ChChart mychart = myspace.Charts.Add(0);
//设置图表类型为柱形
mychart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;
//设置图表的相关属性
mychart.HasLegend = true;//添加图列
mychart.HasTitle = true;//添加主题
mychart.Title.Caption = "员工信息图表";//设置主题内容
//设置x,y轴
mychart.Axes[0].HasTitle = true;
mychart.Axes[0].Title.Caption = "籍贯";
mychart.Axes[1].HasTitle = true;
mychart.Axes[1].Title.Caption = "人数";
//连接数据库
String str = ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(str);
con.Open();
String sel = "select jiguan,count(jiguan) as number from xx group by jiguan";
SqlDataAdapter adsa = new SqlDataAdapter(sel, con);
DataSet adds = new DataSet();
adsa.Fill(adds);
if (adds.Tables[0].Rows.Count > 0)
{
//添加图表块
for (int i = 0; i < adds.Tables[0].Rows.Count; i++)
{
mychart.SeriesCollection.Add(0);
}
for (int j = 0; j < adds.Tables[0].Rows.Count; j++)
{
//设置图表的属性
mychart.SeriesCollection[j].Caption = adds.Tables[0].Rows[j][0].ToString();
mychart.SeriesCollection[j].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, adds.Tables[0].Rows[j][0].ToString());
mychart.SeriesCollection[j].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, Convert.ToInt32(adds.Tables[0].Rows[j][1].ToString()));
}
}
con.Close();
myspace.ExportPicture(Server.MapPath(".") + @"\temp.jpg", "jpg", 600, 450);
}
}
接下来是饼图
public partial class pie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//创建图表控件
ChartSpace myspace = new ChartSpace();
//添加一个图表对象
ChChart mychart = myspace.Charts.Add(0);
//设置图表类型为柱形
mychart.Type = ChartChartTypeEnum.chChartTypePie;
//设置图表的相关属性
mychart.HasLegend = true;//添加图列
mychart.HasTitle = true;//添加主题
mychart.Title.Caption = "员工信息图表";//设置主题内容
//连接数据库
String str = ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(str);
con.Open();
String sel = "select jiguan,count(jiguan) as number from xx group by jiguan";
SqlDataAdapter adsa = new SqlDataAdapter(sel, con);
DataSet adds = new DataSet();
adsa.Fill(adds);
if (adds.Tables[0].Rows.Count > 0)
{
string strDataName = "";
string strData = "";
//添加图表块
mychart.SeriesCollection.Add(0);
//添加图表数据
for (int j = 0; j < adds.Tables[0].Rows.Count; j++)
{
if (j == adds.Tables[0].Rows.Count - 1)
{
strDataName += adds.Tables[0].Rows[j][0].ToString();
}
else
{
strDataName += adds.Tables[0].Rows[j][0].ToString() + "\t";
}
strData += adds.Tables[0].Rows[j][1].ToString() + "\t";
}
//设置图表的属性
mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral,strDataName);
mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strData);
//设置百分比
ChDataLabels labels=mychart.SeriesCollection[0].DataLabelsCollection.Add();
labels.HasPercentage=true;
}
con.Close();
myspace.ExportPicture(Server.MapPath(".") + @"\temp.jpg", "jpg", 600, 450);
}
}
再接下来是折线图
public partial class line: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//创建ChartSpace对象来放置图表
ChartSpace laySpace=new ChartSpace();
//在ChartSpace对象中添加图表
ChChart InsertChart=laySpace.Charts.Add(0);
//指定绘制图表类型
InsertChart.Type=ChartChartTypeEnum.chChartTypeLine;//折线图
//指定图表是否需要图例标注
InsertChart.HasLegend=false;
InsertChart.HasTitle=true;//为图表添加标题
InsertChart.Title.Caption = "员工信息表";
//为x,y轴添加图示说明
InsertChart.Axes[0].HasTitle=true;
InsertChart.Axes[0].Title.Caption="籍贯";
InsertChart.Axes[1].HasTitle=true;
InsertChart.Axes[1].Title.Caption="人数";
//连接数据库
String str = ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(str);
con.Open();
String sel = "select jiguan,count(jiguan) as number from xx group by jiguan";
SqlDataAdapter adsa = new SqlDataAdapter(sel, con);
DataSet adds = new DataSet();
adsa.Fill(adds);
//为x,y轴指定特定字符串,以便显示数据
string strXdata = String.Empty;
string strYdata = String.Empty;
if (adds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < adds.Tables[0].Rows.Count;i++)
{
strXdata = strXdata + adds.Tables[0].Rows[i][0].ToString() + "\t";
strYdata = strYdata + adds.Tables[0].Rows[i][1].ToString() + "\t";
}
}
//添加图表块
InsertChart.SeriesCollection.Add(0);
//设置图表的属性
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);
InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral,strYdata);
con.Close();
laySpace.ExportPicture(Server.MapPath(".") + @"\temp.jpg", "jpg", 600, 450);
}
}
最后是做成一个excel表
public partial class excel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//创建图表控件
ChartSpace laySpace = new ChartSpace();
//添加一个表容器
SpreadsheetClass myexcel = new SpreadsheetClass();
Worksheet mysheet = myexcel.ActiveSheet;
//添加表标题
myexcel.Cells[1, 1] = "籍贯";
myexcel.Cells[1, 2] = "人数";
//连接数据库
String str = ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(str);
con.Open();
String sel = "select jiguan,count(jiguan) as number from xx group by jiguan";
SqlDataAdapter adsa = new SqlDataAdapter(sel, con);
DataSet adds = new DataSet();
adsa.Fill(adds);
if (adds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < adds.Tables[0].Rows.Count; i++)
{
mysheet.Cells[i + 2, 1] = adds.Tables[0].Rows[i][0].ToString();
mysheet.Cells[i + 2, 2] = adds.Tables[0].Rows[i][1].ToString();
}
//导出表格
myexcel.Export(Server.MapPath(".")+@"\test.xls", SheetExportActionEnum.ssExportActionOpenInExcel, SheetExportFormat.ssExportXMLSpreadsheet);
}
con.Close();
}
}
利用owc做图表到此为止就完成了。代码在https://github.com/1126048156/owc.git