How to Create a pie chart and a column chart in winform of C #

Source: Internet
Author: User
When our software requires various pie charts and bar charts to represent data, we may think of using graphical controls or third-party controls in offices, but most of the third-party controls need to be registered now, some free controls have developer tags. However, for image controls that use offices Program It is well controlled, and its ease of use is also relatively low, so here I provide a method to use GDI + in C # To connect the pie chart and column chart to the database to display data.
Using system;
Using system. IO; // used for File Access
Using system. Data; // used for data access
Using system. Drawing; // provides basic functions for drawing GDI + graphics.
Using system. Drawing. Text; // provides advanced functions for drawing GDI + graphics.
Using system. Drawing. drawing2d; // provides advanced 2D and vector graphics Functions.
Using system. Drawing. imaging; // provides advanced functions for drawing GDI + graphics.
Namespace baselayer
{
Public class piechart
{
Public piechart ()
{
}
// Render It is the big title of the graph, the title of the graph, the width of the graph, the length of the graph, the data set of the pie chart and the data set of the pie chart to represent.
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];

// Input parameters to obtain the total base in the pie chart
Float sumdata = 0;
Foreach (datarow DR in DT. Rows)
{
Sumdata + = convert. tosingle (Dr [dataline]);
}
// Generate an image object and generate a graphics object
Bitmap Bm = new Bitmap (width, height );
Graphics G = graphics. fromimage (BM );
// Set the properties of object g
G. scaletransform (convert. tosingle (width)/side_length, (convert. tosingle (height)/side_length );
G. smoothingmode = smoothingmode. default;
G. textrenderinghint = textrenderinghint. antialias;

// Canvas and edge settings
G. Clear (color. White );
G. drawrectangle (pens. Black, SIDE_LENGTH-1, SIDE_LENGTH-1 );
// Pie chart title
G. drawstring (title, new font (tahoma, 14), brushes. Black, new pointf (5, 5 ));
// Pie chart legend
G. drawstring (subtitle, new font (tahoma, 12), brushes. Black, new pointf (7,35 ));
// Pie chart
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;
}< br> // draw the legend box and 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);

// Draw legend 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])/Samsung data * 100;
G. drawstring (DT. rows [I] [1]. tostring () +-+ dt. rows [I] [0]. tostring () + (+ percent. tostring (0) + %), new font (tahoma, 10), brushes. black, textorigin );
Boxorigin. Y + = 15;
Textorigin. Y + = 15;
}
// Reclaim Resources
G. Dispose ();
Return (image) BM;

}
}

// Draw a bar chart
Public class barchart
{
Public barchart ()
{
}
// Render is a big title, title, width, length, and 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 character height = 200;
Const int chart_left = 50;
Const int chart_width = 300;
Datatable dt = chartdata. Tables [0];

// Calculate the highest Vertex
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 graph and text attributes
G. scaletransform (convert. tosingle (width)/side_length, (convert. tosingle (height)/side_length );
G. smoothingmode = smoothingmode. default;
G. textrenderinghint = textrenderinghint. antialias;

// Set the canvas and edge
G. Clear (color. White );
G. drawrectangle (pens. Black, SIDE_LENGTH-1, SIDE_LENGTH-1 );
// Draw a large title
G. drawstring (title, new font (tahoma, 14), brushes. Black, new pointf (5, 5 ));
// Draw a title
G. drawstring (subtitle, new font (tahoma, 12), 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 the 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 ));
// Draw a legend box 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 ));

// Draw a legend
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, 10), brushes. black, textorigin );
Boxorigin. Y + = 15;
Textorigin. Y + = 15;
}
// Output image
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;
}
}
}

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.