winform中柱状图

来源:互联网 发布:docker -it 编辑:程序博客网 时间:2024/05/04 01:26

在[工具]->[添加/移出工具箱]->鼠标单击->选择[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和OleDbConnectionprivate 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){

}}}