Monday, August 31, 2009

Coding for creating chart using c#.net

Add references of  Microsoft office components from com tab.

Include the header file

Using System.OWC;

  //Create chartspace object to hola a chart
  OWC.ChartSpace draw_chart = new OWC.ChartSpace();

  //Add a chart in chartspace
  OWC.WCChart chart_obj = draw_chart.Charts.Add(0);

  //specify the type of chart to be displayed
  chart_obj.Type = OWC.ChartChartTypeEnum.chChartTypeColumnClustered;

  //specify the chart legend value,if it is needed.
  chart_obj.HasLegend = true;

  //set the chart caption on or off and set the caption value of chart.
  chart_obj.HasTitle = true;
  chart_obj.Title.Caption = "Demo Chart";

  //set the caption of x and y axis positions
  chart_obj.Axes[0].HasTitle = true;
  chart_obj.Axes[0].Title.Caption = "POSITION X";
  chart_obj.Axes[1].HasTitle = true;
  chart_obj.Axes[1].Title.Caption = "POSITION Y";

  //creating a values for a chart 
  String bar_name = "Line 1";
  string x_values = "1" + '\t' + "2" + '\t' + "3";
  string y_values = "1" + '\t' + "2" + '\t' + "3";

  //add the seriescollection in chart chart object
  chart_obj.SeriesCollection.Add(0);

  //Binding the chart values in seriescollection array.
  chart_obj.SeriesCollection[0].SetData(OWC.ChartDimensionsEnum.chDimSeriesNames, (int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, bar_name);
  chart_obj.SeriesCollection[0].SetData(OWC.ChartDimensionsEnum.chDimCategories, (int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, x_values);
  chart_obj.SeriesCollection[0].SetData(OWC.ChartDimensionsEnum.chDimValues, (int)OWC.ChartSpecialDataSourcesEnum.chDataLiteral, y_values);

  //specify the chart export path.
  string chart_path = "chart.gif";
  draw_chart.ExportPicture(chart_path, "gif", 500, 500);

  //chart file name to be displayed
  string gif_file_path = "chart.gif";

  //binding the filename into a image tag.
  string img_tag="include image tag here with file source name of chart file name mentioned above";

  //Add a imagetage into a placedholder to view the chart
  chart_container.Controls.Add(new LiteralControl(img_tag));

No comments: