c#mschart简单例子.docx
- 文档编号:28570305
- 上传时间:2023-07-19
- 格式:DOCX
- 页数:17
- 大小:19.56KB
c#mschart简单例子.docx
《c#mschart简单例子.docx》由会员分享,可在线阅读,更多相关《c#mschart简单例子.docx(17页珍藏版)》请在冰豆网上搜索。
c#mschart简单例子
c#mschart简单例子
2010-04-1618:
43:
14| 分类:
控件| 标签:
|字号大中小 订阅
第一个简单的chart:
spline
//Createnewdataseriesandsetit'svisualattributes
Seriesseries=newSeries("Spline");
series.ChartType=SeriesChartType.Spline;
series.BorderWidth=3;
series.ShadowOffset=2;
//Populatenewserieswithdata
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);
//Addseriesintothechart'sseriescollection
Chart1.Series.Add(series);
同时显示2条曲线
//Populateserieswithrandomdata
Randomrandom=newRandom();
for(intpointIndex=0;pointIndex<10;pointIndex++)
{
Chart1.Series["Series1"].Points.AddY(random.Next(45,95));
Chart1.Series["Series2"].Points.AddY(random.Next(5,75));
}
//Setseriescharttype
Chart1.Series["Series1"].ChartType=SeriesChartType.Line;
Chart1.Series["Series2"].ChartType=SeriesChartType.Spline;
//Setpointlabels
Chart1.Series["Series1"].IsValueShownAsLabel=true;
Chart1.Series["Series2"].IsValueShownAsLabel=true;
//EnableXaxismargin
Chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible=true;
//Enable3D,andshowdatapointmarkerlines
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D=true;
Chart1.Series["Series1"]["ShowMarkerLines"]="True";
Chart1.Series["Series2"]["ShowMarkerLines"]="True";
显示column类型图
//Createnewdataseriesandsetit'svisualattributes
Seriesseries=newSeries("FlowRead");
series.ChartType=SeriesChartType.Column;
series.BorderWidth=3;
series.ShadowOffset=2;
//Populatenewserieswithdata
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);
//Addseriesintothechart'sseriescollection
Chart1.Series.Add(series);
很多点,效率还可以
//Fillseriesdata
doubleyValue=50.0;
Randomrandom=newRandom();
for(intpointIndex=0;pointIndex<20000;pointIndex++)
{
yValue=yValue+(random.NextDouble()*10.0-5.0);
Chart1.Series["Series1"].Points.AddY(yValue);
}
//Setfastlinecharttype
Chart1.Series["Series1"].ChartType=SeriesChartType.FastLine;
}
日期,xy类型
//Createanewrandomnumbergenerator
Randomrnd=newRandom();
//DatapointsXvalueisusingcurrentdate
DateTimedate=DateTime.Now.Date;
//Addpointstothestockchartseries
for(intindex=0;index<10;index++)
{
Chart1.Series["Series1"].Points.AddXY(
date, //Xvalueisadate
rnd.Next(40,50)); //CloseYvalue
//Add1daytoourXvalue
date=date.AddDays
(1);
}
int-int的xy数据绘图
//Createanewrandomnumbergenerator
Randomrnd=newRandom();
//Addpointstothestockchartseries
for(intindex=0;index<10;index++)
{
Chart1.Series["Series1"].Points.AddXY(
rnd.Next(10,90), //Xvalueisadate
rnd.Next(40,50)); //CloseYvalue
}
数据库数据,datetime-int类型
stringconnStr="server=localhost;database=seis_project;uid=seisprjs;pwd=seisprjs";
SqlConnectionmyConn=newSqlConnection(connStr);
stringselStr="select时间,序号frompub_log_readorderby序号asc";
SqlCommandmyCmd=myConn.CreateCommand();
myCmd.CommandText=selStr;
myConn.Open();
SqlDataReadersdr=myCmd.ExecuteReader(CommandBehavior.CloseConnection);
//SincethereaderimplementsandIEnumerable,passthereaderdirectlyinto
//theDataBindTablemethodwiththenameoftheColumntobeusedastheXValue
Chart1.DataBindTable(sdr,"时间");
sdr.Close();
myConn.Close();
数据库数据2,string-int类型
stringconnStr="server=localhost;database=seis_project;uid=seisprjs;pwd=seisprjs";
SqlConnectionmyConn=newSqlConnection(connStr);
stringselStr="select帐号,count(帐号)as次数frompub_log_readgroupby帐号orderby帐号asc";
SqlCommandmyCmd=myConn.CreateCommand();
myCmd.CommandText=selStr;
myConn.Open();
SqlDataReadersdr=myCmd.ExecuteReader(CommandBehavior.CloseConnection);
//SincethereaderimplementsandIEnumerable,passthereaderdirectlyinto
//theDataBindTablemethodwiththenameoftheColumntobeusedastheXValue
Chart1.DataBindTable(sdr,"帐号");
sdr.Close();
myConn.Close();
数据库绑定3-error?
stringconnStr="server=localhost;database=seis_project;uid=seisprjs;pwd=seisprjs";
SqlConnectionmyConn=newSqlConnection(connStr);
stringselStr="select帐号,count(帐号)as次数frompub_log_readgroupby帐号orderby帐号asc";
SqlCommandmyCmd=myConn.CreateCommand();
myCmd.CommandText=selStr;
myConn.Open();
//Setchartdatasource
Chart1.DataSource=myCmd;
//SetseriesmembersnamesfortheXandYvalues
Chart1.Series["Series1"].XValueMember="帐号";
Chart1.Series["Series1"].YValueMembers="次数";
//Databindtotheselecteddatasource
Chart1.DataBind();
myConn.Close();
数据库4,只绑定y
stringconnStr="server=localhost;database=seis_project;uid=seisprjs;pwd=seisprjs";
SqlConnectionmyConn=newSqlConnection(connStr);
stringselStr="select序号frompub_log_readorderby序号asc";
SqlCommandmyCmd=myConn.CreateCommand();
myCmd.CommandText=selStr;
myConn.Open();
SqlDataReadersdr=myCmd.ExecuteReader(CommandBehavior.CloseConnection);
//SincethereaderimplementsandIEnumerable,passthereaderdirectlyinto
//theDataBindTablemethodwiththenameoftheColumntobeusedastheXValue
Chart1.Series[0].Points.DataBindY(sdr);
sdr.Close();
myConn.Close();
数据库5,绑定xy
stringconnStr="server=localhost;database=seis_project;uid=seisprjs;pwd=seisprjs";
SqlConnectionmyConn=newSqlConnection(connStr);
stringselStr="select帐号,count(帐号)as次数frompub_log_readgroupby帐号orderby帐号desc";
SqlCommandmyCmd=myConn.CreateCommand();
myCmd.CommandText=selStr;
myConn.Open();
SqlDataReadersdr=myCmd.ExecuteReader(CommandBehavior.CloseConnection);
//SincethereaderimplementsandIEnumerable,passthereaderdirectlyinto
//theDataBindTablemethodwiththenameoftheColumntobeusedastheXValue
Chart1.Series[0].Points.DataBindXY(sdr,"帐号",sdr,"次数");
sdr.Close();
myConn.Close();
数据库6,支持显示参数
//ResolvetheaddresstotheAccessdatabase
stringfileNameString=this.MapPath(".");
fileNameString+="..\\..\\..\\data\\chartdata.mdb";
//Initializeaconnectionstring
stringmyConnectionString="PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource="+fileNameString;
//Definethedatabasequery
stringmySelectQuery="SELECT*FROMREPSALESWHEREYear=2004;";
//Createadatabaseconnectionobjectusingtheconnectionstring
OleDbConnectionmyConnection=newOleDbConnection(myConnectionString);
//Createadatabasecommandontheconnectionusingquery
OleDbCommandmyCommand=newOleDbCommand(mySelectQuery,myConnection);
//Opentheconnection
myCommand.Connection.Open();
//Createadatabasereader
OleDbDataReadermyReader=myCommand.ExecuteReader(CommandBehavior.CloseConnection);
//SincethereaderimplementsandIEnumerable,passthereaderdirectlyinto
//theDataBindmethodwiththenameoftheColumnsassignedtotheappropriateproperty
Chart1.Series["Series1"].Points.DataBind(
myReader,
"Name",
"Sales",
"Tooltip=Year,Label=Commissions{C2}");
//Closethereaderandtheconnection
myReader.Close();
myConnection.Close();
数据库7,支持多line
//ResolvetheaddresstotheAccessdatabase
stringfileNameString=this.MapPath(".");
fileNameString+="..\\..\\..\\data\\chartdata.mdb";
//Initializeaconnectionstring
stringmyConnectionString="PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource="+fileNameString;
//Definethedatabasequery
stringmySelectQuery="SELECT*FROMREPSALES;";
//Createadatabaseconnectionobjectusingtheconnectionstring
OleDbConnectionmyConnection=newOleDbConnection(myConnectionString);
//Createadatabasecommandontheconnectionusingquery
OleDbCommandmyCommand=newOleDbCommand(mySelectQuery,myConnection);
//Opentheconnection
myCommand.Connection.Open();
//Createadatabasereader
OleDbDataReadermyReader=myCommand.ExecuteReader(CommandBehavior.CloseConnection);
//Databindcharttoatablewhereallrowsaregroupedinseriesbythe"Name"column
Chart1.DataBindCrossTable(
myReader,
"Name",
"Year",
"Sales",
"Label=Commissions{C}");
//Closethereaderandtheconnection
myReader.Close();
myConnection
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- mschart 简单 例子
![提示](https://static.bdocx.com/images/bang_tan.gif)