C drawing pie chart line chart implementation method

  • 2020-10-23 21:12:09
  • OfStack

The example of C# drawing pie chart line chart implementation method is a very practical skill in C# programming. Share to everybody for everybody reference. The specific analysis is as follows:

The control to display the image is defined as follows:


public PlaceHolder PlaceHolder1;

The category names of each image are as follows:

PictureType Graph type 5 chChartTypeBarClustered cluster bar chart 0 NULL

Type of PictureType Graphics 7 chChartTypeBarClustered3D 3-dimensional cluster bar graph 0 NULL

PictureType Graph type 6 chChartTypeBarStacked Stacked bar Chart 0 NULL

PictureType Graphic category 8 chChartTypeBarStacked3D 3 d stacked bar chart 0 NULL

Type of PictureType graphics 1 chChartTypeColumnClustered Cluster bar Graph 0 NULL

Type of PictureType graphics 3 chChartTypeColumnClustered3D 3d Clustered bar graph 0 NULL

Figure type 2 chChartTypeColumnStacked Stacking Bar Figure 1 NULL

Type of PictureType graphics 4 chChartTypeColumnStacked3D 3d stacked bar chart 0 NULL

PictureType figure type 13 chChartTypeLine line figure 0 NULL

Graph type 15 chChartTypeLineMarkers Line graph of data points 0 NULL

PictureType graphics category 14 chChartTypeLineStacked Stacked polyline Graph 0 NULL

Graph type 16 chChartTypeLineStackedMarkers Stacked data point line graph 0 NULL

PictureType Figure type 17 chChartTypePie Pie Chart 1 NULL

Type 19 chChartTypePie3D 3d pie Chart 0 NULL

Figure type 18 chChartTypePieExploded Detached pie chart 0 NULL

20 chChartTypePieExploded3D Detached 3d pie chart 0 NULL

PictureType graph type 9 chChartTypeSmoothLine smooth line graph 0 NULL

PictureType Graph category 10 chChartTypeSmoothLineMarkers Data point smooth line graph 0 NULL

PictureType Graph type 11 chChartTypeSmoothLineStacked Stacked Smooth line graph 0 NULL

PictureType Graph category 12 chChartTypeSmoothLineStackedMarkers Stacked Data smoothing line 0 NULL

The method to get the image is as follows:


/// </summary>
/// <param name="dbDtViewWrk"> Transmitted data </param>
/// <param name="strAbsolutePath">绝对路径</param>
/// <param name="strRelativePath"> Relative paths </param>
/// <param name="ChartType"> Chart format to draw (pie chart, line chart, etc.) </param>
/// <param name="strTitle"> Statistical name </param>
public void PaintToImage(DataTable dbDtViewWrk, string strAbsolutePath, string strRelativePath, ChartChartTypeEnum ChartType, string strTitle)
{
  string strSeriesName = " legend ";
  // Storage project 
  string[] ItemsName = new string[dbDtViewWrk.Rows.Count];
  // To store data 
  string[] ItemsCount = new string[dbDtViewWrk.Rows.Count];
  // Calibration unit 
  int iUnit = 1;
  // The maximum 
  int iMaxValue = 0;
  string strXdata = String.Empty;
  string strYdata = String.Empty;
  
 // Assign an array value 
 for (int i = 0; i < dbDtViewWrk.Rows.Count; i++)
 {
   ItemsName[i] = dbDtViewWrk.Rows[i][0].ToString(); // The name of the field to be counted 
   ItemsCount[i] = dbDtViewWrk.Rows[i][5].ToString();// The field data to be counted 
 }
  // for x The axis specifies a specific string to display the data 
  // string strXdata = String.Empty;
  foreach (string strData in ItemsName)
  {
 strXdata += strData + "\t";
  }
  // string strYdata = String.Empty;
  // for y The axis specifies a specific string to be associated with x Shaft corresponding 
  foreach (string strValue in ItemsCount)
  {
 strYdata += strValue + "\t";
 if (int.Parse(strValue) > iMaxValue)
 {
   iMaxValue = int.Parse(strValue);
 }
  }
  if (iMaxValue > 20)
  {
 iUnit = iMaxValue / 10;
  }
  // create ChartSpace Object to place the diagram 
  ChartSpace laySpace = new ChartSpaceClass();
  
  // in ChartSpace Object to add a chart 
  ChChart InsertChart = laySpace.Charts.Add(0);
  
  // The base color 
  InsertChart.PlotArea.Interior.Color = "white";

  // Specifies the type of diagram to draw. The type can go through OWC.ChartChartTypeEnum Enumeration is worth 
  InsertChart.Type = ChartType;// Bar charts 


  // Specifies whether the diagram requires legend annotation 
  InsertChart.HasLegend = true;
  InsertChart.BarWidth = 0;
  InsertChart.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;

  InsertChart.HasTitle = true;// Add a title to the chart 
  InsertChart.Title.Caption = strTitle;// The name of the title 

  // for x,y Axis add graphical description 
  if (ChartType.ToString().IndexOf("ChartTypePie") == -1)
  {
 InsertChart.Axes[0].Font.Size = 11; //X shaft 

 InsertChart.Axes[1].Font.Size = 11; //Y shaft 
 InsertChart.Legend.Font.Size = 11;
 InsertChart.Axes[0].HasTitle = true;
 InsertChart.Axes[0].Title.Caption = "";// in 
 InsertChart.Axes[1].HasTitle = true;
 //InsertChart.Axes[1].Scaling.SplitMinimum = 200;
 InsertChart.Axes[1].Title.Caption = " The number of ";
 InsertChart.Axes[1].MajorUnit = iUnit; // Calibration unit setting 
 InsertChart.Axes[1].Scaling.Minimum = 0;// Minimum scale =0
  }

  // add 1 a series A series of 
  InsertChart.SeriesCollection.Add(0);
  
  // For a given series Name of series 
  InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);

  // Classification of a given 
  strXdata = strXdata.Substring(0, strXdata.Length - 1);
  InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);

  // A given value 
  strYdata = strYdata.Substring(0, strYdata.Length - 1);
  InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYdata);

  // Add tags 
  ChDataLabels dls = InsertChart.SeriesCollection[0].DataLabelsCollection.Add();
  if (ChartType.ToString().IndexOf("ChartTypePie") != -1)
  {
 dls.Position = ChartDataLabelPositionEnum.chLabelPositionCenter;
 dls.HasPercentage = false;
 //dls.HasValue = false;
 dls.HasCategoryName = false;
 // Specifies whether the diagram requires legend annotation 
 InsertChart.HasLegend = true;
 InsertChart.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;
  }

  // The output file .
  int iImageLength = 0;
  int iImageWidth = 0;

  // from Config File acquisition Settings 
  //iImageLength = int.Parse(WebConfigurationManager.AppSettings["ShowImageLength"]);
  //iImageWidth = int.Parse(WebConfigurationManager.AppSettings["ShowImageWidth"]);
  iImageLength = 450;
  iImageWidth = 300;

  string strImageName = ChartType.ToString() + "_" + Guid.NewGuid().ToString("N") + ".png";
  laySpace.ExportPicture(strAbsolutePath + strImageName, "PNG", 450, 300);

  // Add the image to placeholder , and displayed on the page 
  string strImageTag = "<IMG WIDTH='450' SRC='" + strRelativePath + strImageName + "'/>";
  
  this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));
  // return strImageTag;
}

Hopefully this article has helped you with your C# programming.


Related articles: