天天看点

[VC] 在VC++中使用MSChart表格控件(小结)(转载)

SChart,微软的一个很不错的画图控件,以下转载自:http://blog.tom.com/easehaibo/article/1374.html

1. 在工程中添加MSChart控件

Project—〉Add to Project—〉Registered ActiveX Controls,选中Microsoft Chart Control 6.0(SP4)(OLEDB)

点击Insert,一路确定

2.         在用到控件的地方加上相应的头文件,mschart.h,还有其他比较常用的头文件:(重要!上面两个例子没有提到,可能都是在视窗中使用,在对话框应用中需要添加)

#include "VcPlot.h"

#include "VcAxis.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"

#include "VcAxisScale.h"

#include "VcAxisGrid.h"

3.         定义并create控件对象

CMSChart m_Chart;

m_Chart.Create("mschart", WS_CHILD| WS_VISIBLE, rc, this, 10);// this 为窗口指针

4.         设置控件的属性

//设置标题

       m_Chart.SetTitleText(Title);//Title 为CString类型

//设置栈模式

       m_Chart.SetStacking(FALSE);

//设置行数及列数

m_Chart.SetRowCount(iRowCount);//iRowCount和iColumnCount为int型

m_Chart.SetColumnCount(iColummCount);

//设置控件的数据,int型的iRow,iColumn可看成是数据所在的行和列,Data即是所要设的数值型数据

       m_Chart.GetDataGrid().SetData(iRow, iColumn, Data, 0);

//设置图例

       m_Chart.SetShowLegend(TRUE);//显示图例

       m_Chart.SetColumn(iColumn);

       m_Chart.SetColumnLabel(slegend);//slegend为CString型

//设置x轴下方显示的标记

       m_Chart.SetRow(iRow);

       m_Chart.SetRowLabel(sLabel);//sLabel为CString型

//设置x轴及y轴的标题。xTitle和yTitle为CString型

       m_Chart.GetPlot().GetAxis(0,var).GetAxisTitle().SetText(xTitle); //x轴

       m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().SetText(yTitle); //y轴

//设置控件类型

       m_Chart.SetChartType(3);//3:曲线型;1:条形;14:饼图

//设置背景颜色

       m_Chart.GetBackdrop().GetFill().SetStyle(1);

       m_Chart.GetBackdrop().GetFill().GetBrush().GetFillColor().Set(255, 255, 255);

//设置数据系列的颜色:如果是曲线图则对应每条曲线的颜色

       for (int i = 1; i <= m_Chart.GetColumnCount(); i++ )

       {//这里设置为随机颜色

              m_Chart.GetPlot().GetSeriesCollection().GetItem(i).GetPen().GetVtColor().Set(rand() * 230 / RAND_MAX, rand() * 230 / RAND_MAX, rand() * 230 / RAND_MAX);

              m_Chart.GetPlot().GetSeriesCollection().GetItem(i).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocationType(1);

       }

//设置x轴的其他属性

       m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetAuto(FALSE); // 不自动标注X轴刻度

       m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerLabel(1);// 每刻度一个标注

       m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerTick(1); // 每刻度一个刻度线

//自动标注y轴

       m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetAuto(TRUE);

在这里,也可以手动设置y轴,如下:

m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMaximum(100);     // y轴最大刻度为100

m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinimum(0);         // y轴最小刻度为0

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("YourTitle");     // y的轴名称

//不要与x轴垂直的表格线

       m_Chart.GetPlot().GetAxis(0,var).GetAxisGrid().GetMajorPen().SetStyle(0);// no x grids

//隐藏第二y轴,即右边的y轴

       m_Chart.GetPlot().GetAxis(2,var).GetAxisScale().SetHide(TRUE);

//刷新控件

       m_Chart.Refresh();

继续阅读