您的位置:首页 > 数据库

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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: