How to make pie and columnar graphs in C # WinForm

Source: Internet
Author: User
Tags foreach count
Pie chart | Histogram when our software needs a variety of pie and bar graphs to represent data, we might think of graphics controls or Third-party controls in offices, but now third-party controls are mostly registered, and some free controls will have developers ' tags. For graphical controls that use offices, they cannot be well controlled in a program and are less easy to use, so here's how you can use GDI + to implement pie and column graphs and database joins to display data in C #.
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 Baselayer
{
public class Piechart
{
Public Piechart ()
{
}
Render is a large graphic title, the graph opens the small title, the graph width, the graph length, the pie chart dataset and the pie chart data set to represent the data
Public Image Render (string title, string subtitle, int width, int height, DataSet chartdata,int dataline)
{
const int side_length = 400;
const int pie_diameter = 200;
DataTable dt = chartdata.tables[0];

Get the total cardinality in the pie chart by inputting parameters
float sumdata = 0;
foreach (DataRow dr in Dt. Rows)
{
Sumdata + + convert.tosingle (Dr[dataline]);
}
Produces an image object, and thus produces a graphics object
Bitmap BM = new Bitmap (width,height);
Graphics g = graphics.fromimage (BM);
Set properties of Object G
G.scaletransform ((Convert.tosingle (width))/side_length, (convert.tosingle (height))/side_length);
G.smoothingmode = Smoothingmode.default;
G.textrenderinghint = Textrenderinghint.antialias;

setting of canvas and Edge
G.clear (Color.White);
G.drawrectangle (pens.black,0,0,side_length-1,side_length-1);
Gao Chart Title
g.DrawString (title,new Font ("Tahoma"), Brushes.black,new PointF (5,5));
The legend of the Gao graph
g.DrawString (subtitle,new Font ("Tahoma",), Brushes.black,new PointF (7,35));
Gao Map
float Curangle = 0;
float Totalangle = 0;
for (int i=0;i<dt. rows.count;i++)
{
Curangle = convert.tosingle (dt. Rows[i][dataline])/sumdata * 360;

G.fillpie (New SolidBrush (Chartutil.getchartitemcolor (i)), 100,65,pie_diameter,pie_diameter,totalangle,curangle);
G.drawpie (Pens.black,100,65,pie_diameter,pie_diameter,totalangle,curangle);
Totalangle + = Curangle;
}
Paint Case box and its text
G.drawrectangle (pens.black,200,300,199,99);
g.DrawString ("Chart description", New Font ("Tahoma", 12,fontstyle.bold), Brushes.black,new PointF (200,300));

Drawing example Items
PointF boxorigin = new PointF (210,330);
PointF textorigin = new PointF (235,326);
float percent = 0;
for (int i=0;i<dt. rows.count;i++)
{
G.fillrectangle (New SolidBrush (Chartutil.getchartitemcolor (i)), boxorigin.x,boxorigin.y,20,10);
G.drawrectangle (pens.black,boxorigin.x,boxorigin.y,20,10);
Percent = convert.tosingle (dt. Rows[i][dataline])/sumdata * 100;
g.drawstring (dt. ROWS[I][1]. ToString () + "-" + dt. Rows[i][0]. ToString () + "(" + percent.) ToString ("0") + "%", new Font ("Tahoma", "Brushes.black,textorigin");
BOXORIGIN.Y + 15;
TEXTORIGIN.Y + 15;
}
Recycle resources
G.dispose ();
Return (Image) BM;

}
}

Draw a bar chart
public class Barchart
{
Public Barchart ()
{
}
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, string subtitle, int width, int height, DataSet chartData)
{
const int side_length = 400;
const int chart_top = 75;
const int chart_height = 200;
const int chart_left = 50;
const int chart_width = 300;
DataTable dt = chartdata.tables[0];

Calculate the highest point
float Highpoint = 0;
foreach (DataRow dr in Dt. Rows)
{
if (Highpoint<convert.tosingle (dr[0]))
{
Highpoint = Convert.tosingle (dr[0]);
}
}
Create a Graphics object instance
Bitmap BM = new Bitmap (width,height);
Try
{
Graphics g = graphics.fromimage (BM);
Set bar graph and Text properties
G.scaletransform ((Convert.tosingle (width))/side_length, (convert.tosingle (height))/side_length);
G.smoothingmode = Smoothingmode.default;
G.textrenderinghint = Textrenderinghint.antialias;

Set Canvas and Edge
G.clear (Color.White);
G.drawrectangle (pens.black,0,0,side_length-1,side_length-1);
Draw a big headline
g.DrawString (title,new Font ("Tahoma"), Brushes.black,new PointF (5,5));
Draw a small title
g.DrawString (subtitle,new Font ("Tahoma",), Brushes.black,new PointF (7,35));
Draw a bar chart
float barwidth = chart_width/(dt. Rows.Count * 2);
PointF barorigin = new PointF (Chart_left + (BARWIDTH/2), 0);
float barheight = dt. Rows.Count;
for (int i=0;i<dt. rows.count;i++)
{
Barheight = convert.tosingle (dt. ROWS[I][0]) * 200/highpoint * 1;
BARORIGIN.Y = Chart_top + chart_height-barheight;
G.fillrectangle (New SolidBrush (Chartutil.getchartitemcolor (i)), barorigin.x,barorigin.y,barwidth,barheight);
barorigin.x = barorigin.x + (BarWidth * 2);
}
Set Edge
G.drawline (New Pen (color.black,2), new Point (Chart_left,chart_top), new Point (Chart_left,chart_top + chart_height));
G.drawline (New Pen (color.black,2), new Point (Chart_left,chart_top + chart_height), new Point (Chart_left + chart_width, Chart_top + chart_height));
Paint example Boxes and text
G.drawrectangle (New Pen (color.black,1), 200,300,199,99);
g.DrawString ("Chart description", New Font ("Tahoma", 12,fontstyle.bold), Brushes.black,new PointF (200,300));

Drawing example
PointF boxorigin = new PointF (210,330);
PointF textorigin = new PointF (235,326);
for (int i=0;i<dt. rows.count;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 (dt. ROWS[I][1]. ToString () + "-" + dt. Rows[i][0]. ToString (), New Font ("Tahoma", "Brushes.black,textorigin");
BOXORIGIN.Y + 15;
TEXTORIGIN.Y + 15;
}
Output graphics
G.dispose ();
return BM;
}
Catch
{
return BM;
}
}
}
public class Chartutil
{
Public Chartutil ()
{
}
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;
}
}
}
The above is a complete winform in the production of pie chart and column diagram source program, you can through the above procedures to make changes to meet the needs of their own procedures.

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.