工控编程吧
标题:
MFC扩展编程实例3D叠加面积图表创建显示
[打印本页]
作者:
qq263946146
时间:
2020-2-20 15:00
标题:
MFC扩展编程实例3D叠加面积图表创建显示
MFC扩展编程实例3D叠加面积图表创建显示
当前例程实现三维的叠加面积图表与带百分比叠加面积图表创建显示。
效果如下图。
(, 下载次数: 8)
上传
点击文件名下载附件
初始显示的是一般的三维面积图表,点击图表类型按钮可以三个类型图表来回切换。
一般的,叠加的,带百分比叠加的三种类型图表。
点击标签设置可以设置图表标签的显示角度。
点击旋转控件,可以旋转三维图表。
下面是例程实现过程。
创建基于扩展库的单文档工程class CMy123View : public CBCGPFormView。
在默认对话框资源添加两图片控件,ID修改为IDC_ROTATE,IDC_CHART,用于旋转与显示图表。
在视窗类添加自定义变量与函数,函数用于旋转图表。
CBCGPRotationCtrl m_wndRotate;
CBCGPChartCtrl m_wndChart;
void RotateChart(CBCGPRotationObject::RotationElement hit, double xDelta=10., double yDelta=10., double persperctiveDelta=0.1);
复制代码
在视窗类添加虚函数Create,用于初始化图表等变量。
inline double Rand (double dblStart, double dblFinish)
{
double minVal = min(dblStart, dblFinish);
double maxVal = max(dblStart, dblFinish);
return (maxVal - minVal) * (double)rand() / (RAND_MAX + 1) + minVal;
}
BOOL CMy123View::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
BOOL bRst = CBCGPFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);;
//旋转图表控件初始化;
m_wndRotate.SubclassDlgItem(IDC_ROTATE,this);
m_wndRotate.GetRotationObject()->SetAutorepeatMode(100);
m_wndRotate.GetRotationObject()->SetColorTheme(CBCGPRotationObject::BCGP_COLOR_THEME_VISUAL_MANAGER);
m_wndRotate.GetRotationObject()->EnablePart(CBCGPRotationObject::BCGP_ROTATION_CLOCKWISE, FALSE);
m_wndRotate.GetRotationObject()->EnablePart(CBCGPRotationObject::BCGP_ROTATION_COUNTER_CLOCKWISE, FALSE);
m_wndRotate.GetRotationObject()->EnableFlatIcons();
//图表初始化;
m_wndChart.SubclassDlgItem(IDC_CHART,this);
CBCGPInfoTipOptions infoTipOptions;
infoTipOptions.m_StemLocation = CBCGPPopupWindow::BCGPPopupWindowStemLocation_Left;
m_wndChart.EnableInfoTip(TRUE, &infoTipOptions);//显示提示文本;
CBCGPChartVisualObject* pChart = m_wndChart.GetChart();//获取图表指针;
BCGPChartCategory style = BCGPChartArea3D;
BCGPChartType type = BCGP_CT_SIMPLE;
pChart->SetChartType(style,type);//图表类型设置;
pChart->SetChartTitle(_T("3D面积图表"));//设置图表标题;
pChart->SetSeriesShadow(true);//数据系列显示阴影;
pChart->ShowDataMarkers(true, 5, BCGPChartMarkerOptions::MS_CIRCLE);//o数据标志点设置;
// pChart->ShowDataLabels(true, TRUE, TRUE, 45);//数据标签默认不显示;
pChart->SetThemeOpacity(70);//透明度设置;
pChart->SetCurveType(BCGPChartFormatSeries::CCT_SPLINE);//设置数据系列曲线样式;
//创建数据系列,添加数据;
CBCGPChartSeries* pSeries1 = pChart->CreateSeries(_T("第一年"));
CBCGPChartSeries* pSeries2 = pChart->CreateSeries(_T("第二年"));
CBCGPChartSeries* pSeries3 = pChart->CreateSeries(_T("第三年"));
COleDateTime today = COleDateTime::GetCurrentTime();
double arTemp[] = { 5., 12., 19, 22, 19, 24, 22, 24, 18, 14, 10, 7 };
for (int nMonth = 1; nMonth <= 12; nMonth++)
{
COleDateTime date (today.GetYear(), nMonth, 1, 0, 0, 0);
CString strMonth = date.Format(_T("%b"));
pSeries1->AddDataPoint(strMonth, arTemp[nMonth - 1]);
pSeries2->AddDataPoint((int)(arTemp[nMonth - 1] + Rand(-5, 5)));
pSeries3->AddDataPoint((int)(arTemp[nMonth - 1] + Rand(-5, 5)));
}
//添加添加到布局管理器统一管理布局;
if (GetLayout() == NULL)
return bRst;
CBCGPStaticLayout* pLayout = (CBCGPStaticLayout*)GetLayout();
if (pLayout == NULL)
return bRst;
pLayout->AddAnchor(m_wndChart.GetDlgCtrlID(), CBCGPStaticLayout::e_MoveTypeNone, CBCGPStaticLayout::e_SizeTypeBoth);
//
return bRst;
}
复制代码
添加图表旋转控件的点击函数,用于旋转图表。
void CMy123View::OnRotate()
{
RotateChart(m_wndRotate.GetRotationObject()->GetClicked());
}
复制代码
最后就是图表类型切换与标签设置按钮的实现。
void CMy123View::OnBnClickedButton1()
{
static int nIndex=1;
CBCGPChartVisualObject* pChart = m_wndChart.GetChart();//获取图表指针;
if (nIndex == 0)
{
pChart->SetChartType(BCGPChartArea3D, BCGP_CT_SIMPLE);
}
else if (nIndex == 1)
{
pChart->SetChartType(BCGPChartArea3D, BCGP_CT_STACKED);
}
else
{
pChart->SetChartType(BCGPChartArea3D, BCGP_CT_100STACKED);
}
//设置3D图表背景墙相关属性;
{
CBCGPChartDiagram3D* pDiagram3D = pChart->GetDiagram3D();
ASSERT(pDiagram3D != NULL);
pDiagram3D->SetDepthScalePercent(2);
pDiagram3D->SetFrontDistancePercent(0.5);
pDiagram3D->SetHeightScalePercent(1);
DWORD dwoFlags = CBCGPChartDiagram3D::DWO_OUTLINE_ALL|CBCGPChartDiagram3D::DWO_DRAW_ALL_WALLS | CBCGPChartDiagram3D::DWO_DRAW_FLOOR;
pDiagram3D->SetDrawWallOptions((CBCGPChartDiagram3D::DrawWallOptions)dwoFlags);
pDiagram3D->SetThickWallsAndFloor(true);
}
pChart->Redraw();
nIndex++;
nIndex = nIndex>2?0:nIndex;
}
void CMy123View::OnBnClickedButton2()
{
static int nIndex=0;
bool bShow = nIndex==8?false:true;
double dbDegree = 45*nIndex;
CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
ASSERT_VALID(pChart);
pChart->ShowDataLabels(bShow, TRUE, FALSE, dbDegree);
pChart->Redraw();
nIndex++;
nIndex=nIndex>8?0:nIndex;
}
复制代码
例程使用的MFC扩展库可以网站搜索下载。
例程源代码下载地址:
[weixinlianxi]1[/weixinlianxi]
[MFC408]1[/MFC408]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4