中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Visual C++ > COM技术
mschart示例
作者:未知 时间:2005-07-20 14:21 出处:VC知识库 责编:chinaitpower
              摘要: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_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 示例 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 设置数据
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();
}

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有