说明:本文将网上一些MSChart的使用方法进行了一个总结,按下面的步骤可以实现一个产生三条曲线(每条曲线6个随机数点)的表。
1 在工程中加入 mschart
MSChart是VC++6.0中自带的一个特殊控件类,用于绘制坐标曲线图。首先建一个基于对话框的MFC程序,插入MsChart 控件,在工程中加入 mschart :
菜单->Project->Add
2 添加头文件
在要使用的类的实现文件中包含如下头文件
第一部分:
#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 "math.h"
在要使用的类的头文件中包含:
#include "mschart.h"
3 类中定义CMSChart 变量
class CMsChartDlgDlg : public CDialog
{
// Construction
… …
private:
CMSChart m_Chart; //对话框关联的类中定义CMSChart类
… …
};
4 创建m_Chart
CMsChartDlgDlg::OnInitDialog()函数中添加如下代码
CRect rc;
GetClientRect(&rc);
rc.top += 30;
rc.left += 50;
rc.bottom -= 80;
rc.right -= 50;
m_Chart.Create("mschart", WS_CHILD| WS_VISIBLE, rc, this, 10);
//控件资源初始化
initmschart();
5 初始化m_Chart
在CMsChartDlgDlg中添加函数:
void CMsChartDlgDlg::initmschart(); //初始化
//主要设置表的样式:表的XY轴长度色彩等等
void CMsChartDlgDlg::initmschart()
{
// 设置标题
m_Chart.SetTitleText("mschart 示例 by thinkry@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轴设置
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().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().SetLocationType(1);
m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocationType(1);
m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocationType(1);
}
6 设置m_Chart数据
//画曲线
void CMsChartDlgDlg::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().SetDivisionsPerLabel(1);
// 每刻度一个刻度线
m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerTick(1);
// X轴名称
m_Chart.GetPlot().GetAxis(0,var).GetAxisTitle().SetText("日期");
char buf[32];
srand( (unsigned)time( NULL ) );
//随机产生三条曲线的6个点
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);//设置第一条曲线的第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();
}
7 添ON_SIZE消息响应函数
在 CMsChartDlgDlg::OnSize 中调整 m_Chart 的大小,使之能随窗口大小变化而变化
//添加ON_SIZE消息响应函数
void CMsChartDlgDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if( m_Chart.GetSafeHwnd() )
m_Chart.MoveWindow( 0, 0, cx, cy );
// TODO: Add your message handler code here
}
8 改变显示类型
// 折线图
void CDemoView::OnChartLine()
{
m_Chart.SetChartType(3);
DrawChart();
}
// 柱状图
void CDemoView::OnChartCombi()
{
m_Chart.SetChartType(1);
DrawChart();
}
// 饼状图
void CDemoView::OnChartPie()
{
m_Chart.SetChartType(14);
DrawChart();
}
9 程序运行效果图
10 MsChart函数说明:
1. m_Chart.GetPlot().GetAxis(1,var)//获取纵轴
2. //设置是否支持自动标准;控件默认支持自动标准。
3. m_Chart.GetPlot().GetAxis().GetValuesScale().SetAuto(FALSE);
4. //设置最大刻度为M;
5. m_Chart.GetPlot().GetAxis().GetValuesScale().SetMaximum(M);
6. //设置最小刻度为m;
7. m_Chart.GetPlot().GetAxis().GetValuesScale().SetMinimum(m);
8. //设置轴的等分数D;
9. m_Chart.GetPlot().GetAxis().GetValuesScale().SetMajorDivision(D);
10. //设置每等分的刻度线数n;
11. m_Chart.GetPlot().GetAxis().GetValuesScale().SetMinorDivision(n);
12. //b)横轴初始化属性
13. VARIANT var;
14. m_Chart.GetPlot().GetAxis(0,var)//获取横轴
15. //其他属性设置跟纵轴相同。
16. //1.2 数据显示
17. //a)设置标题栏和标签
18. m_Chart.SetTitleText(“标题”);//设置标题栏
19. m_Chart.SetRowLabel((“第I行”);//设置第i行标签
20. m_Chart.SetColumnLabel((“第j列”);//设置第j列标签
21. //b)行列的显示布局
22. // MSChart的行列显示布局有其自身的特点:下面显示是一个行列4×3(矩形图),即(四行,三列)的布局示意图。
23. m_Chart.SetRowCount(4); //没条曲线三个四个点 (曲线图)
24. m_Chart.SetColumnCount(3); //设置曲线条数为三条(曲线图)
25. //c)行列操作
26. // 操作行列第i行、第j列
27. m_Chart.SetRow(i);// 第i行
28. m_Chart.SetColumn(j);//第j列
29. m_Chart.SetRowLabel((“第i行”);//设置第i行标签
30. CString str=”90.5”;
31. m_Chart.SetData((LPCTSTR(str)); //设置行列,j>的显示数据
32. m_Chart.Refresh();//刷新视图
33. //d)显示方式
34. //获取当前的显示方式:
35. long nType =m_Chart.GetChartType();
36. //设置显示方式:
37. m_Chart.SetChartType(0);//3D(三维) 显示
38. m_Chart.SetChartType(1);//2D(二维) 显示
39. m_Chart.Refresh();
40. //其它常用组合方式为:
41. m_Chart.SetChartType(1|0) //2D柱(条)形,
42. m_Chart.SetChartType(0|0) //3D柱(条)形
43. m_Chart.SetChartType(1|2) //2D线条型
44. m_Chart.SetChartType(0|2) //3D线条型
45. m_Chart.SetChartType(1|4) //2D区域型
46. m_Chart.SetChartType(0|4) //3D区域型
47. m_Chart.SetChartType(1|6) //2D阶梯型
48. m_Chart.SetChartType(0|6) //3D阶梯型
49. m_Chart.SetChartType(1|8) //2D复(混)合型
50. m_Chart.SetChartType(0|8) //3D复(混)合型
51. //另外,在2D方式中,还有两类:饼型和XY型
52. m_Chart.SetChartType(14) //2D 饼型
53. m_Chart.SetChartType(16) //2DXY型
54. //e)其他
55. //其他属性,比如设置字体,颜色,对齐方式等。