zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

Net实例ASP3D代码 调用 制作 Office
2023-06-13 09:15:07 时间

1、首先下载owc11COM组件

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

2、注册owc11

  在工程中添加C:/ProgramFiles/CommonFiles/MicrosoftShared/WebComponents/11 文件下的owc11.dll引用

  3、在工程中添加

  usingOWC11;

  4、开始coding 举例如下:

复制代码代码如下:


   publicclassChartFactory
  {
  publicChartFactory()
  {
  InitTypeMap();
  //
  //TODO:在此处添加构造函数逻辑
  //
  }
  protectedSystem.Web.UI.WebControls.ImageimgHondaLineup;
  privatestring[]chartCategoriesArr;
  privatestring[]chartValuesArr;
  privateOWC11.ChartChartTypeEnumchartType= OWC11.ChartChartTypeEnum.chChartTypeColumn3D;//默认值
  privatestaticHashtablechartMap=newHashtable();
  privatestaticstringchartTypeCh="垂直柱状图";
  privatestaticstringchartTitle="";

  privatevoidInitTypeMap()
  {
  chartMap.Clear();
  OWC11.ChartChartTypeEnum[]chartTypes=newOWC11.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=newstring[]{"垂直柱状统计图","3D垂直柱状统计图","水平柱状统计图","3D水平柱状统计图","区域统计图","3D区域统计图","中空饼图","折线统计图","3D折线统计图","折线带点统计图","饼图","3D饼图","网状统计图","弧线统计图"};


   for(inti=0;i<chartTypes.Length;i++)
  {
  chartMap.Add(chartTypesCh[i],chartTypes[i]);
  }
  }
  publicChartSpaceClassBuildCharts()
  {
  stringchartCategoriesStr=String.Join("/t",chartCategoriesArr);
  stringchartValuesStr=String.Join("/t",chartValuesArr);

OWC11.ChartSpaceClass      oChartSpace=newOWC11.ChartSpaceClass();

  //------------------------------------------------------------------------
  //Givepieanddoughnutchartsalegendonthebottom.Fortherestof
  //themletthecontrolfigureitoutonitsown.
  //------------------------------------------------------------------------

  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();

  //------------------------------------------------------------------------
  //Ifyou"rechartingapieoravariationthereofpercentagesmakealot
  //moresensethanvalues...
  //------------------------------------------------------------------------
   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;
  }
  //------------------------------------------------------------------------
  //Notsoforothercharttypeswherevalueshavemoremeaningthan
  //percentages.
  //------------------------------------------------------------------------
  else
  {
  oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].HasPercentage=false;
  oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].HasValue=true;
  }
  
  //------------------------------------------------------------------------
  //Plugyourownvisualbellsandwhistleshere
  //------------------------------------------------------------------------
  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);

  returnoChartSpace;
  }

  #region 属性设置
  publicstring[]chartCategoriesArrValue
  {
  get
  {
  returnchartCategoriesArr;
  }
  set
  {
  chartCategoriesArr=value;
  }
  }

  publicstring[]chartValuesArrValue
  {
  get
  {
  returnchartValuesArr;

   }
  set
  {
  chartValuesArr=value;
  }
  }
  publicstringchartTypeValue
  {
  get
  {
  returnchartTypeCh;
  }
  set
  {
  chartTypeCh=value;
  }
  }
  publicstringchartTitleValue
  {
  get
  {
  returnchartTitle;
  }
  set
  {
  chartTitle=value;
  }
  }
  #endregion
  }

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

  publicvoidShowChart()
  {

  //初始化赋值
  chartFactory.chartCategoriesArrValue=chartCategories;
  chartFactory.chartValuesArrValue=chartValues;
  chartFactory.chartTitleValue=chartTitle;
  chartFactory.chartTypeValue=chartType;

  OWC11.ChartSpaceClassoChartSpace=chartFactory.BuildCharts();
  stringpath=Server.MapPath(".")+@"/images/Chart.jpeg"; //产生图片并保存页可以是pnggif图片
  oChartSpace.ExportPicture(path,"jpeg",745,500);
  Image1.ImageUrl=path; //显示统计图
  }

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

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