欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net中调用Office来制作3D统计图的实例代码

程序员文章站 2024-02-27 15:45:57
1、首先下载owc11 com组件 http://www.microsoft.com/downloads/details.aspx?familyid=7287252c-4...

1、首先下载owc11 com组件

http://www.microsoft.com/downloads/details.aspx?familyid=7287252c-402e-4f72-97a5-e0fd290d4b76&displaylang=en

2、注册owc11

  在工程中添加 c:/program files/common files/microsoft shared/web components/11  文件下的owc11.dll引用

  3、在工程中添加

  using owc11;

  4、开始coding  举例如下:

复制代码 代码如下:

   public class chartfactory
  {
  public chartfactory()
  {
  inittypemap();
  //
  // todo: 在此处添加构造函数逻辑
  //
  }
  protected system.web.ui.webcontrols.image imghondalineup;
  private string[] chartcategoriesarr;
  private string[] chartvaluesarr;
  private owc11.chartcharttypeenum charttype =  owc11.chartcharttypeenum.chcharttypecolumn3d;//默认值
  private static hashtable chartmap = new hashtable();
  private static string charttypech = "垂直柱状图" ;
  private static string charttitle = "";

  private void inittypemap()
  {
  chartmap.clear();
  owc11.chartcharttypeenum[] charttypes = new owc11.chartcharttypeenum[]{ chartcharttypeenum.chcharttypecolumnclustered,
  chartcharttypeenum.chcharttypecolumn3d,
  chartcharttypeenum.chcharttypebarclustered,
  chartcharttypeenum.chcharttypebar3d,
  chartcharttypeenum.chcharttypearea,
  chartcharttypeenum.chcharttypearea3d,
  chartcharttypeenum.chcharttypedoughnut,
  chartcharttypeenum.chcharttypelinestacked,
  chartcharttypeenum.chcharttypeline3d,
  chartcharttypeenum.chcharttypelinemarkers,
  chartcharttypeenum.chcharttypepie,
  chartcharttypeenum.chcharttypepie3d,
   chartcharttypeenum.chcharttyperadarsmoothline,
  chartcharttypeenum.chcharttypesmoothline};

 

  string[] charttypesch = new string [] {"垂直柱状统计图","3d垂直柱状统计图","水平柱状统计图","3d水平柱状统计图","区域统计图","3d区域统计图","中空饼图","折线统计图","3d折线统计图","折线带点统计图","饼图","3d饼图","网状统计图","弧线统计图"};


   for(int i=0;i<charttypes.length;i++)
  {
  chartmap.add(charttypesch[i],charttypes[i]);
  }
  }
  public chartspaceclass buildcharts ()
  {
  string chartcategoriesstr = string.join ("/t", chartcategoriesarr);
  string chartvaluesstr = string.join ("/t", chartvaluesarr);

owc11.chartspaceclass       ochartspace = new owc11.chartspaceclass ();

  // ------------------------------------------------------------------------
  // give pie and doughnut charts a legend on the bottom. for the rest of
  // them let the control figure it out on its own.
  // ------------------------------------------------------------------------

  charttype = (chartcharttypeenum)chartmap[charttypech];

  if (charttype == chartcharttypeenum.chcharttypepie ||
  charttype == chartcharttypeenum.chcharttypepie3d ||
  charttype == chartcharttypeenum.chcharttypedoughnut)
  {
  ochartspace.haschartspacelegend = true;
  ochartspace.chartspacelegend.position = chartlegendpositionenum.chlegendpositionbottom;
  }

  ochartspace.border.color = "blue";
  ochartspace.charts.add(0);
  ochartspace.charts[0].hastitle = true;
  ochartspace.charts[0].type = charttype;
  ochartspace.charts[0].chartdepth = 125;
  ochartspace.charts[0].aspectratio = 80;
  ochartspace.charts[0].title.caption = charttitle;
  ochartspace.charts[0].title.font.bold = true;

  ochartspace.charts[0].seriescollection.add(0);
  ochartspace.charts[0].seriescollection[0].datalabelscollection.add ();

  // ------------------------------------------------------------------------
  // if you're charting a pie or a variation thereof percentages make a lot
  // more sense than values...
  // ------------------------------------------------------------------------
   if (charttype == chartcharttypeenum.chcharttypepie ||
  charttype == chartcharttypeenum.chcharttypepie3d ||
  charttype == chartcharttypeenum.chcharttypedoughnut)
  {
  ochartspace.charts[0].seriescollection[0].datalabelscollection[0].haspercentage = true;
  ochartspace.charts[0].seriescollection[0].datalabelscollection[0].hasvalue = false;
  }
  // ------------------------------------------------------------------------
  // not so for other chart types where values have more meaning than
  // percentages.
  // ------------------------------------------------------------------------
  else
  {
  ochartspace.charts[0].seriescollection[0].datalabelscollection[0].haspercentage = false;
  ochartspace.charts[0].seriescollection[0].datalabelscollection[0].hasvalue = true;
  }
  
  // ------------------------------------------------------------------------
  // plug your own visual bells and whistles here
  // ------------------------------------------------------------------------
  ochartspace.charts[0].seriescollection[0].caption = string.empty;
  ochartspace.charts[0].seriescollection[0].datalabelscollection[0].font.name = "verdana";
  ochartspace.charts[0].seriescollection[0].datalabelscollection[0].font.size = 10;
  ochartspace.charts[0].seriescollection[0].datalabelscollection[0].font.bold = true;
  ochartspace.charts[0].seriescollection[0].datalabelscollection[0].font.color = "red";
  ochartspace.charts[0].seriescollection[0].datalabelscollection[0].position = chartdatalabelpositionenum.chlabelpositioncenter;
  
  if (charttype == chartcharttypeenum.chcharttypebarclustered ||
  charttype == chartcharttypeenum.chcharttypebar3d ||
  charttype == chartcharttypeenum.chcharttypecolumnclustered ||
  charttype == chartcharttypeenum.chcharttypecolumn3d)
  {
  ochartspace.charts[0].seriescollection[0].datalabelscollection[0].position = chartdatalabelpositionenum.chlabelpositionoutsideend;
  }
  
  ochartspace.charts[0].seriescollection[0].setdata (owc11.chartdimensionsenum.chdimcategories,
  convert.toint32(owc11.chartspecialdatasourcesenum.chdataliteral), chartcategoriesstr);


ochartspace.charts[0].seriescollection[0].setdata (owc11.chartdimensionsenum.chdimvalues,
  convert.toint32(owc11.chartspecialdatasourcesenum.chdataliteral), chartvaluesstr);

  return ochartspace;
  }

  #region  属性设置
  public string[] chartcategoriesarrvalue
  {
  get
  {
  return chartcategoriesarr;
  }
  set
  {
  chartcategoriesarr = value;
  }
  }

  public string[] chartvaluesarrvalue
  {
  get
  {
  return chartvaluesarr;

   }
  set
  {
  chartvaluesarr = value;
  }
  }
  public string charttypevalue
  {
  get
  {
  return charttypech;
  }
  set
  {
  charttypech = value;
  }
  }
  public string charttitlevalue
  {
  get
  {
  return charttitle;
  }
  set
  {
  charttitle = value;
  }
  }
  #endregion
  }

  //调用   首先需要在页面上放置一个image来显示产生的统计图

  public void showchart()
  {

  //初始化赋值
  chartfactory.chartcategoriesarrvalue = chartcategories;
  chartfactory.chartvaluesarrvalue = chartvalues;
  chartfactory.charttitlevalue = charttitle;
  chartfactory.charttypevalue = charttype;

  owc11.chartspaceclass ochartspace = chartfactory.buildcharts();
  string path = server.mappath(".") + @"/images/chart.jpeg";  //产生图片并保存 页可以是png gif图片
  ochartspace.exportpicture(path,"jpeg", 745, 500);
  image1.imageurl = path;  // 显示统计图
  }

  // 保存统计图请参照上一篇文章

  //由于每次生成的统计图都会覆盖原来的图片所以有必要的话可以用日期加时间的方式来作为图片的名字,但是这样将会产生很多图片需及时处理,如不需要只需取同名覆盖原来图片即可。