c#制作图表(从数据库读取数据,制作柱状图,扇形图)
生活随笔
收集整理的這篇文章主要介紹了
c#制作图表(从数据库读取数据,制作柱状图,扇形图)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.IO; using System.Drawing.Drawing2D;namespace WindowsApplication30 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) {}//柱狀圖 private void button3_Click(object sender, EventArgs e) { this.Height = 600; this.Width = 500; pictureBox1.Height = 500; pictureBox1.Width=450; int[] a =new int[]{30,20,50,40,40,78,45,98,87,47,46,98 }; int w=400; int h=450; Bitmap bmp = new Bitmap(w, h); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); g.DrawRectangle(Pens.AliceBlue, 0, 0, 399, 399); string title = "2009銷售情況統(tǒng)計表"; Font f = new Font("黑體", 16); g.DrawString(title, f, Brushes.Blue, (w / 2 - g.MeasureString(title, f).Width / 2), 2); float v_width = w; float v_height=w-2-g.MeasureString(title,f).Height-10; float p_width = w / 25; float p_heigth = v_height / 150; float x = 0; float y = 0; double k = 0; Font f9 = new Font("宋體", 9); for (int i = 0; i < 12;i++ ) { x = (i * 2 + 1) * p_width; y = w - p_heigth * a[i]; g.FillRectangle(Brushes.Black, x, y, p_width, a[i] * p_heigth); k = i + 1; g.DrawString(k.ToString() + "月", f9, Brushes.Black, x, w + 2);g.DrawString(a[i].ToString(), f9, Brushes.Black, x-4,y - 2 - g.MeasureString(a[i].ToString(), f9).Width / 2-2); } MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Image img = Image.FromStream(ms); pictureBox1.Image = img; ms.Close(); } //畫扇形圖 private void button4_Click(object sender, EventArgs e) { List<int> mylist=new List<int>(); mylist.Add(200); mylist.Add(100); mylist.Add(300); mylist.Add(350);int sum = 0; for (int i = 0; i < mylist.Count; i++) { sum += mylist[i];} float pangle = 360.0F/sum; Bitmap bmp = new Bitmap(500, 450); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); g.SmoothingMode = SmoothingMode.AntiAlias; g.DrawRectangle(Pens.Blue, 0, 0, 449, 449); Font f16 = new Font("黑體", 16); g.DrawString("2009年季度銷售情況統(tǒng)計表", f16, Brushes.Black, 225 - g.MeasureString("2009年季度銷售情況統(tǒng)計表", f16).Width / 2, 2);Brush[] mybrush=new Brush[]{Brushes.Aqua,Brushes.AntiqueWhite,Brushes.AliceBlue,Brushes.Chocolate}; Rectangle r = new Rectangle(25, 25, 400, 400); float angle = 0; float sangle=0; for (int i = 0; i < mylist.Count; i++) { angle = pangle * mylist[i]; g.DrawPie(Pens.Brown, r, sangle, angle); g.FillPie(mybrush[i], r, sangle, angle); sangle = sangle + angle;} MemoryStream ms = new MemoryStream(); bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Gif); this.Width = 500; this.Height = 500; pictureBox1.Width = 455; pictureBox1.Height = 455; pictureBox1.Image = Image.FromStream(ms); ms.Close(); }//畫曲線圖 private void button5_Click(object sender, EventArgs e) { this.Height = 600; this.Width = 500; pictureBox1.Height = 500; pictureBox1.Width = 450; int[] a = new int[] { 30, 20, 50, 40, 40, 78, 45, 98, 87, 47, 46, 98 }; int w = 400; int h = 450; Bitmap bmp = new Bitmap(w, h); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); g.DrawRectangle(Pens.AliceBlue, 0, 0, 399, 399); string title = "2009銷售情況統(tǒng)計表"; Font f = new Font("黑體", 16); g.DrawString(title, f, Brushes.Blue, (w / 2 - g.MeasureString(title, f).Width / 2), 2); float v_width = w; float v_height = w - 2 - g.MeasureString(title, f).Height - 10; float p_width = w / 25; float p_heigth = v_height / 150; float x = 0; float y = 0; float x1 = 0; float y1 = 0; double k = 0;Font f9 = new Font("宋體", 9); for (int i = 0; i < 11; i++) { x1 = ((i+1) * 2 + 1) * p_width; y1 = h - 50 - p_heigth * a[i+1]; x = (i * 2 + 1) * p_width; y = h-50 - p_heigth * a[i];Point p1 = new Point((int)x, (int)y); Point p2 = new Point((int)x1, (int)y1);g.DrawLine(Pens.BlueViolet, p1, p2);k = i + 1; g.DrawString(k.ToString() + "月", f9, Brushes.Black, x, w + 2);g.DrawString(a[i].ToString(), f9, Brushes.Black, x - 4, y - 2 - g.MeasureString(a[i].ToString(), f9).Width / 2 - 2);} MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Image img = Image.FromStream(ms); pictureBox1.Image = img; ms.Close(); } } }https://blog.csdn.net/anmushi3186/article/details/102282775?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
總結(jié)
以上是生活随笔為你收集整理的c#制作图表(从数据库读取数据,制作柱状图,扇形图)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实验13:20220625 1+X 中级
- 下一篇: 2021-07-18 三种视图的dico