Call office in asp.net to make various (3D) statistical graphs

 
1. Download owc11 COM components

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

2. Register owc11

  Add owc11.dll reference under C:/Program Files/Common Files/Microsoft Shared/Web Components/11 in the project or add com as shown below


3. Add in the project

    using OWC11;

4. Examples of starting coding are as follows:

 public class ChartFactory
 {   public ChartFactory()   {    InitTypeMap();    //    // TODO: add constructor logic here    //   }   protected System.Web.UI.WebControls.Image imgHondaLineup;   private string[] chartCategoriesArr;   private string[ ] chartValuesArr;   private OWC11.ChartChartTypeEnum chartType = OWC11.ChartChartTypeEnum.chChartTypeColumn3D;//default   private static Hashtable chartMap = new Hashtable();   private static string chartTypeCh = "vertical histogram";   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 []"," vertical bar chart []", vertical bar chart "Picture", "3D horizontal column statistical graph", "area statistical graph", "3D area statistical graph", "hollow pie graph", "line statistical graph", "3D polyline statistical graph", "polyline statistical graph with dots" ,"Pie Chart","3D Pie Chart","Network Statistics Chart","Arc Chart"};
  
   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
 }

//Call First, you need to place an Image on the page to display the generated statistical graph

  public void ShowChart()
  {

//Initial assignment
   chartFactory.chartCategoriesArrValue = chartCategories;
   chartFactory.chartValuesArrValue = chartValues;
   chartFactory.chartTitleValue = chartTitle;
   chartFactory.chartTypeValue = chartType;

   OWC11.ChartSpaceClass oChartSpace = chartFactory.BuildCharts();
   string path = Server.MapPath(".") + @"/images/Chart.jpeg"; //Generate picture and save page can be png gif picture
   oChartSpace.ExportPicture(path ,"jpeg", 745, 500);
   Image1.ImageUrl = path; // show statistical graph
  }

// Please refer to the previous article for saving statistical graphs

//Because each generated statistical graph will overwrite the original picture, if necessary, date and time can be used as the name of the picture, but this will produce a lot of pictures that need to be processed in time, if you don’t need to just overwrite it with the same name The original picture is fine.

Guess you like

Origin blog.csdn.net/jinzhengquanqq/article/details/5878244