In our program development process will often need to draw graphs and histogram, etc., especially when doing statistical functions. But sometimes we feel that there is no need to use third party controls (such as zedgraph, etc.), which is the ability to write our own code to implement these graphical drawings. The following is my use in the development process of two pieces of code, is to share everyone, I hope to bring some help, such as improper please treatise!
1. Column chart, the effect of the chart is as follows
The code is as follows:
Note: Please note that some elements in the parameter charttable graph need to be taken from the charttable. Please see the code for details.
Render is a large graphic title, the graph opens the small title, the graph width, the graph length, the pie chart data set and the pie chart dataset
Public Image Render (string title, int width, int height, DataTable charttable)
{
Bitmap BM = new Bitmap (width, height);
Graphics g = graphics.fromimage (BM);
G.clear (Color.White);
DataTable dt = charttable;
const int top = 30;
const int left = 35;
if (Width < left * 2 | | Height < top * 2)
{
g.DrawString ("Plot area is too small", new Font ("Tahoma", 8),
Brushes.blue, New PointF (0, 0));
return BM;
}
Calculate the highest point
float Highpoint = 1;
foreach (DataRow dr in Dt. Rows)
{
if (Highpoint < Convert.tosingle (dr[0))
{
Highpoint = Convert.tosingle (dr[0]);
}
if (Highpoint < Convert.tosingle (dr[1))
{
Highpoint = Convert.tosingle (dr[1]);
}
}
Try
{
Draw a big headline
g.DrawString (title, New Font ("Tahoma",), Brushes.black, New PointF (2, 2));
StringFormat Drawformat = new StringFormat ();
Drawformat.formatflags = stringformatflags.directionvertical;
g.DrawString ("[red-] + DT". Columns[0]. ToString () + "]", New Font ("Tahoma", 8),
Brushes.Red, New PointF (2, top), Drawformat);
g.DrawString ("[Blue--" + dt.) COLUMNS[1]. ToString () + "]", New Font ("Tahoma", 8),
Brushes.blue, new PointF (top), Drawformat);
Draw a bar chart
float BarWidth = (convert.tosingle (width)-left)/(dt. Rows.Count * 3 + 1);
PointF barorigin = new PointF (left + barwidth, 0);
float barheight = dt. Rows.Count;
float topfontsize = (barwidth/highpoint.tostring (). Length);
if (Topfontsize > 2*top/3)
{
Topfontsize = 2*TOP/3;
}
if (Topfontsize < 5)
{
Topfontsize = 5;
}
for (int i = 0; i < dt. Rows.Count; i++)
{
The size of the bottom font
float bottomfontsize = (2 * barwidth/dt. ROWS[I][2]. ToString (). Length) + 2;
if (Bottomfontsize > 2 * top/3)
{
Bottomfontsize = 2 * TOP/3;
}
Barheight = convert.tosingle (dt. ROWS[I][0]) * (HEIGHT-2 * top)/highpoint * 1;
BARORIGIN.Y = Height-barheight-top;
G.fillrectangle (New SolidBrush (color.red), barorigin.x, BARORIGIN.Y, BarWidth, barheight);
Bottom of Column chart
g.drawstring (dt. ROWS[I][2]. ToString (), New Font ("Tahoma", bottomfontsize), Brushes.black,
New PointF (barorigin.x, height-top));
Top of Bar chart
g.drawstring (dt. Rows[i][0]. ToString (), New Font ("Tahoma", topfontsize), brushes.red,
New PointF (barorigin.x, BARORIGIN.Y-3*TOPFONTSIZE/2));
barorigin.x = barorigin.x + barwidth;
Barheight = convert.tosingle (dt. ROWS[I][1]) * (HEIGHT-2 * top)/highpoint * 1;
BARORIGIN.Y = Height-barheight-top;
G.fillrectangle (New SolidBrush (Color.Blue), barorigin.x, BARORIGIN.Y, BarWidth,
Barheight);
Top of Bar chart
g.drawstring (dt. ROWS[I][1]. ToString (), New Font ("Tahoma", topfontsize), Brushes.blue,
New PointF (barorigin.x, barorigin.y-3 * topfontsize/2));
barorigin.x = barorigin.x + (BarWidth * 2);
}
Set Edge
G.drawline (New Pen (Color.Blue, 2), new Point (left, top),
New Point (left, height-top));
G.drawline (New Pen (Color.Blue, 2), new Point (left, height-top),
New Point (left + width, height-top));
G.dispose ();
return BM;
}
Catch
{
return BM;
}
}