c#中连接数据库后绘制柱状图和饼状图
2013-01-24 12:32
351 查看
自己在项目中使用的绘制柱状图和饼状图的方法
简单实用,可直接调用
public class GongNeng
{
System.Data.DataTable table1 = new System.Data.DataTable();
public GongNeng()
{
}
//定义绘制饼状图的颜色
public static Color GetChartItemColor(int itemIndex)
{
Color selectedColor;
switch(itemIndex)
{
case 0:
selectedColor = Color.Blue;
break;
case 1:
selectedColor = Color.Red;
break;
case 2:
selectedColor = Color.Yellow;
break;
case 3:
selectedColor = Color.Brown;
break;
default:
selectedColor = Color.Green;
break;
}
return selectedColor;
}
//绘制柱状图
public System.Drawing.Image DrawZhuTu(string time1,string time2,string ky, string sql2)
{
//获取相关数据
LData ld = new LData();
SqlDataReader dr = null;
//dr = ld.exeDataReader(sql1);
//int max = 0;
//int min = 0;
//while (dr.Read())
//{
// max = Convert.ToInt32(dr[0].ToString());
// min = Convert.ToInt32(dr[1].ToString());
//}
//dr.Close();
System.Data.DataTable table1 = new System.Data.DataTable();
table1 = ld.exeDataSet(sql2).Tables[0];
ld.close();
int rowNum = table1.Rows.Count;
int[] ShuZhi = new int[rowNum];
//string[] RQ_Year = new string[rowNum];
//string[] RQ_Day = new string[rowNum];
//string[] RQ_Yue = new string[rowNum];
string[] XueYuan = new string[rowNum];
for (int i = 0; i < rowNum; i++)
{
//RQ_Year[i] = Convert.ToDateTime(table1.Rows[i][0].ToString()).Year.ToString();//获取横坐标的时间
//RQ_Yue[i] = Convert.ToDateTime(table1.Rows[i][0].ToString()).Month.ToString();
//RQ_Day[i] = Convert.ToDateTime(table1.Rows[i][0].ToString()).Day.ToString();
ShuZhi[i] = Convert.ToInt32(table1.Rows[i][1].ToString());
XueYuan[i] = table1.Rows[i][0].ToString();
}
ld.close();
//开始绘制图线
int height = 450, width = 800;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
//清空图片背景色
g.Clear(Color.White);
System.Drawing.Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
System.Drawing.Font font1 = new System.Drawing.Font("宋体", 16, FontStyle.Regular);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
//画图片的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 4, image.Height - 4);
Pen mypen = new Pen(brush, 1);
//绘制线条
//绘制纵向线条
int x = 80;
for (int i = 0; i < 15; i++)
{
g.DrawLine(mypen, x, 80, x, 380);
x = x + 40;
}
Pen mypen1 = new Pen(Color.Blue, 2);
g.DrawLine(mypen1, 80, 80, 80, 380);
//绘制横向线条
int y = 80;
for (int i = 0; i < 10; i++)
{
g.DrawLine(mypen, 80, y, 680, y);
y = y + 30;
}
g.DrawLine(mypen1, 80, y, 680, y);
//标题
string Title = time1 + " 到 " + time2 + "各个学院的" + ky + "科研数量比例图";
g.DrawString(Title, font1, Brushes.Blue, 50, 25);
//y轴数值
int[] Y_zhi ={ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
y = 370;
for (int i = 0; i < 11; i++)
{
g.DrawString(Y_zhi[i].ToString(), font, Brushes.Red, 50, y);
y = y - 30;
}
//x轴数值
x = 105;
for (int i = 0; i < rowNum; i++)
{
Char[] ch = XueYuan[i].ToString().ToCharArray();
String str1 = ch[0].ToString() + ch[1].ToString();
string str2 = ch[2].ToString() + ch[3].ToString();
//g.DrawString(RQ_Year[i].ToString(), font, Brushes.Red, x, 380);
//g.DrawString("年", font, Brushes.Red, x, 395);
//g.DrawString(RQ_Yue[i].ToString(), font, Brushes.Red, x, 410);
//g.DrawString("月", font, Brushes.Red, x, 425);
//g.DrawString(RQ_Day[i].ToString(), font, Brushes.Red, x, 440);
//g.DrawString("日", font, Brushes.Red, x, 455);
g.DrawString(str1, font, Brushes.Red, x, 380);
g.DrawString(str2, font, Brushes.Red, x, 395);
x = x + 40;
}
g.DrawString("学院", font1, Brushes.Blue, 350, 425);
//显示柱状效果
int[] X_zhi = new int[rowNum];
for (int i = 0; i < rowNum; i++)
{
X_zhi[i] = (ShuZhi[i] - 0) * 30 / 10;
}
x = 110;
for (int i = 0; i < rowNum; i++)
{
SolidBrush mybrush = new SolidBrush(Color.Red);
g.FillRectangle(mybrush, x, 380 - X_zhi[i], 20, X_zhi[i]);
x = x + 40;
}
return image;
}
//绘制饼状图
public System.Drawing.Image DrawBingTu(string sql1,string time1,string time2,string XueYuan)
{
int height = 400, width = 800;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
//清空图片背景色
g.Clear(Color.White);
System.Drawing.Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
System.Drawing.Font font1 = new System.Drawing.Font("宋体", 16, FontStyle.Regular);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
//画图片的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 4, image.Height - 4);
Pen mypen = new Pen(brush, 1);
//饼图标题
string Title = time1 + "到" + time2 +"间"+ XueYuan + "四类科研项目数量比例图";
g.DrawString(Title, font1, Brushes.Blue, 70, 20);
//获取数据用来绘制比例
System.Data.DataTable table = new System.Data.DataTable();
LData ld=new LData();
table = ld.exeDataSet(sql1).Tables[0];
float sumData = 0;
for (int i = 0; i < table.Rows.Count; i++) {
sumData += Convert.ToSingle(table.Rows[i][1]);
}
float curAngle = 0;
float totalAngle = 0;
for (int i = 0; i < table.Rows.Count; i++)
{
curAngle = Convert.ToSingle(table.Rows[i][1]) / sumData * 360;
g.FillPie(new SolidBrush(GetChartItemColor(i)), 100, 65, 200, 200, totalAngle, curAngle);
g.DrawPie(Pens.Black, 100, 65, 200, 200, totalAngle, curAngle);
totalAngle += curAngle;
}
g.DrawRectangle(Pens.Black,250,300,250,90);
g.DrawString("图表说明", new System.Drawing.Font("Tahoma", 12, FontStyle.Bold), Brushes.Black, new PointF(260, 300));
//画图例各项
PointF boxOrigin = new PointF(260,330);
PointF textOrigin = new PointF(280,326);
float percent = 0;
for (int i = 0; i < table.Rows.Count; i++)
{
g.FillRectangle(new SolidBrush(GetChartItemColor(i)),boxOrigin.X,boxOrigin.Y,20,10);
g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10);
percent = Convert.ToSingle(table.Rows[i][1]) / sumData * 100;
g.DrawString(table.Rows[i][1].ToString() + " - " + table.Rows[i][0].ToString() + " (" + percent.ToString("0") + "%)", new System.Drawing.Font("Tahoma", 10), Brushes.Black, textOrigin);
boxOrigin.Y += 15;
textOrigin.Y += 15;
}
return image;
}
简单实用,可直接调用
public class GongNeng
{
System.Data.DataTable table1 = new System.Data.DataTable();
public GongNeng()
{
}
//定义绘制饼状图的颜色
public static Color GetChartItemColor(int itemIndex)
{
Color selectedColor;
switch(itemIndex)
{
case 0:
selectedColor = Color.Blue;
break;
case 1:
selectedColor = Color.Red;
break;
case 2:
selectedColor = Color.Yellow;
break;
case 3:
selectedColor = Color.Brown;
break;
default:
selectedColor = Color.Green;
break;
}
return selectedColor;
}
//绘制柱状图
public System.Drawing.Image DrawZhuTu(string time1,string time2,string ky, string sql2)
{
//获取相关数据
LData ld = new LData();
SqlDataReader dr = null;
//dr = ld.exeDataReader(sql1);
//int max = 0;
//int min = 0;
//while (dr.Read())
//{
// max = Convert.ToInt32(dr[0].ToString());
// min = Convert.ToInt32(dr[1].ToString());
//}
//dr.Close();
System.Data.DataTable table1 = new System.Data.DataTable();
table1 = ld.exeDataSet(sql2).Tables[0];
ld.close();
int rowNum = table1.Rows.Count;
int[] ShuZhi = new int[rowNum];
//string[] RQ_Year = new string[rowNum];
//string[] RQ_Day = new string[rowNum];
//string[] RQ_Yue = new string[rowNum];
string[] XueYuan = new string[rowNum];
for (int i = 0; i < rowNum; i++)
{
//RQ_Year[i] = Convert.ToDateTime(table1.Rows[i][0].ToString()).Year.ToString();//获取横坐标的时间
//RQ_Yue[i] = Convert.ToDateTime(table1.Rows[i][0].ToString()).Month.ToString();
//RQ_Day[i] = Convert.ToDateTime(table1.Rows[i][0].ToString()).Day.ToString();
ShuZhi[i] = Convert.ToInt32(table1.Rows[i][1].ToString());
XueYuan[i] = table1.Rows[i][0].ToString();
}
ld.close();
//开始绘制图线
int height = 450, width = 800;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
//清空图片背景色
g.Clear(Color.White);
System.Drawing.Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
System.Drawing.Font font1 = new System.Drawing.Font("宋体", 16, FontStyle.Regular);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
//画图片的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 4, image.Height - 4);
Pen mypen = new Pen(brush, 1);
//绘制线条
//绘制纵向线条
int x = 80;
for (int i = 0; i < 15; i++)
{
g.DrawLine(mypen, x, 80, x, 380);
x = x + 40;
}
Pen mypen1 = new Pen(Color.Blue, 2);
g.DrawLine(mypen1, 80, 80, 80, 380);
//绘制横向线条
int y = 80;
for (int i = 0; i < 10; i++)
{
g.DrawLine(mypen, 80, y, 680, y);
y = y + 30;
}
g.DrawLine(mypen1, 80, y, 680, y);
//标题
string Title = time1 + " 到 " + time2 + "各个学院的" + ky + "科研数量比例图";
g.DrawString(Title, font1, Brushes.Blue, 50, 25);
//y轴数值
int[] Y_zhi ={ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
y = 370;
for (int i = 0; i < 11; i++)
{
g.DrawString(Y_zhi[i].ToString(), font, Brushes.Red, 50, y);
y = y - 30;
}
//x轴数值
x = 105;
for (int i = 0; i < rowNum; i++)
{
Char[] ch = XueYuan[i].ToString().ToCharArray();
String str1 = ch[0].ToString() + ch[1].ToString();
string str2 = ch[2].ToString() + ch[3].ToString();
//g.DrawString(RQ_Year[i].ToString(), font, Brushes.Red, x, 380);
//g.DrawString("年", font, Brushes.Red, x, 395);
//g.DrawString(RQ_Yue[i].ToString(), font, Brushes.Red, x, 410);
//g.DrawString("月", font, Brushes.Red, x, 425);
//g.DrawString(RQ_Day[i].ToString(), font, Brushes.Red, x, 440);
//g.DrawString("日", font, Brushes.Red, x, 455);
g.DrawString(str1, font, Brushes.Red, x, 380);
g.DrawString(str2, font, Brushes.Red, x, 395);
x = x + 40;
}
g.DrawString("学院", font1, Brushes.Blue, 350, 425);
//显示柱状效果
int[] X_zhi = new int[rowNum];
for (int i = 0; i < rowNum; i++)
{
X_zhi[i] = (ShuZhi[i] - 0) * 30 / 10;
}
x = 110;
for (int i = 0; i < rowNum; i++)
{
SolidBrush mybrush = new SolidBrush(Color.Red);
g.FillRectangle(mybrush, x, 380 - X_zhi[i], 20, X_zhi[i]);
x = x + 40;
}
return image;
}
//绘制饼状图
public System.Drawing.Image DrawBingTu(string sql1,string time1,string time2,string XueYuan)
{
int height = 400, width = 800;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
//清空图片背景色
g.Clear(Color.White);
System.Drawing.Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
System.Drawing.Font font1 = new System.Drawing.Font("宋体", 16, FontStyle.Regular);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
//画图片的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 4, image.Height - 4);
Pen mypen = new Pen(brush, 1);
//饼图标题
string Title = time1 + "到" + time2 +"间"+ XueYuan + "四类科研项目数量比例图";
g.DrawString(Title, font1, Brushes.Blue, 70, 20);
//获取数据用来绘制比例
System.Data.DataTable table = new System.Data.DataTable();
LData ld=new LData();
table = ld.exeDataSet(sql1).Tables[0];
float sumData = 0;
for (int i = 0; i < table.Rows.Count; i++) {
sumData += Convert.ToSingle(table.Rows[i][1]);
}
float curAngle = 0;
float totalAngle = 0;
for (int i = 0; i < table.Rows.Count; i++)
{
curAngle = Convert.ToSingle(table.Rows[i][1]) / sumData * 360;
g.FillPie(new SolidBrush(GetChartItemColor(i)), 100, 65, 200, 200, totalAngle, curAngle);
g.DrawPie(Pens.Black, 100, 65, 200, 200, totalAngle, curAngle);
totalAngle += curAngle;
}
g.DrawRectangle(Pens.Black,250,300,250,90);
g.DrawString("图表说明", new System.Drawing.Font("Tahoma", 12, FontStyle.Bold), Brushes.Black, new PointF(260, 300));
//画图例各项
PointF boxOrigin = new PointF(260,330);
PointF textOrigin = new PointF(280,326);
float percent = 0;
for (int i = 0; i < table.Rows.Count; i++)
{
g.FillRectangle(new SolidBrush(GetChartItemColor(i)),boxOrigin.X,boxOrigin.Y,20,10);
g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10);
percent = Convert.ToSingle(table.Rows[i][1]) / sumData * 100;
g.DrawString(table.Rows[i][1].ToString() + " - " + table.Rows[i][0].ToString() + " (" + percent.ToString("0") + "%)", new System.Drawing.Font("Tahoma", 10), Brushes.Black, textOrigin);
boxOrigin.Y += 15;
textOrigin.Y += 15;
}
return image;
}
相关文章推荐
- C#中使用GDI+实现饼状图和柱状图跟数据库联接显示数据的方法
- C# 绘制图表(柱状图,线性图,饼状图)
- C# 数据库系统中使用GDI+绘制柱状图
- C#中使用GDI+实现饼状图和柱状图跟数据库联接显示数据的方法
- C#中连接数据库的代码怎么写?
- c#中连接数据库关于sqlconnection的用户sa无法登录的问题
- [C#] 连接数据库并验证用户名和密码
- C#连接数据库小结
- C#GUI编程学习之ComboBox控件篇1--连接数据库并传递数据到文本框
- C#连接数据库
- NET(C#)连接各类数据库-集锦
- C# WPF 快速开发06ADO.NET连接数据库
- winform中C#调用数据库连接选择对话框
- .NET平台下使用C#连接各种数据库
- C# 连接SQL数据库、常用的连接字符串讲解、常用的数据库操作方法
- C# 连接 SQL Server 2008 基础操作+数据库增删改查
- C#连接数据库
- 自定义Database类用于c#数据库连接
- C#连接各类数据库的方法集合
- C# 绘制统计图大全(柱状图, 折线图, 扇形图)