How to draw pie chart and bar chart in C #
This article mainly introduces how to draw pie chart and bar chart in C#. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
The details are as follows
# Code is as follows:
Using System;using System.Collections;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Drawing.Drawing2D;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace TEST3._2 {public partial class Form1: Form {public Form1 () {InitializeComponent () } private void Form1_Load (object sender, EventArgs e) {BarMap ();} private void BitMap () / / pie chart {int [] saleNum = {300,500,400}; int sum = 0, threeNum = 0, fourNum = 0, fiveNum = 0; for (int I = 0; I < saleNum.Length Sum +) {sum + = saleNum [I]; if (I = 0) threeNum = saleNum [0]; else if (I = = 1) fourNum = saleNum [1]; else fiveNum = saleNum [2] } int height = pictureBox1.Height, width = pictureBox1.Width; Bitmap bitmap = new Bitmap (width, height); Graphics g = Graphics.FromImage (bitmap); g.Clear (Color.White); Pen pen1 = new Pen (Color.Red); Brush brush2 = new SolidBrush (Color.PowderBlue); Brush brush3 = new SolidBrush (Color.Blue) Brush brush4 = new SolidBrush (Color.Wheat); Brush brush5 = new SolidBrush (Color.Orange); Font font1 = new Font ("Couriter New", 16, FontStyle.Bold); Font font2 = new Font ("Couriter New", 10); g.FillRectangle (brush2, 0,0, width, height) G.DrawString ("monthly sales percentage pie chart", font1, brush3, new Point (70,20); int piex = 100,60, piew = 200,pieh = 200; float angle1 = Convert.ToSingle ((360 / Convert.ToSingle (sum)) * Convert.ToSingle (threeNum)); float angle2 = Convert.ToSingle ((Convert.ToSingle (sum)) * Convert.ToSingle (fourNum)) Float angle3 = Convert.ToSingle (Convert.ToSingle (sum)) * Convert.ToSingle (fiveNum); g.FillPie (brush3, piex, piey, piew, pieh, 0, angle1); g.FillPie (brush4, piex, piey, piew, pieh, angle1, angle2); g.FillPie (brush5, piex, piey, piew, pieh, angle1 + angle2, angle3); g.DrawRectangle (pen1, 50,300,310,130) G.FillRectangle (brush3, 90,320,20,10); g.DrawString (string.Format ("March sales: {0:P2}", Convert.ToSingle (threeNum) / Convert.ToSingle (sum)), font2, brush3, 120,320); g.FillRectangle (brush4, 90,360,20,10) G.DrawString (string.Format ("April sales: {0:P2}", Convert.ToSingle (fourNum) / Convert.ToSingle (sum)), font2, brush3, 120360); g.FillRectangle (brush5, 90,400,20,10); g.DrawString ("May sales share: {0:P2}", Convert.ToSingle (fiveNum) / Convert.ToSingle (sum)), font2, brush3, 120,400) This.groupBox1.Text = "pie chart"; this.pictureBox1.Width = bitmap.Width; this.pictureBox1.Height = bitmap.Height; this.pictureBox1.BackgroundImage = bitmap;} private void BarMap () / / histogram {int [] saleNum = {300500400} Int sum = saleNum [0] + saleNum [1] + saleNum [2]; float [] Y_Num = {Convert.ToSingle (saleNum [0]) / Convert.ToSingle (sum), Convert.ToSingle (saleNum [1]) / Convert.ToSingle (sum), Convert.ToSingle (saleNum [2]) / Convert.ToSingle (sum)}; int height = pictureBox1.Height, width = pictureBox1.Width Bitmap image = new Bitmap (width, height); / / create Graphics class object Graphics g = Graphics.FromImage (image); try {/ / clear picture background color g.Clear (Color.White); Font font = new Font ("Arial", 10, FontStyle.Regular) Font font1 = new Font (Arial, 20, FontStyle.Bold); LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0,0, image.Width, image.Height), Color.Blue, Color.BlueViolet, 1.2f, true); Font font2 = new System.Drawing.Font ("Arial", 10, FontStyle.Bold); SolidBrush mybrush = new SolidBrush (Color.Red) SolidBrush mybrush3 = new SolidBrush (Color.Green); Pen mypen = new Pen (brush, 1); / / draw lines / / draw horizontal lines int x = 100; Pen mypen1 = new Pen (Color.Blue, 2); x = 60 G.DrawLine (mypen1, x, 0, x, 300); / / draw vertical lines int y = 0; for (int I = 0; I = 0; iMurray -) {g.DrawString (m [I] .ToString (), font, Brushes.Blue, 20, y) / / set text content and output position y = y + 30;} / / draw logo Font font3 = new System.Drawing.Font ("Arial", 10, FontStyle.Regular); g.DrawRectangle (new Pen (Brushes.Blue), 170,390,250,50) / draw scope box g.FillRectangle (Brushes.Red, 200,410,20,10); / draw small rectangle g.DrawString ("monthly sales percentage", font3, Brushes.Red, 292408); this.button1.Text = "View pie chart"; this.groupBox1.Text = "bar chart"; this.pictureBox1.Width = image.Width This.pictureBox1.Height = image.Height; this.pictureBox1.BackgroundImage = image;} catch {} private void Button1_Click (object sender, EventArgs e) {BitMap (); this.button1.Visible = false / / hide button} above is all the content of the article "how to draw pie chart and bar chart in C #". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!