本文实例讲述了C#画图之饼图折线图的实现方法,是C#程序设计中非常实用的技巧。分享给大家供大家参考。具体方法分析如下:
显示图像的控件定义如下:
public PlaceHolder PlaceHolder1;
各个图像的类别名称如下:
PictureType 图形种类 5 chChartTypeBarClustered 簇状条形图 0 NULL
PictureType 图形种类 7 chChartTypeBarClustered3D 三维簇状条形图 0 NULL
PictureType 图形种类 6 chChartTypeBarStacked 堆积条形图 0 NULL
PictureType 图形种类 8 chChartTypeBarStacked3D 三维堆积条形图 0 NULL
PictureType 图形种类 1 chChartTypeColumnClustered 簇状柱形图 0 NULL
PictureType 图形种类 3 chChartTypeColumnClustered3D 三维簇状柱形图 0 NULL
PictureType 图形种类 2 chChartTypeColumnStacked 堆积柱状图 1 NULL
PictureType 图形种类 4 chChartTypeColumnStacked3D 三维堆积柱形图 0 NULL
PictureType 图形种类 13 chChartTypeLine 折线图 0 NULL
PictureType 图形种类 15 chChartTypeLineMarkers 数据点折线图 0 NULL
PictureType 图形种类 14 chChartTypeLineStacked 堆积折线图 0 NULL
PictureType 图形种类 16 chChartTypeLineStackedMarkers 堆积数据点折线图 0 NULL
PictureType 图形种类 17 chChartTypePie 饼图 1 NULL
PictureType 图形种类 19 chChartTypePie3D 三维饼图 0 NULL
PictureType 图形种类 18 chChartTypePieExploded 分离型饼图 0 NULL
PictureType 图形种类 20 chChartTypePieExploded3D 分离型三维饼图 0 NULL
PictureType 图形种类 9 chChartTypeSmoothLine 平滑线图 0 NULL
PictureType 图形种类 10 chChartTypeSmoothLineMarkers 数据点平滑线图 0 NULL
PictureType 图形种类 11 chChartTypeSmoothLineStacked 堆积平滑线图 0 NULL
PictureType 图形种类 12 chChartTypeSmoothLineStackedMarkers 堆积数据平滑线图 0 NULL
取图像的方法如下:
/// </summary>
/// <param name="dbDtViewWrk">传递的数据</param>
/// <param name="strAbsolutePath">绝对路径</param>
/// <param name="strRelativePath">相对路径</param>
/// <param name="ChartType">要画的图格式(饼图或者折线图等)</param>
/// <param name="strTitle">统计名称</param>
public void PaintToImage(DataTable dbDtViewWrk, string strAbsolutePath, string strRelativePath, ChartChartTypeEnum ChartType, string strTitle)
{
  string strSeriesName = "图例";
  //存放项目
  string[] ItemsName = new string[dbDtViewWrk.Rows.Count];
  //存放数据
  string[] ItemsCount = new string[dbDtViewWrk.Rows.Count];
  //刻度单位
  int iUnit = 1;
  //最大值
  int iMaxValue = 0;
  string strXdata = String.Empty;
  string strYdata = String.Empty;
 //为数组赋值
 for (int i = 0; i < dbDtViewWrk.Rows.Count; i++)
 {
   ItemsName[i] = dbDtViewWrk.Rows[i][0].ToString(); //要统计的字段名字
   ItemsCount[i] = dbDtViewWrk.Rows[i][5].ToString();//要统计的字段数据
 }
  //为x轴指定特定字符串,以便显示数据
  // string strXdata = String.Empty;
  foreach (string strData in ItemsName)
  {
 strXdata += strData + "\t";
  }
  // string strYdata = String.Empty;
  //为y轴指定特定的字符串,以便与x轴相对应
  foreach (string strValue in ItemsCount)
  {
 strYdata += strValue + "\t";
 if (int.Parse(strValue) > iMaxValue)
 {
   iMaxValue = int.Parse(strValue);
 }
  }
  if (iMaxValue > 20)
  {
 iUnit = iMaxValue / 10;
  }
  //创建ChartSpace对象来放置图表
  ChartSpace laySpace = new ChartSpaceClass();
  //在ChartSpace对象中添加图表
  ChChart InsertChart = laySpace.Charts.Add(0);
  //底座颜色
  InsertChart.PlotArea.Interior.Color = "white";
  //指定绘制图表的类型。类型可以通过OWC.ChartChartTypeEnum枚举值得到
  InsertChart.Type = ChartType;//柱形图
  //指定图表是否需要图例标注
  InsertChart.HasLegend = true;
  InsertChart.BarWidth = 0;
  InsertChart.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;
  InsertChart.HasTitle = true;//为图表添加标题
  InsertChart.Title.Caption = strTitle;//标题名称
  //为x,y轴添加图示说明
  if (ChartType.ToString().IndexOf("ChartTypePie") == -1)
  {
 InsertChart.Axes[0].Font.Size = 11; //X轴
 InsertChart.Axes[1].Font.Size = 11; //Y轴
 InsertChart.Legend.Font.Size = 11;
 InsertChart.Axes[0].HasTitle = true;
 InsertChart.Axes[0].Title.Caption = "";//月份
 InsertChart.Axes[1].HasTitle = true;
 //InsertChart.Axes[1].Scaling.SplitMinimum = 200;
 InsertChart.Axes[1].Title.Caption = "数量";
 InsertChart.Axes[1].MajorUnit = iUnit; //刻度单位设置
 InsertChart.Axes[1].Scaling.Minimum = 0;//最小刻度=0
  }
  //添加一个series系列
  InsertChart.SeriesCollection.Add(0);
  //给定series系列的名字
  InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);
  //给定分类
  strXdata = strXdata.Substring(0, strXdata.Length - 1);
  InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strXdata);
  //给定值
  strYdata = strYdata.Substring(0, strYdata.Length - 1);
  InsertChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, strYdata);
  //添加标签
  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;
 //指定图表是否需要图例标注
 InsertChart.HasLegend = true;
 InsertChart.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;
  }
  //输出文件.
  int iImageLength = 0;
  int iImageWidth = 0;
  //从Config文件取得设置
  //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);
  //把图片添加到placeholder中,并在页面上显示
  string strImageTag = "<IMG WIDTH=‘450‘ SRC=‘" + strRelativePath + strImageName + "‘/>";
  this.PlaceHolder1.Controls.Add(new LiteralControl(strImageTag));
  // return strImageTag;
}
希望本文所述对大家的C#程序设计有所帮助。
 
        
