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 ); }

#region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.ds = new System.Data.DataSet(); this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand(); this.da = new System.Data.OleDb.OleDbDataAdapter(); this.comm = new System.Data.OleDb.OleDbCommand(); this.cn = new System.Data.OleDb.OleDbConnection(); this.axMSChart1 = new AxMSChart20Lib.AxMSChart(); this.t1 = new System.Windows.Forms.TextBox(); ((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.axMSChart1)).BeginInit(); this.SuspendLayout(); // // ds // this.ds.DataSetName = "NewDataSet"; this.ds.Locale = new System.Globalization.CultureInfo("zh-CN"); // // da // this.da.DeleteCommand = this.oleDbDeleteCommand1; this.da.InsertCommand = this.oleDbInsertCommand1; this.da.SelectCommand = this.oleDbSelectCommand1; this.da.UpdateCommand = this.oleDbUpdateCommand1; // // axMSChart1 // this.axMSChart1.DataSource = null; this.axMSChart1.Location = new System.Drawing.Point(0, 8); this.axMSChart1.Name = "axMSChart1"; this.axMSChart1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMSChart1.OcxState"))); this.axMSChart1.Size = new System.Drawing.Size(688, 328); this.axMSChart1.TabIndex = 0; // // t1 // this.t1.Location = new System.Drawing.Point(80, 384); this.t1.Name = "t1"; this.t1.Size = new System.Drawing.Size(544, 21); this.t1.TabIndex = 1; this.t1.Text = "textBox1"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(704, 445); this.Controls.Add(this.t1); this.Controls.Add(this.axMSChart1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.axMSChart1)).EndInit(); this.ResumeLayout(false);

} #endregion

/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() {     Form1 myf=new Form1(); myf.myphoto(); Application.Run(myf); }

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

} } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值