程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net中調用Office來制作3D統計圖的實例代碼

asp.net中調用Office來制作3D統計圖的實例代碼

編輯:ASP.NET基礎

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;  // 顯示統計圖
  }

  // 保存統計圖請參照上一篇文章

  //由於每次生成的統計圖都會覆蓋原來的圖片所以有必要的話可以用日期加時間的方式來作為圖片的名字,但是這樣將會產生很多圖片需及時處理,如不需要只需取同名覆蓋原來圖片即可。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved