C#实现饼图与棒图

原文地址:http://blog.csdn.net/asthlon/archive/2004/11/22/190965.aspx

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace xcrm.Module.report
{
 /// <summary>
 /// draw 的摘要说明。
 /// </summary>
 public class draw : System.Web.UI.Page
 {
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   Draw_Imgbar();
  }
  //画棒图
  void Draw_Imgbar(){
   //创建一个长度为400,宽带为400的Bitmap实例
   Bitmap bmp = new Bitmap(400,300);
   Graphics g;
   g = Graphics.FromImage(bmp);
   g.Clear(Color.Snow);
   string[] sitem = {"很好","好","一般","差"};
   int[] num = {1000,69,90,2000};
   int cnt,i,len,iBarWidth;
   float scale;
   float[] nflt;
   string header;
   header = "";
   cnt =0;
      iBarWidth = 40;
   scale = 1;
   len = num.Length;
   //nflt.Length = len;
   nflt = new float[len];
   for(i=0;i<len;i++)
   {
    cnt += num[i];
   }
   //flt = cnt /len;
   for(i=0;i<len;i++)
   {
   nflt[i] = 200 * num[i]/cnt;
    //nflt[i] = scale * num[i]/cnt;
   }
           

   header = "调查统计结果一览图";
   g.DrawString ( header , new Font( "宋体", 12,FontStyle.Bold) , Brushes.Black , new Point( 75 , 10 ) ) ;
   Point myRec = new Point ( 300 , 40 ) ;
   Point myDec = new Point ( 320 , 40 ) ;

         
   for(i=0;i<len;i++)
   {
    g.DrawRectangle ( Pens.Black , myRec.X , myRec.Y , 20 , 10 ) ;
    //绘制小方块
    g.FillRectangle ( new SolidBrush( Return_Color( i ) ) , myRec.X , myRec.Y , 20 , 10 ) ;
    //填充小方块
    g.DrawString ( " " + sitem[i], new Font( "宋体" , 9 ) , Brushes.Black , myDec ) ;
    //绘制小方块右边的文字
    myRec.Y += 15 ;
    myDec.Y += 15 ;

    g . DrawRectangle ( Pens.Black , ( i * iBarWidth ) + 15 , 290 - (nflt[ i ] * scale ) , 20 , ( nflt[ i ] * scale ) + 5 ) ;
    //绘制Bar图
    g . FillRectangle ( new SolidBrush ( Return_Color( i ) ) , ( i * iBarWidth ) + 15 , 290 - ( nflt[ i ] * scale ) , 20 , ( nflt[ i ] * scale ) + 5 ) ;
    //以指定的色彩填充Bar图
    g . DrawString ( num[ i ].ToString ( ) , new Font( "宋体" , 9 ) , Brushes.Black , ( i * iBarWidth ) + 20 , 275 - (nflt[ i ] * scale ) ) ;
    //显示Bar图代表的数据

    //s = s + nflt[i];    
   }
   Pen p = new Pen ( Color.Black , 1 ) ;
   g . DrawRectangle ( p , 1 , 1 , 398 , 298 ) ;
   bmp.Save ( Response.OutputStream , System.Drawing.Imaging.ImageFormat.Jpeg);
   bmp.Dispose();
  }


  //画饼图
  void Draw_Img(){
   Bitmap bmp = new Bitmap(400,300);
            //创建一个长度为400,宽带为400的Bitmap实例
            Graphics g;
   g = Graphics.FromImage(bmp);
   g.Clear(Color.Snow);
   string[] sitem = {"很好","好","一般","差"};
   int[] num = {1000,69,90,20};
   int cnt,i,len;
   float s;
   float[] nflt;
   string header;
   header = "";
   cnt =0;
   s = 0;
   len = num.Length;
   //nflt.Length = len;
   nflt = new float[len];
   for(i=0;i<len;i++){
    cnt += num[i];
   }
   //flt = cnt /len;
   for(i=0;i<len;i++){
   
    nflt[i] = 360 * num[i]/cnt;
   }


   header = "调查统计结果一览图";
   g.DrawString ( header , new Font( "宋体", 12,FontStyle.Bold) , Brushes.Black , new Point( 75 , 10 ) ) ;
   g.DrawString ( "单位:次" , new Font ( "宋体" , 9 ) , Brushes . Black , new Point(300 , 25 ) ) ;

   Point myRec = new Point ( 300 , 40 ) ;
   Point myDec = new Point ( 320 , 40 ) ;

         
   for(i=0;i<len;i++)
   {
    if(i==len-1){
     //s = 360-s;
     nflt[i] = 360-s;
    }
   
    g.DrawRectangle ( Pens.Black , myRec.X , myRec.Y , 20 , 10 ) ;
    //绘制小方块
    g.FillRectangle ( new SolidBrush( Return_Color( i ) ) , myRec.X , myRec.Y , 20 , 10 ) ;
    //填充小方块
    g.DrawString ( " " + sitem[i] + " " + num[i], new Font( "宋体" , 9 ) , Brushes.Black , myDec ) ;
    //绘制小方块右边的文字
    myRec.Y += 15 ;
    myDec.Y += 15 ;

    g.FillPie( new SolidBrush (Return_Color(i)) , 50 , 50 , 200 , 200 , s , nflt[i] ) ;
    g.DrawPie( Pens.Black , 50 , 50 , 200 , 200 , s , nflt[i]);
    s = s + nflt[i];    
   }
   Pen p = new Pen ( Color.Black , 1 ) ;
   g . DrawRectangle ( p , 1 , 1 , 398 , 298 ) ;
             bmp.Save ( Response.OutputStream , System.Drawing.Imaging.ImageFormat.Jpeg);
  }
  public Color Return_Color(int i){
   switch(i){
    case 0:
     return Color.Red;
     //break;
    case 1:
     return Color.Blue;
     //break;
    case 2:
     return Color.Yellow;
    case 3:
     return Color.Green;
     //break;
    case 4:
     return Color.Pink;
     //break;
    case 5:
     return Color.Plum;
     //break;
    case 6:
     return Color.Gray;
     //break;
    case 7:
     return Color.Salmon;
     //break;
    case 8:
     return Color.RosyBrown;
     //break;
    case 9:
     return Color.Teal;
     //break;
    case 10:
     return Color.Orange;
     //break;
    case 11:
     return Color.Thistle;
     //break;
    case 12:
     return Color.Maroon;
     //break;
    default:
     return Color.WhiteSmoke;
     //break;

   }
  }
  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
 }

web页上绘制饼图曲线等组件(c#) 例子: 生成饼图表******************************************** private void InitializeComponent() { this.myBarGraph.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraphBar); this.myLineGraph.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraphLine); } private void OnRenderGraphBar(ZedGraphWeb zgw, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane) { myBarMethod(zgw, g, masterPane, strYear, strMonth, compareType); } private void OnRenderGraphLine(ZedGraphWeb zgw, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane) { myLineMethod(zgw, g, masterPane, douYear, douMonth); } private void myBarMethod(ZedGraphWeb zgw, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane, string[] strYear, string[] strMonth, string compareType) { GraphPane myPane = masterPane[0]; myPane.Title.Text = title + "柱状分析"; myPane.XAxis.Title.Text = "时间(月/年)"; if (compareType == "AddValue") { myPane.YAxis.Title.Text = "增加值"; } else { myPane.YAxis.Title.Text = "增加比例(%)"; } List<PointPairList> ListPointParitList = new List<PointPairList>(); for (int i = 0; i < strYear.Length; i++) { ListPointParitList.Add(new PointPairList()); } int n = 0; for (double x = 0; x < strMonth.Length; x += 1.0) { for (int i = 0; i < strYear.Length; i++) { //ListPointParitList[i].Add(x, randNum[n++] * multiplyValue); ListPointParitList[i].Add(x, GetFXData(strYear[i], strMonth[(int)x])); } } List<BarItem> ListBarItem = new List<BarItem>(); List<Color> ListColor = GetColor(); for (int i = 0; i < strYear.Length; i++) { ListBarItem.Add(new BarItem(strYear[i], ListPointParitList[i], ListColor[i])); ListBarItem[i] = myPane.AddBar(strYear[i], ListPointParitList[i], ListColor[i]); } myPane.XAxis.MajorTic.IsBetweenLabels = true; myPane.XAxis.Scale.TextLabels = strMonth; myPane.XAxis.Type = AxisType.Text; myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f); myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f); masterPane.AxisChange(g); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值