日期:2013-06-12  浏览次数:20421 次

 

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 Drawpicture
 {
  /// <summary>
  /// BarChart 的摘要说明。
  /// </summary>
  public class BarChart
  {
   private   int Pic_Width = 410;
   private   int Pic_Height = 320;
           
   public static bool hasNeg = false;

   private const string FontWord = "Arial";
   private const int FontSize = 9;

   public  int SIDE_WIDTH = 400;
   public  int SIDE_HEIGHT = 400;
   private const int CHART_TOP = 60;
   private  int CHART_HEIGHT = 300;
   private const int CHART_LEFT = 60;
   private const int CHART_WIDTH = 300;

   public void Render(string[] word,int[] data, out string fileName)
   {
    
    for(int i=0;i<data.Length;i++)
    {
     if(data[i]<0)
     {
      Pic_Height *=2;
      hasNeg = true;
      SIDE_HEIGHT = (int)(SIDE_HEIGHT*1.5);
      CHART_HEIGHT =(int)(CHART_HEIGHT*0.3);
      break;
     }
    }
    ChartUtil cu = new ChartUtil();

    PointF[] zuoBiaoStart;
    PointF[] zuoBiaoEnd;
    Graphics g;
    Bitmap bm;
    createCanvas(out bm, out g, out zuoBiaoEnd, out zuoBiaoStart,cu);

    //画坐标轴
    int chiDu = drawAxes(data, zuoBiaoStart, g, zuoBiaoEnd,cu);

    //画条形图
    float barWidth = CHART_WIDTH / (2 * 2);
    PointF barOrigin = new PointF(CHART_LEFT + (barWidth / 2),0);
    float barHeight = 2;
    for(int i=0;i<2;i++)
    {
     barHeight = ((float)data[i]/chiDu) *(CHART_HEIGHT/5) ;
     if(barHeight<0)
     {
      barOrigin.Y = CHART_TOP + CHART_HEIGHT;
     }
     else
     {
      barOrigin.Y = CHART_TOP + CHART_HEIGHT - barHeight;
     }
     
     g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),barOrigin.X,barOrigin.Y,barWidth,Math.Abs(barHeight));
     barOrigin.X = barOrigin.X + (barWidth * 2);
    }
    
  
    //画右上角的说明图形
    drawInstruction(CHART_LEFT, CHART_HEIGHT, CHART_TOP, g, word, data);

    //输出图形
    fileName = cu.PicPath + Guid.NewGuid().ToString() + ".gif";
    bm.Save(fileName, ImageFormat.Gif);
  
    //资源回收
    bm.Dispos