C#編寫OWC11組件原始碼

來源:互聯網
上載者:User

利用OWC編寫的組件 其實已經有很多網上的現成的,但是用自己的可以隨意改動還是比較爽的,代碼功能不是很強,僅推出供大家學習:)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Office.Interop;

namespace FishPro
{
 /// <summary>
 /// 利用OWC組件產生柱狀圖樣本
 /// </summary>
 public class TestOWC : System.Web.UI.Page
 {
  private void Page_Load(object sender, System.EventArgs e)
  {

   string strCategory = "1" + '\t' + "2" + '\t' + "3" + '\t'+"4" + '\t' + "5" + '\t' + "6" + '\t'+"7" + '\t' + "8" + '\t' + "9" + '\t'+"10" + '\t' + "11" + '\t' + "12" + '\t';
   string strValue = "9000" + '\t' + "8000" + '\t' + "4007" + '\t'+"10" + '\t' + "12760" + '\t' + "6678" + '\t'+"10000" + '\t' + "20999" + '\t' + "3567" + '\t'+"456" + '\t' + "125" + '\t' + "66765" + '\t';
   string mTitle="建科院月報表分析圖";
   string xTitle="月份";
   string yTitle="工作量";
   int imgWidth=780;
   int imgHeight=600;
   int chartType=0;
   //this.CreateChartSmoothLine(strCategory,strValue,mTitle,xTitle,yTitle,imgWidth,imgHeight,chartType);
   FishPro.OWCChart11 chart = new OWCChart11(Server.MapPath("."),"費用",mTitle,1,xTitle,yTitle,imgWidth,imgHeight);
   chart.OCategory=strCategory;
   chart.OValue=strValue;
   if(chart.Create())
   {
    Response.Write( "<IMG SRC='"+chart.FileName+"'/>");
   }
   else
   {
    Response.Write("shibai");
   }
   
//   // 在此處放置使用者代碼以初始化頁面
//   string strCategory = "1" + '\t' + "2" + '\t' + "3" + '\t'+"4" + '\t' + "5" + '\t' + "6" + '\t'+"7" + '\t' + "8" + '\t' + "9" + '\t'+"10" + '\t' + "11" + '\t' + "12" + '\t';
//   string strValue = "9" + '\t' + "8" + '\t' + "4" + '\t'+"10" + '\t' + "12" + '\t' + "6" + '\t'+"1" + '\t' + "2" + '\t' + "3" + '\t'+"4" + '\t' + "12" + '\t' + "6" + '\t';
//
//   //聲明對象
//   Microsoft.Office.Interop.Owc11.ChartSpace ThisChart = new  Microsoft.Office.Interop.Owc11.ChartSpaceClass();
//   Microsoft.Office.Interop.Owc11.ChChart ThisChChart  = ThisChart.Charts.Add(0);
//   Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries = ThisChChart.SeriesCollection.Add(0);
//   
//
//   //顯示圖例
//   ThisChChart.HasLegend = true;
//   //標題
//   ThisChChart.HasTitle = true;
//   ThisChChart.Title.Caption = "統計圖";
//   //給定x,y軸圖示說明
//   ThisChChart.Axes[0].HasTitle = true;
//   ThisChChart.Axes[1].HasTitle = true;
//   ThisChChart.Axes[0].Title.Caption = "月份";
//   ThisChChart.Axes[1].Title.Caption = "數量";
//
//   //圖表類型
//   //ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumnClustered3D;//3D 柱狀圖
//   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeSmoothLine;//平滑曲線圖
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeArea; //折線面積圖
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeArea3D;//折線3D面積圖
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeAreaOverlapped3D;//折線3D面積圖
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeAreaStacked;//折線面積圖加邊框
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeAreaStacked100;//折線面積圖加邊框百分比圖
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeAreaStacked1003D;//折線3D面積圖加邊框百分比圖
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeAreaStacked3D;//折線3D面積圖加邊框
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeBar3D;//橫道圖3D
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeBarClustered;//橫道圖
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeBarClustered3D;//橫道圖3D
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeBarStacked;//橫道圖3D
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeBarStacked100;//橫道圖3D百分比圖
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeBarStacked1003D;//橫道圖3D百分比圖
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeBubble; //測試不通過
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeBubbleLine;//測試不通過
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumn3D;//柱狀圖3D
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumnClustered;//柱狀圖3D
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumnClustered3D;//柱狀圖3D
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumnStacked;//柱狀圖
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumnStacked100;//柱狀圖3D 百分比圖
////   ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumnStacked1003D;//柱狀圖3D百分比圖
//   //ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartLegendPositionEnum.chLegendPositionBottom;
//   //旋轉
//   ThisChChart.Rotation  = 360;
//   ThisChChart.Inclination = 10;
//   //背景顏色
//   ThisChChart.PlotArea.Interior.Color = "red";
//   ThisChChart.PlotArea.Floor.Interior.Color = "green";
//   
//   ThisChChart.Overlap = 50;
//
//   /////給定series的名字
//   ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames,Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),"日期");
//   //給定分類
//   ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),strCategory);
//   //給定值
//   ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),strValue);
//
//   //匯出影像檔
//   try
//   {
//                  
//    ThisChart.ExportPicture(Server.MapPath("chart.gif"),"gif",600,350);
//    Response.Write( "<IMG SRC='chart.gif'/>");
//   }
//        
//   catch(Exception ee)
//                
//   {
//                    
//   }

   

  }

  //平滑曲線圖
  //ChartType 0 預設柱狀圖 1 橫道圖 2 平滑曲線圖
  public  void  CreateChartSmoothLine(string strCategory,string strValue,string mTitle,string xTitle,string yTitle,int imgWidth,int imgHeight,int chartType)
  {
   //聲明對象
   Microsoft.Office.Interop.Owc11.ChartSpace ThisChart = new  Microsoft.Office.Interop.Owc11.ChartSpaceClass();
   Microsoft.Office.Interop.Owc11.ChChart ThisChChart  = ThisChart.Charts.Add(0);
   Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries = ThisChChart.SeriesCollection.Add(0);
   
   //顯示圖例
   ThisChChart.HasLegend = true;

   //顯示標題選項
   ThisChChart.HasTitle = true;
   ThisChChart.Title.Font.Name="黑體";
   ThisChChart.Title.Font.Size=14;
   ThisChChart.Title.Caption = mTitle;//from

   //x,y軸說明
   //x
   ThisChChart.Axes[0].HasTitle=true;
   ThisChChart.Axes[0].Title.Font.Name="黑體";
   ThisChChart.Axes[0].Title.Font.Size=12;
   ThisChChart.Axes[0].Title.Caption=xTitle;

   ThisChChart.Axes[1].HasTitle=true;
   ThisChChart.Axes[1].Title.Font.Name="黑體";
   ThisChChart.Axes[1].Title.Font.Size=12;
   ThisChChart.Axes[1].Title.Caption=yTitle;

   //圖表類型
   switch(chartType)
   {
    case 0: 
     ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumn3D;//柱狀圖3D
     break;
    case 1:
     ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeBar3D;//橫道圖3D
     break;
    case 2:
     ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeSmoothLine;//平滑曲線圖
     break;
    case 3:
     ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypePie;//圓餅圖
     break;
   }
   //ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeSmoothLine;//平滑曲線圖
   //旋轉
   ThisChChart.Rotation  = 360;
   ThisChChart.Inclination = 10;
   //背景顏色
   ThisChChart.PlotArea.Interior.Color = "red";
   ThisChChart.PlotArea.Floor.Interior.Color = "green";
   
   //ThisChChart.Overlap = 50;

   /////給定series的名字
   ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames,Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),"日期");
   //給定分類
   ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),strCategory);
   //給定值
   ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(),strValue);

   //匯出影像檔
   try
   {
                  
    ThisChart.ExportPicture(Server.MapPath("chart.gif"),"gif",imgWidth,imgHeight);
    Response.Write( "<IMG SRC='chart.gif'/>");
   }
        
   catch(Exception ee)
                
   {
                    
   }

   

   
  }

  #region Web Form設計器產生的程式碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.