• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#中用GDI+生成饼状图和柱状图

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

using System;
using System.IO;//用于文件存取 
using System.Data;//用于数据访问 
using System.Drawing;//提供画GDI+图形的基本功能 
using System.Drawing.Text;//提供画GDI+图形的高级功能 
using System.Drawing.Drawing2D;//提供画高级二维,矢量图形功能 
using System.Drawing.Imaging;//提供画GDI+图形的高级功能 
namespace lc.laili.Web.Code
{
 /// <summary>
 /// MyImage 的摘要说明。
 /// </summary>
 public class MyImage
 {
  public MyImage()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //


  }

 


  /// <summary>
  /// 数据源是PieChartValue对象的饼状图
  /// </summary>
  /// <param name="title">饼状图大标题</param>
  /// <param name="subTitle">饼状图小标题</param>
  /// <param name="width">图宽</param>
  /// <param name="height">图高</param>
  /// <param name="Mydata">PieChartValue对象</param>
  /// <returns>饼状图路径</returns>
  public string Render(string title, string subTitle, int width, int height, PieChartValue[] Mydata)
  {
   const int SIDE_LENGTH = 400;
   const int PIE_DIAMETER = 200;
   //DataTable dt = DataTable;

   //通过输入参数,取得饼图中的总基数 
   float sumData = 0;
   for (int i = 0; i < Mydata.Length; i++)
   {
    sumData += Convert.ToSingle(Mydata[i].MValue);
   }
   //产生一个image对象,并由此产生一个Graphics对象 
   Bitmap bm = new Bitmap(width, height);
   Graphics g = Graphics.FromImage(bm);
   //设置对象g的属性 
   g.ScaleTransform((Convert.ToSingle(width)) / SIDE_LENGTH, (Convert.ToSingle(height)) / SIDE_LENGTH);
   g.SmoothingMode = SmoothingMode.Default;
   g.TextRenderingHint = TextRenderingHint.AntiAlias;

   //画布和边的设定 
   g.Clear(Color.White);
   g.DrawRectangle(Pens.Black, 0, 0, SIDE_LENGTH - 1, SIDE_LENGTH - 1);
   //画饼图标题 
   g.DrawString(title, new Font("Tahoma", 14), Brushes.Black, new PointF(5, 5));
   //画饼图的图例 
   g.DrawString(subTitle, new Font("Tahoma", 12), Brushes.Black, new PointF(7, 35));
   //画饼图 
   float curAngle = 0;
   float totalAngle = 0;
   for (int j = 0; j < Mydata.Length; j++)
   {
    curAngle = Convert.ToSingle(Mydata[j].MValue) / sumData * 360;

    g.FillPie(new SolidBrush(ChartUtil.GetChartItemColor(j)), 100, 65, PIE_DIAMETER, PIE_DIAMETER, totalAngle, curAngle);
    g.DrawPie(Pens.Black, 100, 65, PIE_DIAMETER, PIE_DIAMETER, totalAngle, curAngle);
    totalAngle += curAngle;
   }
   //画图例框及其文字 
   g.DrawRectangle(Pens.Black, 200, 300, 199, 99);
   g.DrawString("图表说明", new Font("Tahoma", 12, FontStyle.Bold), Brushes.Black, new PointF(200, 300));

   //画图例各项 
   PointF boxOrigin = new PointF(210, 330);
   PointF textOrigin = new PointF(235, 326);
   float percent = 0;
   for (int k = 0; k < Mydata.Length; k++)
   {
    g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(k)), boxOrigin.X, boxOrigin.Y, 20, 10);
    g.DrawRectangle(Pens.Black, boxOrigin.X, boxOrigin.Y, 20, 10);
    percent = Convert.ToSingle(Mydata[k].MValue) / sumData * 100;
    g.DrawString(Mydata[k].MValue.ToString() + " - " + Mydata[k].Name.ToString() + " (" + percent.ToString("0") + "%)", new Font("Tahoma", 10), Brushes.Black, textOrigin);
    boxOrigin.Y += 15;
    textOrigin.Y += 15;
   }
   //回收资源 
   g.Dispose();
//此处存储路径需要自己改
   System.Drawing.Image MyImage=(System.Drawing.Image)bm;
   string TruePath= System.Web.HttpContext.Current.Server.MapPath("~/images");
   string TrueName=DateTime.Now.ToString("yyyyMMddhhmmss")+DateTime.Now.Millisecond;
   MyImage.Save(TruePath+"\\"+TrueName+".jpg", System.Drawing.Imaging.ImageFormat.Gif);
   return "~/images/"+TrueName+".jpg";
  }

 

 

 

  /// <summary>
  /// 数据源是BarChartValue对象的柱状图
  /// </summary>
  /// <param name="title">大标题</param>
  /// <param name="subTitle">小标题</param>
  /// <param name="width">图宽</param>
  /// <param name="height">图高</param>
  /// <param name="Mydata">BarChartValue对象</param>
  /// <returns>柱状图路径</returns>
  public string Render(string title, string subTitle, int width, int height, BarChartValue[] Mydata)
  {
   const int SIDE_LENGTH = 400;
   const int CHART_TOP = 75;
   const int CHART_HEIGHT = 200;
   const int CHART_LEFT = 50;
   const int CHART_WIDTH = 300;

   //计算最高的点 
   float highPoint = 0;
   for (int i=0; i < Mydata.Length;i++ )
   {
    if (highPoint < Convert.ToSingle(Mydata[i].MValue))
    {
     highPoint = Convert.ToSingle(Mydata[i].MValue);
    }
   }
   //建立一个Graphics对象实例 
   Bitmap bm = new Bitmap(width, height);

    Graphics g = Graphics.FromImage(bm);
    //设置条图图形和文字属性 
    g.ScaleTransform((Convert.ToSingle(width)) / SIDE_LENGTH, (Convert.ToSingle(height)) / SIDE_LENGTH);
    g.SmoothingMode = SmoothingMode.Default;
    g.TextRenderingHint = TextRenderingHint.AntiAlias;

    //设定画布和边 
    g.Clear(Color.White);
    g.DrawRectangle(Pens.Black, 0, 0, SIDE_LENGTH - 1, SIDE_LENGTH - 1);
    //画大标题 
    g.DrawString(title, new Font("Tahoma", 14), Brushes.Black, new PointF(5, 5));
    //画小标题 
    g.DrawString(subTitle, new Font("Tahoma", 12), Brushes.Black, new PointF(7, 35));
    //画条形图 
    float barWidth = CHART_WIDTH / (Mydata.Length * 2);
    PointF barOrigin = new PointF(CHART_LEFT + (barWidth / 2), 0);
    float barHeight = Mydata.Length;
    for (int i = 0; i < Mydata.Length; i++)
    {
     barHeight = Convert.ToSingle(Mydata[i].MValue) * 200 / highPoint * 1;
     barOrigin.Y = CHART_TOP + CHART_HEIGHT - barHeight;
     g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)), barOrigin.X, barOrigin.Y, barWidth, barHeight);


 
     g.DrawString(Mydata[i].Name, new Font("Tahoma", 3), Brushes.Black, new PointF(barOrigin.X-3, 277));

 

     barOrigin.X = barOrigin.X + (barWidth * 2);
    }
    //设置边 
    g.DrawLine(new Pen(Color.Black, 2), new Point(CHART_LEFT, CHART_TOP), new Point(CHART_LEFT, CHART_TOP + CHART_HEIGHT));
    g.DrawLine(new Pen(Color.Black, 2), new Point(CHART_LEFT, CHART_TOP + CHART_HEIGHT), new Point(CHART_LEFT + CHART_WIDTH, CHART_TOP + CHART_HEIGHT));
    //画图例框和文字 


    //g.DrawRectangle(Pens.Black, 200, 300, 199, 99);
    //g.DrawString("图表说明", new Font("Tahoma", 12, FontStyle.Bold), Brushes.Black, new PointF(200, 300));

    g.DrawRectangle(new Pen(Color.Black, 1), 10, 290, 299, 110);
    g.DrawString("图表说明", new Font("Tahoma", 10, FontStyle.Bold), Brushes.Black, new PointF(11, 290));

    //画图例 
    PointF boxOrigin = new PointF(10, 310);
    PointF textOrigin = new PointF(35, 306);
    for (int i = 0; i < Mydata.Length; i++)
    {
     g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)), boxOrigin.X, boxOrigin.Y, 20, 10);
     g.DrawRectangle(Pens.Black, boxOrigin.X, boxOrigin.Y, 2, 1);
     g.DrawString(Mydata[i].Name.ToString() + " - " + Mydata[i].MValue.ToString(), new Font("Tahoma", 10), Brushes.Black, textOrigin);
     if(i<5)
     {
      boxOrigin.Y += 15;
      textOrigin.Y += 15;
     }
     else if(i==5)
     {
      boxOrigin.X = 150;
      boxOrigin.Y = 310;
      textOrigin.X = 176;
      textOrigin.Y = 306;

     }
     else
     {
      boxOrigin.Y += 15;
      textOrigin.Y += 15;
     }

    }
    //输出图形 
    g.Dispose();
//此处存储路径需要自己改
   System.Drawing.Image MyImage=(System.Drawing.Image)bm;
   string TruePath= System.Web.HttpContext.Current.Server.MapPath("~/images");
   string TrueName=DateTime.Now.ToString("yyyyMMddhhmmss")+DateTime.Now.Millisecond;
   MyImage.Save(TruePath+"\\"+TrueName+".jpg", System.Drawing.Imaging.ImageFormat.Gif);
   return "~/images/"+TrueName+".jpg";

  }
 }

 /// <summary>
 /// 选择颜色
 /// </summary>
 public class ChartUtil
 {
  public ChartUtil()
  {
  }
  public static Color GetChartItemColor(int itemIndex)
  {
   Color selectedColor;
   switch (itemIndex)
   {
    case 0:
     selectedColor = Color.AliceBlue;
     break;
    case 1:
     selectedColor = Color.Red;
     break;
    case 2:
     selectedColor = Color.Yellow;
     break;
    case 3:
     selectedColor = Color.AntiqueWhite;
     break;
    case 4:
     selectedColor = Color.Aqua;
     break;
    case 5:
     selectedColor = Color.Aquamarine;
     break;
    case 6:
     selectedColor = Color.Azure;
     break;
    case 7:
     selectedColor = Color.Beige;
     break;
    case 8:
     selectedColor = Color.Black;
     break;
    case 9:
     selectedColor = Color.Brown;
     break;
    case 10:
     selectedColor = Color.Coral;
     break;
    case 11:
     selectedColor = Color.DarkCyan;
     break;
    case 12:
     selectedColor = Color.DarkOrange;
     break;
    default:
     selectedColor = Color.DarkViolet;
     break;
   }
   return selectedColor;
  }
 }

 /// <summary>
 /// 饼状图的数据对象
 /// </summary>
 public class PieChartValue
 {
  public PieChartValue()
  {

  }
  public PieChartValue(string MyName, int Myvalue)
  {
   Name = MyName;
   MValue = Myvalue;
  }
  private string name;
  public string Name
  {
   get
   {
    return name;
   }
   set
   {
    name = value;
   }
  }

  private int mvalue;
  public int MValue
  {
   get
   {
    return mvalue;
   }
   set
   {
    mvalue = value;
   }
  }
 }

 /// <summary>
 /// 柱状图的数据对象
 /// </summary>
 public class BarChartValue
 {
  public BarChartValue()
  {

  }
  public BarChartValue(string MyName, int Myvalue)
  {
   Name = MyName;
   MValue = Myvalue;
  }
  private string name;
  public string Name
  {
   get
   {
    return name;
   }
   set
   {
    name = value;
   }
  }

  private int mvalue;
  public int MValue
  {
   get
   {
    return mvalue;
   }
   set
   {
    mvalue = value;
   }
  }
 }
}

用法


   #region 生成柱状图

   MyImage MyMyImage = new MyImage();
   BarChartValue[] BarChartValue ={
          new BarChartValue("第1个月",5),
          new BarChartValue("第2个月",2),
          new BarChartValue("第3个月",3),
          new BarChartValue("第4个月",4),
          new BarChartValue("第5个月",5),
          new BarChartValue("第6个月",6),
          new BarChartValue("第7个月",7),
          new BarChartValue("第8个月",8),
          new BarChartValue("第9个月",9)

             };
   Image1.ImageUrl = MyMyImage.Render("全年统计", "98年", 1000, 1000, BarChartValue);
   #endregion

 

   #region 生成饼状图
   PieChartValue[] PieChartValue ={
              new PieChartValue("第一个仓库",1),
              new PieChartValue("第一个仓库",2)
             };

   Image2.ImageUrl = MyMyImage.Render("仓库统计饼状图", "小仓库", 500, 500, PieChartValue);
   #endregion


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#:Socket通信C#:Socket通信发布时间:2022-07-14
下一篇:
DataGridColumnCollection.GetEnumerator 方法的C#例子发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap