c#mschart控件使用方法汇总(附统计图).doc_第1页
c#mschart控件使用方法汇总(附统计图).doc_第2页
c#mschart控件使用方法汇总(附统计图).doc_第3页
c#mschart控件使用方法汇总(附统计图).doc_第4页
c#mschart控件使用方法汇总(附统计图).doc_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

c# ms chart 控件使用方法 第一个简单的chart:创建曲线图 chart1.Series.Clear(); Series series = new Series(Spline); series.ChartType = SeriesChartType.Spline; series.BorderWidth = 3; series.ShadowOffset = 2; / Populate new series with data series.Points.AddY(67); series.Points.AddY(57); series.Points.AddY(83); series.Points.AddY(23); series.Points.AddY(70); series.Points.AddY(60); series.Points.AddY(90); series.Points.AddY(20); / Add series into the charts series collection chart1.Series.Add(series);同时显示2条曲线/ Populate series with random dataRandom random = new Random();for (int pointIndex = 0; pointIndex 10;pointIndex+)Chart1.SeriesSeries1.Points.AddY(random.Next(45, 95);Chart1.SeriesSeries2.Points.AddY(random.Next(5, 75);/ Set series chart typeChart1.SeriesSeries1.ChartType = SeriesChartType.Line;Chart1.SeriesSeries2.ChartType = SeriesChartType.Spline;/ Set point labelsChart1.SeriesSeries1.IsValueShownAsLabel = true;Chart1.SeriesSeries2.IsValueShownAsLabel = true;/ Enable X axis marginChart1.ChartAreasChartArea1.AxisX.IsMarginVisible = true;/ Enable 3D, and show data point marker linesChart1.ChartAreasChartArea1.Area3DStyle.Enable3D = true;Chart1.SeriesSeries1ShowMarkerLines = True;Chart1.SeriesSeries2ShowMarkerLines = True;显示column类型图,柱状图/ Create new data series and set its visual attributesChart1.Series.Clear();Series series = new Series(FlowRead);series.ChartType = SeriesChartType.Column;series.BorderWidth = 3;series.ShadowOffset = 2;/ Populate new series with dataseries.Points.AddY(67);series.Points.AddY(57);series.Points.AddY(83);series.Points.AddY(23);series.Points.AddY(70);series.Points.AddY(60);series.Points.AddY(90);series.Points.AddY(20);/ Add series into the charts series collectionChart1.Series.Add(series);很多点,效率还可以/ Fill series datadouble yValue = 50.0;Random random = new Random();for (int pointIndex = 0; pointIndex 20000;pointIndex+)yValue = yValue + (random.NextDouble() * 10.0 - 5.0);Chart1.SeriesSeries1.Points.AddY(yValue);/ Set fast line chart typeChart1.SeriesSeries1.ChartType = SeriesChartType.FastLine; 日期,xy类型/ Create a new random number generatorRandom rnd = new Random();/ Data points X value is using current dateDateTime date = DateTime.Now.Date;/ Add points to the stock chart seriesfor (int index = 0; index 10; index+)Chart1.SeriesSeries1.Points.AddXY(date,/ X value is a daternd.Next(40,50); /Close Y value/ Add 1 day to our X valuedate = date.AddDays(1);int-int的xy数据绘图/ Create a new random number generatorRandom rnd = new Random();/ Add points to the stock chart seriesfor (int index = 0; index 10; index+)Chart1.SeriesSeries1.Points.AddXY(rnd.Next(10,90),/ X value is a daternd.Next(40,50); /Close Y value数据库数据,datetime-int类型 Chart1.Series.Clear(); OleDbConnection conn = new OleDbConnection(Provider=Microsoft.Jet.OLEDB.4.0;Data Source= + Application.StartupPath + db.mdb + ;Persist Security Info=False); OleDbCommand cmd = conn.CreateCommand(); conn.Open(); cmd.CommandText = select 时间,序号 from pub_log_read order by 序号 asc; /DataSet ds = new DataSet(); /OleDbDataAdapter da = new OleDbDataAdapter(); /da.SelectCommand = cmd; /da.Fill(ds, tbscore); OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); Chart1.DataBindTable(dr, 时间); dr.Close();数据库数据2,string-int类型 Chart1.Series.Clear(); OleDbConnection conn = new OleDbConnection(Provider=Microsoft.Jet.OLEDB.4.0;Data Source= + Application.StartupPath + db.mdb + ;Persist Security Info=False); OleDbCommand cmd = conn.CreateCommand(); conn.Open(); cmd.CommandText = select 账号,count(账号) as 次数 from pub_log_read group by 账号 order by 账号 asc; /DataSet ds = new DataSet(); /OleDbDataAdapter da = new OleDbDataAdapter(); /da.SelectCommand = cmd; /da.Fill(ds, tbscore); OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); Chart1.DataBindTable(dr, 账号); dr.Close();数据库绑定3-string-int型 Chart1.Series.Clear(); Chart1.Series.Add(Series1); OleDbConnection conn = new OleDbConnection(Provider=Microsoft.Jet.OLEDB.4.0;Data Source= + Application.StartupPath + db.mdb + ;Persist Security Info=False); OleDbCommand cmd = conn.CreateCommand(); cmd.CommandText = select 账号,count(账号) as 次数 from pub_log_read group by 账号 order by 账号 asc; conn.Open(); DataSet ds = new DataSet(); OleDbDataAdapter da = new OleDbDataAdapter(); da.SelectCommand = cmd; da.Fill(ds, tbscore); Chart1.DataSource = ds; Chart1.SeriesSeries1.XValueMember = 账号; Chart1.SeriesSeries1.YValueMembers = 次数; / Data bind to the selected data source Chart1.DataBind(); conn.Close();数据库4,只绑定y Chart1.Series.Clear(); Chart1.Series.Add(序号); OleDbConnection conn = new OleDbConnection(Provider=Microsoft.Jet.OLEDB.4.0;Data Source= + Application.StartupPath + db.mdb + ;Persist Security Info=False); OleDbCommand cmd = conn.CreateCommand(); conn.Open(); cmd.CommandText = select 序号 from pub_log_read order by 序号 asc; /DataSet ds = new DataSet(); /OleDbDataAdapter da = new OleDbDataAdapter(); /da.SelectCommand = cmd; /da.Fill(ds, tbscore); OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); Chart1.Series0.Points.DataBindY(dr); dr.Close();数据库5,绑定xy Chart1.Series.Clear(); Chart1.Series.Add(序号); OleDbConnection conn = new OleDbConnection(Provider=Microsoft.Jet.OLEDB.4.0;Data Source= + Application.StartupPath + db.mdb + ;Persist Security Info=False); OleDbCommand cmd = conn.CreateCommand(); conn.Open(); cmd.CommandText = select 账号,count(账号) as 次数 from pub_log_read group by 账号 order by 账号 desc; /DataSet ds = new DataSet(); /OleDbDataAdapter da = new OleDbDataAdapter(); /da.SelectCommand = cmd; /da.Fill(ds, tbscore); OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); Chart1.Series0.Points.DataBindXY(dr,账号,dr,次数); dr.Close();数据库6,支持显示参数 Chart1.Series.Clear(); Chart1.Series.Add(S1); OleDbConnection conn = new OleDbConnection(Provider=Microsoft.Jet.OLEDB.4.0;Data Source= + Application.StartupPath + db.mdb + ;Persist Security Info=False); OleDbCommand cmd = conn.CreateCommand(); conn.Open(); cmd.CommandText = SELECT * FROM REPSALES WHERE Year=2004; /DataSet ds = new DataSet(); /OleDbDataAdapter da = new OleDbDataAdapter(); /da.SelectCommand = cmd; /da.Fill(ds, tbscore); OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); Chart1.Series0.Points.DataBind(dr, name, sales, Tooltip=Year, Label=CommissionsC2); dr.Close();数据库7,支持多line Chart1.Series.Clear(); Chart1.Series.Add(S1); OleDbConnection conn = new OleDbConnection(Provider=Microsoft.Jet.OLEDB.4.0;Data Source= + Application.StartupPath + db.mdb + ;Persist Security Info=False); OleDbCommand cmd = conn.CreateCommand(); conn.Open(); cmd.CommandText = SELECT * FROM REPSALES; /DataSet ds = new DataSet(); /OleDbDataAdapter da = new OleDbDataAdapter(); /da.SelectCommand = cmd; /da.Fill(ds, tbscore); OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); Chart1.DataBindCrossTable(dr, Name, Year, Sales, Label=CommissionsC); dr.Close();数据库8,按照行添加数据/ Resolve the address to the Access database stringfileNameString = this.MapPath(.);fileNameString += .datachartdata.mdb; /Initialize a connectionstring stringmyConnectionString = PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource= + fileNameString; / Definethe databasequery stringmySelectQuery=SELECT * FROM SALESCOUNTS; / Createa database connection object using the connectionstringOleDbConnection myConnection = newOleDbConnection(myConnectionString); / Create adatabase command on the connection usingquery OleDbCommandmyCommand = new OleDbCommand(mySelectQuery, myConnection); / Open theconnectionmyCommand.Connection.Open(); /Initializes a new instance of the OleDbDataAdapter classOleDbDataAdapter myDataAdapter = new OleDbDataAdapter();myDataAdapter.SelectCommand = myCommand; /Initializes a new instance of the DataSet class DataSetmyDataSet = new DataSet(); / Addsrows in the DataSetmyDataAdapter.Fill(myDataSet,Query);foreach(DataRow row in myDataSet.TablesQuery.Rows) / For each Row add a new seriesstring seriesName = rowSalesRep.ToString();Chart1.Series.Add(seriesName);Chart1.SeriesseriesName.ChartType = SeriesChartType.Line;Chart1.SeriesseriesName.BorderWidth = 2;for(int colIndex = 1; colIndex myDataSet.TablesQuery.Columns.Count; colIndex+)/ For each column (column 1 and onward) add the value as apointstring columnName =myDataSet.TablesQuery.ColumnscolIndex.ColumnName;int YVal = (int) rowcolumnName;Chart1.SeriesseriesName.Points.AddXY(columnName, YVal); DataGrid.DataSource = myDataSet;DataGrid.DataBind(); / Closesthe connection to the data source. This is the preferred / method ofclosing any open connection.myCommand.Connection.Close();使用xml数据/ resolve the address to the XML document stringfileNameString = this.MapPath(.); stringfileNameSchema = this.MapPath(.);fileNameString += .datadata.xml;fileNameSchema += .datadata.xsd; /Initializes a new instance of the DataSet class DataSetcustDS = new DataSet(); / Read XMLschema into the DataSet.custDS.ReadXmlSchema( fileNameSchema ); / ReadXML schema and data into the DataSet.custDS.ReadXml( fileNameString ); /Initializes a new instance of the DataView class DataViewfirstView = new DataView(custDS.Tables0); / Sincethe DataView implements and IEnumerable, pass the reader directlyinto / theDataBindTable method with the name of the column used for the Xvalue.Chart1.DataBindTable(firstView, Name);使用excel数据/ resolve the address to the Excel file stringfileNameString = this.MapPath(.);fileNameString += .dataExcelData.xls; / Createconnection object by using the preceding connection string. string sConn= Provider=Microsoft.Jet.OLEDB.4.0;Data Source= +fileNameString + ;Extended Properties=Excel8.0;HDR=YES;OleDbConnection myConnection = new OleDbConnection( sConn );myConnection.Open(); / Thecode to follow uses a SQL SELECT command to display the data fromthe worksheet. / Createnew OleDbCommand to return data from worksheet. OleDbCommandmyCommand = new OleDbCommand( Select * From data1$A1:E25,myConnection ); / createa databasereaderOleDbDataReader myReader =myCommand.ExecuteReader(CommandBehavior.CloseConnection); /Populate the chart with data in the fileChart1.DataBindTable(myReader, HOUR); / closethe reader and the connectionmyReader.Close();myConnection.Close();使用csv数据/ Filename of the CSV file string file= DataFile.csv; / Getthe path of the CSV file string path= this.MapPath(.); path +=.data; / Createa select statement and a connection string. stringmySelectQuery = Select * from + file; stringConStr = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=+path+ ;Extended Properties=Text;HDR=No;FMT=Delimited;OleDbConnection myConnection = new OleDbConnection(ConStr); / Createa database command on the connection using query OleDbCommandmyCommand = new OleDbCommand(mySelectQuery, myConnection); / Openthe connection and create the readermyCommand.Connection.Open();OleDbDataReader myReader =myCommand.ExecuteReader(CommandBehavior.CloseConnection); / Column1 is a time value, column 2 is a double / Databindthe reader to the chart using the DataBindXY methodChart1.Series0.Points.DataBindXY(myReader, 1, myReader,2); / Closeconnection and data readermyReader.Close();myConnection.Close();数组绘图/ Initialize an array of doublesdouble yval = 2, 6, 4, 5, 3 ;/ Initialize an array of stringsstring xval = Peter, Andrew, Julie, Mary, Dave ;/ Bind the double array to the Y axis points of the Default dataseriesChart1.SeriesSeries1.Points.DataBindXY(xval, yval);数据库9,dataview/ Resolve the address to the Access database stringfileNameString = this.MapPath(.);fileNameString += .datachartdata.mdb; /Initialize a connectionstring stringmyConnectionString = PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource= + fileNameString; / Definethe databasequery stringmySelectQuery=SELECT * FROM REPS; / Createa database connection object using the connectionstringOleDbConnection myConnection = newOleDbConnection(myConnectionString); / Create adatabase command on the connection usingquery OleDbCommandmyCommand = new OleDbCommand(mySelectQuery, myConnection); / Open theconnectionmyCommand.Connection.Open(); /Initializes a new instance of the OleDbDataAdapter classOleDbDataAdapter custDA = new OleDbDataAdapter();custDA.SelectCommand = myCommand; /Initializes a new instance of the DataSet class DataSetcustDS = new DataSet(); / Addsrows in the DataSetcustDA.Fill(custDS, Customers); /Initializes a new instance of the DataView class DataViewfirstView = new DataView(custDS.Tables0); / Sincethe DataView implements IEnumerable, pass the dataview directlyinto/ the DataBind m

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论