VC6.0中MSChart画图

一、在工程中加入 mschart

菜单->Project->Add To Project->Components and Controls->Registered ActiveX Controls->Microsoft Chart Control, version 6.0 (OLEDB)

二、在CDemoView中加入:CMSChart m_Chart

三、创建及设置m_Chart

3.1 在 CDemoView::OnCreate 中创建CMSChart

  1. // CDemoView::OnCreate() 
  2. CRect rc; 
  3. GetClientRect(&rc); 
  4. if(!m_Chart.Create("mschart", WS_CHILD| WS_VISIBLE, rc, this, 10)) 
  5. return -1; 
// 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 的大小,使之能随窗口大小变化而变化

  1. // CDemoView::OnSize 
  2. if( m_Chart.GetSafeHwnd() ) 
  3. m_Chart.MoveWindow( 0, 0, cx, cy );  
// CDemoView::OnSize
if( m_Chart.GetSafeHwnd() )
m_Chart.MoveWindow( 0, 0, cx, cy ); 


3.3 设置 m_Chart

  1. void CDemoView::InitChart() 
  2.     // 设置标题 
  3.     m_Chart.SetTitleText("mschart 示例 by thinkry@263.net"); 
  4.     // 下面两句改变背景色 
  5.     m_Chart.GetBackdrop().GetFill().SetStyle(1); 
  6.     m_Chart.GetBackdrop().GetFill().GetBrush().GetFillColor().Set(255, 255, 255); 
  7.     // 显示图例 
  8.     m_Chart.SetShowLegend(TRUE); 
  9.     m_Chart.SetColumn(1); 
  10.     m_Chart.SetColumnLabel((LPCTSTR)"1号机"); 
  11.     m_Chart.SetColumn(2); 
  12.     m_Chart.SetColumnLabel((LPCTSTR)"2号机"); 
  13.     m_Chart.SetColumn(3); 
  14.     m_Chart.SetColumnLabel((LPCTSTR)"3号机"); 
  15.     // 栈模式 
  16.     // m_Chart.SetStacking(TRUE); 
  17.     // Y轴设置 
  18.     VARIANT var; 
  19.     m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetAuto(FALSE); // 不自动标注Y轴刻度 
  20.     m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMaximum(100); // Y轴最大刻度 
  21.     m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinimum(0); // Y轴最小刻度 
  22.     m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMajorDivision(5); // Y轴刻度5等分 
  23.     m_Chart.GetPlot().GetAxis(1,var).GetValueScale().SetMinorDivision(1); // 每刻度一个刻度线 
  24.     m_Chart.GetPlot().GetAxis(1,var).GetAxisTitle().SetText("小时"); // Y轴名称 
  25.     // 3条曲线 
  26.     m_Chart.SetColumnCount(3);  
  27.     // 线色 
  28.     m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetPen().GetVtColor().Set(0, 0, 255); 
  29.     m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetPen().GetVtColor().Set(255, 0, 0); 
  30.     m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetPen().GetVtColor().Set(0, 255, 0); 
  31.     // 线宽(对点线图有效) 
  32.     m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetPen().SetWidth(50); 
  33.     m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetPen().SetWidth(100); 
  34.     m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetPen().SetWidth(2); 
  35.     // 数据点类型显示数据值的模式(对柱柱状图和点线图有效) 
  36.     // 0: 不显示 1: 显示在柱状图外 
  37.     // 2: 显示在柱状图内上方 3: 显示在柱状图内中间 4: 显示在柱状图内下方 
  38.     m_Chart.GetPlot().GetSeriesCollection().GetItem(1).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocationType(1); 
  39.     m_Chart.GetPlot().GetSeriesCollection().GetItem(2).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocationType(1); 
  40.     m_Chart.GetPlot().GetSeriesCollection().GetItem(3).GetDataPoints().GetItem(-1).GetDataPointLabel().SetLocationType(1); 
  41. }      
void CDemoView::InitChart()
{
	// 设置标题
	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);
}     


3.4 设置数据

  1. void CDemoView::DrawChart() 
  2.     int nRowCount = 6; 
  3.     m_Chart.SetRowCount(nRowCount); 
  4.     VARIANT var; 
  5.      
  6.     // 不自动标注X轴刻度 
  7.     m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetAuto(FALSE);  
  8.      
  9.     // 每刻度一个标注 
  10.     m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerLabel(1); 
  11.      
  12.     // 每刻度一个刻度线 
  13.     m_Chart.GetPlot().GetAxis(0,var).GetCategoryScale().SetDivisionsPerTick(1);  
  14.      
  15.     // X轴名称 
  16.     m_Chart.GetPlot().GetAxis(0,var).GetAxisTitle().SetText("日期");  
  17.     char buf[32]; 
  18.     srand( (unsigned)time( NULL ) ); 
  19.     for(int row = 1; row <= nRowCount; ++row) 
  20.     { 
  21.         m_Chart.SetRow(row); 
  22.         sprintf(buf, "%d号", row); 
  23.         m_Chart.SetRowLabel((LPCTSTR)buf); 
  24.         m_Chart.GetDataGrid().SetData(row, 1, rand() * 100 / RAND_MAX, 0); 
  25.         m_Chart.GetDataGrid().SetData(row, 2, rand() * 100 / RAND_MAX, 0); 
  26.         m_Chart.GetDataGrid().SetData(row, 3, rand() * 100 / RAND_MAX, 0); 
  27.     } 
  28.     m_Chart.Refresh();  
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().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 ) );
	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 改变显示类型

  1. // 折线图 
  2. void CDemoView::OnChartLine()  
  3.     m_Chart.SetChartType(3);  
  4.     DrawChart(); 
  5.  
  6. // 柱状图  
  7. void CDemoView::OnChartCombi()  
  8.     m_Chart.SetChartType(1);  
  9.     DrawChart(); 
  10. // 饼状图 
  11. void CDemoView::OnChartPie()  
  12.     m_Chart.SetChartType(14);  
  13.     DrawChart(); 
// 折线图
void CDemoView::OnChartLine() 
{
	m_Chart.SetChartType(3); 
	DrawChart();
}

// 柱状图 
void CDemoView::OnChartCombi() 
{
	m_Chart.SetChartType(1); 
	DrawChart();
}
// 饼状图
void CDemoView::OnChartPie() 
{
	m_Chart.SetChartType(14); 
	DrawChart();
}



一、在工程中加入 mschart
     菜单->Project->Add To Project->Components and Controls->Registered ActiveX Controls->Microsoft Chart Control, version 6.0 (OLEDB)

二、在CDemoView中加入:CMSChart m_Chart

三、创建及设置m_Chart
3.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::OnSize
if( 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().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);
}    
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().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 ) );
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().GetItem(-1).GetBrush().GetFillColor().Set(红, 绿, 蓝); ( 3las 发表于 2005-3-27 22:00:00)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值