c#中连接数据库后绘制柱状图和饼状图
来源:互联网 发布:c语言strcpy函数实现 编辑:程序博客网 时间:2024/05/21 15:02
自己在项目中使用的绘制柱状图和饼状图的方法
简单实用,可直接调用
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#中连接数据库后绘制柱状图和饼状图
- C# 数据库系统中使用GDI+绘制柱状图
- C#绘制曲线图和柱状图
- C#绘制曲线图和柱状图
- C#绘制曲线图和柱状图的方法
- c#绘制曲线图和柱状图等
- C#饼状图和柱状图
- C#绘制柱状图
- 【转载】C#下如何实现 绘制曲线图和柱状图
- TeeChart在C#和.Net下绘制柱状图
- 如何在C#的WinForm中制作饼状图和柱状图
- 如何在C#的WinForm中制作饼状图和柱状图
- 如何在C#的WinForm中制作饼状图和柱状图
- 如何在C#的WinForm中制作饼状图和柱状图
- 如何在C#的WinForm中制作饼状图和柱状图
- C#的WinForm中制作饼状图和柱状图
- C#实现WinForm中制作饼状图和柱状图
- 如何在C#的WinForm中制作饼状图和柱状图
- JAVA操作properties文件
- 需求那些事儿
- ASCII码排序 STL练手题
- Redis学习手册
- Android--ContentProvider跨程序访问数据
- c#中连接数据库后绘制柱状图和饼状图
- Red Bend与阿朗推LTE移动设备管理 使4G设备兼容OMA DM
- Android学习备忘019——AlertDialog问题
- 字对齐概念
- 表达式求值[java版本]
- __attribute__ ((aligned (n), packed))
- loading图标现在相关链接
- printf修饰符
- [分享] 教你如何让谷歌浏览器新建标签自动打开你想要的网址