Chart.cs
Using System;
Using System.Drawing;
Namespace
{
<summary>
Summary description of the Chart.
/// ==================================================================================================
///
ClassName:Report.Chart
Intro:
Example:
ver:0.2
///
Author:ttyp
Email:ttyp@21cn.com
Date:2007-7-30
/// ==================================================================================================
</summary>
public class Chart
{
Public Chart () {}
private String _data = "";
private int _width = 100;
private int _height = 100;
private int _padding= 8;
Private Color _grid_color = Color.FromArgb (0x93,0xbe,0xe2);
Private Color _border_color = Color.FromArgb (0x93,0xbe,0xe2);
Private font _font = new Font ("Arial", 8);
Public font Font
{
get {return _font;}
set {_font = value;}
}
Public Color BorderColor
{
get {return _border_color;}
set {_border_color = value;}
}
Public Color Gridcolor
{
get {return _grid_color;}
set {_grid_color = value;}
}
public int Padding
{
get {return _padding;}
set {_padding = Math.max (0,value);}
}
public int Width
{
get {return _width;}
set {_width = Math.max (0,value);}
}
public int Height
{
get {return _height;}
set {_height = Math.max (0,value);}
}
public string Data
{
get {return _data;}
set {_data = value;}
}
public void Render ()
{
int width = this. Width;
int height = this. Height;
int padding = this. Padding;
System.Drawing.Bitmap image = new System.Drawing.Bitmap (width,height);
Graphics g = graphics.fromimage (image);
Empty picture background color
G.clear (Color.White);
Dashed brush
Pen dot = new Pen (this. Gridcolor);
Dot. DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Solid line Brush
Pen solid = new Pen (this. BorderColor);
Text font
Font font = this. Font;
Try
{
Redundancy, removing the final data segmentation tag, preventing null data
if (this. Data.endswith (";"))
{
This. Data = this. Data.substring (0,this. DATA.LENGTH-1);
}
String[] Info = this. Data.split (';'); Data information
if (info. length>=2)
{
String[] lines = info[0]. Split (', '); Legend
String[] units = info[1]. Split (', '); Unit and title format, a,b,c,d a ordinate unit B ordinate format N digit D time back is specific format, C coordinate unit d horizontal coordinate format (same b)
Curve Color Table
color[] color = new Color[]{color.blue,color.green,color.red,color.gray,color.black,color.magenta,color.cyan, Color.yellow,color.deeppink,color.burlywood,color.darkred,color.gold};
Size of legend Text
SizeF Sfont = getmaxsize (Lines,g,font);
Get the height of the tick text
int textHeight = (int) (SFONT.HEIGHT*3/2);
Number of curve points
int points = info. Length-2;
Get a set of points in a curve
string[,] curve = new String[info. Length-2,lines. LENGTH+1];
for (int i=0;i<points;i++)
{
string[] L = info[i+2]. Split (', ');
int len = l.length;
for (int j=0;j<=lines. length;j++)
{
if (J<len)
{
CURVE[I,J] = L[j];
}
Else
{
CURVE[I,J] = "N"; Non-data, no line drawing
}
}
}
Get maximum, minimum value
Double Maxy,miny,maxx,minx;
Getmaxmin (curve,out maxy,out miny,out maxx,out MinX);
Redundancy Maximum minimum value
if (Maxy==miny)
{
if (maxy==0)
{
Maxy = 10;
Miny =-10;
}
Else
{
if (maxy>0)
{
Maxy = maxy*2;
Miny = 0;
}
Else
{
Maxy = 0;
Miny = maxy*2;
}
}
}
if (Maxx==minx)
{
if (maxx==0)
{
MaxX = 10;
MinX =-10;
}
Else
{
if (maxx>0)
{
MaxX = maxx*2;
Miny = 0;
}
Else
{
MaxX = 0;
MinX = maxx*2;
}
}
}
Gets the top or bottom of the coordinate box
float left = (padding*2+sfont.height+2 + sfont.width + padding+getmaxsize (units[1],g,font). width+padding);
float bottom = height-padding-textheight;
float top = padding;
float right = width-padding;
Gets the width and height of the curved frame (slightly smaller than the coordinate box)
float ywidth = bottom-top-getmaxsize (Units[0],g,font). height*3/2-padding;
float xwidth = right-left-getmaxsize (Units[3],g,font). WIDTH/2-sfont.width-padding;
//---------------------------------------------------------------------------------
Get Maximum row
int maxrow = (int) (ywidth/(sfont.height/2*3));
MaxRow = Math.max (maxrow,1);
Get y Step value
float stepyv = (float) ((maxy-miny)/(MaxRow));
if (units[1). LENGTH>1)
{
Integer segmentation, adjusting maximum row and maximum minimum value
if (units[1). Substring (0,1). ToLower () = = "D")
{
Maxy = math.ceiling (Maxy);
Miny = Math.floor (miny);
Stepyv = (float) math.ceiling ((maxy-miny)/maxrow);
MaxRow = (int) ((maxy-miny)/stepyv);
}
}
float stepy = (float) ((ywidth/(maxy-miny)) *stepyv);
//---------------------------------------------------------------------------------
Get the largest grid column (up to 10 columns)
int maxcol = points;
Maxcol = Math.min (Points,maxcol);
Maxcol = Math.max (maxcol,1);
Get x Step value
float STEPXV = (float) ((Maxx-minx)/(Maxcol));
if (units[3). LENGTH>1)
{
Integer segmentation, adjusting maximum and minimum values, and stepping
if (units[3). Substring (0,1). ToLower () = = "D")
{
MaxX = math.ceiling (MaxX);
MinX = Math.floor (MinX);
STEPXV = (float) math.ceiling ((Maxx-minx)/maxcol);
Maxcol = (int) ((Maxx-minx)/STEPXV);
}
}
Get maximum number of columns to display
int dispcol = (int) (xwidth)/(Getmaxsize (units[3). Substring (1), G,font). width+padding));
Dispcol = Math.max (dispcol,1);
If the maximum display column is less than the maximum column, you should reduce the
if (Dispcol<maxcol)
{
STEPXV = (float) math.ceiling ((Maxx-minx)/dispcol);
Maxcol = (int) ((Maxx-minx)/STEPXV);
}
float stepx = (float) ((xwidth/(Maxx-minx)) *STEPXV);
Get the maximum number of curves
int maxline = color. Length;
Maxline = Math.min (maxline,lines. Length);
Paint Example Border
G.drawrectangle (solid, padding, (height-((sfont.height+5) *maxline+2*padding))/2,padding*2+sfont.height+2 + Sfont.width, (sfont.height+5) *maxline+2*padding);
Drawing example
for (int i=0;i<maxline;i++)
{
SolidBrush fb = new SolidBrush (Color[i]);
SolidBrush bl = new SolidBrush (color.black);
Paint Example Box
G.fillrectangle (Fb,padding*2, (height-(sfont.height+5) *maxline+2*padding)/2+ sfont.height+5 (*i+padding), Sfont.height, Sfont.height);
Paint Example Text
g.DrawString (Lines[i], font, BL, padding*2+sfont.height+2, (height-(sfont.height+5) *maxline+2*padding))/2+ ( sfont.height+5) *i+padding);
}
Draw coordinates
G.drawline (Solid,left,top,left,bottom); Y
G.drawline (Solid,left,bottom,right,bottom); X
Draw a coordinate arrow
G.drawline (SOLID,LEFT,TOP,LEFT-PADDING/3,TOP+PADDING/2); Y Arrow
G.drawline (SOLID,LEFT,TOP,LEFT+PADDING/3,TOP+PADDING/2);
G.drawline (SOLID,RIGHT,BOTTOM,RIGHT-PADDING/2,BOTTOM-PADDING/3); X Arrow
G.drawline (SOLID,RIGHT,BOTTOM,RIGHT-PADDING/2,BOTTOM+PADDING/3);
Draw x Scale
for (int i=0;i<=maxcol;i++)
{
SolidBrush bl = new SolidBrush (color.black);
if (i>0)
{
G.drawline (Dot,left+i*stepx,top+padding,left+i*stepx,bottom);
}
String text = "";
Switch (units[3). Substring (0,1). ToString ())
{
Case "N":
Text = (minx+stepxv*i). ToString (units[3). Substring (1));
Break
Case "D":
Text = datetime.fromoadate ((int) (minx+stepxv*i)). ToString (units[3). Substring (1));
Break
}
SizeF XF = getmaxsize (Text,g,font);
g.DrawString (text, font, BL, left+i*stepx-xf. Width/2,bottom+xf. HEIGHT/2);
if (i==0)
{
g.DrawString (units[2], font, BL, right-getmaxsize (Units[2],g,font). Width,bottom);
}
if (points<=1)
{
Break
}
}
Draw Y Scale
for (int i=0;i<=maxrow;i++)
{
SolidBrush bl = new SolidBrush (color.black);
if (i>0)
{
G.drawline (dot,left,bottom-i*stepy,right-padding,bottom-i*stepy);
}
String text = "";
Switch (units[1). Substring (0,1). ToString ())
{
Case "N":
Text = (MINY+I*STEPYV). ToString (units[1). Substring (1));
Break
Case "D":
Text = datetime.fromoadate (int. Parse (curve[i,0])). ToString (units[1). Substring (1));
Break
}
SizeF XF = getmaxsize (Text,g,font);
g.DrawString (text, font, BL, left-xf. Width,bottom-stepy*i-xf. HEIGHT/2);
if (i==0)
{
g.DrawString (units[0], font, BL, left-getmaxsize (Units[0],g,font). Width-3,top);
}
}
Draw a picture's border line
G.drawrectangle (solid, 0, 0, image. Width-1, image. HEIGHT-1);
float[] px = new Float[maxline];
float[] py = new Float[maxline];
Bool[] PS = new Bool[maxline];
Draw curve
for (int j=0;j<points;j++)
{
Float v = float. Parse (curve[j,0]);
float cx = (float) (left+ (xwidth) * (V-minx)/(Maxx-minx));
for (int i=0;i<maxline;i++)
{
Try
{
float w = float. Parse (curve[j,i+1]);
float cy = (float) (bottom-(ywidth) * (w-miny)/(Maxy-miny));
if (Ps[i])
{
Pen cp = new pen (color[i]);
G.drawline (Cp,px[i],py[i],cx,cy);
}
Px[i] = CX;
Py[i] = cy;
Ps[i] = true;
if (points==1)
{
Image. SetPixel ((int) CX, (int) cy,color[i]);
}
}
Catch
{
Ps[i] = false;
}
}
}
}
Else
{
String msg = "no data";
g.DrawString (msg,font,new SolidBrush (Color.Black), Width-getmaxsize (Msg,g,font). Width)/2, (Height-getmaxsize (Msg,g,font). Height)/2);
}
System.Web.HttpContext.Current.Response.ClearContent ();
System.web.httpcontext.current.response.contenttype= "Image/gif";
Image. Save (system.web.httpcontext.current.response.outputstream,system.drawing.imaging.imageformat.gif);
}
Finally
{
G.dispose ();
Image. Dispose ();
}
}
Private SizeF getmaxsize (string[] s,graphics G,font f)
{
string max = "";
for (int i=0;i<s.length;i++)
{
if (s[i). Length>max. Length)
{
max = S[i];
}
}
Return g.measurestring (MAX,F);
}
Private SizeF getmaxsize (string s,graphics g,font f)
{
Return g.measurestring (S,F);
}
private void Getmaxmin (string[,] data,out double maxy,out double miny,out double maxx,out double MinX)
{
int row = 0;
int col = 0;
Double M =double. Minvalue,n=double. MaxValue;
Double P =double. Minvalue,q=double. MaxValue;
row = data. GetLength (0);
Col = data. GetLength (1);
for (int i=0;i<row;i++)
{
for (int j=0;j<col;j++)
{
Double v = 0;
Try
{
v = Double. Parse (Data[i,j]);
if (j>0)
{
if (v>m)
{
m = v;
}
if (v<n)
{
n = v;
}
}
Else
{
if (v>p)
{
p = V;
}
if (V<Q)
{
Q = v;
}
}
}
catch{}
}
}
Maxy = m;
Miny = n;
MaxX = p;
MinX = q;
}
}
}