|
介绍MSChart的常用属性和事件.............................................................................................................................2
数据源绑定(一),具体代码如下:.....................................................................................................................6
数据源绑定方式(二)...........................................................................................................................................8
数据源绑定(三):.................................................................................................................................................9
数据源绑定(四):...............................................................................................................................................11
数据源绑定(四)——行绑定.............................................................................................................................13
数据源绑定(五).................................................................................................................................................15
MSChart的研究(二)收藏......................................................................................................................................17
一.数据源——xml..................................................................................................................................................18
二.数据源——Excel...............................................................................................................................................18
三.数据源——CSV文件........................................................................................................................................20
继续对MSChart学习:本次主要针对MSChart的图形交互和AJAX的应用。.....................................................22
二.AJAX(一)——ClickEvent................................................................................................................................23
三.AJAX(二)——ChartasTrriger.........................................................................................................................29
四.AJAX(三)——AJAX更新................................................................................................................................31
五.AJAX(四)——RealTimeChart........................................................................................................................35
解决MSChart底部横坐标显示不全的问题收藏.................................................................................................38
mschart上下限阈值画横线收藏.......................................................................................................................38
MSChartforvs2008收藏......................................................................................................................................39
MSCHART全部属性方法介绍收藏....................................................................................................................41
在access中创建库表(.net实现).......................................................................................................................57
MSChart学习帮助-开发日志(二).net3.5图表控件收藏..............................................................................62
三个电压,三个电流.............................................................................................................................................70
.NETMSChart应用的一个简单例子.....................................................................................................................76
初试ASP.NETChartControl.................................................................................................................................83
微软图表控件使用初探--数据绑定(交叉表)........................................................................................................88
MSChart图表控件的一些使用...............................................................................................................................96
微软Chart控件导学--数据绑定............................................................................................................................110
Mschart学习(MSchart介绍)..................................................................................................................................116
微软图表控件MsChart使用初探.........................................................................................................................122
微软Chart控件导学--渲染图表............................................................................................................................135
介绍MSChart的常用属性和事件
MSChart的元素组成
最常用的属性包括
ChartAreas:增加多个绘图区域,每个绘图区域包含独立的图表组、数据源,用于多个图表类型在一个绘
图区不兼容时。
最常用的属性包括
ChartAreas:增加多个绘图区域,每个绘图区域包含独立的图表组、数据源,用于多个图表类型在一个绘
图区不兼容时。
AlignmentOrientation:图表区对齐方向,定义两个绘图区域间的对齐方式。
AlignmentStyle:图表区对齐类型,定义图表间用以对其的元素。
AlignWithChartArea:参照对齐的绘图区名称。
InnerPlotPosition:图表在绘图区内的位置属性。
Auto:是否自动对齐。
Height:图表在绘图区内的高度(百分比,取值在0-100)
Width:图表在绘图区内的宽度(百分比,取值在0-100)
X,Y:图表在绘图区内左上角坐标
Position:绘图区位置属性,同InnerPlotPosition。
Name:绘图区名称。
Axis:坐标轴集合
Title:坐标轴标题
TitleAlignment:坐标轴标题对齐方式
Interval:轴刻度间隔大小
IntervalOffset:轴刻度偏移量大小
MinorGrid:次要辅助线
MinorTickMark:次要刻度线
MajorGrid:主要辅助线
MajorTickMark:主要刻度线
DataSourceID:MSChart的数据源。
Legends:图例说明。
Palette:图表外观定义。
Series:最重要的属性,图表集合,就是最终看到的饼图、柱状图、线图、点图等构成的集合;可以将多
种相互兼容的类型放在一个绘图区域内,形成复合图。
IsValueShownAsLabel:是否显示数据点标签,如果为true,在图表中显示每一个数据值
Label:数据点标签文本
LabelFormat:数据点标签文本格式
LabelAngle:标签字体角度
Name:图表名称
Points:数据点集合
XValueType:横坐标轴类型
YValueType:纵坐标轴类型
XValueMember:横坐标绑定的数据源(如果数据源为Table,则填写横坐标要显示的字段名称)
YValueMembers:纵坐标绑定的数据源(如果数据源为Table,则填写纵坐标要显示的字段名称,纵坐标可
以有两个)
ChartArea:图表所属的绘图区域名称
ChartType:图表类型(柱形、饼形、线形、点形等)
Legend:图表使用的图例名称
Titles:标题集合。
width:MSChart的宽度。
height:MSChart的高度。
常用事件:
Series1.Points.DataBind()
绑定数据点集合,如果要在一个MSChart控件的一个绘图区(ChartArea)内添加多个不同数据源的图表,就
用这个主动绑定数据集合的方法。可以将表中指定字段的值绑定到指定的坐标轴上。
MSChart1.DataBind()
给整个MSChart绑定一个数据源,该MSChart中的图表全部可以使用该数据源作为统计来源。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jetable/archive/2009/01/08/3727444.aspx
下面给一个自己做的例子:
viewplaincopytoclipboardprint?
int[]date=newint[]{20,40,50,80,30,10,60};
int[]test=newint[]{10,80,70,40,20,50,90};
Chart1.Width=600;//图片宽度
Chart1.Height=400;//图片高度
Chart1.BackColor=Color.Azure;//图片背景色
//建一个图表集合
Seriesseries=newSeries("test");
series.ChartType=SeriesChartType.Column;//图标集类型,Line为直线,SpLine
为曲线
series.Color=Color.Green;//线条颜色
series.BorderWidth=2;//线条宽度
series.ShadowOffset=1;//阴影宽度
series.IsVisibleInLegend=false;//是否显示数据说明
series.IsValueShownAsLabel=true;
series.MarkerStyle=MarkerStyle.Diamond;//线条上的数据点标志类型
series.MarkerSize=8;//标志的大小
DateTimedate1=DateTime.Now.Date;
for(inti=0;i {
series.Points.AddXY(date1,date[i]);
date1=date1.AddDays(1);
}
Chart1.Series.Add(series);//把数据集添加到Chart1中
//再建一个图表集合
Seriesseries1=newSeries("ok");
series1.ChartType=SeriesChartType.Column;//图标集类型,Line为直线,SpLine
为曲线
series1.Color=Color.Red;//线条颜色
series1.BorderWidth=2;//线条宽度
series1.ShadowOffset=1;//阴影宽度
series1.IsVisibleInLegend=false;//是否显示数据说明
series1.IsValueShownAsLabel=true;
series1.MarkerStyle=MarkerStyle.Diamond;
series1.MarkerSize=8;
DateTimedate2=DateTime.Now.Date;
for(inti=0;i {
series1.Points.AddXY(date2,test[i]);
date2=date2.AddDays(1);
}
Chart1.Series.Add(series1);//把数据集添加到Chart1中
//设置坐标轴
Chart1.ChartAreas[0].AxisX.LineColor=Color.Blue;
Chart1.ChartAreas[0].AxisY.LineColor=Color.Blue;
Chart1.ChartAreas[0].AxisX.LineWidth=2;
Chart1.ChartAreas[0].AxisY.LineWidth=2;
Chart1.ChartAreas[0].AxisY.Title="总额";
//设置网格线
Chart1.ChartAreas[0].AxisX.MajorGrid.LineColor=Color.Blue;
Chart1.ChartAreas[0].AxisY.MajorGrid.LineColor=Color.Blue;
int[]date=newint[]{20,40,50,80,30,10,60};
int[]test=newint[]{10,80,70,40,20,50,90};
Chart1.Width=600;//图片宽度
Chart1.Height=400;//图片高度
Chart1.BackColor=Color.Azure;//图片背景色
//建一个图表集合
Seriesseries=newSeries("test");
series.ChartType=SeriesChartType.Column;//图标集类型,Line为直线,SpLine
为曲线
series.Color=Color.Green;//线条颜色
series.BorderWidth=2;//线条宽度
series.ShadowOffset=1;//阴影宽度
series.IsVisibleInLegend=false;//是否显示数据说明
series.IsValueShownAsLabel=true;
series.MarkerStyle=MarkerStyle.Diamond;//线条上的数据点标志类型
series.MarkerSize=8;//标志的大小
DateTimedate1=DateTime.Now.Date;
for(inti=0;i {
series.Points.AddXY(date1,date[i]);
date1=date1.AddDays(1);
}
Chart1.Series.Add(series);//把数据集添加到Chart1中
//再建一个图表集合
Seriesseries1=newSeries("ok");
series1.ChartType=SeriesChartType.Column;//图标集类型,Line为直线,SpLine
为曲线
series1.Color=Color.Red;//线条颜色
series1.BorderWidth=2;//线条宽度
series1.ShadowOffset=1;//阴影宽度
series1.IsVisibleInLegend=false;//是否显示数据说明
series1.IsValueShownAsLabel=true;
series1.MarkerStyle=MarkerStyle.Diamond;
series1.MarkerSize=8;
DateTimedate2=DateTime.Now.Date;
for(inti=0;i {
series1.Points.AddXY(date2,test[i]);
date2=date2.AddDays(1);
}
Chart1.Series.Add(series1);//把数据集添加到Chart1中
//设置坐标轴
Chart1.ChartAreas[0].AxisX.LineColor=Color.Blue;
Chart1.ChartAreas[0].AxisY.LineColor=Color.Blue;
Chart1.ChartAreas[0].AxisX.LineWidth=2;
Chart1.ChartAreas[0].AxisY.LineWidth=2;
Chart1.ChartAreas[0].AxisY.Title="总额";
//设置网格线
Chart1.ChartAreas[0].AxisX.MajorGrid.LineColor=Color.Blue;
Chart1.ChartAreas[0].AxisY.MajorGrid.LineColor=Color.Blue;
最后显示的图片:
数据源绑定(一),具体代码如下:
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfileName=Server.MapPath(".");
fileName+="\\App_Data\\chartdata.mdb";
Chart1.Width=600;//图表宽度
Chart1.Height=400;//图表高度
Chart1.BackColor=Color.Azure;//图表背景色
Chart1.Titles.Add("房地产");//图表标题
//新建连接
using(OleDbConnectioncon=newOleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data
Source="+fileName))
{
OleDbCommandcmd=newOleDbCommand("SELECTName,SalesFROMREPS",con);
con.Open();
OleDbDataReaderread=cmd.ExecuteReader();
Chart1.DataBindTable(read,"Name");
read.Close();
}
//注意数据绑定后,它的series是1而不是0
Chart1.Series[1].IsValueShownAsLabel=true;//是否显示数据
Chart1.Series[1].IsVisibleInLegend=false;//是否显示数据说明
Chart1.Series[1].MarkerStyle=MarkerStyle.Circle;//线条上的数据点标志类型
Chart1.Series[1].MarkerSize=8;//标志大小
Chart1.ChartAreas[0].AxisX.LineColor=Color.Blue;//X轴颜色
Chart1.ChartAreas[0].AxisY.LineColor=Color.Blue;//Y轴颜色
Chart1.ChartAreas[0].AxisX.LineWidth=2;//X轴宽度
Chart1.ChartAreas[0].AxisY.LineWidth=2;//Y轴宽度
Chart1.ChartAreas[0].AxisY.Title="价格";//Y轴标题
}
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfileName=Server.MapPath(".");
fileName+="\\App_Data\\chartdata.mdb";
Chart1.Width=600;//图表宽度
Chart1.Height=400;//图表高度
Chart1.BackColor=Color.Azure;//图表背景色
Chart1.Titles.Add("房地产");//图表标题
//新建连接
using(OleDbConnectioncon=newOleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data
Source="+fileName))
{
OleDbCommandcmd=newOleDbCommand("SELECTName,SalesFROMREPS",con);
con.Open();
OleDbDataReaderread=cmd.ExecuteReader();
Chart1.DataBindTable(read,"Name");
read.Close();
}
//注意数据绑定后,它的series是1而不是0
Chart1.Series[1].IsValueShownAsLabel=true;//是否显示数据
Chart1.Series[1].IsVisibleInLegend=false;//是否显示数据说明
Chart1.Series[1].MarkerStyle=MarkerStyle.Circle;//线条上的数据点标志类型
Chart1.Series[1].MarkerSize=8;//标志大小
Chart1.ChartAreas[0].AxisX.LineColor=Color.Blue;//X轴颜色
Chart1.ChartAreas[0].AxisY.LineColor=Color.Blue;//Y轴颜色
Chart1.ChartAreas[0].AxisX.LineWidth=2;//X轴宽度
Chart1.ChartAreas[0].AxisY.LineWidth=2;//Y轴宽度
Chart1.ChartAreas[0].AxisY.Title="价格";//Y轴标题
}
效果图如下:
数据源绑定方式(二)
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfileName=Server.MapPath(".");
fileName+="\\App_Data\\chartdata.mdb";
Chart1.Width=600;//图表宽度
Chart1.Height=400;//图表高度
Chart1.BackColor=Color.Azure;//图表背景色
Chart1.Titles.Add("房地产");//图表标题
//新建连接
using(OleDbConnectioncon=newOleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data
Source="+fileName))
{
OleDbCommandcmd=newOleDbCommand("SELECTName,SalesFROMREPS",con);
con.Open();
//OleDbDataReaderread=cmd.ExecuteReader();
//Chart1.DataBindTable(read,"Name");
//read.Close();
Chart1.DataSource=cmd;
Chart1.Series[0].XValueMember="Name";
Chart1.Series[0].YValueMembers="Sales";
con.Close();
Chart1.DataBind();
}
}
效果图如下:
数据源绑定(三):
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfileName=Server.MapPath(".");
fileName+="\\App_Data\\chartdata.mdb";
Chart1.Width=600;//图表宽度
Chart1.Height=400;//图表高度
Chart1.BackColor=Color.Azure;//图表背景色
Chart1.Titles.Add("房地产");//图表标题
//新建连接
using(OleDbConnectioncon=newOleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data
Source="+fileName))
{
OleDbCommandcmd=newOleDbCommand("SELECTGrossSalesFROMSALESWHERE
QuarterEnding<#01/01/2002#",con);
con.Open();
OleDbDataReaderread=cmd.ExecuteReader();
Chart1.Series[0].Points.DataBindY(read,"GrossSales");
read.Close();
}
}
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfileName=Server.MapPath(".");
fileName+="\\App_Data\\chartdata.mdb";
Chart1.Width=600;//图表宽度
Chart1.Height=400;//图表高度
Chart1.BackColor=Color.Azure;//图表背景色
Chart1.Titles.Add("房地产");//图表标题
//新建连接
using(OleDbConnectioncon=newOleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data
Source="+fileName))
{
OleDbCommandcmd=newOleDbCommand("SELECTGrossSalesFROMSALESWHERE
QuarterEnding<#01/01/2002#",con);
con.Open();
OleDbDataReaderread=cmd.ExecuteReader();
Chart1.Series[0].Points.DataBindY(read,"GrossSales");
read.Close();
}
}
效果图如下:
数据源绑定(四):
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfileName=Server.MapPath(".");
fileName+="\\App_Data\\chartdata.mdb";
Chart1.Width=600;//图表宽度
Chart1.Height=400;//图表高度
Chart1.BackColor=Color.Azure;//图表背景色
Chart1.Titles.Add("房地产");//图表标题
//新建连接
using(OleDbConnectioncon=newOleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data
Source="+fileName))
{
OleDbCommandcmd=newOleDbCommand("SELECTFROMREPSALES",con);
con.Open();
OleDbDataReaderread=cmd.ExecuteReader();
Chart1.DataBindCrossTable(read,
"Name",
"Year",
"Sales",
"Label=Commissions{C}");
read.Close();
}
//Chart1.Legends.Add("Name");
}
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfileName=Server.MapPath(".");
fileName+="\\App_Data\\chartdata.mdb";
Chart1.Width=600;//图表宽度
Chart1.Height=400;//图表高度
Chart1.BackColor=Color.Azure;//图表背景色
Chart1.Titles.Add("房地产");//图表标题
//新建连接
using(OleDbConnectioncon=newOleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data
Source="+fileName))
{
OleDbCommandcmd=newOleDbCommand("SELECTFROMREPSALES",con);
con.Open();
OleDbDataReaderread=cmd.ExecuteReader();
Chart1.DataBindCrossTable(read,
"Name",
"Year",
"Sales",
"Label=Commissions{C}");
read.Close();
}
//Chart1.Legends.Add("Name");
}
效果图如下:(有点不理想,Legend中出现了Series1,关于legend的设置还有待研究)
数据源绑定(四)——行绑定
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfileName=Server.MapPath(".");
fileName+="\\App_Data\\chartdata.mdb";
Chart1.Width=600;//图表宽度
Chart1.Height=400;//图表高度
Chart1.BackColor=Color.Azure;//图表背景色
Chart1.Titles.Add("BindByRows");//图表标题
using(OleDbConnectioncon=newOleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data
Source="+fileName))
{
OleDbCommandcmd=newOleDbCommand("SELECTFROMSALESCOUNTS",con);
OleDbDataAdapteroda=newOleDbDataAdapter();
oda.SelectCommand=cmd;
DataSetds=newDataSet();
oda.Fill(ds);
foreach(DataRowrowinds.Tables[0].Rows)
{
stringseriesName=row["SalesRep"].ToString();
Chart1.Series.Add(seriesName);
Chart1.Series[seriesName].ChartType=SeriesChartType.Line;
Chart1.Series[seriesName].BorderWidth=2;
for(intcol=1;col {
stringcolName=ds.Tables[0].Columns[col].ColumnName;
intyVal=Convert.ToInt32(row[colName].ToString());
Chart1.Series[seriesName].Points.AddXY(colName,yVal);
}
}
}
}
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfileName=Server.MapPath(".");
fileName+="\\App_Data\\chartdata.mdb";
Chart1.Width=600;//图表宽度
Chart1.Height=400;//图表高度
Chart1.BackColor=Color.Azure;//图表背景色
Chart1.Titles.Add("BindByRows");//图表标题
using(OleDbConnectioncon=newOleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data
Source="+fileName))
{
OleDbCommandcmd=newOleDbCommand("SELECTFROMSALESCOUNTS",con);
OleDbDataAdapteroda=newOleDbDataAdapter();
oda.SelectCommand=cmd;
DataSetds=newDataSet();
oda.Fill(ds);
foreach(DataRowrowinds.Tables[0].Rows)
{
stringseriesName=row["SalesRep"].ToString();
Chart1.Series.Add(seriesName);
Chart1.Series[seriesName].ChartType=SeriesChartType.Line;
Chart1.Series[seriesName].BorderWidth=2;
for(intcol=1;col {
stringcolName=ds.Tables[0].Columns[col].ColumnName;
intyVal=Convert.ToInt32(row[colName].ToString());
Chart1.Series[seriesName].Points.AddXY(colName,yVal);
}
}
}
}
效果图如下:
数据源绑定(五)
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{
//Initializearraysforseries1
double[]yval1={2,6,5};
string[]xval1={"Peter","Andrew","Julie"};
//Initializearraysforseries2
double[]yval2={4,5,3};
string[]xval2={"Peter","Andrew","Dave"};
//Initializearraysforseries3
double[]yval3={6,5};
string[]xval3={"Julie","Mary"};
//Bindthearraystoeachdataseries
Chart1.Series["Series1"].Points.DataBindXY(xval1,yval1);
Chart1.Series["Series2"].Points.DataBindXY(xval2,yval2);
Chart1.Series["Series3"].Points.DataBindXY(xval3,yval3);
//AlignseriesusingtheirXaxislabels
Chart1.AlignDataPointsByAxisLabel();
Chart1.ChartAreas[0].Area3DStyle.Enable3D=true;//展示3D
}
protectedvoidPage_Load(objectsender,EventArgse)
{
//Initializearraysforseries1
double[]yval1={2,6,5};
string[]xval1={"Peter","Andrew","Julie"};
//Initializearraysforseries2
double[]yval2={4,5,3};
string[]xval2={"Peter","Andrew","Dave"};
//Initializearraysforseries3
double[]yval3={6,5};
string[]xval3={"Julie","Mary"};
//Bindthearraystoeachdataseries
Chart1.Series["Series1"].Points.DataBindXY(xval1,yval1);
Chart1.Series["Series2"].Points.DataBindXY(xval2,yval2);
Chart1.Series["Series3"].Points.DataBindXY(xval3,yval3);
//AlignseriesusingtheirXaxislabels
Chart1.AlignDataPointsByAxisLabel();
Chart1.ChartAreas[0].Area3DStyle.Enable3D=true;//展示3D
}
效果图如下:
MSChart的研究(二)收藏
继续上篇:
chart可以有下列几种数据源:
1.视图对象
2.dataRead对象(xml,sql,oledb)
3.dataSet对象
4.dataRow对象
5.dataColumn对象
6.数组对象
7.List对象
8.sqlCommand对象(oledbCommand)
9.sqlDataAdapter对象(oledbDataAdapter)
10.所有实现IEnumerable借口的对象
一.数据源——xml
代码:
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfileNameString=Server.MapPath(".\\App_Data\\data.xml");
stringfileNameSchema=Server.MapPath(".\\App_data\\data.xsd");
DataSetds=newDataSet();
ds.ReadXmlSchema(fileNameSchema);
ds.ReadXml(fileNameString);
DataViewdv=newDataView(ds.Tables[0]);
Chart1.DataBindTable(dv,"Name");
}
效果图:
二.数据源——Excel
代码:
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfileNameString=Server.MapPath(".\\App_Data\\ExcelData.xls");
//连接Excel数据源
stringconStr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+fileNameString+";Extended
Properties=\"Excel8.0;HDR=YES\"";
OleDbConnectioncon=newOleDbConnection(conStr);
con.Open();
OleDbCommandcmd=newOleDbCommand("selectfrom[data1$A1:E25]",con);
OleDbDataReaderread=cmd.ExecuteReader();
chart1.DataBindTable(read,"HOUR");
read.Close();
con.Close();
for(inti=1;i {
chart1.Series[i].ChartType=SeriesChartType.Line;
chart1.Series[i].BorderWidth=2;
}
chart1.Legends.Add("Default");
//设置表
chart1.Width=600;
chart1.Height=400;
chart1.BackColor=Color.Azure;
}
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfileNameString=Server.MapPath(".\\App_Data\\ExcelData.xls");
//连接Excel数据源
stringconStr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+fileNameString+";Extended
Properties=\"Excel8.0;HDR=YES\"";
OleDbConnectioncon=newOleDbConnection(conStr);
con.Open();
OleDbCommandcmd=newOleDbCommand("selectfrom[data1$A1:E25]",con);
OleDbDataReaderread=cmd.ExecuteReader();
chart1.DataBindTable(read,"HOUR");
read.Close();
con.Close();
for(inti=1;i {
chart1.Series[i].ChartType=SeriesChartType.Line;
chart1.Series[i].BorderWidth=2;
}
chart1.Legends.Add("Default");
//设置表
chart1.Width=600;
chart1.Height=400;
chart1.BackColor=Color.Azure;
}
效果图:
三.数据源——CSV文件
代码:
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfile="DataFile.csv";
//获取路径
stringpath=Server.MapPath(".\\App_Data\\");
stringsql="selectfrom"+file;
stringconStr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+
path+";ExtendedProperties=\"Text;HDR=No;FMT=Delimited\"";
OleDbConnectioncon=newOleDbConnection(conStr);
con.Open();
OleDbCommandcmd=newOleDbCommand(sql,con);
OleDbDataReaderread=cmd.ExecuteReader();
//"1"表示第一列即时间列,"2"表示第二列
Chart1.Series[0].Points.DataBindXY(read,"1",read,"2");
Chart1.Series[0].ChartType=SeriesChartType.Line;
Chart1.Width=600;
Chart1.Height=400;
Chart1.BackColor=Color.Azure;
read.Close();
con.Close();
}
protectedvoidPage_Load(objectsender,EventArgse)
{
stringfile="DataFile.csv";
//获取路径
stringpath=Server.MapPath(".\\App_Data\\");
stringsql="selectfrom"+file;
stringconStr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+
path+";ExtendedProperties=\"Text;HDR=No;FMT=Delimited\"";
OleDbConnectioncon=newOleDbConnection(conStr);
con.Open();
OleDbCommandcmd=newOleDbCommand(sql,con);
OleDbDataReaderread=cmd.ExecuteReader();
//"1"表示第一列即时间列,"2"表示第二列
Chart1.Series[0].Points.DataBindXY(read,"1",read,"2");
Chart1.Series[0].ChartType=SeriesChartType.Line;
Chart1.Width=600;
Chart1.Height=400;
Chart1.BackColor=Color.Azure;
read.Close();
con.Close();
}
效果图:
继续对MSChart学习:本次主要针对MSChart的图形交互和AJAX的应用。
一.图形交互:
概述:当点击饼图或legend中相应的模块,饼图会发生缺省:
代码如下:
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{Seriesseries=chart1.Series.Add("Myseries");
//Setseriesandlegendtooltips
series.ToolTip="#VALX:#VAL{C}million";
series.LegendToolTip="#PERCENT";
series.PostBackValue="#INDEX";
series.LegendPostBackValue="#INDEX";
//Populateseriesdata
double[]yValues={65.62,75.54,60.45,34.73,85.42,32.12,55.18,67.15,56.24,23.65};
string[]xValues={"France","Canada","Germany","USA","Italy","Russia","China","Japan",
"Sweden","Spain"};
series.Points.DataBindXY(xValues,yValues);
//Setseriesvisualattributes
series.ChartType=SeriesChartType.Pie;
series.ShadowOffset=2;
series.BorderColor=Color.DarkGray;
//series.CustomAttributes="LabelStyle=Outside";
chart1.Width=600;
chart1.Height=400;
}
protectedvoidchart1_Click(objectsender,ImageMapEventArgse)
{intpointIndex=int.Parse(e.PostBackValue);
Seriesseries=chart1.Series["Myseries"];
if(pointIndex>=0&&pointIndex {series.Points[pointIndex].CustomProperties+="Exploded=true";}
}
效果图:
二.AJAX(一)——ClickEvent
概述:当单击图中相应的列柱,标题2就会相应的变化,若单击Chart中其他地方,会捕捉Chart中的相
应对象的名称。
先在前台设置Chart,添加三个Series,分别对其设置Point,添加数据,如下:viewplaincopytoclipboardprint?
BorderColor="180,26,59,105"
Color="220,65,140,240"Legend="Legend1">
Color="220,252,180,65"Name="Series2"Legend="Legend1">
Color="220,224,64,10"Name="Series3"Legend="Legend1">
BackSecondaryColor="0,0,0,0"
BackColor="0,0,0,0"ShadowColor="0,0,0,0"
BackGradientStyle="TopBottom">
mce_style="Bold"">
mce_style="Bold"">
BorderColor="180,26,59,105"
Color="220,65,140,240"Legend="Legend1">
Color="220,252,180,65"Name="Series2"Legend="Legend1">
Color="220,224,64,10"Name="Series3"Legend="Legend1">
BackSecondaryColor="0,0,0,0"
BackColor="0,0,0,0"ShadowColor="0,0,0,0"
BackGradientStyle="TopBottom">
mce_style="Bold"">
mce_style="Bold"">
后台代码:
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{//这个事件在这没必要
//chart1.Click+=newImageMapEventHandler(chart1_Click);
foreach(Seriesseriesinchart1.Series)
{series.PostBackValue="series:"+series.Name+",#INDEX";
}//transferofclickcoordinates.getCoordinatesisajavascriptfunction.
this.chart1.Attributes["onclick"]=ClientScript.GetPostBackEventReference(this.chart1,
"@").Replace("''@''","''chart:''+_getCoord(event)");
//setpositiontorelativeinordertogetpropercoordinates.
this.chart1.Style[HtmlTextWriterStyle.Position]="relative";
this.ClientScript.RegisterClientScriptBlock(
typeof(Chart),
"Chart",
@"function_getCoord(event){if(typeof(event.x)==''undefined''){return
event.layerX+'',''+event.layerY;}returnevent.x+'',''+event.y;}",
true);
}
protectedvoidchart1_Click(objectsender,ImageMapEventArgse)
{
chart1.Titles[1].Text="Nothing";
string[]input=e.PostBackValue.Split('':'');
//若长度等于2
if(input.Length==2)
{string[]seriesData=input[1].Split('','');
//若是series,则输出相应的列数据
if(input[0]=="series")
{chart1.Titles[1].Text="最后单击元素;"+seriesData[0]+"DataPoint#"+seriesData[1];
}
//否则输出Chart中相应的元素
elseif(input[0]=="chart")
{//确定是Chart中的哪个元素
HitTestResult
hitTestResult=chart1.HitTest(Int32.Parse(seriesData[0]),Int32.Parse(seriesData[1]));
chart1.Titles[1].Text="最后单击元素;"+hitTestResult.ChartElementType.ToString();
}
}
}
protectedvoidPage_Load(objectsender,EventArgse)
{
//这个事件在这没必要
//chart1.Click+=newImageMapEventHandler(chart1_Click);
foreach(Seriesseriesinchart1.Series)
{
series.PostBackValue="series:"+series.Name+",#INDEX";
}
//transferofclickcoordinates.getCoordinatesisajavascriptfunction.
this.chart1.Attributes["onclick"]=ClientScript.GetPostBackEventReference(this.chart1,
"@").Replace("''@''","''chart:''+_getCoord(event)");
//setpositiontorelativeinordertogetpropercoordinates.
this.chart1.Style[HtmlTextWriterStyle.Position]="relative";
this.ClientScript.RegisterClientScriptBlock(
typeof(Chart),
"Chart",
@"function_getCoord(event){if(typeof(event.x)==''undefined''){return
event.layerX+'',''+event.layerY;}returnevent.x+'',''+event.y;}",
true);
}
protectedvoidchart1_Click(objectsender,ImageMapEventArgse)
{
chart1.Titles[1].Text="Nothing";
string[]input=e.PostBackValue.Split('':'');
//若长度等于2
if(input.Length==2)
{
string[]seriesData=input[1].Split('','');
//若是series,则输出相应的列数据
if(input[0]=="series")
{
chart1.Titles[1].Text="最后单击元素;"+seriesData[0]+"DataPoint#"+seriesData[1];
}
//否则输出Chart中相应的元素
elseif(input[0]=="chart")
{
//确定是Chart中的哪个元素
HitTestResult
hitTestResult=chart1.HitTest(Int32.Parse(seriesData[0]),Int32.Parse(seriesData[1]));
chart1.Titles[1].Text="最后单击元素;"+hitTestResult.ChartElementType.ToString();
}
}
}
效果图:
三.AJAX(二)——ChartasTrriger
概述:当单击图标上的列柱时,Gridview相应的发生变化。主要靠AJAX的UpdatePanel控件的Trriger属
性实现异步回调。
前台主要代码:
viewplaincopytoclipboardprint?
DataFile="~/App_Data/chartdata.mdb"
SelectCommand="SELECTREGIONS.RegionName,SUM(REPS_SALES.Sales)
ASSumOfSalesFROM(REPS_SALESINNERJOINREGIONSONREPS_SALES.RegionID=
REGIONS.RegionID)GROUPBYREGIONS.RegionName">
onclick="Chart1_Click">
YValueMembers="SumOfSales">
DataFile="~/App_Data/chartdata.mdb"
SelectCommand="SELECTREGIONS.RegionName,SUM(REPS_SALES.Sales)
ASSumOfSalesFROM(REPS_SALESINNERJOINREGIONSONREPS_SALES.RegionID=
REGIONS.RegionID)GROUPBYREGIONS.RegionName">
onclick="Chart1_Click">
YValueMembers="SumOfSales">
后台代码:
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{
this.Chart1.Series[0].PostBackValue="#AXISLABEL";
this.Chart1.Series[0].ToolTip="#AXISLABELRegion:#VAL{C}";
}
protectedvoidChart1_Click(objectsender,ImageMapEventArgse)
{DetailsSource.SelectParameters["RegionName"].DefaultValue=e.PostBackValue;
GridView1.Caption=string.Format("{0}Region",e.PostBackValue);
}
protectedvoidPage_Load(objectsender,EventArgse)
{
this.Chart1.Series[0].PostBackValue="#AXISLABEL";
this.Chart1.Series[0].ToolTip="#AXISLABELRegion:#VAL{C}";
}
protectedvoidChart1_Click(objectsender,ImageMapEventArgse)
{
DetailsSource.SelectParameters["RegionName"].DefaultValue=e.PostBackValue;
GridView1.Caption=string.Format("{0}Region",e.PostBackValue);
}
效果图:
四.AJAX(三)——AJAX更新
概述:当点击图列时,自动弹出一框框,显示当前图列柱的值,当修改txtBox中的值点确定后,图列表相
应的发生变化,框框消失。
具体前台代码:(panel必须放在UpdatePanel中,不然txtBox中值不会出现)
viewplaincopytoclipboardprint?
BackGradientStyle="TopBottom"BorderlineColor="181,64,1"
BorderlineDashStyle="Solid"onclick="Chart1_Click">
ChartType="Bar"CustomProperties="DrawingStyle=Cylinder"
XValueType="Double"YValueType="Double">
CustomProperties="DrawingStyle=Cylinder"Name="Series2"XValueType="Double"
YValueType="Double">
BorderDashStyle="Solid">
40px;left:100px;border:black1pxridge;padding:3px;background-color:White;">
Width="85px"/>
BackGradientStyle="TopBottom"BorderlineColor="181,64,1"
BorderlineDashStyle="Solid"onclick="Chart1_Click">
ChartType="Bar"CustomProperties="DrawingStyle=Cylinder"
XValueType="Double"YValueType="Double">
CustomProperties="DrawingStyle=Cylinder"Name="Series2"
XValueType="Double"
YValueType="Double">
BorderDashStyle="Solid">
40px;left:100px;border:black1pxridge;padding:3px;background-color:White;">
Width="85px"/>
后台代码:
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{
//对每个Series赋值
foreach(SeriesseriesinChart1.Series)
{
series.PostBackValue="#SERIESNAME;#INDEX";
}
}
protectedvoidChart1_Click(objectsender,ImageMapEventArgse)
{
string[]pointDate=e.PostBackValue.Split('';'');
//转化为相应的Series
Seriesseries=Chart1.Series[pointDate[0]];
//转化为相应的DataPoint
DataPointdataPoint=series.Points[Int32.Parse(pointDate[1])];
//给txtBox和Hidderfield赋值
hidderField.Value=e.PostBackValue;
txtValue.Text=dataPoint.YValues[0].ToString();
//显示Panel
InputPanel.Visible=true;
//PointValue.Focus();
}
protectedvoidButton1_Click(objectsender,EventArgse)
{
string[]pointData=hidderField.Value.Split('';'');
//转化为相应的Series
Seriesseries=Chart1.Series[pointData[0]];
//转化为相应的DataPoint
DataPointdataPoint=series.Points[Int32.Parse(pointData[1])];
//给txtBox和Hidderfield赋值
dataPoint.YValues[0]=Double.Parse(txtValue.Text);
//隐藏Panel
InputPanel.Visible=false;
}
protectedvoidPage_Load(objectsender,EventArgse)
{
//对每个Series赋值
foreach(SeriesseriesinChart1.Series)
{
series.PostBackValue="#SERIESNAME;#INDEX";
}
}
protectedvoidChart1_Click(objectsender,ImageMapEventArgse)
{
string[]pointDate=e.PostBackValue.Split('';'');
//转化为相应的Series
Seriesseries=Chart1.Series[pointDate[0]];
//转化为相应的DataPoint
DataPointdataPoint=series.Points[Int32.Parse(pointDate[1])];
//给txtBox和Hidderfield赋值
hidderField.Value=e.PostBackValue;
txtValue.Text=dataPoint.YValues[0].ToString();
//显示Panel
InputPanel.Visible=true;
//PointValue.Focus();
}
protectedvoidButton1_Click(objectsender,EventArgse)
{
string[]pointData=hidderField.Value.Split('';'');
//转化为相应的Series
Seriesseries=Chart1.Series[pointData[0]];
//转化为相应的DataPoint
DataPointdataPoint=series.Points[Int32.Parse(pointData[1])];
//给txtBox和Hidderfield赋值
dataPoint.YValues[0]=Double.Parse(txtValue.Text);
//隐藏Panel
InputPanel.Visible=false;
}
效果图:
五.AJAX(四)——RealTimeChart
概述:每过一秒钟,Chart就增加五个点,动态的生成一条曲线。
代码:(前台代码一定要设EnableViewState="true",否则一直就是五个点)
viewplaincopytoclipboardprint?
protectedvoidTimer1_Tick(objectsender,EventArgse)
{
Randomrandom=newRandom();
foreach(SeriesseriesinChart1.Series)
{
doublexLastValue=series.Points[series.Points.Count-1].XValue+1;
doubleyLastValue=series.Points[series.Points.Count-1].YValues[0];
for(inti=0;i<5;i++)
{
yLastValue+=random.Next(-3,4);
if(yLastValue>=100.0)
{
yLastValue-=25.0;
}
if(yLastValue<=10.0)
{
yLastValue+=25.0;
}
series.Points.AddXY(xLastValue++,yLastValue);
}
}
while(Chart1.Series[0].Points.Count>100)
{
foreach(SeriesseriesinChart1.Series)
{
series.Points.RemoveAt(0);
}
}
doubleaxisMinimum=this.Chart1.Series[0].Points[0].XValue;
this.Chart1.ChartAreas[0].AxisX.Minimum=axisMinimum;
this.Chart1.ChartAreas[0].AxisX.Maximum=axisMinimum+100;
}
protectedvoidTimer1_Tick(objectsender,EventArgse)
{
Randomrandom=newRandom();
foreach(SeriesseriesinChart1.Series)
{
doublexLastValue=series.Points[series.Points.Count-1].XValue+1;
doubleyLastValue=series.Points[series.Points.Count-1].YValues[0];
for(inti=0;i<5;i++)
{
yLastValue+=random.Next(-3,4);
if(yLastValue>=100.0)
{
yLastValue-=25.0;
}
if(yLastValue<=10.0)
{
yLastValue+=25.0;
}
series.Points.AddXY(xLastValue++,yLastValue);
}
}
while(Chart1.Series[0].Points.Count>100)
{
foreach(SeriesseriesinChart1.Series)
{
series.Points.RemoveAt(0);
}
}
doubleaxisMinimum=this.Chart1.Series[0].Points[0].XValue;
this.Chart1.ChartAreas[0].AxisX.Minimum=axisMinimum;
this.Chart1.ChartAreas[0].AxisX.Maximum=axisMinimum+100;
}
效果图:
解决MSChart底部横坐标显示不全的问题收藏
还是这个MSChart,先前做一个test的时候都正常,正式用的时候出现这样一个问题,绑定数据之后,如
果x轴坐标过多,会显示不全,我这里的情况是底部坐标一般最多显示八九个,再多了就隔一个显示一个,
或者显示的更少。
开始的时候一直以为是绑定数据的问题,一步步的断点看,都没有问题。后来网上搜到这个页面
http://www.cnblogs.com/tristinjet/archive/2009/11/09/1598981.html
底下的留言里也有人问到了这个问题,联系了blog作者,发现只需要设置以下属性就可以了:
viewplaincopytoclipboardprint?
//需要设置横坐标的间隔和偏移都设为1不然坐标多了会显示不全
Chart2.ChartAreas[0].AxisX.Interval=1;
Chart2.ChartAreas[0].AxisX.IntervalOffset=1;
Chart2.ChartAreas[0].AxisX.LabelStyle.IsStaggered=true;
//需要设置横坐标的间隔和偏移都设为1不然坐标多了会显示不全
Chart2.ChartAreas[0].AxisX.Interval=1;
Chart2.ChartAreas[0].AxisX.IntervalOffset=1;
Chart2.ChartAreas[0].AxisX.LabelStyle.IsStaggered=true;
一个是横坐标间隔,一个是偏移,IsStaggered是设置是否交错显示,比如横坐标多的时候可以分成两行来
显示。
花了我将近一天时间调试,原来这么简单。
mschart上下限阈值画横线收藏
开发中,发现图表中,显示上下限很重要,于是想办法。mschart,开始想增加一个Series,然后Y值设
置为固定值。发现效果:有数据区域有上下限线,无法从纵轴开始。
后来google,找到这种方法:
viewplaincopytoclipboardprint?
StripLinesl1=newStripLine();
sl1.BackColor=System.Drawing.Color.Red;
sl1.IntervalOffset=1;
sl1.StripWidth=0.001;
sl1.Text="上限";
sl1.TextAlignment=StringAlignment.Near;
ChartLine1.ChartAreas[0].AxisY.StripLines.Add(sl1);
StripLinesl1=newStripLine();
sl1.BackColor=System.Drawing.Color.Red;
sl1.IntervalOffset=1;
sl1.StripWidth=0.001;
sl1.Text="上限";
sl1.TextAlignment=StringAlignment.Near;
ChartLine1.ChartAreas[0].AxisY.StripLines.Add(sl1);
其中StripWidth为线的宽度,我的Y轴值比较小,所以设为0.001
设大值时:
IntervalOffset为距离Y轴原点的距离。
X轴方向类似!
MSChartforvs2008收藏
最近在做统计图标,使用mschartforvs2008,用asp.net开发,感觉不错
MSChart下载
http://download.microsoft.com/download/c/c/4/cc4dcac6-ea60-4868-a8e0-62a8510aa747/MSChart.exe
如果要在VS2008IDE环境中使用,需要安装插件支持
MSChart_VisualStudioAddOn.exe
http://download.microsoft.com/download/e/6/f/e6fedd87-9119-4037-8da8-e5de429d940a/MSChart_VisualStudio
AddOn.exe
装完这两个东西以后,会在工具箱Data栏出现一个Chart图标,可以使用了。
Chart组件可以与Excel图标相媲美
下图是Chart组件一些关键元素组成:
更多内容可以查看MS的DEMO
DataVisChartControl.exe
http://download.microsoft.com/download/5/4/a/54ad5fde-dd31-4a16-82db-72885da83a1f/DataVisChartControl.ex
e
这是这两天做的一些截图
饼图,比较好实现
条状图,花了些时间,主要是X轴有多个统计项,每个统计项又有好几个子项,还要把X轴的统计项用
竖线隔开
在MSChart使用中可能碰到一些错误
1、“XXXTempImage目录无效”,修改web.config中的
“
2、“msc_cntr_0.txt”不可写,原因是临时目录中存在了这个文件,删除掉就OK了
3、“XXXChartImg.axdXXX”错误,也修改web.config,添加一个"POST"即可
" MSCHART全部属性方法介绍收藏
voidCMoreDMcode::OnCacluteDelta(intnRow)
{
m_Chart.SetRedraw(false);
m_Chart.SetRow(1);
for(inti=1;i<=64;i++)
{
m_Chart.GetDataGrid().SetData(i,1,nDelta[i-1],0);
}
CRgnrgn;
m_Chart.GetUpdateRgn(&rgn);
m_Chart.SetRedraw(true);
m_Chart.InvalidateRgn(&rgn,false);
}
voidCMoreDMcode::InitChart()
{
VARIANTvar;
m_Chart.SetColumnCount(1);//设置曲线条数
m_Chart.SetRowCount(64);//一条曲线有64个点
m_Chart.SetTitleText("二位形态码△t变化趋势";
m_Chart.SetChartType(1|2);//
//m_Chart.SetShowLegend(TRUE);
//设置X轴
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetAuto(FALSE);//不自动标注X轴刻度
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerLabel(8);//每四刻度一个标注
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerTick(8);//每刻度一个刻度线
//m_Chart.GetPlot().GetAxis(0,var).GetValueScale().SetMinorDivision(1);//每刻度一个刻度线
m_Chart.GetPlot().GetAxis(0,var).GetValueScale().SetMaximum(64);//X轴最大刻度
m_Chart.GetPlot().GetAxis(0,var).GetValueScale().SetMinimum(0);//X轴最小刻度
m_Chart.GetPlot().GetAxis(0,var).GetAxisTitle().SetText("二位形态码";
//设置Y轴
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetAuto(FALSE);//不自动标注Y轴刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMaximum(100);//Y轴最大刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinimum(0);//Y轴最小刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMajorDivision(10);//Y轴刻度10等分
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinorDivision(1);//每刻度一个刻度线
//m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().SetText("△t变化趋势";//Y轴名称
//m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().GetTextLayout().SetOrientation(2);//Y轴名称排列方式
m_Chart.GetPlot().SetUniformAxis(FALSE);
}
voidCMoreDMcode::OnMSChartDraw()
{
CStringstr;
intnRow=m_Grid.GetFocusCell().row;
if(nRow<0)
{
MessageBox("请在左表选择要排序的行!","操作错误",MB_OK|MB_ICONWARNING);
return;
}
for(inti=0;i<64;i++)
{
str=m_Grid.GetItemText(nRow,i+3);
nDelta=atoi(str.GetBuffer(0));
}
InitChart();//初始化坐标轴
//根据不同的数据设定不同的Y轴最大刻度
intm=GetMaxData(nDelta);
m=(m/50)50+50;//取靠近M的比M大的50的倍数
str=m_Grid.GetItemText(nRow,0);
VARIANTvar;
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMaximum(m);
OnCacluteDelta(nRow);//绘图
str="当前绘制的是第"+str+"期的△t变化趋势";
m_Tip.SetWindowText(str);
}
函数名字取得不好,大家见笑了,开始取名字错误,后来不想该了,呵呵^_^
====================================================
图表由于其直观明了的特性,在实际应用中十分很广泛。我们常常希望数据能通过图表来显示其特性。例
如在Delphi和C++Builder编程中,我们可以很方便地实现数据图表。MsChart(6.0或5.0版)是Windows
系统中Visualstudio自带的一个ACTIVEX控件,它功能强大,应用广泛,具有以下特点:
·支持随机数据和随机数组,动态显示。
·支持所有主要的图表类型。
·支持三维显示。
MsChart具有45个属性,9个方法,49事件,可灵活编程,可实现各类表的显示。
1MsChart应用编程
首先插入MsChart控件,
在工程中加入mschart
菜单->Project->AddToProject->ComponentsandControls->RegisteredActiveXControls->MicrosoftChart
Control,version6.0(OLEDB)
1.1坐标系属性的设置
a)纵轴初始化属性
Mschart默认支持自动标准,将自动调整,可以缺省设置。
#include"mschart.h"//添加相关的头文件
#include"VcAxis.h"
#include"VcAxisTitle.h"
#include"VcTextLayout.h"
#include"VcDataGrid.h"
#include"VcPlot.h"
#include"VcValueScale.h"
#include"VcSeriesCollection.h"
#include"VcSeries.h"
#include"VcPen.h"
#include"VcCategoryScale.h"
#include"VcColor.h"
#include"VcDataGrid.h"
#include"VcBackdrop.h"
#include"VcFill.h"
#include"VcBrush.h"
#include"VcDataPoints.h"
#include"VcDataPoint.h"
#include"VcDataPointLabel.h"
#include"VcAxisTitle.h"
//最好添加全部头函数,
CMSChartm_Chart;//m_Chart为图表变量
VARIANTvar;
m_Chart.GetPlot().GetAxis(1,var)//获取纵轴
//设置是否支持自动标准;控件默认支持自动标准。
m_Chart.GetPlot().GetAxis().GetValuesScale().SetAuto(FALSE);
//设置最大刻度为M;
m_Chart.GetPlot().GetAxis().GetValuesScale().SetMaximum(M);
//设置最小刻度为m;
m_Chart.GetPlot().GetAxis().GetValuesScale().SetMinimum(m);
//设置轴的等分数D;
m_Chart.GetPlot().GetAxis().GetValuesScale().SetMajorDivision(D);
//设置每等分的刻度线数n;
m_Chart.GetPlot().GetAxis().GetValuesScale().SetMinorDivision(n);
b)横轴初始化属性
VARIANTvar;
m_Chart.GetPlot().GetAxis(0,var)//获取横轴
其他属性设置跟纵轴相同。
1.2数据显示
a)设置标题栏和标签
m_Chart.SetTitleText(“标题”);//设置标题栏
m_Chart.SetRowLabel((“第I行”);//设置第i行标签
m_Chart.SetColumnLabel((“第j列”);//设置第j列标签
b)行列的显示布局
MSChart的行列显示布局有其自身的特点:下面显示是一个行列4×3(矩形图),即(四行,三列)的布
局示意图。
m_Chart.SetRowCount(4);//没条曲线三个四个点(曲线图)
m_Chart.SetColumnCount(3);//设置曲线条数为三条(曲线图)
c)行列操作
//操作行列第i行、第j列
m_Chart.SetRow(i);//第i行
m_Chart.SetColumn(j);//第j行
m_Chart.SetRowLabel((“第i行”);//设置第i行标签
CStringstr=”90.5”;
m_Chart.SetData((LPCTSTR(str));//设置行列,j>的显示数据
m_Chart.Refresh();//刷新视图
d)显示方式
获取当前的显示方式:
longnType=m_Chart.GetChartType();
设置显示方式:
m_Chart.SetChartType(0);//3D(三维)显示
m_Chart.SetChartType(1);//2D(二维)显示
m_Chart.Refresh();
其它常用组合方式为:
m_Chart.SetChartType(1|0)//2D柱(条)形,
m_Chart.SetChartType(0|0)//3D柱(条)形
m_Chart.SetChartType(1|2)//2D线条型
m_Chart.SetChartType(0|2)//3D线条型
m_Chart.SetChartType(1|4)//2D区域型
m_Chart.SetChartType(0|4)//3D区域型
m_Chart.SetChartType(1|6)//2D阶梯型
m_Chart.SetChartType(0|6)//3D阶梯型
m_Chart.SetChartType(1|8)//2D复(混)合型
m_Chart.SetChartType(0|8)//3D复(混)合型
另外,在2D方式中,还有两类:饼型和XY型
m_Chart.SetChartType(14)//2D饼型
m_Chart.SetChartType(16)//2DXY型
e)其他
其他属性,比如设置字体,颜色,对齐方式等。
//====================================================
//VC知识库上的参考文章
http://www.vckbase.com/document/viewdoc/?id=959
3.3设置m_ChartvoidCDemoView::InitChart()
{
//设置标题
m_Chart.SetTitleText("mschart示例bythinkry@263.net";
//下面两句改变背景色
m_Chart.GetBackdrop().GetFill().SetStyle(1);
m_Chart.GetBackdrop().GetFill().GetBrush().GetFillColor().Set(255,255,255);
//显示图例
m_Chart.SetShowLegend(TRUE);
m_Chart.SetColumn(1);
m_Chart.SetColumnLabel((LPCTSTR)"1号机";
m_Chart.SetColumn(2);
m_Chart.SetColumnLabel((LPCTSTR)"2号机";
m_Chart.SetColumn(3);
m_Chart.SetColumnLabel((LPCTSTR)"3号机";
//栈模式
//m_Chart.SetStacking(TRUE);
//Y轴设置
VARIANTvar;
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetAuto(FALSE);//不自动标注Y轴刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMaximum(100);//Y轴最大刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinimum(0);//Y轴最小刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMajorDivision(5);//Y轴刻度5等分
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinorDivision(1);//每刻度一个刻度线
m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().SetText("小时";//Y轴名称
//3条曲线
m_Chart.SetColumnCount(3);
//线色
m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetPen().GetVtColor().Set(0,0,255);
m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetPen().GetVtColor().Set(255,0,0);
m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetPen().GetVtColor().Set(0,255,0);
//线宽(对点线图有效)
m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetPen().SetWidth(50);
m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetPen().SetWidth(100);
m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetPen().SetWidth(2);
//数据点类型显示数据值的模式(对柱柱状图和点线图有效)
//0:不显示1:显示在柱状图外
//2:显示在柱状图内上方3:显示在柱状图内中间4:显示在柱状图内下方
m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocati
onType(1);
m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocati
onType(1);
m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocati
onType(1);
}
3.4设置数据voidCDemoView:rawChart()
{
intnRowCount=6;
m_Chart.SetRowCount(nRowCount);
VARIANTvar;
//不自动标注X轴刻度
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetAuto(FALSE);
//每刻度一个标注
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerLabel(1);
//每刻度一个刻度线
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerTick(1);
//X轴名称
m_Chart.GetPlot().GetAxis(0,var).GetAxisTitle().SetText("日期";
charbuf[32];
srand((unsigned)time(NULL));
for(introw=1;row<=nRowCount;++row)
{
m_Chart.SetRow(row);
sprintf(buf,"%d号",row);
m_Chart.SetRowLabel((LPCTSTR)buf);
m_Chart.GetDataGrid().SetData(row,1,rand()100/RAND_MAX,0);//设置第一条曲线的第row个点
m_Chart.GetDataGrid().SetData(row,2,rand()100/RAND_MAX,0);//设置第二条曲线的第row个点
m_Chart.GetDataGrid().SetData(row,3,rand()100/RAND_MAX,0);//设置第三条曲线的第row个点
}
m_Chart.Refresh();
}
/
SetData(row,column,dataPoint,nullFlag)
SetData方法语法包括以下组成部分:
rowInteger类型。标识包含数据点值的行。
columnInteger类型。标识包含数据点值的列。
dataPointDouble类型。数据点值。
nullFlagInteger类型。指示数据点值是否为空
/
3.5改变显示类型//折线图
voidCDemoView::OnChartLine()
{
m_Chart.SetChartType(3);
DrawChart();
}
//柱状图
voidCDemoView::OnChartCombi()
{
m_Chart.SetChartType(1);
DrawChart();
}
//饼状图
voidCDemoView::OnChartPie()
{
m_Chart.SetChartType(14);
DrawChart();
MSCHART全部属性方法介绍收藏
voidCMoreDMcode::OnCacluteDelta(intnRow)
{
m_Chart.SetRedraw(false);
m_Chart.SetRow(1);
for(inti=1;i<=64;i++)
{
m_Chart.GetDataGrid().SetData(i,1,nDelta[i-1],0);
}
CRgnrgn;
m_Chart.GetUpdateRgn(&rgn);
m_Chart.SetRedraw(true);
m_Chart.InvalidateRgn(&rgn,false);
}
voidCMoreDMcode::InitChart()
{
VARIANTvar;
m_Chart.SetColumnCount(1);//设置曲线条数
m_Chart.SetRowCount(64);//一条曲线有64个点
m_Chart.SetTitleText("二位形态码△t变化趋势";
m_Chart.SetChartType(1|2);//
//m_Chart.SetShowLegend(TRUE);
//设置X轴
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetAuto(FALSE);//不自动标注X轴刻度
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerLabel(8);//每四刻度一个标注
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerTick(8);//每刻度一个刻度线
//m_Chart.GetPlot().GetAxis(0,var).GetValueScale().SetMinorDivision(1);//每刻度一个刻度线
m_Chart.GetPlot().GetAxis(0,var).GetValueScale().SetMaximum(64);//X轴最大刻度
m_Chart.GetPlot().GetAxis(0,var).GetValueScale().SetMinimum(0);//X轴最小刻度
m_Chart.GetPlot().GetAxis(0,var).GetAxisTitle().SetText("二位形态码";
//设置Y轴
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetAuto(FALSE);//不自动标注Y轴刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMaximum(100);//Y轴最大刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinimum(0);//Y轴最小刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMajorDivision(10);//Y轴刻度10等分
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinorDivision(1);//每刻度一个刻度线
//m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().SetText("△t变化趋势";//Y轴名称
//m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().GetTextLayout().SetOrientation(2);//Y轴名称排列方式
m_Chart.GetPlot().SetUniformAxis(FALSE);
}
voidCMoreDMcode::OnMSChartDraw()
{
CStringstr;
intnRow=m_Grid.GetFocusCell().row;
if(nRow<0)
{
MessageBox("请在左表选择要排序的行!","操作错误",MB_OK|MB_ICONWARNING);
return;
}
for(inti=0;i<64;i++)
{
str=m_Grid.GetItemText(nRow,i+3);
nDelta=atoi(str.GetBuffer(0));
}
InitChart();//初始化坐标轴
//根据不同的数据设定不同的Y轴最大刻度
intm=GetMaxData(nDelta);
m=(m/50)50+50;//取靠近M的比M大的50的倍数
str=m_Grid.GetItemText(nRow,0);
VARIANTvar;
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMaximum(m);
OnCacluteDelta(nRow);//绘图
str="当前绘制的是第"+str+"期的△t变化趋势";
m_Tip.SetWindowText(str);
}
函数名字取得不好,大家见笑了,开始取名字错误,后来不想该了,呵呵^_^
====================================================
图表由于其直观明了的特性,在实际应用中十分很广泛。我们常常希望数据能通过图表来显示其特性。例
如在Delphi和C++Builder编程中,我们可以很方便地实现数据图表。MsChart(6.0或5.0版)是Windows
系统中Visualstudio自带的一个ACTIVEX控件,它功能强大,应用广泛,具有以下特点:
·支持随机数据和随机数组,动态显示。
·支持所有主要的图表类型。
·支持三维显示。
MsChart具有45个属性,9个方法,49事件,可灵活编程,可实现各类表的显示。
1MsChart应用编程
首先插入MsChart控件,
在工程中加入mschart
菜单->Project->AddToProject->ComponentsandControls->RegisteredActiveXControls->MicrosoftChart
Control,version6.0(OLEDB)
1.1坐标系属性的设置
a)纵轴初始化属性
Mschart默认支持自动标准,将自动调整,可以缺省设置。
#include"mschart.h"//添加相关的头文件
#include"VcAxis.h"
#include"VcAxisTitle.h"
#include"VcTextLayout.h"
#include"VcDataGrid.h"
#include"VcPlot.h"
#include"VcValueScale.h"
#include"VcSeriesCollection.h"
#include"VcSeries.h"
#include"VcPen.h"
#include"VcCategoryScale.h"
#include"VcColor.h"
#include"VcDataGrid.h"
#include"VcBackdrop.h"
#include"VcFill.h"
#include"VcBrush.h"
#include"VcDataPoints.h"
#include"VcDataPoint.h"
#include"VcDataPointLabel.h"
#include"VcAxisTitle.h"
//最好添加全部头函数,
CMSChartm_Chart;//m_Chart为图表变量
VARIANTvar;
m_Chart.GetPlot().GetAxis(1,var)//获取纵轴
//设置是否支持自动标准;控件默认支持自动标准。
m_Chart.GetPlot().GetAxis().GetValuesScale().SetAuto(FALSE);
//设置最大刻度为M;
m_Chart.GetPlot().GetAxis().GetValuesScale().SetMaximum(M);
//设置最小刻度为m;
m_Chart.GetPlot().GetAxis().GetValuesScale().SetMinimum(m);
//设置轴的等分数D;
m_Chart.GetPlot().GetAxis().GetValuesScale().SetMajorDivision(D);
//设置每等分的刻度线数n;
m_Chart.GetPlot().GetAxis().GetValuesScale().SetMinorDivision(n);
b)横轴初始化属性
VARIANTvar;
m_Chart.GetPlot().GetAxis(0,var)//获取横轴
其他属性设置跟纵轴相同。
1.2数据显示
a)设置标题栏和标签
m_Chart.SetTitleText(“标题”);//设置标题栏
m_Chart.SetRowLabel((“第I行”);//设置第i行标签
m_Chart.SetColumnLabel((“第j列”);//设置第j列标签
b)行列的显示布局
MSChart的行列显示布局有其自身的特点:下面显示是一个行列4×3(矩形图),即(四行,三列)的布
局示意图。
m_Chart.SetRowCount(4);//没条曲线三个四个点(曲线图)
m_Chart.SetColumnCount(3);//设置曲线条数为三条(曲线图)
c)行列操作
//操作行列第i行、第j列
m_Chart.SetRow(i);//第i行
m_Chart.SetColumn(j);//第j行
m_Chart.SetRowLabel((“第i行”);//设置第i行标签
CStringstr=”90.5”;
m_Chart.SetData((LPCTSTR(str));//设置行列,j>的显示数据
m_Chart.Refresh();//刷新视图
d)显示方式
获取当前的显示方式:
longnType=m_Chart.GetChartType();
设置显示方式:
m_Chart.SetChartType(0);//3D(三维)显示
m_Chart.SetChartType(1);//2D(二维)显示
m_Chart.Refresh();
其它常用组合方式为:
m_Chart.SetChartType(1|0)//2D柱(条)形,
m_Chart.SetChartType(0|0)//3D柱(条)形
m_Chart.SetChartType(1|2)//2D线条型
m_Chart.SetChartType(0|2)//3D线条型
m_Chart.SetChartType(1|4)//2D区域型
m_Chart.SetChartType(0|4)//3D区域型
m_Chart.SetChartType(1|6)//2D阶梯型
m_Chart.SetChartType(0|6)//3D阶梯型
m_Chart.SetChartType(1|8)//2D复(混)合型
m_Chart.SetChartType(0|8)//3D复(混)合型
另外,在2D方式中,还有两类:饼型和XY型
m_Chart.SetChartType(14)//2D饼型
m_Chart.SetChartType(16)//2DXY型
e)其他
其他属性,比如设置字体,颜色,对齐方式等。
//====================================================
//VC知识库上的参考文章
http://www.vckbase.com/document/viewdoc/?id=959
3.3设置m_ChartvoidCDemoView::InitChart()
{
//设置标题
m_Chart.SetTitleText("mschart示例bythinkry@263.net";
//下面两句改变背景色
m_Chart.GetBackdrop().GetFill().SetStyle(1);
m_Chart.GetBackdrop().GetFill().GetBrush().GetFillColor().Set(255,255,255);
//显示图例
m_Chart.SetShowLegend(TRUE);
m_Chart.SetColumn(1);
m_Chart.SetColumnLabel((LPCTSTR)"1号机";
m_Chart.SetColumn(2);
m_Chart.SetColumnLabel((LPCTSTR)"2号机";
m_Chart.SetColumn(3);
m_Chart.SetColumnLabel((LPCTSTR)"3号机";
//栈模式
//m_Chart.SetStacking(TRUE);
//Y轴设置
VARIANTvar;
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetAuto(FALSE);//不自动标注Y轴刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMaximum(100);//Y轴最大刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinimum(0);//Y轴最小刻度
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMajorDivision(5);//Y轴刻度5等分
m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinorDivision(1);//每刻度一个刻度线
m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().SetText("小时";//Y轴名称
//3条曲线
m_Chart.SetColumnCount(3);
//线色
m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetPen().GetVtColor().Set(0,0,255);
m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetPen().GetVtColor().Set(255,0,0);
m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetPen().GetVtColor().Set(0,255,0);
//线宽(对点线图有效)
m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetPen().SetWidth(50);
m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetPen().SetWidth(100);
m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetPen().SetWidth(2);
//数据点类型显示数据值的模式(对柱柱状图和点线图有效)
//0:不显示1:显示在柱状图外
//2:显示在柱状图内上方3:显示在柱状图内中间4:显示在柱状图内下方
m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocati
onType(1);
m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocati
onType(1);
m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocati
onType(1);
}
3.4设置数据voidCDemoView:rawChart()
{
intnRowCount=6;
m_Chart.SetRowCount(nRowCount);
VARIANTvar;
//不自动标注X轴刻度
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetAuto(FALSE);
//每刻度一个标注
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerLabel(1);
//每刻度一个刻度线
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerTick(1);
//X轴名称
m_Chart.GetPlot().GetAxis(0,var).GetAxisTitle().SetText("日期";
charbuf[32];
srand((unsigned)time(NULL));
for(introw=1;row<=nRowCount;++row)
{
m_Chart.SetRow(row);
sprintf(buf,"%d号",row);
m_Chart.SetRowLabel((LPCTSTR)buf);
m_Chart.GetDataGrid().SetData(row,1,rand()100/RAND_MAX,0);//设置第一条曲线的第row个点
m_Chart.GetDataGrid().SetData(row,2,rand()100/RAND_MAX,0);//设置第二条曲线的第row个点
m_Chart.GetDataGrid().SetData(row,3,rand()100/RAND_MAX,0);//设置第三条曲线的第row个点
}
m_Chart.Refresh();
}
/
SetData(row,column,dataPoint,nullFlag)
SetData方法语法包括以下组成部分:
rowInteger类型。标识包含数据点值的行。
columnInteger类型。标识包含数据点值的列。
dataPointDouble类型。数据点值。
nullFlagInteger类型。指示数据点值是否为空
/
3.5改变显示类型//折线图
voidCDemoView::OnChartLine()
{
m_Chart.SetChartType(3);
DrawChart();
}
//柱状图
voidCDemoView::OnChartCombi()
{
m_Chart.SetChartType(1);
DrawChart();
}
//饼状图
voidCDemoView::OnChartPie()
{
m_Chart.SetChartType(14);
DrawChart();
在access中创建库表(.net实现)
1ImportsSystem.Data.OleDb
2
3PublicClassForm1
4
5PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Lo
ad
6DimcnnAsNewOleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\DocumentsandSet
tings\Administrator\MyDocuments\mydata.mdb")
7DimcmdAsNewOleDbCommand("CREATETABLEperson(namevarchar(20),sexvarchar(6),idintprimar
ykey)",cnn)
8DimadapterAsOleDbDataAdapter
9DimdstAsNewDataSet
10Try
11cnn.Open()
12cmd.ExecuteNonQuery()
13cmd.CommandText="insertintopersonvalues(''zzq'',''male'',1)"
14cmd.ExecuteNonQuery()
15cmd.CommandText="selectfromperson"
16adapter=NewOleDbDataAdapter(cmd)
17adapter.Fill(dst)
18DataGridView1.DataSource=dst.Tables(0).DefaultView
19cnn.Close()
20CatchexAsException
21MessageBox.Show(ex.Message)
22EndTry
23EndSub
24
25
26EndClass
27
目前access中的控制命令好像只有createtable,altertable,createindex,还有drop。不过网上有说可以用
CREATEPROCEDURE创建存储过程,我一直没有找到。我用的是是access2003+MicrosoftJet4.0Service
Pack8
vs2008开发3D报表-MSChart学习帮助-开发日志(一)-带附件下载收藏
我X这是我第二次写这篇文章了,刚才写了半天IE浏览器给关闭了,我X不过我又狠下心来,为大家
再服务一会:)
首先看到这个报表的界面会不会感觉很爽,没错,这就是用MSchart的图形报表组件,完全免费的开发
的
如果你不看这篇文章,可能会碰很多钉子,写这个就是为了让大家更快,更顺利的去使用这个.net
FrameWork3.5新组件的功能
先发表了,继续修改,免得又出现关闭浏览器问题...
继续首先你要安装Vs2008这我就不说了,.netFrameWork3.5这也是要有的,
然后下载MSChart的和4个补丁文件,在这里我都给大家提供好了,在我的资源里就有下载,5分哦,不
过下载完评论下就不扣分了。
然后依次安装
dotnetfx35setup.exe
MSChart_VisualStudioAddOn.exe
MSChartLP_chs.exe
MSChart.exe
这几个文件,然后OK
启动VS2008准备写程序
创建一个Web项目
然后在左边的工具卡里添加.netFrameWork组件Chart这里添加WebUI的Chart组件如果时WinForm
的话就添加WinForm的
然后在数据的选项卡里,把Chart的控件拖到界面上
HTML代码如下
ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)"Palette="BrightPastel"imagetype="Png"
BorderDashStyle="Solid"BackSecondaryColor="White"BackGradientStyle="TopBottom"BorderWidth="2"
backcolor="#D3DFF0"BorderColor="26,59,105">
BackColor="Transparent"Font="TrebuchetMS,8.25pt,style=Bold">
BorderDashStyle="Solid"BackSecondaryColor="White"BackColor="64,165,191,228"
ShadowColor="Transparent"BackGradientStyle="TopBottom">
IsRightAngleAxes="False"wallwidth="0"IsClustered="False">
后台代码如下
usingSystem;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
usingSystem.Web.UI.DataVisualization.Charting;
namespaceWebApplication2
{
publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
//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);
}
}
}
ok,你可以按F5运行了
是不是报错了?
哈哈,当然要报错还要配置Web.Config开个玩笑:)
Web.Config还要配置如下
type="System.Web.Script.Services.ScriptHandlerFactory,System.Web.Extensions,Version=3.5.0.0,
Culture=neutral,PublicKeyToken=31BF3856AD364E35"/>
type="System.Web.Script.Services.ScriptHandlerFactory,System.Web.Extensions,Version=3.5.0.0,
Culture=neutral,PublicKeyToken=31BF3856AD364E35"/>
type="System.Web.Handlers.ScriptResourceHandler,System.Web.Extensions,Version=3.5.0.0,Culture=neutral,
PublicKeyToken=31BF3856AD364E35"validate="false"/>
type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler,System.Web.DataVisualization,
Version=3.5.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"validate="false"/>
path="ChartImg.axd"type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler,
System.Web.DataVisualization,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/>
ok,再次运行,你会看到你喜欢看到的东西了,哈哈~~~感谢大家关注~~~GavinLuo的博客
MSChart学习帮助-开发日志(二).net3.5图表控件收藏
type="text/javascript">
仔细看了Scott写的如何开发MSchart,但始终还是有一个地方不明白,原话上说:可用于免费使用
ASP.NET3.5。但在官方网站上的MSChart板块又看到了license,如果真有人想用的话,可以打微软的销售
电话8008203800,或者等我知道了,我会为大家解答,因为这个好东西,我肯定会找机会用在我的项目里
今天又把MSChart拿出来研究,由于没有帮助文档,网上也没有案例和资料,唯一的一个帮助就是
Microsoft给的200个例子。于是就看源码,看属性,尝试着每个属性的作用-_-这是很辛苦的
ok终于基本搞懂了一些首先介绍一下这两个图的做法
我把源码贴出来这是前台的
viewplaincopytoclipboardprint?
ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)"Palette="BrightPastel"
imagetype="Png"BorderDashStyle="Solid"BackSecondaryColor="White"
BackGradientStyle="TopBottom"BorderWidth="2"backcolor="#D3DFF0">
Font="TrebuchetMS,8.25pt,style="Bold"mce_style="Bold"">
CustomProperties="MinPixelPointWidth=1,PixelPointWidth=30,
DrawingStyle=Cylinder,MaxPixelPointWidth=30,DrawSideBySide=True,PointWidth=0"
Color="224,224,224"Font="TimesNewRoman,9pt"
MarkerColor="Navy"MarkerSize="12"
BorderWidth="3"ShadowOffset="2"IsValueShownAsLabel="True"
LabelForeColor="64,0,0">
LabelBackColor="Olive"LabelForeColor="RosyBrown"
MarkerSize="12"/>
CalloutLineAnchorCapStyle="Diamond"
CalloutStyle="None"/>
BorderDashStyle="Solid"BackSecondaryColor="White"BackColor="64,165,191,228"
ShadowColor="Transparent"BackGradientStyle="TopBottom">
perspective="2"Inclination="20"LightStyle="Realistic"wallwidth="0"IsClustered="False">
mce_style="Bold""/>
mce_style="Bold""/>
ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)"Palette="BrightPastel"
imagetype="Png"BorderDashStyle="Solid"BackSecondaryColor="White"
BackGradientStyle="TopBottom"BorderWidth="2"backcolor="#D3DFF0">
Name="Default"BackColor="Transparent"Font="TrebuchetMS,8.25pt,style="Bold"
mce_style="Bold"">
BorderWidth="3"
XValueType="Double"ChartType="Line"MarkerStyle="Circle"
ShadowColor="Black"
BorderColor="180,26,59,105"Color="Red"ShadowOffset="2"
YValueType="Double">
ChartType=Line
BorderWidth="3"ShadowOffset="2"MarkerSize="8"
MarkerStyle="Circle"
ShadowColor="Black"BorderColor="180,26,59,105">
BorderDashStyle="Solid"BackSecondaryColor="White"
BackColor="64,165,191,228"ShadowColor="Transparent"
BackGradientStyle="TopBottom"IsSameFontSizeForAllAxes="True">
perspective="2"Inclination="20"LightStyle="Realistic"wallwidth="0"IsClustered="False">
mce_style="Bold""/>
mce_style="Bold""/>
ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)"Palette="BrightPastel"
imagetype="Png"BorderDashStyle="Solid"BackSecondaryColor="White"
BackGradientStyle="TopBottom"BorderWidth="2"backcolor="#D3DFF0">
Font="TrebuchetMS,8.25pt,style="Bold"mce_style="Bold"">
CustomProperties="MinPixelPointWidth=1,PixelPointWidth=30,
DrawingStyle=Cylinder,MaxPixelPointWidth=30,DrawSideBySide=True,PointWidth=0"
Color="224,224,224"Font="TimesNewRoman,9pt"
MarkerColor="Navy"MarkerSize="12"
BorderWidth="3"ShadowOffset="2"IsValueShownAsLabel="True"
LabelForeColor="64,0,0">
LabelBackColor="Olive"LabelForeColor="RosyBrown"
MarkerSize="12"/>
CalloutLineAnchorCapStyle="Diamond"
CalloutStyle="None"/>
BorderDashStyle="Solid"BackSecondaryColor="White"BackColor="64,165,191,228"
ShadowColor="Transparent"BackGradientStyle="TopBottom">
perspective="2"Inclination="20"LightStyle="Realistic"wallwidth="0"IsClustered="False">
mce_style="Bold""/>
mce_style="Bold""/>
ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)"Palette="BrightPastel"
imagetype="Png"BorderDashStyle="Solid"BackSecondaryColor="White"
BackGradientStyle="TopBottom"BorderWidth="2"backcolor="#D3DFF0">
Name="Default"BackColor="Transparent"Font="TrebuchetMS,8.25pt,style="Bold"
mce_style="Bold"">
BorderWidth="3"
XValueType="Double"ChartType="Line"MarkerStyle="Circle"
ShadowColor="Black"
BorderColor="180,26,59,105"Color="Red"ShadowOffset="2"
YValueType="Double">
ChartType=Line
BorderWidth="3"ShadowOffset="2"MarkerSize="8"
MarkerStyle="Circle"
ShadowColor="Black"BorderColor="180,26,59,105">
BorderDashStyle="Solid"BackSecondaryColor="White"
BackColor="64,165,191,228"ShadowColor="Transparent"
BackGradientStyle="TopBottom"IsSameFontSizeForAllAxes="True">
perspective="2"Inclination="20"LightStyle="Realistic"wallwidth="0"IsClustered="False">
mce_style="Bold""/>
mce_style="Bold""/>
这是后台的
viewplaincopytoclipboardprint?
protectedvoidPage_Load(objectsender,EventArgse)
{
showAirExample();
ShowPointExample();
}
protectedvoidshowAirExample()
{
Seriesseries=newSeries("2008Air");
series.ChartType=SeriesChartType.Column;
series["DrawingStyle"]="Cylinder";
series.IsValueShownAsLabel=true;
series.BorderWidth=3;
series.ShadowOffset=2;
series["PixelPointWidth"]="30";
//Populatenewserieswithdata
series.Points.AddY(0.281);
series.Points.AddY(1);
series.Points.AddY(1);
//Addseriesintothechart''sseriescollection
Chart1.Series.Add(series);
Chart1.Series["2008Air"].ChartType=SeriesChartType.Column;
//Showas3D
Chart1.ChartAreas["Air"].Area3DStyle.Enable3D=false;
//Drawas3DCylinder
Chart1.Series["2008Air"]["DrawingStyle"]="Cylinder";
Chart1.Series["2008Air"].LabelForeColor=Color.FromArgb(0,0,192);
}
protectedvoidShowPointExample(){
Randomrandom=newRandom();
for(intpointIndex=0;pointIndex<5;pointIndex++)
{
Chart2.Series[0].Points.AddY(random.Next(45,95));
Chart2.Series[1].Points.AddY(random.Next(5,75));
}
//Setpointlabels
//Chart2.Series["TBUint`1(Ship/CW)"].IsValueShownAsLabel=true;
//Chart2.Series["TBUChina(includeGSM)(Ship/CW)"].IsValueShownAsLabel=true;
//EnableXaxismargin
Chart2.ChartAreas["Ship/CW"].AxisX.IsMarginVisible=false;
//Enable3D,andshowdatapointmarkerlines
Chart2.ChartAreas["Ship/CW"].Area3DStyle.Enable3D=false;
Chart2.Series[0]["ShowMarkerLines"]="True";
Chart2.Series[1]["ShowMarkerLines"]="True";
}
protectedvoidPage_Load(objectsender,EventArgse)
{
showAirExample();
ShowPointExample();
}
protectedvoidshowAirExample()
{
Seriesseries=newSeries("2008Air");
series.ChartType=SeriesChartType.Column;
series["DrawingStyle"]="Cylinder";
series.IsValueShownAsLabel=true;
series.BorderWidth=3;
series.ShadowOffset=2;
series["PixelPointWidth"]="30";
//Populatenewserieswithdata
series.Points.AddY(0.281);
series.Points.AddY(1);
series.Points.AddY(1);
//Addseriesintothechart''sseriescollection
Chart1.Series.Add(series);
Chart1.Series["2008Air"].ChartType=SeriesChartType.Column;
//Showas3D
Chart1.ChartAreas["Air"].Area3DStyle.Enable3D=false;
//Drawas3DCylinder
Chart1.Series["2008Air"]["DrawingStyle"]="Cylinder";
Chart1.Series["2008Air"].LabelForeColor=Color.FromArgb(0,0,192);
}
protectedvoidShowPointExample(){
Randomrandom=newRandom();
for(intpointIndex=0;pointIndex<5;pointIndex++)
{
Chart2.Series[0].Points.AddY(random.Next(45,95));
Chart2.Series[1].Points.AddY(random.Next(5,75));
}
//Setpointlabels
//Chart2.Series["TBUint`1(Ship/CW)"].IsValueShownAsLabel=true;
//Chart2.Series["TBUChina(includeGSM)(Ship/CW)"].IsValueShownAsLabel=true;
//EnableXaxismargin
Chart2.ChartAreas["Ship/CW"].AxisX.IsMarginVisible=false;
//Enable3D,andshowdatapointmarkerlines
Chart2.ChartAreas["Ship/CW"].Area3DStyle.Enable3D=false;
Chart2.Series[0]["ShowMarkerLines"]="True";
Chart2.Series[1]["ShowMarkerLines"]="True";
}
三个电压,三个电流
C#code
stringmyConnectionString=ReportForm.Properties.Settings.Default.CEC710ConnectionString;
stringmySelectQuery="SELECTvol_va,vol_vb,vol_vc,cur_ia,cur_ib,cur_ic,opertimeFROM
[AmmeterData103]";
SqlConnectionmyConnection=newSqlConnection(myConnectionString);
SqlCommandmyCommand=newSqlCommand(mySelectQuery,myConnection);
//Setchartdatasource
chart1.DataSource=myCommand;
chart1.BorderlineWidth=3;
//SetseriesmembersnamesfortheXandYvalues
chart1.Series["A相电压"].XValueMember="OperTime";
chart1.Series["A相电压"].YValueMembers="vol_va";
chart1.Series["B相电压"].XValueMember="OperTime";
chart1.Series["B相电压"].YValueMembers="vol_vb";
chart1.Series["C相电压"].XValueMember="OperTime";
chart1.Series["C相电压"].YValueMembers="vol_vc";
chart1.Series["A相电流"].XValueMember="OperTime";
chart1.Series["A相电流"].YValueMembers="cur_ia";
chart1.Series["B相电流"].XValueMember="OperTime";
chart1.Series["B相电流"].YValueMembers="cur_ib";
chart1.Series["C相电流"].XValueMember="OperTime";
chart1.Series["C相电流"].YValueMembers="cur_ic";
//Databindtotheselecteddatasource
chart1.DataBind();
foreach(Seriessinchart1.Series)
{
s.ChartType=SeriesChartType.Spline;
}
我的数据库的AmmeterData103表里有多个字段,但我这里画图只需要三个电压,三个电流,请问如何画
到一张图中呢?
#1楼得分:0回复于:2010-05-1310:22:12问题已经解决,贴出实现线程控制spline图的显示。
【实现的CS代码】
C#code
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Windows.Forms.DataVisualization.Charting;
usingSystem.Data.SqlClient;
usingReportForm.DataSet;
usingSystem.Threading;
usingwl.CommonUtil.Sys;
namespaceReportForm
{
publicpartialclassBrokenlinegraphForm:Form
{
publicMainFormmForm=(MainForm)Context.getAttribute("main");
//Chartdataaddingthread
privateThreadaddDataRunner;
//ThreadAddDatadelegate
publicdelegatevoidAddDataDelegate();
publicAddDataDelegateaddDataDel;
publicstringbnumber=string.Empty;
publicstringsearch=string.Empty;
publicstringdrawerName=string.Empty;
publicstaticDateTimedt1=newDateTime();
publicstaticDateTimedt2=newDateTime();
publicDateTimedateTest=DateTime.Now;
publicdoublemaxValue=0.0;
publicinti=0;
publicBrokenlinegraphForm()
{
InitializeComponent();
}
privatevoidBrokenlinegraphForm_Load(objectsender,EventArgse)
{
//"AmS1031AT32A2010-05-122010-05-12S"
bnumber=mForm.Sqlstring.Substring(3,3);
drawerName=mForm.Sqlstring.Substring(7,5);
search="D"+mForm.Sqlstring.Substring(3,3)+mForm.Sqlstring.Substring(7,5);
dt1=Convert.ToDateTime(mForm.Sqlstring.Substring(13,10)+""+"0:00:00");
dt2=Convert.ToDateTime(mForm.Sqlstring.Substring(24,10)+""+"0:00:00");
dt2=dt2.AddDays(1);
addDataRunner=newThread(newThreadStart(CallData));
addDataRunner.Start();
addDataRunner.IsBackground=true;
//updatecharttitle
chart1.Titles[0].Text=bnumber.ToString()+"号楼变电站"+"抽屉名:"+drawerName;
}
publicvoidCallData()
{
for(;;)
{
AddDataThreadLoop();
Thread.Sleep(1000);
}
}
///
///^
///
privatevoidAddDataThreadLoop()
{
AddDataDelegateaddDataDel=newAddDataDelegate(AddData);
this.BeginInvoke(addDataDel);
}
///
///Methodwhichisinvokedfromthethreadtoaddseriespoints
///
publicvoidAddData()
{
doubletempMax=0.0;
stringmyConnectionString=ReportForm.Properties.Settings.Default.CEC710ConnectionString;
//stringmySelectQuery="SELECTvol_va,vol_vb,vol_vc,cur_ia,cur_ib,cur_ic,opertimeFROM
[AmmeterData103]whereammeteridlike''d103at32a%''andopertimebetween''"+dt1+"''and''"+dt2+"''
orderbyopertimeasc";
stringmySelectQuery="SELECTvol_va,vol_vb,vol_vc,cur_ia,cur_ib,cur_ic,opertimeFROM
[AmmeterData"+bnumber+"]whereammeteridlike''"
+search+"''andopertimebetween''"+dt1+"''and''"+dt2+"''
orderbyopertimeasc";
SqlConnectionmyConnection=newSqlConnection(myConnectionString);
SqlDataAdapterda=null;
AmmeterDataSet.DataTableDataTableamData=newAmmeterDataSet.DataTableDataTable();
da=newSqlDataAdapter(mySelectQuery,myConnection);
da.Fill(amData);
for(intk=0;k {
for(intm=0;m<6;m++)
{
if(maxValue {
maxValue=Convert.ToDouble(amData.Rows[k][m].ToString());
}
}
}
tempMax=Convert.ToInt16(maxValue11/50)5;
//设置Y轴的最大值
chart1.ChartAreas[0].AxisY.Maximum=tempMax;
chart1.Palette=ChartColorPalette.Pastel;
DateTimedateTest=DateTime.Now;
if(i {
dateTest=Convert.ToDateTime(amData.Rows[i].ItemArray[6].ToString());
chart1.Series[0].Points.AddXY(dateTest,amData.Rows[i].ItemArray[0]);
chart1.Series[1].Points.AddXY(dateTest,amData.Rows[i].ItemArray[1]);
chart1.Series[2].Points.AddXY(dateTest,amData.Rows[i].ItemArray[2]);
chart1.Series[3].Points.AddXY(dateTest,amData.Rows[i].ItemArray[3]);
chart1.Series[4].Points.AddXY(dateTest,amData.Rows[i].ItemArray[4]);
chart1.Series[5].Points.AddXY(dateTest,amData.Rows[i].ItemArray[5]);
i++;
//移走时间
doubleremoveDate=dateTest.AddMinutes((double)(90)(-1)).ToOADate();
for(intj=0;j<6;j++)
{
//判断当前最小的时间值是否小于移走时间
while(chart1.Series[j].Points[0].XValue {
chart1.Series[j].Points.RemoveAt(0);
}
}
chart1.ChartAreas[0].AxisX.Minimum=chart1.Series[0].Points[0].XValue;
chart1.ChartAreas[0].AxisX.Maximum=
DateTime.FromOADate(chart1.Series[0].Points[0].XValue).AddMinutes(100).ToOADate();
}
//标签提示
chart1.Series[0].MarkerStyle=MarkerStyle.Square;
chart1.Series[0].MarkerColor=Color.Red;
chart1.Series[0].Label="#VAL{G}";
chart1.Series[0].LabelToolTip="A相电压";
chart1.Series[1].MarkerStyle=MarkerStyle.Square;
chart1.Series[1].MarkerColor=Color.Green;
chart1.Series[1].Label="#VAL{G}";
chart1.Series[1].LabelToolTip="B相电压";
chart1.Series[2].MarkerStyle=MarkerStyle.Square;
chart1.Series[2].MarkerColor=Color.Yellow;
chart1.Series[2].Label="#VAL{G}";
chart1.Series[2].LabelToolTip="C相电压";
chart1.Invalidate();
}
//关闭按钮事件
privatevoidBrokenlinegraphForm_FormClosing(objectsender,FormClosingEventArgse)
{
try
{
addDataRunner.Abort();
}
catch
{
}
}
///
///停止线程
///
///
///
privatevoidstopTrending_Click(objectsender,EventArgse)
{
if(addDataRunner.IsAlive==true)
{
addDataRunner.Suspend();
}
continueTrending.Enabled=true;
}
///
///继续线程
///
///
///
privatevoidcontinueTrending_Click(objectsender,EventArgse)
{
if(addDataRunner.ThreadState==ThreadState.Suspended)
{
addDataRunner.Resume();
}
elseif(addDataRunner.IsAlive==false)
{
addDataRunner.Start();
}
}
}
}
复于:2010-05-1315:48:09
.NETMSChart应用的一个简单例子
这几天都在研究微软才发布的MSChart图表工具,我写了简单的例子发上来让大家看看。
使用前需要做得工作:1.把.net平台升级为3.5sp1的。
2.安装MSChart控件。
3.在使用时必须在项目中的web.canfig中添加一些内容;
mpImages/;"/>
aVisualization.Charting.ChartHttpHandler,System.Web.DataVisualization,Version=
3.5.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"validate="false"/>
="GET,HEAD"path="ChartImg.axd"type="System.Web.UI.DataVisualization.Charting.C
hartHttpHandler,System.Web.DataVisualization,Version=3.5.0.0,Culture=neutra
l,PublicKeyToken=31bf3856ad364e35"/>
这样我们就可以使用控件来画图表了
我在实例中主要对实例2做了详细的说明,实例1是ms上的例子!
前台代码:
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="test1.aspx.cs"Inherit
s="webchart_test1.test1"%>
g/TR/xhtml1/DTD/xhtml1-transitional.dtd">
无标题页
Location="~/TempImages/ChartPic_#SEQ(300,3)"
Palette="BrightPastel"ImageType="Png"BackSecondaryColor="White"Ba
ckGradientStyle="TopBottom"
BorderWidth="2"BackColor="#D3DFF0"BorderColor="26,59,105"AntiAl
iasing="None"
BorderlineDashStyle="Solid">
sparent"Font="TrebuchetMS,8.25pt,style=Bold">
rderDashStyle="Solid"
BackSecondaryColor="White"BackColor="64,165,191,228"Sha
dowColor="Transparent"
BackGradientStyle="TopBottom">
"IsRightAngleAxes="False"
WallWidth="0"IsClustered="False">
htPastel"BorderWidth="2"BorderColor="#cc9900">
sparent"Font="TrebuchetMS,8.25pt,style=Bold">
<%--控制小图标的显示格式--%>
<%----%>
<%--数据(一个数据集为)--%>
<%--作图区的修饰--%>
| | |