C # Drawing (can handle negative values)

Source: Internet
Author: User
Tags bool tostring

Using System;
Using system.io;//for file access
Using system.data;//for data access
Using system.drawing;//provides basic functionality for drawing GDI + graphics
Using system.drawing.text;//provides advanced functionality for drawing GDI + graphics
Using system.drawing.drawing2d;//provides picture advanced two-dimensional, vector graphics function
Using system.drawing.imaging;//provides advanced functionality for drawing GDI + graphics


Namespace Drawpicture
{
<summary>
Summary description of the 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 =;

   public void Render (string[] word,int[] data, out string fileName)
   {
&NBSP;&NBSP;&NBSP;&NBSP
    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 of BM, out of G, out of zuobiaoend, out ZUOBIAOSTART,CU);

Draw axes
int chidu = drawaxes (data, Zuobiaostart, G, ZUOBIAOEND,CU);

Draw a bar chart
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);
}


Illustration of the upper right corner of the drawing
Drawinstruction (Chart_left, Chart_height, chart_top, G, Word, data);

Output graphics
FileName = cu. Picpath + guid.newguid (). ToString () + ". gif";
Bm. Save (FileName, imageformat.gif);

Resource Recycling
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]);    
&NBSP;&NBSP;&NBSP;&NBSP}
    return Chidu;
   .}

   private  void Createcanvas (out of Bitmap BM, out of Graphics G, out pointf[) Zuobiaoend, out pointf[] Zuobiaostart,chartutil cu)
   {
    //Establish a Graphics object instance
   &NBSP;&NBSP;BM = new Bitmap (pic_width,pic_height);
    g = graphics.fromimage (BM);
    //layout Graphic and Text properties

    g.scaletransform (Convert.tosingle (pic_width))/side_width, Convert.ToSingle (Pic_ Height))/side_height);
    g.smoothingmode = Smoothingmode.default;
    g.textrenderinghint = Textrenderinghint.antialias;
 
    //set the canvas and Edge
    g.clear (Color.White);
    g.drawrectangle (pens.black,0,0,side_width-1,side_height-1);
 
    //Set the edge of the bar (
    g.drawrectangle) (The 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
}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.