MSChart控件

合集下载

VB环境下MSChart控件的应用与研究

VB环境下MSChart控件的应用与研究

某个油田的区块做横坐标,井口数做纵坐
标而形成的“井口数统计图”为例,如图
1所示。
图1 井口数统计图
在此图中,由于区块过多,显示得不清楚,完全分不出哪个区块
对应的数值。解决这样的问
题,操作步骤为:
(1)将区块用编码表
示,如图2所示。
(2)用CommandButton
按钮,显示编码所表示的区
图2 井口数统计数据编码图
If Not IsNull(mRecordset1.Fields(1).Value) Then
js = js + mRecordset1.Fields(1).Value
End If
MRecordset1.Close
mRecordset_main.MoveNext
Wend
End With
(2)区块大于10个时的具体代码:
.RowLabel = ""
Else
.RowLabel = mRecordset_main!qk
'行显示的内容(油田区块)
End If
If Not IsNull(mRecordset1.Fields(0).Value) Then
k = k + mRecordset1.Fields(0).Value
End If
网络纵横
2008年第9期 146
VB环境下MSChart控件的应用与研究
李旭东
(山东胜利职业学院)
摘 要 在Visual Basic 6.0环境下,MSChart控件还有些不能实现的功能,如横坐标的内容显示不清楚,MSChart控件的宽度如何自 动调整以及调整后如何显示看不见的部分。本文主要针对以上问题,就如何改进及应用MSChart控件进行了探讨。

MSChart控件series属性的看法和心得

MSChart控件series属性的看法和心得

MSChart控件series属性的看法和心得1.Annotations --图形注解集合2.ChartAreas --图表区域集合3.Legends --图例集合4.Series --图表序列集合(相当于层次,同一个area中不同series相当于不同层次的关系,也可以理解为不同对象的关系,后面细说)5.Titles --图标的标题集合MSChart是微软在.NET3.5下开发出来的免费图标控件,在VS2008以上版本才能使用。

MSDN上面虽然能下载WinSamples,但是对于初学者来说,MSChart还是有难度的,如果没有人指点的话,那是很费时间的;而demo里面都是些相对复杂的例子,比如要实现两个ChartAreas的series重叠起来通过手动切换柱状图和饼状图这个效果就让我纠结了好久。

研究了很久,特别是在柱状图显示不同颜色的地方,如果对于series没有参透明白的话真的相当麻烦。

要实现同一个series里不同颜色仅需设置series.palette属性即可,上图中属性值为BrightPastel而要实现两个图标在同一个位置切换,在设计时,可调整chartarea的position属性,实现两个图标重叠总结:series——可以把它想象成是一个透明的箱子但是这个箱子只能装同一种类型的东西装了衣服就不能装裤子了说是透明箱子是因为考虑到chart工具的特性,无伤大雅如果你要比较衣服和裤子的数量就需要两个箱子(series)而要比较每条裤子的不同就只需要一个箱子(series)就可以了每条裤子就是一个point,设置好series里的point就可以了,不过饼状图由于是圆形的,所以只用设置point的Y轴数据就可以了。

c#_ms_chart_控件使用方法汇总(附统计图)

c#_ms_chart_控件使用方法汇总(附统计图)

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 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 chart's series collectionchart1.Series.Add(series);同时显示2条曲线// Populate series with random dataRandom random = new Random();for (int pointIndex = 0; pointIndex < 10;pointIndex++){Chart1.Series["Series1"].Points.AddY(random.Next(45, 95)); Chart1.Series["Series2"].Points.AddY(random.Next(5, 75)); }// Set series chart typeChart1.Series["Series1"].ChartType = SeriesChartType.Line; Chart1.Series["Series2"].ChartType = SeriesChartType.Spline;// Set point labelsChart1.Series["Series1"].IsValueShownAsLabel = true;Chart1.Series["Series2"].IsValueShownAsLabel = true;// Enable X axis marginChart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true;// Enable 3D, and show data point marker linesChart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true; Chart1.Series["Series1"]["ShowMarkerLines"] = "True";Chart1.Series["Series2"]["ShowMarkerLines"] = "True";显示column类型图,柱状图// Create new data series and set it's 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 chart's 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.Series["Series1"].Points.AddY(yValue);}// Set fast line chart typeChart1.Series["Series1"].ChartType = SeriesChartType.FastLine;日期,xy类型// Create a new random number generatorRandom rnd = new Random();// Data points X value is using current date DateTime date = DateTime.Now.Date;// Add points to the stock chart seriesfor (int index = 0; index < 10; index++){Chart1.Series["Series1"].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.Series["Series1"].Points.AddXY(rnd.Next(10,90), // X value is a date rnd.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();mandText = "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();mandText = "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();mandText = "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.Series["Series1"].XValueMember = "账号";Chart1.Series["Series1"].YValueMembers = "次数";// Data bind to the selected data sourceChart1.DataBind();conn.Close();数据库4,只绑定yChart1.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();mandText = "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.Series[0].Points.DataBindY(dr);dr.Close();数据库5,绑定xyChart1.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();mandText = "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.Series[0].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();mandText = "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.Series[0].Points.DataBind(dr, "name", "sales", "Tooltip=Year,Label=Commissions{C2}");dr.Close();数据库7,支持多lineChart1.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();mandText = "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=Commissions{C}");dr.Close();数据库8,按照行添加数据// Resolve the address to the Access databasestringfileNameString = this.MapPath(".");fileNameString += "..\\..\\..\\data\\chartdata.mdb";//Initialize a connectionstringstringmyConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource=" + fileNameString;// Definethe databasequerystringmySelectQuery="SELECT * FROM SALESCOUNTS;";// Createa database connection object using the connectionstringOleDbConnection myConnection = newOleDbConnection(myConnectionString);// Create adatabase command on the connection usingqueryOleDbCommandmyCommand = 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 classDataSetmyDataSet = new DataSet();// Addsrows in the DataSetmyDataAdapter.Fill(myDataSet,"Query");foreach(DataRow row in myDataSet.Tables["Query"].Rows){// For each Row add a new seriesstring seriesName = row["SalesRep"].ToString();Chart1.Series.Add(seriesName);Chart1.Series[seriesName].ChartType = SeriesChartType.Line;Chart1.Series[seriesName].BorderWidth = 2;for(int colIndex = 1; colIndex <myDataSet.Tables["Query"].Columns.Count; colIndex++) {// For each column (column 1 and onward) add the value as apointstring columnName =myDataSet.Tables["Query"].Columns[colIndex].ColumnName; int YVal = (int) row[columnName];Chart1.Series[seriesName].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 documentstringfileNameString = this.MapPath(".");stringfileNameSchema = this.MapPath(".");fileNameString += "..\\..\\..\\data\\data.xml";fileNameSchema += "..\\..\\..\\data\\data.xsd";//Initializes a new instance of the DataSet classDataSetcustDS = 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 classDataViewfirstView = new DataView(custDS.Tables[0]);// 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 filestringfileNameString = this.MapPath(".");fileNameString += "..\\..\\..\\data\\ExcelData.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 filestring file= "DataFile.csv";// Getthe path of the CSV filestring 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 queryOleDbCommandmyCommand = 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.Series[0].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.Series["Series1"].Points.DataBindXY(xval, yval);数据库9,dataview// Resolve the address to the Access databasestringfileNameString = this.MapPath(".");fileNameString += "..\\..\\..\\data\\chartdata.mdb";//Initialize a connectionstringstringmyConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource=" + fileNameString;// Definethe databasequerystringmySelectQuery="SELECT * FROM REPS;";// Createa database connection object using the connectionstringOleDbConnection myConnection = newOleDbConnection(myConnectionString);// Create adatabase command on the connection usingqueryOleDbCommandmyCommand = 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 classDataSetcustDS = new DataSet();// Addsrows in the DataSetcustDA.Fill(custDS, "Customers");//Initializes a new instance of the DataView classDataViewfirstView = new DataView(custDS.Tables[0]);// Sincethe DataView implements IEnumerable, pass the dataview directlyinto// the DataBind method with thename of the Columns selected in thequeryChart1.Series["Default"].Points.DataBindXY(firstView,"Name",firstView, "Sales");// Closesthe connection to the data source. This is the preferred// method of closing any openconnection.myCommand.Connection.Close();指定坐标轴的数据显示范围// Create a new random number generatorRandom rnd = new Random();// Add points to the stock chart seriesfor (int index = 0; index < 10; index++){Chart1.Series["Series1"].Points.AddXY(rnd.Next(10,90), // X value is a daternd.Next(40,50)); //Close Y value}Chart1.ChartAreas[0].AxisY.Minimum = 40;Chart1.ChartAreas[0].AxisY.Maximum = 50;数据排序// Use point index for drawing the chartChart1.Series["Series1"].IsXValueIndexed = true;// Sortseries points by second Y valueChart1.DataManipulator.Sort(PointSortOrder.Ascending, "Y2","Series1");查找数据最大值和最小值// Find point with maximum Y value and change colorDataPointmaxValuePoint =Chart1.Series["Series1"].Points.FindMaxValue(); maxValuePoint.Color = Color.FromArgb(255, 128, 128);// Findpoint with minimum Y value and change colorDataPointminValuePoint =Chart1.Series["Series1"].Points.FindMinValue(); minValuePoint.Color = Color.FromArgb(128, 128, 255);pie显示交互private void Page_Load(object sender, System.EventArgs e){// Addseries to the chartSeriesseries = Chart1.Series.Add("My series");// Setseries and legend tooltipsseries.ToolTip = "#VALX: #VAL{C} million";series.LegendToolTip = "#PERCENT";series.PostBackValue = "#INDEX";series.LegendPostBackValue = "#INDEX";// Populateseries datadouble[] 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);// Setseries visual attributesseries.Type= SeriesChartType.Pie;series.ShadowOffset = 2;series.BorderColor = Color.DarkGray;series.CustomAttributes = "LabelStyle=Outside";}protected void Chart1_Click(object sender, ImageMapEventArgse){intpointIndex = int.Parse(e.PostBackValue);Seriesseries = Chart1.Series["My series"];if(pointIndex >= 0 &&pointIndex < series.Points.Count){series.Points[pointIndex].CustomProperties +="Exploded=true";}}chart点击事件/// <summary>/// Page Load event handler./// </summary>protected void Page_Load(object sender, System.EventArgs e){this.Chart1.Click += new ImageMapEventHandler(Chart1_Click);// directusing of PostBackValueforeach(Series series in this.Chart1.Series){series.PostBackValue = "series:" + + ",#INDEX";}// transferof click coordinates. getCoordinates is a javascriptfunction.stringpostbackScript =ClientScript.GetPostBackEventReference(this.Chart1,"chart:@");this.Chart1.Attributes["onclick"] = postbackScript.Replace("@'", "'+ getCoordinates(event)"); }/// <summary>/// Handles the Click event of the Chart1 control./// </summary>/// <param name="sender">The sourceof the event.</param>/// <paramname="e">The<seecref="System.Web.UI.WebControls.ImageMapEventArgs"/>instance containing the eventdata.</param>protected void Chart1_Click(object sender, ImageMapEventArgse){this.Chart1.Titles["ClickedElement"].Text = "Nothing";string[]input = e.PostBackValue.Split(':');if(input.Length == 2){string[] seriesData = input[1].Split(',');if (input[0].Equals("series")){this.Chart1.Titles["ClickedElement"].Text = "Last Clicked Element:" + seriesData[0] + " - Data Point #" + seriesData[1];}else if (input[0].Equals("chart")){// hit test of X and Y click pointHitTestResult hitTestResult=this.Chart1.HitTest(Int32.Parse(seriesData[0]),Int32.Parse(seriesData[1]));if (hitTestResult != null){this.Chart1.Titles["ClickedElement"].Text = "Last Clicked Element:" + hitTestResult.ChartElementType.ToString();}}}}。

MSChart控件的属性与属性对话框

MSChart控件的属性与属性对话框

MSChart控件的属性与属性对话框t控件的属性tType属性:用于设置或返回图表类型,MSChart控件图表类型与对应ChartType属性值如表8.7所示。

如,ChartType=维直方图,ChartType=14则显示饼图。

表8.7 MSChart图表类型格)属性nt属性:用于表示图表中总格(行)数。

例如:控件显示二维数组Array_2(M,N),则总格(行)数RowCount=M。

unt=5,表示有5格(行)数据。

控件显示一维数组Array_1(N)的元素值,则总行数RowCount=1。

性:用于表示图表中某格(行)的序号。

控件显示二维数组Array_2(M,N),则图表中第I格的序号Row=I,当Row=1表示第1格(行)数据。

el属性:用于表示格(行)标签名,默认值为Ri。

用户可以修改其值,如改为无锡地区人数、南京地区人数等。

lCount属性:用于表示格(行)标签数,MSChart控件允许设置多个格(行)标签。

通常取值为1,当需要用2行以上的才修改此属性。

elIndex属性:用于表示格(行)标签序号,用户通过设置不同格(行)标签序号选择不同格(行)标签进行编辑。

性ount属性:用于表示图表中每格(行)中的列数,即数组中列数N。

如设置ColumnCount=3,则每格(行)中有3列,图格用3个矩形或3个扇形表示。

属性:用于表示图表中某格(行)某列的列序号,例如:umn=1,表示图表中第1格(行)第1列。

Label属性:用于表示图表列标签名,默认为Ci。

LabelCount属性:用于表示图表某格中的列标签数。

LabelIndex属性:用于表示图表某格中的列标签序号。

属性属性用于表示图表中由数据格(行)序号Row与列序号Column所指定值,即数组Array_2(Row,Column)的值。

用户可修改如在MSChart1的属性框内:ow=1,Column=1,Data=60,表示将图表中第1个数据格(行)中第1列的矩形高度改为60。

MSChart控件

MSChart控件

一、在工程中加入 mschart菜单->Project->Add To Project->Components and Controls->Registered ActiveX Controls->Microsoft Chart Control, version 6.0 (OLEDB)二、在CDemoView中加入:CMSChart m_Chart三、创建及设置m_Chart3.1 在 CDemoView::OnCreate 中创建CMSChart// CDemoView::OnCreate()CRect rc;GetClientRect(&rc);if(!m_Chart.Create("mschart", WS_CHILD| WS_VISIBLE, rc, this, 10)) return -1;3.2 在 CDemoView::OnSize 中调整 m_Chart 的大小,使之能随窗口大小变化而变化 // CDemoView::OnSizeif( m_Chart.GetSafeHwnd() )m_Chart.MoveWindow( 0, 0, cx, cy );3.3 设置 m_Chart void CDemoView::InitChart(){// 设置标题m_Chart.SetTitleText("mschart 示例");// 下面两句改变背景色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轴设置VARIANT var;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().GetVtCol or().Set(0, 0, 255);m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetPen().GetVtCol or().Set(255, 0, 0);m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetPen().GetVtCol or().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().G etItem(-1).GetDataPointLabel().SetLocationType(1);m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetDataPoints().G etItem(-1).GetDataPointLabel().SetLocationType(1);m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetDataPoints().G etItem(-1).GetDataPointLabel().SetLocationType(1);}3.4 设置数据 void CDemoView::DrawChart(){int nRowCount = 6;m_Chart.SetRowCount(nRowCount);VARIANT var;// 不自动标注X轴刻度m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetAuto(FALSE);// 每刻度一个标注m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerL abel(1);// 每刻度一个刻度线m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerT ick(1);// X轴名称m_Chart.GetPlot().GetAxis(0,var).GetAxisTitle().SetText("日期"); char buf[32];srand( (unsigned)time( NULL ) );for(int row = 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); m_Chart.GetDataGrid().SetData(row, 2, rand() * 100 / RAND_MAX, 0); m_Chart.GetDataGrid().SetData(row, 3, rand() * 100 / RAND_MAX, 0); }m_Chart.Refresh();}3.5 改变显示类型 // 折线图void CDemoView::OnChartLine(){m_Chart.SetChartType(3);DrawChart();}// 柱状图void CDemoView::OnChartCombi(){m_Chart.SetChartType(1);DrawChart();}// 饼状图void CDemoView::OnChartPie(){m_Chart.SetChartType(14);DrawChart();}//////////////////////////////////在设计使用上没问题,vs2005的,但做成安装包在其它电脑上安装运行出错(14007)。

MSChart控件教程

MSChart控件教程

MSChart控件这些内容是在学习过程中进行的整理,由于感到书籍资料或软件帮助中有时让人很不容易理解,特制作这个教程,以方便其他朋友学习,从新学习此控件的人的角度来描述相应的属性,相信也能让跟我一样程度的人更容易理解,可能在学习的过程中对某些属性与方法有理解不正确,请各位指正。

附录:1、AllowDynamicRotation作用:返回或设置是否可旋转图表注:需要图表的ChartType是3d类型才可以,当AllowDynamicRotation=True时,在图表中可按住ctrl+鼠标左键旋转图表。

示例:下列代码在窗体加载时将MSChart1设置为3d柱形图,并允许按住ctrl+鼠标左键旋转。

(窗体上放一个MSChart控件)Private Sub Form_Load()With MSChart1.chartType = VtChChartType3dBar '类型为3d柱形图,类形常数详见附录.AllowDynamicRotation = True '允许旋转End WithEnd Sub下列代码将以对话框的形式返回上面示例的AllowDynamicRotation值:MsgBox MSChart1.AllowDynamicRotation2、ChartData注:书中及软件帮助文档中说到数组需要变体型数值,是指如果要将数组第一个元素作为标题,其他作为数据,那个这个数组既有字符又有数字,那么就需要一个变体形。

如果数组中并不包含标题(或是之后使用属性来赋于标题),那么数组是可以定义类形的,个人建议先给定数组类型,之后再使用属性来设置标题。

作用:设置:将一个数组的数据作表图表的显示数据返回:将图表数据赋给一个数组示例:将数组元素作为图表的显示数据制作一个1行6列的的图表(数组的维代表图表的行,每维的元素代表列)Private Sub Form_Load()Dim 一维数组(1 To 6)'为了表示得更清楚,我们采取逐个赋值的方法一维数组(1) = 1一维数组(2) = 2一维数组(3) = 3一维数组(4) = 4一维数组(5) = 5一维数组(6) = 6With MSChart1.chartType = VtChChartType2dBar '类型为3d柱形图.ChartData = 一维数组()End WithEnd Sub为了理解图表的行列与数组的维数的对应关系,再举例一个二维数组(图表对应就形成2行6列)Private Sub Form_Load()Dim 二维数组(1 To 2, 1 To 6)'为了表示得更清楚,我们采取逐个赋值的方法二维数组(1, 1) = 1二维数组(1, 2) = 2二维数组(1, 3) = 3二维数组(1, 4) = 4二维数组(1, 5) = 5二维数组(1, 6) = 6二维数组(2, 1) = 1二维数组(2, 2) = 2二维数组(2, 3) = 3二维数组(2, 4) = 4二维数组(2, 5) = 5二维数组(2, 6) = 6With MSChart1.chartType = VtChChartType2dBar '类型为2d柱形图.ChartData = 二维数组()End WithEnd Sub下面是将数组的第一个元素设置成字符,使图表加载时将其作为标题的示例:Private Sub Form_Load()Dim 二维数组(1 To 2, 1 To 6)'为了表示得更清楚,我们采取逐个赋值的方法二维数组(1, 1) = "第一行标题"二维数组(1, 2) = 2二维数组(1, 3) = 3二维数组(1, 4) = 4二维数组(1, 5) = 5二维数组(1, 6) = 6二维数组(2, 1) = "第二行标题"二维数组(2, 2) = 5二维数组(2, 3) = 4二维数组(2, 4) = 3二维数组(2, 5) = 2二维数组(2, 6) = 1With MSChart1.chartType = VtChChartType2dBar '类型为2d柱形图.ChartData = 二维数组()End WithEnd Sub最后总结数组元素与维和图表行列的对应关系,作以下关系图:上述例子是将数组的内容反应到图表中,下面我们将使用上面的一维和二维图表,从图表中获得数据给数组,并将数组显示出来以验证结果(我们采取设置一个断点,然后通过视图\本地的方式来查看生成的数组):从图中可以看到,对于我们用2给数组生成的图表,再赋给新的数组时,数组变成了3维,并且每维还多了一个元素用来存储行标签,既“R1”、“R2”,多出来的一维用来存储列标签,既“C1”、“C2”等。

VS_MSChart控件的安装及应用

VS_MSChart控件的安装及应用

VS_MSChart控件的安装及应用1. 安装在VS中使用 Chart 控件的要求是 .net framework service pack 1准备 4 个文件,分别是:dotnetfx35sp1.exe(若当前 framework 版本为 sp1.1 就不需要装这个了)MSChart.exeMSChart_VisualStudioAddOn.exeMSChartLP_chs.exe顺序:MSChart.exeMSChart_VisualStudioAddOn.exe(至此,打开 VS 可看到工具箱中新增了[data]选项卡,下面有 Chart 控件)注意:安装时 VS 需要关闭安装中遇到的问题的小结:运行 dotNetFx35setup.exe 时,首先会提示下载,下载完毕之后进入安装。

但安装出错,报错原因为 dotnetfx2.0 SP1、dotnetfx3.0 SP1 安装不正确,错误代码 1603。

根据网上的资料,需要先清除系统中所有的 .net framework,然后重新安装。

清除时使用 microsoft 的工具dotnetfx_cleanup_tool 完全清除。

之后关闭 Cryptographic Services 服务,并删除system32\catroot2 目录下的所有内容。

再重新启动 Cryptographic Services 服务。

打开 dotnetfx35\wcu\dotNetFramework\dotNetFX30 目录,找到 WIC_x86_enu.exe,点击安装。

之后安装 dotnetfx35\wcu\dotNetFramework 目录中的 dotNetFx35setup.exe 独立安装包即可成功。

2. 使用需要添加如下引用System.Windows.Forms.DataVisualization.dll并导入如下命名空间using System.Windows.Forms.DataVisualization.Charting;但是这个命名空间只有在安装了 ChartControl.exe 之后才能在添加引用窗口中的 .NET 组件中找到,但是要安装 ChartControl 组件,需要当前的系统环境为 .net framework 3.5 SP1。

MSChart控件的用法

MSChart控件的用法

介绍MSChart的常用属性和事件介绍MSChart的常用属性和事件MSChart的元素组成最常用的属性包括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:横坐标绑定的数据源(如果数据源为T able,则填写横坐标要显示的字段名称)YValueMembers:纵坐标绑定的数据源(如果数据源为Table,则填写纵坐标要显示的字段名称,纵坐标可以有两个)ChartArea:图表所属的绘图区域名称ChartType:图表类型(柱形、饼形、线形、点形等)Legend:图表使用的图例名称Titles:标题集合。

MSChart属性

MSChart属性

数据图表/报表技术图表/报表是数据信息输出最常用的格式,在各类管理系统中具有重要的作用和地位。

本章将通过数据报表设计器(Data Report Designer)、图表、直接编程打印、利用Microsoft Excel 和Microsoft Word等设计多种格式的报表。

这些报表格式是比较典型的报表形式,在实际开发中具有重要意义和作用。

在本章中将介绍有图表/报表的知识。

27.1 图表技术在实际编程过程中,有时需要将查询统计出来的数据用以图表的形式显示出来。

VB中提供了一个图表控件,使用该图表控件可以很方便地将数据以图表的形式显示统计出来。

在本节中将介绍一些有关图表的知识。

27.1.1 图表控件概述图表控件(MSChart)是可以按照一定的规范将数据以图表的形式绘制出来。

其绘制的数据源可以是动态数据,也可以存储数据中数据库中的数据、电子表格的数据等存储介质的数据。

可通过在控件的属性页中设置数据来创建图表,或者也可以从其他数据源为控件对象设置数据。

MSChart控件主要支持真三维表示,支持所有主要的图表和图形类型。

由于MSChart控件不是标准控件,要使用MSChart控件必需要将MSChart控件添加到控件工具箱中。

添加MSChart控件的方法为选择"工程"|"部件"命令,在出现的"部件"对话框中选择"Microsoft MSChart Controls 6.0(SP4)"选项并单击"确定"按钮。

这时候MSChart控件就被添加到VB的控件工具箱中。

27.1.2 图表控件的基本属性(1)设置MSChart控件对象的名称、大小、图表的标题、图表的页眉、图表的脚注、图表的行列数等,可通过设置MSChart控件的一些属性来完成。

MSChart控件的基本属性如表27-1所示。

表27-1 MSChart控件的基本属性续表续表下面将介绍MSChart控件中一些常用的属性。

微软图表控件MsChart使用指南

微软图表控件MsChart使用指南

微软图表控件MsChart使用指南昨天在网上看到了微软发布了.NET 3.5框架下的图表控件,第一时间抓下来看了一下,发觉功能很强劲,基本上能想到的图表都可以使用它绘制出来,给图形统计和报表图形显示提供了很好的解决办法,同时支持Web和WinForm两种方式,不过缺点也比较明显,只能在最新的开发环境中使用,需要.Net 3.5 Sp1以及VS 2008的开发环境。

下面是下载地址:mework 3.5)–1.包含英文版,中文版。

上面提供的链接是中文版的,可以更改为英文版。

2.语言包:Microsoft Chart Controls for Microsoft .NETFramework 3.5 Language Pack3.Microsoft .NET Framework 3.5 的Microsoft 图表控件的语言包,包含23中语言。

4.Microsoft Chart Controls Add-on for Microsoft Visual Studio 2008–这个只有英文的,没找到中文的。

5.文档(Microsoft Chart Controls for .NET Framework Documentation)–这个只有英文的,没找到中文的。

6.WinForm 和的例子(Samples Environmentfor Microsoft Chart Controls)–这个只有英文的,没找到英文的。

7.Demo 下载:/mschart下了它的示例程序后,运行了一下,非常的强大,可以支持各种各样的图形显示,常见的:点状图、饼图、柱状图、曲线图、面积图、排列图等等,同时也支持3D样式的图表显示,不过我觉得最有用的功能还是支持图形上各个点的属性操作,它可以定义图形上各个点、标签、图形的提示信息(Tooltip)以及超级链接、Jav ascript动作等,而不是像其它图形类库仅生成一幅图片而已,通过这些,加上微软自己的Ajax框架,可以建立一个可以互动的图形统计报表了。

微软图表控件MsChart使用文档

微软图表控件MsChart使用文档

一、mschart控件使用详解 3.5 ChartAreas控件中的ChartAreas属性是ChartArea对象的集合,ChartArea负责显示容器的属性或图表的背景,由于不止一个,这就意味着MSChart控件可以包含多个图表。

在使用多个ChartAreas时理解下面几点内容非常重要:在技术上可以控制ChartArea的位置,因此多个ChartArea可以叠加,但不推荐这么做,建议在MSChart 控件内的独立区域内绘制它们,为了合并或覆盖数据点,推荐在一个ChartArea内使用多个序列,后面将会有介绍。

默认情况下,控件会为你自动调整大小和位置。

单个ChartArea将会独立调整以适应数据,正如上图所显示的,第二个ChartArea中的Y值更大,数据点也更少。

多个ChartAreas控件允许你使用多个不相容的ChartTypes(序列对象属性,控制图表的显示类型,如条形、柱状和饼状)显示图表,图表任然显示在相同的MSChart控件内。

对于单个ChartArea,有许多独立的属性可以设置和调整,这样你就可以自行调整图表区域以满足不同的需要,它的大部分属性和面板控件的属性都差不多,因此这里我们就不多说了,只说一下ChartArea 唯一的属性,下面是这些唯一属性的清单:3D样式:使用ChartArea的Area3DStyle属性和子属性,我们可以创建漂亮的、十分抢眼的3D图表,无论是在设计器中还是在代码中都必需将Enable3D属性设置为TRUE,其余的参数可以通过调整旋转、视角、照明方式和其它3D元素,让一个图像看起来具有3D效果。

坐标轴控制和样式:坐标轴集合包括x轴和y轴,以及第二个x轴和y轴,这四个项目的属性允许你设置样式、设置标签、定义间隔、设置工具提示、设置缩放等,如果你的图标要求精确的间隔、标签或其它特殊的显示需要,你可以使用这些属性。

例如,你可以颠倒坐标轴的值,或控制如何在x轴上显示标签。

MS Chart控件简单示例

MS Chart控件简单示例

VS2010新增Chart控件,可以直接在页面插入图表:简单示例:<asp:Chart ID="Chart1"runat="server"Height="400px"Width="500px"><Series><asp:Series Name="Series1"ChartType="Column"ChartArea="ChartArea1"> </asp:Series><asp:Series Name="Series2"ChartType="Column"ChartArea="ChartArea1"> </asp:Series></Series><ChartAreas><asp:ChartArea Name="ChartArea1"></asp:ChartArea></ChartAreas></asp:Chart>后台代码:protectedvoid Page_Load(object sender, EventArgs e){DataTable dt =default(DataTable);dt = CreateDataTable();//设置图表的数据源Chart1.DataSource = dt;//设置图表Y轴对应项Chart1.Series[0].YValueMembers = "Volume1";Chart1.Series[1].YValueMembers = "Volume2";//设置图表X轴对应项Chart1.Series[0].XValueMember = "Date";//绑定数据Chart1.DataBind();}private DataTable CreateDataTable(){//Create a DataTable as the data source of the Chart control DataTable dt =new DataTable();//Add three columns to the DataTabledt.Columns.Add("Date");dt.Columns.Add("Volume1");dt.Columns.Add("Volume2");DataRow dr;//Add rows to the table which contains some random data for demonstration dr = dt.NewRow();dr["Date"] = "Jan";dr["Volume1"] = 3731;dr["Volume2"] = 4101;dt.Rows.Add(dr);dr = dt.NewRow();dr["Date"] = "Feb"; dr["Volume1"] = 6024; dr["Volume2"] = 4324; dt.Rows.Add(dr);dr = dt.NewRow();dr["Date"] = "Mar"; dr["Volume1"] = 4935; dr["Volume2"] = 2935; dt.Rows.Add(dr);dr = dt.NewRow();dr["Date"] = "Apr"; dr["Volume1"] = 4466; dr["Volume2"] = 5644; dt.Rows.Add(dr);dr = dt.NewRow();dr["Date"] = "May"; dr["Volume1"] = 5117; dr["Volume2"] = 5671; dt.Rows.Add(dr);dr = dt.NewRow();dr["Date"] = "Jun"; dr["Volume1"] = 3546; dr["Volume2"] = 4646; dt.Rows.Add(dr);return dt;}浏览页面:。

微软图形控件(MSchart)

微软图形控件(MSchart)

三、数据绑定
(一)数据绑定方式 可以在图表的设计界面,在属性设置窗口中的Series 属性下的Points中添加需要的数据. 可以在图表的设计界面中,在属性中绑定一个数据源. 可以在后台代码中,动态添加数据. 可以在后台代码中设置一个或多个数据源,直接绑定 到图表中.
三、数据绑定(方法一)
<asp:Series Name="Series1" Legend="Legend1"> <Points> <asp:DataPoint XValue="1" YValues="700" /> <asp:DataPoint XValue="2" YValues="400" /> <asp:DataPoint XValue="3" YValues="200" /> <asp:DataPoint XValue="4" YValues="450" /> <asp:DataPoint XValue="5" YValues="300" /> </Points> </asp:Series>
五、CHARTAREAS(重要属性)
Area3DStyle:设置3D效果 Enable3D :是否开启3D效果 WallWidth:控制墙宽 IsRightAngleAxes:是否开启倾斜 Inclination :X轴倾斜度 Rotation :Y轴倾斜度 Y
三、数据绑定(方法四)
可绑定的数据源
根据官方文档的说法,只要是实现了IEnumerable接口的 数据类型,都可以绑定到图表控件中,例如:DataView, DataReader, DataSet, DataRow, DataColumn, Array, List, SqlCommand, OleDbCommand, SqlDataAdapter, 及 OleDbDataAdapter对象。 对于开发来说,最常用的还是DataView、DataReader、 DataSet、DataRow、Array、List这几种类型了。

MicrosoftVisualStudio2010中MSChart控件用于仿真曲线样例

MicrosoftVisualStudio2010中MSChart控件用于仿真曲线样例

MSchart控件在C++6.0和Microsoft visual studio 2005中使用都需要额外安装控件,但是Microsoft visual studio 2010中已经内含此控件,所以此教程用于Microsoft visual studio 2010版本使用。

1:新建MFC工程,此处可以选择对话框程序,如图1所示,其余设置可默认。

图12:MSChart控件位于工具箱下端的常规控件选项卡中,若是工具箱面板中未找到,可以右击工具箱→选择项→COM组件中选择Microsoft Chart Control,version5.0。

控件位置及图标如图2所示。

注:此处除Microsoft Chart Control,version5.0外,还有Microsoft Chart Control 6.0,所使用的是version5.0,但添加的类却是6.0,对于此处差别还不太清楚。

图23:添加Microsoft Chart Control控件到对话框程序中,如图3所示。

图34:点击Microsoft Chart Control控件,右端出现属性窗口,如图4所示,在此处可以对于此控件进行设置,下面对于各个控件属性含义进行具体讲解。

图4更改属性页内属性,属性页位于属性列表上端快捷键最后一位,属性页如图5所示。

图5属性列表主要包括以下几个方面的设置:Chart主要包括对于图表类型的选择,此例中主要使用的是Line(2D),选项Show legend 为显示图例,在此例中需要进行勾选。

设置如图6所示。

图6Axis主要是对于各个坐标轴的设置,其中Axis可以进行X Axis、Y Axis及Second Y Axis 三个轴的选择,其中Show scale选项可以选择对应轴是否显示,Automatic scaling可以对于坐标轴上最大值、最小值、标注标签间隔和每个间隔细分格数(对于X及Y轴是不同的,X轴若是100为一个格,50需要为一个小格,Y轴Major divisions对应的是大格格数,Minor divisions对应小格格数)。

利用MsChart控件绘制多曲线图表

利用MsChart控件绘制多曲线图表

利⽤MsChart控件绘制多曲线图表在.Net4.0框架中,微软已经将Mschart控件集成了进来,以前⼀直在web下⾯⽤过,原来winform下的Mschart控件更加简单更加⽅便,今天我们⽤mschart绘制⼀个多曲线图,发现MsChart 的显⽰效果还是不错的,话不多说,直接上效果图:代码如下,供⼤家参考:public partial class FrmMain : Form{public FrmMain(){InitializeComponent();InitializeChart();this.Load += new EventHandler(FrmMain_Load);this.myChart.GetToolTipText += new EventHandler<ToolTipEventArgs>(myChart_GetToolTipText);}void FrmMain_Load(object sender, EventArgs e){float[][] data = new float[3][];//第⼀条数据data[0] = new float[10] { 1.3f, 2.5f, 2.1f, 3.3f, 2.8f, 3.9f, 4.3f, 3.6f, 4.2f, 3.6f };//第⼆条数据data[1] = new float[12] { -2f, -1.3f, 0.1f, 0.5f, -1.5f, 0.7f, 1f, 1.4f, 1.9f, 2f, 2.6f, 3.1f };//第三条数据data[2] = new float[10] { 7.8f, 9.2f, 6.5f, 8.3f, 9.0f, 5.9f, 6.3f, 7.2f, 8.8f, 9.8f };for (int i = 0; i < data.Length; i++){//横坐标时间DateTime dt = DateTime.Now.Date;Series series = this.SetSeriesStyle(i);for (int j = 0; j < data[i].Length; j++){series.Points.AddXY(dt, data[i][j]);dt = dt.AddDays(1);}this.myChart.Series.Add(series);}}private void myChart_GetToolTipText(object sender, ToolTipEventArgs e){if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint){int i = e.HitTestResult.PointIndex;DataPoint dp = e.HitTestResult.Series.Points[i];e.Text = string.Format("时间:{0};数值:{1:F1} ", DateTime.FromOADate(dp.XValue),dp.YValues[0]);}}///<summary>///初始化Char控件样式///</summary>public void InitializeChart(){#region设置图表的属性//图表的背景⾊myChart.BackColor = Color.FromArgb(211, 223, 240);//图表背景⾊的渐变⽅式myChart.BackGradientStyle = GradientStyle.TopBottom;//图表的边框颜⾊、myChart.BorderlineColor = Color.FromArgb(26, 59, 105);//图表的边框线条样式myChart.BorderlineDashStyle = ChartDashStyle.Solid;//图表边框线条的宽度myChart.BorderlineWidth = 2;//图表边框的⽪肤myChart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;#endregion#region设置图表的TitleTitle title = new Title();//标题内容title.Text = "多曲线图演⽰";//标题的字体title.Font = new System.Drawing.Font("Microsoft Sans Serif", 12, FontStyle.Bold);//标题字体颜⾊title.ForeColor = Color.FromArgb(26, 59, 105);//标题阴影颜⾊title.ShadowColor = Color.FromArgb(32, 0, 0, 0);//标题阴影偏移量title.ShadowOffset = 3;myChart.Titles.Add(title);#endregion#region设置图表区属性//图表区的名字ChartArea chartArea = new ChartArea("Default");//背景⾊chartArea.BackColor = Color.FromArgb(64, 165, 191, 228);//背景渐变⽅式chartArea.BackGradientStyle = GradientStyle.TopBottom;//渐变和阴影的辅助背景⾊chartArea.BackSecondaryColor = Color.White;//边框颜⾊chartArea.BorderColor = Color.FromArgb(64, 64, 64, 64);//阴影颜⾊chartArea.ShadowColor = Color.Transparent;//设置X轴和Y轴线条的颜⾊和宽度chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);chartArea.AxisX.LineWidth = 1;chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);chartArea.AxisY.LineWidth = 1;//设置X轴和Y轴的标题chartArea.AxisX.Title = "横坐标标题";chartArea.AxisY.Title = "纵坐标标题";//设置图表区⽹格横纵线条的颜⾊和宽度chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64); chartArea.AxisX.MajorGrid.LineWidth = 1;chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64); chartArea.AxisY.MajorGrid.LineWidth = 1;myChart.ChartAreas.Add(chartArea);#endregion#region图例及图例的位置Legend legend = new Legend();legend.Alignment = StringAlignment.Center;legend.Docking = Docking.Bottom;this.myChart.Legends.Add(legend);#endregion}//设置Series样式private Series SetSeriesStyle(int i){Series series = new Series(string.Format("第{0}条数据", i + 1));//Series的类型series.ChartType = SeriesChartType.Line;//Series的边框颜⾊series.BorderColor = Color.FromArgb(180, 26, 59, 105);//线条宽度series.BorderWidth = 3;//线条阴影颜⾊series.ShadowColor = Color.Black;//阴影宽度series.ShadowOffset = 2;//是否显⽰数据说明series.IsVisibleInLegend = true;//线条上数据点上是否有数据显⽰series.IsValueShownAsLabel = false;//线条上的数据点标志类型series.MarkerStyle = MarkerStyle.Circle;//线条数据点的⼤⼩series.MarkerSize = 8;//线条颜⾊switch (i){case0:series.Color = Color.FromArgb(220, 65, 140, 240);break;case1:series.Color = Color.FromArgb(220, 224, 64, 10);break;case2:series.Color = Color.FromArgb(220, 120, 150, 20);break;}return series;}}Demo 分为WEB版本和WinForm版本,样式和图表内容很全,希望对⼤家有所帮助。

MSChart经验与技巧

MSChart经验与技巧
1. MajorGrid属性——主辅助线对象引用,沿水平方向绘制。 用法示例: ChartArea1.AxisY.MajorGrid.LineColor = System.Drawing. Color.FromArgb(248, 245, 248); 2. MinorGrid属性——次辅助线对象引用,沿垂直方向绘制,用法和 MajorGrid一样。 3. Title属性——轴标题文本。 4. TitleAlignment属性——轴标题文本对齐方式,取值:[Center|Far |Near]。 5. StripLines属性——轴序列分割线集合引用,集合中的元素为 StripLine实例化对象。 6. ArrowStyle属性——轴末端箭头样式,取值:[None(默 认)|Lines|SharpTriangle|Triangle]。
每一个 对象最终都被添加至 集合属性中才能呈现出来 注:每一个Series对象最终都被添加至Chart .Series集合属性中才能呈现出来。 对象最终都被添加至Chart .Series集合属性中才能呈现出来。
四. MSChart主要控件类使用
• Point类使用 类
1. Label属性 ——点标签文本。 2. LegendText属性 ——图例项文本。 3. XValue属性 ——点对应X轴值。 4. YValues属性 ——点对应Y轴值集合,一般取第1个即可。 5. AxisLabel属性 ——点对应X轴标签文字,绘制饼图时会用到。 6. MarkerSize、MarkerStyle属性 ——和Series用法一致,且其优先级高于 Series级别定义。 7. Exploded 属性 ——仅适用域饼图的point对象,值为布尔型,用于控制饼块 是否分离显示,通过point.CustomProperty或point[“Exploded ”]来设置。 8. ToolTip、Url、MapAreaAttributes属性 ——实现浏览器端交互使用到 的关键属性,用法参见本幻灯片前文“三. MSChart公共属性”相关部分。

VS2013 使用MsChart图表控件

VS2013 使用MsChart图表控件

VS2013 使用MsChart图表控件显示曲线波形图和数据波峰波谷标记MSChart.exe安装MSCHART注册.exeMSCHRT20.OCX也可以手动注册32位系统下:1. 将mschrt20.ocx放在windows/system32目录下。

(注意,这个文件名中的没有字母a,即没有写chart中的字母a)2.使用命令regsvr32 MSCHRT20.OCX来注册。

64位系统下:1. 确保win7管理员权限2. 把OCX控件,放到C:\Windows\SysWow64路径下面3. 进入cmd,管理员权限运行4. regsvr32 C:\Windows\SysWow64\MSCHRT20.OCX注册成功会提示的。

在VS2013的对话框中的应用如下:打开VS2013,新建一个MFC工程,点击VS中的“工具->选择工具箱->COM组件”,找到Microsoft Chart Control,version 6.0(OLEDB)并勾选上。

接下来,就可以在工具箱中看到该控件首先建立一个基于对话框的工程文件,在对话框界面单击右键->插入Activex控件->Microsoft Chart Control,version 6.0(OLEDB)即可。

新建MFC工程建立对话框应该程序点击完成,右击插入ActiveX控件列表中找到Microsoft Chart Control 6.0(SP4) (OLEDB)控件点确定按钮插入MsChart图表控件在控件上右击添加变量这时候并不能使用MsChart控件的所有功能,还需要添加控件的所有类,添加步骤如下,先打开类向导点“…”找到MSCHRT20.OCX文件注意OCX控件32位系统在C:\ windows/system32目录下64位系统在C:\Windows\SysWow64路径下面注意左侧会列出控件的所有类,可选择添加点击“>”逐个添加要用到的类,好像只能一个一个添加,最好全部添加添加完成按确定会自动加载.h和.cpp文件到工程中,这时才可以使用控件的所有功能下面是整理出来的一些主要功能void CMyPD3000Dlg::Init_Mschart(CMschart1& m_chartt1){m_chartt1.put_AllowSelections(FALSE); //是否可以选择图表对象m_chartt1.put_AllowSeriesSelection(FALSE);//单击单个的图表数据点时是否可选定整个系列。

MSChart图表控件的一些使用

MSChart图表控件的一些使用

MSChart图表控件的一些使用最近使用MSChart做了几张图表,微软提供的例子中对这个图表控件的使用已经说的比较详细了,我这里记录一些需要注意的。

1. Chart图表的元素要使用该图表首先要了解该图表的元素组成,就是下面这张图了,图表主要包含:Annotations (图形注解集合)ChartAreas(图表区域集合)Legends(图例集合)Series图表序列集合即图表数据对象集合)Title(图标的标题集合)其他大家可以参考Samples中的Char Features一节。

2. 如何在 MVC中使用Chart控件2.1.需要在"<system.web><pages><controls>"中添加如下:<add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization,Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>2.2.在"<httpHandlers>"中添加如下部分:<add path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHan dler,System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>2.3. 有了上面的配置就可以测试了:2.3.1.View中添加:<asp:chart id="Chart1" runat="server" Height="296px"Width="412px" Palette="BrightPastel" imagetype="Png"BorderDashStyle="Solid" BackSecondaryColor="White" BackGradientStyle="TopBottom" BorderWidth="2" backcolor="#D3DFF0"BorderColor="26, 59, 105"><Titles><asp:Title Text="With datasource in code behind" /></Titles><legends><asp:Legend IsTextAutoFit="False" Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold"></asp:Legend></legends><borderskin skinstyle="Emboss"></borderskin><series><asp:Series Name="Column" BorderColor="180, 26, 59, 105"> </asp:Series></series><chartareas><asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid" BackSecondaryColor="White"BackColor="64, 165, 191, 228" ShadowColor="Transparent" BackGradientStyle="TopBottom"><area3dstyle Rotation="10" perspective="10" Inclination="15" IsRightAngleAxes="False" wallwidth="0"IsClustered="False"></area3dstyle><axisy linecolor="64, 64, 64, 64"><labelstyle font="Trebuchet MS, 8.25pt, style=Bold" /><majorgrid linecolor="64, 64, 64, 64" /></axisy><axisx linecolor="64, 64, 64, 64"><labelstyle font="Trebuchet MS, 8.25pt, style=Bold" /><majorgrid linecolor="64, 64, 64, 64" /></axisx></asp:ChartArea></chartareas></asp:chart>2.3.2.index.aspx.cs中添加protected void Page_Load(object sender, System.EventArgs e) {foreach (int value in (List<int>)this.ViewData["Chart"]){this.Chart1.Series["Column"].Points.Add(value);}}2.3.3.Controller中:public ActionResult Index(){ViewData["Title"] = "Home Page";ViewData["Message"] = "Welcome to MVC!";List<int> chartList = new List<int>();chartList.Add(1);chartList.Add(2);chartList.Add(6);chartList.Add(5);chartList.Add(4);ViewData["Chart"] = chartList;return View();}2.3.4.添加System.Web.ui.DataVisualization引用3. Tooltip的使用ToolTip用于在各个关键点,如:标签、图形关键点、标题等当鼠标移动上去的时候,提示用户一些相关的详细或说明信息。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

一、在工程中加入 mschart菜单->Project->Add To Project->Components and Controls->Registered ActiveX Controls->Microsoft Chart Control, version 6.0 (OLEDB)二、在CDemoView中加入:CMSChart m_Chart三、创建及设置m_Chart3.1 在 CDemoView::OnCreate 中创建CMSChart// CDemoView::OnCreate()CRect rc;GetClientRect(&rc);if(!m_Chart.Create("mschart", WS_CHILD| WS_VISIBLE, rc, this, 10)) return -1;3.2 在 CDemoView::OnSize 中调整 m_Chart 的大小,使之能随窗口大小变化而变化 // CDemoView::OnSizeif( m_Chart.GetSafeHwnd() )m_Chart.MoveWindow( 0, 0, cx, cy );3.3 设置 m_Chart void CDemoView::InitChart(){// 设置标题m_Chart.SetTitleText("mschart 示例");// 下面两句改变背景色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轴设置VARIANT var;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().GetVtCol or().Set(0, 0, 255);m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetPen().GetVtCol or().Set(255, 0, 0);m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetPen().GetVtCol or().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().G etItem(-1).GetDataPointLabel().SetLocationType(1);m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetDataPoints().G etItem(-1).GetDataPointLabel().SetLocationType(1);m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetDataPoints().G etItem(-1).GetDataPointLabel().SetLocationType(1);}3.4 设置数据 void CDemoView::DrawChart(){int nRowCount = 6;m_Chart.SetRowCount(nRowCount);VARIANT var;// 不自动标注X轴刻度m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetAuto(FALSE);// 每刻度一个标注m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerL abel(1);// 每刻度一个刻度线m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerT ick(1);// X轴名称m_Chart.GetPlot().GetAxis(0,var).GetAxisTitle().SetText("日期"); char buf[32];srand( (unsigned)time( NULL ) );for(int row = 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); m_Chart.GetDataGrid().SetData(row, 2, rand() * 100 / RAND_MAX, 0); m_Chart.GetDataGrid().SetData(row, 3, rand() * 100 / RAND_MAX, 0); }m_Chart.Refresh();}3.5 改变显示类型 // 折线图void CDemoView::OnChartLine(){m_Chart.SetChartType(3);DrawChart();}// 柱状图void CDemoView::OnChartCombi(){m_Chart.SetChartType(1);DrawChart();}// 饼状图void CDemoView::OnChartPie(){m_Chart.SetChartType(14);DrawChart();}//////////////////////////////////在设计使用上没问题,vs2005的,但做成安装包在其它电脑上安装运行出错(14007)。

为什么啊,电脑上已经安装了office,已经有mschar20.ocx了,请问是哪里出问题? ( play100 发表于 2007-11-20 11:21:00)if(!m_Chart.Create("mschart", WS_CHILD| WS_VISIBLE, rc, this, 10))我也是为什么呢?错误是建立空文档失败 ( zltianhen 发表于2007-5-5 20:07:00)我被你快搞疯了,如果大家想动态的增加曲线,并且添加曲线标签的话,最好将m_Chart.SetColumnCount(short n);这条语句放在InitChart()这个函数的开头。

我被这个小问题折磨了一晚上。

( eyon123 发表于 2006-12-22 3:49:00)首先感谢祝小斌的文章,对我很有用,thx~~大概可能是由于版本的关系,线色那段代码有点问题~~(红-绿-蓝是默认色,代码并没有起作用)修改如下:m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetDataPoints().Ge tItem(-1).GetBrush().GetFillColor().Set(红, 绿, 蓝); ( 3las 发表于2005-3-27 22:00:00)SyDes21没有安装mschart 控件 ( pq_vckbase 发表于 2004-1-26 12:28:00)。

相关文档
最新文档