Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4460989
  • 博文数量: 356
  • 博客积分: 10458
  • 博客等级: 上将
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-24 14:59
文章分类

全部博文(356)

文章存档

2020年(17)

2019年(9)

2018年(26)

2017年(5)

2016年(11)

2015年(20)

2014年(2)

2013年(17)

2012年(15)

2011年(4)

2010年(7)

2009年(14)

2008年(209)

分类: C/C++

2008-05-03 20:01:11

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();
阅读(13268) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-05-06 09:17:21

thanks! thanks!