OWC组件生成饼状图

1、介绍和思路请参照上一篇
  http://www.cnblogs.com/XuebinDing/archive/2012/03/29/2422980.html
2、前台代码
  <img alt="" src="temp.gif" style="width: 600px; height: 450px" />
3、后台代码
  3.1、项目添加引用 Microsoft Office Web Components 11.0。
  3.2、在文件里面引用如下

using Microsoft.Office.Interop.Owc11;//Owc组件

using System.Data.Sql;
using System.Data.SqlClient;using System.Configuration;
using System.Data;

  3.3、具体程序代码

 1 namespace GenerateCharts
2 {
3 public partial class Pie : System.Web.UI.Page
4 {
5 protected void Page_Load(object sender, EventArgs e)
6 {
7 //创建图表空间
8 ChartSpace myspace = new ChartSpace();
9 //添加一个图表对象
10 ChChart mychart = myspace.Charts.Add(0);
11 mychart.Type = ChartChartTypeEnum.chChartTypePie;//饼形
12             //mychart.Type = ChartChartTypeEnum.chChartTypeBarClustered;//条形图
13             //mychart.Type = ChartChartTypeEnum.chChartTypeLine;//折线图
14             //mychart.Type = ChartChartTypeEnum.chChartTypeArea;//面积图
15             //mychart.Type = ChartChartTypeEnum.chChartTypeArea3D;//3D面积图
16             //mychart.Type = ChartChartTypeEnum.chChartTypeBar3D;//3D条形图
17             //mychart.Type = ChartChartTypeEnum.chChartTypeColumn3D;//3D柱形图
18             //...等
19
20             //设置表的相关属性
21 mychart.HasLegend = true;
22 mychart.HasTitle = true;
23 mychart.Title.Caption = "员工信息图表";
24
25 // 连接并打开数据库
26 SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
27 sqlcon.Open();
28 string strsqls = "select 籍贯, count(籍贯) as 人数 from tb_ygxx group by 籍贯";
29 SqlDataAdapter adsa = new SqlDataAdapter(strsqls, sqlcon);
30 DataSet adds = new DataSet();
31 adsa.Fill(adds);
32
33 if (adds.Tables[0].Rows.Count>0)
34 {
35 string strDataName = "";
36 string strData = "";
37 //添加图表块
38 mychart.SeriesCollection.Add(0);
39 //添加图表数据
40 for (int j = 0; j < adds.Tables[0].Rows.Count; j++)
41 {
42 if (j == adds.Tables[0].Rows.Count - 1)
43 {
44 strDataName += adds.Tables[0].Rows[j][0].ToString();
45 }
46 else
47 {
48 strDataName += adds.Tables[0].Rows[j][0].ToString() + "\t";
49 }
50 strData += adds.Tables[0].Rows[j][1].ToString() + "\t";
51 }
52 //设置图表块属性
53 mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strDataName);
54 mychart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strData);
55 //设置百分比
56 ChDataLabels labels = mychart.SeriesCollection[0].DataLabelsCollection.Add();
57 labels.HasPercentage = true;
58 }
59 sqlcon.Close();
60 //生成图表
61 myspace.ExportPicture(Server.MapPath(".") + @"\temp.gif", "gif", 600, 450);
62 }
63 }
64 }

4、效果图如下

posted @ 2012-03-29 13:55  XuebinDing  阅读(965)  评论(0编辑  收藏  举报