当前位置: 移动技术网 > IT编程>开发语言>c# > C#画图之饼图折线图的实现方法

C#画图之饼图折线图的实现方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网