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.dispose(); 
    g.dispose(); 
   }
   private  int drawaxes(int[] data, pointf[] zuobiaostart, graphics g, pointf[] zuobiaoend,chartutil cu)
   {
    int chidu =1;
    int[] t = cu.getzuobiaovalue(data,out chidu);
    for(int i=0;i<zuobiaostart.length;i++)
    {
     pointf txtpos = new pointf();
     txtpos.x = zuobiaostart[i].x -50;
     txtpos.y = zuobiaostart[i].y - 5;
     g.drawstring(t[i].tostring(),new font(fontword,8),brushes.black,txtpos); 
     g.drawline(pens.black,zuobiaostart[i],zuobiaoend[i]);   
    }
    return chidu;
   }
   private  void createcanvas(out bitmap bm, out graphics g, out pointf[] zuobiaoend, out pointf[] zuobiaostart,chartutil cu)
   {
    //建立一個graphics對象實例 
    bm = new bitmap(pic_width,pic_height);
    g = graphics.fromimage(bm);
    //設置條圖圖形和文字屬性 
    g.scaletransform((convert.tosingle(pic_width))/side_width,(convert.tosingle(pic_height))/side_height); 
    g.smoothingmode = smoothingmode.default; 
    g.textrenderinghint = textrenderinghint.antialias; 
 
    //設定畫布和邊 
    g.clear(color.white); 
    g.drawrectangle(pens.black,0,0,side_width-1,side_height-1); 
 
    //設置條形圖的邊 
    g.drawrectangle(new pen(color.black,1),chart_left,chart_top,chart_width, chart_height);
    if(hasneg)
    {
     g.drawrectangle(new pen(color.black,1),chart_left,chart_top+chart_height,chart_width, chart_height);
    }
    zuobiaoend = null;
   zuobiaostart = cu.getzuobiaopoint(new pointf(chart_top,chart_left),chart_height,chart_width,out zuobiaoend,hasneg);
   }
   private  void drawinstruction(int chart_left, int chart_height, int chart_top, graphics g, string[] word, int[] data)
   {
    pointf colsnamepoint = new pointf(chart_left + 55,chart_height + chart_top + 2);
    if(hasneg)
    {
     colsnamepoint = new pointf(chart_left + 55,chart_height*2 + chart_top + 2);
    }
    
    pointf boxorigin = new pointf(pic_width*5/6-30,chart_top*1/3); 
    pointf textorigin = new pointf(pic_width*4/5+8,chart_top*1/3 -3 ); 
    for(int i=0;i<2;i++) 
    { 
     g.fillrectangle(new solidbrush(chartutil.getchartitemcolor(i)),boxorigin.x,boxorigin.y,20,10); 
     //g.drawrectangle(pens.black,boxorigin.x,boxorigin.y,20,10); 
     g.drawstring(word[i],new font(fontword,fontsize),brushes.darkblue,colsnamepoint);
     g.drawstring(data[i].tostring(),new font(fontword,fontsize),brushes.black,textorigin); 
     colsnamepoint.x += 150;
     boxorigin.y += 15; 
     textorigin.y += 15; 
    }
   }
  } 
  public class chartutil 
  { 
   
   public  readonly string picpath = system.appdomain.currentdomain.basedirectory + "temp/";
   public  pointf[] getzuobiaopoint(pointf orign,int height,int width,out pointf[] endpoint,bool hasneg)
   {
    int num = 6;
    if(hasneg) num = 11;
    pointf[] startpoint = new pointf[num];
    endpoint = new pointf[num];
    for(int i =0;i<num;i++)
    {
     pointf pfstart = new pointf();
     pointf pfend = new pointf();
     pfstart.x = orign.x;
     pfend.x = orign.x + width;
     pfstart.y = orign.y + i*height/5;
     pfend.y = pfstart.y;
     startpoint[i] = pfstart;
     endpoint[i] = pfend;
    }
    return startpoint;
   }
   public  int[] getzuobiaovalue(int[] data,out int chidu)
   {
    int[] result = new int32[11];
    int max = data[0];
    
    for(int i=0;i<data.length;i++)
    {
     if(max < data[i])
      max = data[i];
    }
    string strmax = max + "";
    chidu =(int)((convert.toint32(strmax.substring(0,1)) +1)* math.pow(10,strmax.length-1))/5;
    for(int i=0;i<result.length;i++)
    {
     result[i] = chidu* (5-i) ;
    }
    return result;
   }
   
   #region get color
   public static color getchartitemcolor(int itemindex) 
   { 
    color selectedcolor; 
    switch(itemindex) 
    { 
     case 0: 
      selectedcolor = color.blue; 
      break; 
     case 1: 
      selectedcolor = color.red; 
      break; 
     case 2: 
      selectedcolor = color.yellow; 
      break; 
     case 3: 
      selectedcolor = color.purple; 
      break; 
     default: 
      selectedcolor = color.green; 
      break; 
    } 
    return selectedcolor; 
   }
   #endregion
  } 
 } 
新聞熱點
疑難解答