winform中添加柱状图

 

在[工具]->[添加/移出工具箱]->鼠标单击->选择[COM组件]
找到Mcrosoft Chart Control 
6.0(SP4)(oledb) 选中,然后[确定],你会发现你的工具箱里面有了一个这样的控件了!然后接着开始写代码!
____________________________________慢慢看把,道路很长的哦(记得给我分哦)_____________
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>

public class Form1 : System.Windows.Forms.Form
{
//这里申明了DataSet,OleDbCommand,OleDbDataAdapter和OleDbConnection
private System.Data.DataSet ds;
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private System.Data.OleDb.OleDbDataAdapter da;
private System.Data.OleDb.OleDbCommand comm;
private System.Data.OleDb.OleDbConnection cn;

//这里声明了一个AxMSChart类的实例;
private AxMSChart20Lib.AxMSChart axMSChart1;

private System.Windows.Forms.TextBox t1;
/// <summary>
/// 必需的设计器变量。
/// </summary>

private System.ComponentModel.Container components = null;

 

public void myphoto()
{
//装载数据库的数据
cn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+"\\data\\hzj.mdb"+";Persist Security Info=False";
cn.Open();
comm.Connection
=cn;
//我的表命名为myid,ST字段代表累计时间(s),yl代表压力(MPa)
string sql="select st,yl from myid";
comm.CommandText
=sql;
da.SelectCommand
=comm; 
da.Fill(ds,
"newtb");

//设计原理,由于axMSChart1的数据是直接可以接受一个二为数组的,
//所以我把事先读好的数据放到一个数组里
//注意数据库对应二为数组,是这样的行是一位,列是二位;
//所以我们看看就这样做
//由于楼主提出以时间(第2列)为横坐标,压力(第4列)为纵坐标的曲线图
//所以我们要把时间放在数组里的第一位,而压力放在第2位
//因为在axMSChart里数组第一位代表横坐标
//最后我们申明一个2纬数组,由于2纬数组一位代表行
//所以我们可以用ds.Tables["newtb"].Rows.Count取得行的总数
//而我们只要2列就可以了,所以我们是直接知道2纬数组的第2位的值,也就是2
//最后也是最关键的,也就是axMSChart需要数组里的第一个值来描述坐标点的说明
//所以我们就要空出一个位的数组来做描述
//因此数组真正的长度为ds.Tables["newtb"].Rows.Count+1
             Object[,] myay=new Object[ds.Tables["newtb"].Rows.Count+1,2];

 

myay[
0,0]=(Object)"时间描述";//这里可以不用写什么
myay[0,1]=(Object)"压力描述";//这里是描述坐标点的压力


for(int i=1;i<=ds.Tables["newtb"].Rows.Count;i++)
{


//这里是个重点,为什么要加上"["+"]"
//其实这个axMSChart1玩意很有意识的,如果你的字符串是数字类型的描述
//就算是字符串类型也不会当成横坐标哦!!
//嘿嘿有意识把,本人曾经为了这个问题搞了几天的!
myay[i,0]=(Object)"["+ds.Tables["newtb"].Rows[i-1]["st"].ToString()+"]";
myay[i,
1]=(Object)ds.Tables["newtb"].Rows[i-1]["yl"].ToString();


}


//设计axMSChart1的数据

axMSChart1.ChartData 
= myay;


//设计图表的表头名称和对齐方式
axMSChart1.Title.Text = "这里是一个图表";

axMSChart1.Legend.Location.LocationType 
= MSChart20Lib.VtChLocationType.VtChLocationTypeRight;

axMSChart1.Legend.Location.Visible 
= true;


            
//设计X轴名称
axMSChart1.Plot.get_Axis(MSChart20Lib.VtChAxisId.VtChAxisIdX, null).AxisTitle.Text = "这里是时间描述";
            
            
//设计Y轴名称
axMSChart1.Plot.get_Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY, null).AxisTitle.Text = "这里是压力描述";

 


//设计图表形状的描述方式,是并图还是线图等等
//axMSChart1.chartType = MSChart20Lib.VtChChartType.VtChChartType2dLine;


ds.Tables.Clear();
cn.Close();


}


 

 

 

 

 

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}


/// <summary>
/// 清理所有正在使用的资源。
/// </summary>

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null
{
components.Dispose();
}

}

base.Dispose( disposing );
}


Windows 窗体设计器生成的代码

/// <summary>
/// 应用程序的主入口点。
/// </summary>

[STAThread]
static void Main() 
{
    Form1 myf
=new Form1();
myf.myphoto();
Application.Run(myf);
}


private void Form1_Load(object sender, System.EventArgs e)
{

}

}

}


 
posted @ 2008-07-07 09:41  梦极无边界  阅读(1187)  评论(1)    收藏  举报