c#制作图表(从数据库读取数据,制作柱状图,扇形图)
2010-11-13 21:39
489 查看
代码
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销售情况统计表"; 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年季度销售情况统计表", f16, Brushes.Black, 225 - g.MeasureString("2009年季度销售情况统计表", 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销售情况统计表"; 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(); } } }
相关文章推荐
- 利用Python数据可视化工具plotly从数据库读取数据制作本地图表应用实例
- C#连接内存数据库redis【1、Redis存读取数据】
- 用C#如何读取EXCEL表格中的数据,再导入我数据库中,
- C#从文本文件中读取数据大批量导入数据库
- 比较C#三种方法实现读取Execl数据到数据库
- C#读取从数据库读取数据判断每一行每一列
- C#从数据库中读取数据加入到combox的items中,并显示出当前model值在combox中
- C#中读取数据库中Image数据
- C# SqlDataAdapter,读取数据库数据
- C# 连接SQL Server 数据库的 读取数据的基本原理方法
- C#后台制作之数据库(Access数据库+datagirdview控件显示+串口数据存储)
- C# 异步读取数据库里面的数据与绑定UI的解决办法
- Java读取数据库数据生成柱状图
- c# 在datagridview中添加comboboxcolumn 绑定数据库读取显示数据
- Asp.net(C#)读取数据库并生成JS文件制作首页图片切换效果(附demo源码下载)
- C#读取txt文档并上传至数据库,将上传的数据逐行删除
- 用C#编程从数据库中读取图片数据导进Excel文件的方法
- C#创建数据库和表并把读取到的csv数据存入新建表中
- [C#] 从数据库读取数据并输出
- C#中读取数据库中Image数据