C # using GDI +
When our software needs a variety of pie charts and histograms to represent data, we might think of using graphical controls in offices or third-party controls, but now most third-party controls need to be registered, and some free controls will have developer tags. For graphical controls that use offices, they are not very well controlled in the program and are less easy to use, so here I give you a way to use GDI + in C # to display data in a pie chart and histogram with a database join.
Using System; Using system.io;//for file access using system.data;//for data access using system.drawing;//provides the basic functionality of drawing GDI + graphics using system.drawing.text;/ /provides advanced features for drawing GDI + graphics using system.drawing.drawing2d;//provides advanced two-dimensional drawing, vector graphics features using system.drawing.imaging;//provides high-level features for drawing GDI + graphics Namespace Baselayer {public class Piechart {public Piechart () {}//render is a graphic headline, a graph with a small title, a graph width, a graph length, a pie chart dataset, and a pie chart data set to represent Out of 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]; By entering parameters, get the total cardinality of the pie chart 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); Sets the properties of the 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,0,0,side_length-1,side_length-1); appeased Figure title g.DrawString (title,new Font ("Tahoma"), Brushes.black,new PointF (5,5)); appeased diagram of the legend g.drawstring (subtitle,new Font ("Tahoma", "n"), Brushes.black,new PointF (7,35)); appeased Figure 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; }//Drawing example 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", "ten"), Brushes.black,textorigin); BOXORIGIN.Y + = 15; TEXTORIGIN.Y + = 15; }//Recycling resources g.dispose (); Return (Image) BM; }}//Draw bar public class Barchart {public Barchart () {}//render is a graphic headline, graph open caption, graphic width, graph length, pie chart DataSet and pie chart DataSet public Ima GE 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]; Calculates the highest point float Highpoint = 0; foreach (DataRow dr in Dt. Rows) {if (Highpoint<convert.tosingle (Dr[0])) {Highpoint = Convert.tosingle (dr[0]); }}//Build a GrapHics Object instance Bitmap BM = new Bitmap (width,height); try {Graphics g = graphics.fromimage (BM); Set the 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 the headline g.drawstring (Title,new Font ("Tahoma"), Brushes.black,new PointF (5,5)); Draw a small title g.drawstring (subtitle,new Font ("Tahoma", "n"), 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); Baroriginbarorigin.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)); Drawing Example 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)); 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", "ten"), 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 Sele Ctedcolor; 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 chart source program, you can change the above procedures to make the necessary to meet their own procedures. C # uses GDI + to implement methods for pie charts and histogram joins to display data with a database.