图表分析
2010-04-18 10:54 夜雨瞳 阅读(435) 评论(0) 编辑 收藏 举报1 绘制柱状图
1.1 建立数据库表
代码
create database db_Store;
create table tb_store(
sto_id int primary key,
sto_area float not null,
sto_name varchar(20) not null
);
insert into tb_store(sto_id,sto_area,sto_name)
values(1,900,'仓库1');
insert into tb_store(sto_id,sto_area,sto_name)
values(2,800,'仓库1');
insert into tb_store(sto_id,sto_area,sto_name)
values(3,700,'仓库1');
insert into tb_store(sto_id,sto_area,sto_name)
values(4,1300,'仓库1');
insert into tb_store(sto_id,sto_area,sto_name)
values(5,1200,'仓库1');
create table tb_store(
sto_id int primary key,
sto_area float not null,
sto_name varchar(20) not null
);
insert into tb_store(sto_id,sto_area,sto_name)
values(1,900,'仓库1');
insert into tb_store(sto_id,sto_area,sto_name)
values(2,800,'仓库1');
insert into tb_store(sto_id,sto_area,sto_name)
values(3,700,'仓库1');
insert into tb_store(sto_id,sto_area,sto_name)
values(4,1300,'仓库1');
insert into tb_store(sto_id,sto_area,sto_name)
values(5,1200,'仓库1');
1.2 窗体代码
代码
private void button1_Click(object sender, EventArgs e)
{
//生成连接数据库字符串
string ConStr = @"Data Source=YHWY\SQLEXPRESS;Initial Catalog=db_Store;Integrated Security=True";
//定义SQqlConnection对象实例
SqlConnection Con = new SqlConnection(ConStr);
Con.Open();
//select查询语句
string SqlStr = "select sto_id,sto_area from tb_store order by sto_id";
//定义SqlCommand对象实例
SqlCommand cmd = new SqlCommand(SqlStr, Con);
//定义SqlDataReader对象实例
SqlDataReader dr = cmd.ExecuteReader();
//定义Bitmap对象实例
Bitmap bitM=new Bitmap(this.panel1.Width,this.panel1.Height);
//定义Graphics对象实例
Graphics g = Graphics.FromImage(bitM);
g.Clear(Color.Yellow);
for (int i = 0; i < 5; i++)
{
g.DrawLine(new Pen(new SolidBrush(Color.Blue), 2.0f), 80,
this.panel1.Height - 30 - i * 30, this.panel1.Width - 40, this.panel1.Height - 30 - i * 30);
g.DrawString(Convert.ToString(i * 300), new Font("Times New Roman", 15, FontStyle.Regular),
new SolidBrush(Color.Black), 30, this.panel1.Height - 50 - i * 30);
}
for (int j = 0; j < 5; j++)
{
if (dr.Read())
{
int x, y, w, h;
//绘制图标的横坐标
g.DrawString(dr[0].ToString(), new Font("宋体", 12, FontStyle.Regular),
new SolidBrush(Color.Black), 76 + 60 * j, this.panel1.Height - 28);
x = 78 + 58 * j;
y = this.panel1.Height - 30 - Convert.ToInt32((Convert.ToDouble(Convert.ToDouble(dr[1].ToString()) * 20 / 200)));
w = 20;
h = Convert.ToInt32(Convert.ToDouble(dr[1].ToString()) * 20 / 200);
g.FillRectangle(new SolidBrush(Color.FromArgb(255, 0, 0)), x, y, w, h);
}
}
//在Panel控件上绘制柱状图表
this.panel1.BackgroundImage = bitM;
}
/*升序与降序差别在于SQL语句不同*/
private void button2_Click(object sender, EventArgs e)
{
//生成连接数据库字符串
string ConStr = @"Data Source=YHWY\SQLEXPRESS;Initial Catalog=db_Store;Integrated Security=True";
//定义SQqlConnection对象实例
SqlConnection Con = new SqlConnection(ConStr);
Con.Open();
//select查询语句
string SqlStr = "select sto_id,sto_area from tb_store order by sto_area desc";
//定义SqlCommand对象实例
SqlCommand cmd = new SqlCommand(SqlStr, Con);
//定义SqlDataReader对象实例
SqlDataReader dr = cmd.ExecuteReader();
//定义Bitmap对象实例
Bitmap bitM = new Bitmap(this.panel1.Width, this.panel1.Height);
//定义Graphics对象实例
Graphics g = Graphics.FromImage(bitM);
g.Clear(Color.Yellow);
for (int i = 0; i < 5; i++)
{
g.DrawLine(new Pen(new SolidBrush(Color.Blue), 2.0f), 80,
this.panel1.Height - 30 - i * 30, this.panel1.Width - 40, this.panel1.Height - 30 - i * 30);
g.DrawString(Convert.ToString(i * 300), new Font("Times New Roman", 15, FontStyle.Regular),
new SolidBrush(Color.Black), 30, this.panel1.Height - 50 - i * 30);
}
for (int j = 0; j < 5; j++)
{
if (dr.Read())
{
int x, y, w, h;
//绘制图标的横坐标
g.DrawString(dr[0].ToString(), new Font("宋体", 12, FontStyle.Regular),
new SolidBrush(Color.Black), 76 + 60 * j, this.panel1.Height - 28);
x = 78 + 58 * j;
y = this.panel1.Height - 30 - Convert.ToInt32((Convert.ToDouble(Convert.ToDouble(dr[1].ToString()) * 20 / 200)));
w = 20;
h = Convert.ToInt32(Convert.ToDouble(dr[1].ToString()) * 20 / 200);
g.FillRectangle(new SolidBrush(Color.FromArgb(255, 0, 0)), x, y, w, h);
}
}
//在Panel控件上绘制柱状图表
this.panel1.BackgroundImage = bitM;
}
private void button1_Click(object sender, EventArgs e)
{
//生成连接数据库字符串
string ConStr = @"Data Source=YHWY\SQLEXPRESS;Initial Catalog=db_Store;Integrated Security=True";
//定义SQqlConnection对象实例
SqlConnection Con = new SqlConnection(ConStr);
Con.Open();
//select查询语句
string SqlStr = "select sto_id,sto_area from tb_store order by sto_id";
//定义SqlCommand对象实例
SqlCommand cmd = new SqlCommand(SqlStr, Con);
//定义SqlDataReader对象实例
SqlDataReader dr = cmd.ExecuteReader();
//定义Bitmap对象实例
Bitmap bitM=new Bitmap(this.panel1.Width,this.panel1.Height);
//定义Graphics对象实例
Graphics g = Graphics.FromImage(bitM);
g.Clear(Color.Yellow);
for (int i = 0; i < 5; i++)
{
g.DrawLine(new Pen(new SolidBrush(Color.Blue), 2.0f), 80,
this.panel1.Height - 30 - i * 30, this.panel1.Width - 40, this.panel1.Height - 30 - i * 30);
g.DrawString(Convert.ToString(i * 300), new Font("Times New Roman", 15, FontStyle.Regular),
new SolidBrush(Color.Black), 30, this.panel1.Height - 50 - i * 30);
}
for (int j = 0; j < 5; j++)
{
if (dr.Read())
{
int x, y, w, h;
//绘制图标的横坐标
g.DrawString(dr[0].ToString(), new Font("宋体", 12, FontStyle.Regular),
new SolidBrush(Color.Black), 76 + 60 * j, this.panel1.Height - 28);
x = 78 + 58 * j;
y = this.panel1.Height - 30 - Convert.ToInt32((Convert.ToDouble(Convert.ToDouble(dr[1].ToString()) * 20 / 200)));
w = 20;
h = Convert.ToInt32(Convert.ToDouble(dr[1].ToString()) * 20 / 200);
g.FillRectangle(new SolidBrush(Color.FromArgb(255, 0, 0)), x, y, w, h);
}
}
//在Panel控件上绘制柱状图表
this.panel1.BackgroundImage = bitM;
}
/*升序与降序差别在于SQL语句不同*/
private void button2_Click(object sender, EventArgs e)
{
//生成连接数据库字符串
string ConStr = @"Data Source=YHWY\SQLEXPRESS;Initial Catalog=db_Store;Integrated Security=True";
//定义SQqlConnection对象实例
SqlConnection Con = new SqlConnection(ConStr);
Con.Open();
//select查询语句
string SqlStr = "select sto_id,sto_area from tb_store order by sto_area desc";
//定义SqlCommand对象实例
SqlCommand cmd = new SqlCommand(SqlStr, Con);
//定义SqlDataReader对象实例
SqlDataReader dr = cmd.ExecuteReader();
//定义Bitmap对象实例
Bitmap bitM = new Bitmap(this.panel1.Width, this.panel1.Height);
//定义Graphics对象实例
Graphics g = Graphics.FromImage(bitM);
g.Clear(Color.Yellow);
for (int i = 0; i < 5; i++)
{
g.DrawLine(new Pen(new SolidBrush(Color.Blue), 2.0f), 80,
this.panel1.Height - 30 - i * 30, this.panel1.Width - 40, this.panel1.Height - 30 - i * 30);
g.DrawString(Convert.ToString(i * 300), new Font("Times New Roman", 15, FontStyle.Regular),
new SolidBrush(Color.Black), 30, this.panel1.Height - 50 - i * 30);
}
for (int j = 0; j < 5; j++)
{
if (dr.Read())
{
int x, y, w, h;
//绘制图标的横坐标
g.DrawString(dr[0].ToString(), new Font("宋体", 12, FontStyle.Regular),
new SolidBrush(Color.Black), 76 + 60 * j, this.panel1.Height - 28);
x = 78 + 58 * j;
y = this.panel1.Height - 30 - Convert.ToInt32((Convert.ToDouble(Convert.ToDouble(dr[1].ToString()) * 20 / 200)));
w = 20;
h = Convert.ToInt32(Convert.ToDouble(dr[1].ToString()) * 20 / 200);
g.FillRectangle(new SolidBrush(Color.FromArgb(255, 0, 0)), x, y, w, h);
}
}
//在Panel控件上绘制柱状图表
this.panel1.BackgroundImage = bitM;
}
1.3 窗体运行
2 绘制折线图
2.1 建立数据库表
代码
create database db_Store;
create table tb_productsale(
pro_id int primary key,
pro_price real not null,
pro_name varchar(20) not null,
pro_date datetime not null);
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(1,'220','李平','2009/1/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(2,'200','李平','2009/4/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(3,'120','李平','2009/6/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(4,'200','李平','2009/8/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(5,'300','李平','2009/11/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(6,'200','张亮','2009/1/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(7,'100','张亮','2009/4/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(8,'100','张亮','2009/6/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(9,'200','张亮','2009/8/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(10,'300','张亮','2009/11/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(11,'300','周涛','2009/1/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(12,'32','周涛','2009/4/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(13,'300','周涛','2009/6/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(14,'200','周涛','2009/8/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(15,'32','周涛','2009/11/2');
create table tb_productsale(
pro_id int primary key,
pro_price real not null,
pro_name varchar(20) not null,
pro_date datetime not null);
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(1,'220','李平','2009/1/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(2,'200','李平','2009/4/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(3,'120','李平','2009/6/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(4,'200','李平','2009/8/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(5,'300','李平','2009/11/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(6,'200','张亮','2009/1/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(7,'100','张亮','2009/4/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(8,'100','张亮','2009/6/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(9,'200','张亮','2009/8/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(10,'300','张亮','2009/11/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(11,'300','周涛','2009/1/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(12,'32','周涛','2009/4/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(13,'300','周涛','2009/6/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(14,'200','周涛','2009/8/2');
insert into tb_productsale(pro_id,pro_price,pro_name,pro_date)
values(15,'32','周涛','2009/11/2');
2.2 窗体代码
代码
SqlConnection con=new SqlConnection(@"Data Source=YHWY\SQLEXPRESS;Initial Catalog=db_Store;Integrated Security=True");
SqlDataAdapter sqlAda;//定义SqlDataAdapter对象实例
SqlCommand cmd;//定义SqlCommand对象实例
DataSet ds;//定义DataSet对象实例
static int G = 0;//定义静态变量并赋值
private void DrowFont(string str)
{
using (Graphics GrapFont = this.panel1.CreateGraphics())
{
GrapFont.Clear(Color.SeaShell);
Pen p = new Pen(Color.Blue, 2.0f);//定义笔的颜色和粗细
Font f = new Font("华文新魏", 12, FontStyle.Regular);//定义字体样式
Brush b = new SolidBrush(Color.Blue);//设置画笔样式
GrapFont.DrawString(str + "销售业绩走势图分析", f, b, 4.0f, 5.0f);
}
}
private void DrowPic(string str)
{
int MaxValue, MinValue;//定义变量
//获取最大销售金额
using (cmd = new SqlCommand("select Max(pro_price) from tb_productsale where pro_name='" + str + "'", con))
{
con.Open();
MaxValue = Convert.ToInt16(cmd.ExecuteScalar());
con.Close();
}
using (cmd = new SqlCommand("select Min(pro_price) from tb_productsale where pro_name='" + str + "'", con))
{
con.Open();
MinValue = Convert.ToInt16(cmd.ExecuteScalar());
con.Close();
}
Graphics g = this.groupBox1.CreateGraphics();//定义Graphics对象实例
g.Clear(Color.SeaShell);
Brush b = new SolidBrush(Color.Blue);//定义画笔颜色
Font f = new Font("Arial", 9, FontStyle.Regular);//定义字体样式
Pen p = new Pen(b);
using (sqlAda = new SqlDataAdapter("select * from tb_productsale where pro_name='" + str + "' order by pro_date", con))
{
ds = new DataSet();
sqlAda.Fill(ds, "pro_date");
int M = MaxValue / 50 + 1;//最大值
int N = MinValue / 50;//最小值
int T = N;
for (int i = 0; i <= M - N; i++)
{
g.DrawString(Convert.ToString(T * 50), f, b, 0, 190 - 30 * i);
g.DrawLine(p, 30, 200 - 30 * i, 260, 200 - 30 * i);//绘制水平坐标线
T++;
}
int Num = ds.Tables[0].Rows.Count;
int[] Values = new int[Num];
for (int C = 0; C < Num; C++)
{
Values[C] = Convert.ToInt32(ds.Tables[0].Rows[C][1].ToString());
//绘制销售月份文字
g.DrawString(Convert.ToDateTime(ds.Tables[0].Rows[C][3].ToString()).Month + "月", f, b, 30 * (C + 1) - 10, 10);
//绘制垂直坐标线
g.DrawLine(p, 30 * (C + 1), 200, 30 * (C + 1), 30);
}
Point[] P = new Point[Num];//定义点对象
for (int i = 0; i < Num; i++)//利用For循环绘制折线图
{
P[i].X = 30 * (i + 1);
P[i].Y = 260 - Convert.ToInt32(Values[i] / 50f * 30);
}
g.DrawLines(p, P);
}
}
private void DrowInfo(string str)
{
Graphics g = this.groupBox2.CreateGraphics();//创建Graphics对象实例
g.Clear(Color.SeaShell);
Brush b = new SolidBrush(Color.Blue);//定义画笔颜色
Font f = new Font("Arial", 9, FontStyle.Regular);//设置字体样式
using (sqlAda = new SqlDataAdapter("select * from tb_productsale where pro_name='" + str + "' order by pro_date", con))
{
DataSet ds = new DataSet();
sqlAda.Fill(ds, "tb_productsale");
g.DrawString("月份: " + "销售业绩", f, b, 10.0f, 25.0f);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
int month = Convert.ToDateTime(ds.Tables[0].Rows[i][3].ToString()).Month;
if (month >= 10)
{
g.DrawString(month + "月: " + ds.Tables[0].Rows[i][1].ToString(), f, b, 10.0f, (i + 2) * 25.0f);
}
else
{
g.DrawString("0" + month + "月: " + ds.Tables[0].Rows[i][1].ToString(), f, b, 10.0f, (i + 2f) * 25.0f);
}
}
}
}
private void Form2_Load(object sender, EventArgs e)
{
//提取不重复的销售人员信息
using (sqlAda = new SqlDataAdapter("select distinct(pro_name) from tb_productsale", con))
{
DataSet ds = new DataSet();
sqlAda.Fill(ds, "tb_productsale");
this.comboBox1.DataSource = ds.Tables[0];
this.comboBox1.DisplayMember = "pro_name";//把不重复的销售人员添加到下拉列表框中
}
}
private void button1_Click(object sender, EventArgs e)
{
G++;//变量G自动加1
//调用自定义函数DrowFont、DrowInfo和DrowPic
DrowFont(this.comboBox1.Text.ToString());
DrowInfo(this.comboBox1.Text.ToString());
DrowPic(this.comboBox1.Text.ToString());
}
SqlConnection con=new SqlConnection(@"Data Source=YHWY\SQLEXPRESS;Initial Catalog=db_Store;Integrated Security=True");
SqlDataAdapter sqlAda;//定义SqlDataAdapter对象实例
SqlCommand cmd;//定义SqlCommand对象实例
DataSet ds;//定义DataSet对象实例
static int G = 0;//定义静态变量并赋值
private void DrowFont(string str)
{
using (Graphics GrapFont = this.panel1.CreateGraphics())
{
GrapFont.Clear(Color.SeaShell);
Pen p = new Pen(Color.Blue, 2.0f);//定义笔的颜色和粗细
Font f = new Font("华文新魏", 12, FontStyle.Regular);//定义字体样式
Brush b = new SolidBrush(Color.Blue);//设置画笔样式
GrapFont.DrawString(str + "销售业绩走势图分析", f, b, 4.0f, 5.0f);
}
}
private void DrowPic(string str)
{
int MaxValue, MinValue;//定义变量
//获取最大销售金额
using (cmd = new SqlCommand("select Max(pro_price) from tb_productsale where pro_name='" + str + "'", con))
{
con.Open();
MaxValue = Convert.ToInt16(cmd.ExecuteScalar());
con.Close();
}
using (cmd = new SqlCommand("select Min(pro_price) from tb_productsale where pro_name='" + str + "'", con))
{
con.Open();
MinValue = Convert.ToInt16(cmd.ExecuteScalar());
con.Close();
}
Graphics g = this.groupBox1.CreateGraphics();//定义Graphics对象实例
g.Clear(Color.SeaShell);
Brush b = new SolidBrush(Color.Blue);//定义画笔颜色
Font f = new Font("Arial", 9, FontStyle.Regular);//定义字体样式
Pen p = new Pen(b);
using (sqlAda = new SqlDataAdapter("select * from tb_productsale where pro_name='" + str + "' order by pro_date", con))
{
ds = new DataSet();
sqlAda.Fill(ds, "pro_date");
int M = MaxValue / 50 + 1;//最大值
int N = MinValue / 50;//最小值
int T = N;
for (int i = 0; i <= M - N; i++)
{
g.DrawString(Convert.ToString(T * 50), f, b, 0, 190 - 30 * i);
g.DrawLine(p, 30, 200 - 30 * i, 260, 200 - 30 * i);//绘制水平坐标线
T++;
}
int Num = ds.Tables[0].Rows.Count;
int[] Values = new int[Num];
for (int C = 0; C < Num; C++)
{
Values[C] = Convert.ToInt32(ds.Tables[0].Rows[C][1].ToString());
//绘制销售月份文字
g.DrawString(Convert.ToDateTime(ds.Tables[0].Rows[C][3].ToString()).Month + "月", f, b, 30 * (C + 1) - 10, 10);
//绘制垂直坐标线
g.DrawLine(p, 30 * (C + 1), 200, 30 * (C + 1), 30);
}
Point[] P = new Point[Num];//定义点对象
for (int i = 0; i < Num; i++)//利用For循环绘制折线图
{
P[i].X = 30 * (i + 1);
P[i].Y = 260 - Convert.ToInt32(Values[i] / 50f * 30);
}
g.DrawLines(p, P);
}
}
private void DrowInfo(string str)
{
Graphics g = this.groupBox2.CreateGraphics();//创建Graphics对象实例
g.Clear(Color.SeaShell);
Brush b = new SolidBrush(Color.Blue);//定义画笔颜色
Font f = new Font("Arial", 9, FontStyle.Regular);//设置字体样式
using (sqlAda = new SqlDataAdapter("select * from tb_productsale where pro_name='" + str + "' order by pro_date", con))
{
DataSet ds = new DataSet();
sqlAda.Fill(ds, "tb_productsale");
g.DrawString("月份: " + "销售业绩", f, b, 10.0f, 25.0f);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
int month = Convert.ToDateTime(ds.Tables[0].Rows[i][3].ToString()).Month;
if (month >= 10)
{
g.DrawString(month + "月: " + ds.Tables[0].Rows[i][1].ToString(), f, b, 10.0f, (i + 2) * 25.0f);
}
else
{
g.DrawString("0" + month + "月: " + ds.Tables[0].Rows[i][1].ToString(), f, b, 10.0f, (i + 2f) * 25.0f);
}
}
}
}
private void Form2_Load(object sender, EventArgs e)
{
//提取不重复的销售人员信息
using (sqlAda = new SqlDataAdapter("select distinct(pro_name) from tb_productsale", con))
{
DataSet ds = new DataSet();
sqlAda.Fill(ds, "tb_productsale");
this.comboBox1.DataSource = ds.Tables[0];
this.comboBox1.DisplayMember = "pro_name";//把不重复的销售人员添加到下拉列表框中
}
}
private void button1_Click(object sender, EventArgs e)
{
G++;//变量G自动加1
//调用自定义函数DrowFont、DrowInfo和DrowPic
DrowFont(this.comboBox1.Text.ToString());
DrowInfo(this.comboBox1.Text.ToString());
DrowPic(this.comboBox1.Text.ToString());
}
2.3 窗体运行
3 总结
3.1 using(sqlAda=new SqlDataAdapter(".....")
{
}的使用
3.2 利用Graphics对象的DrawLine()方法绘制线段
3.3 Bitmap对象和SqlDataReader对象的应用
3.4 SolidBrush对象和Color对象的应用
3.5 手动绘图必要性(个人观点):存在使用chart以及一些漂亮的infragistics netadvantage控件进行设计数据展示,但是对于大量数据时,算法绘图可以提高效率,更有利于捕捉动态变化情况。