MFC扩展编程实例2D3D金字塔图表创建与标签位置设置
当前例程实现二维与三维图表的创建,可以通过按钮来回切换。
标签的显示角度也可以设置,还可以旋转三维的图表。
效果如下图。
MFC扩展编程实例2D3D金字塔图表创建与标签位置设置
例程界面有一旋转控件可以左右,上下旋转三维类型的图表。
图表的类型也可以点击控件切换,
图表的标签也可以左中右切换显示。
下面是例程实现过程介绍。
先创建基于MFC扩展库的单文档工程,class CMy123View : public CBCGPFormView。
在默认的对话框资源上添加两图片控件IDC_CHART,IDC_ROTATE用于显示,旋转图表。
在视窗类添加变量
CBCGPRotationCtrl m_wndRotate;
CBCGPChartCtrl m_wndChart;
及虚函数Create,用于初始化变量。
- 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);
- CBCGPRotationObject* pRO = m_wndRotate.GetRotationObject();
- ASSERT_VALID(pRO);
- pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_CLOCKWISE, FALSE);
- pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_NARROW_FIELD_OF_VIEW, FALSE);
- pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_WIDEN_FIELD_OF_VIEW, FALSE);
- pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_COUNTER_CLOCKWISE, FALSE);
- pRO->SetAutorepeatMode(100);
- pRO->SetColorTheme(CBCGPRotationObject::BCGP_COLOR_THEME_VISUAL_MANAGER);
- pRO->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();//获取图表指针;
- //BCGPChartPyramid3D,BCGPChartPyramid//二维,三维金字塔图表;
- pChart->SetChartType(BCGPChartPyramid3D);//
- pChart->SetChartTitle(_T("金字塔图表"));//设置图表标题;
- pChart->SetSeriesShadow(true);//数据系列显示阴影;
- //创建一数据系列,添加数据;
- pChart->AddChartData(_T("工控编程吧"), 6700);
- pChart->AddChartData(_T("头条"), 4500);
- pChart->AddChartData(_T("百度"), 2200);
- pChart->AddChartData(_T("微信"), 1500);
- pChart->AddChartData(_T("微博"), 1400);
- pChart->AddChartData(_T("阿里"), 1350);
- pChart->AddChartData(_T("其他"), 1700);
- //添加添加到布局管理器统一管理布局;
- 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()
- {
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- ASSERT_VALID(pChart);
- double xDelta = 10;
- int yDelta = 5;
- for (int i = 0; i < pChart->GetSeriesCount(); i++)
- {
- CBCGPChartPyramidSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPyramidSeries, pChart->GetSeries(i));
- ASSERT_VALID(pSeries);
- double nRotationAngle = pSeries->GetRotation();
- int nDepth = pSeries->GetDepthPercent();
- switch (m_wndRotate.GetRotationObject()->GetClicked())
- {
- case CBCGPRotationObject::BCGP_ROTATION_UP:
- nDepth -= yDelta;
- break;
- case CBCGPRotationObject::BCGP_ROTATION_DOWN:
- nDepth += yDelta;
- break;
- case CBCGPRotationObject::BCGP_ROTATION_LEFT:
- nRotationAngle -= xDelta;
- break;
- case CBCGPRotationObject::BCGP_ROTATION_RIGHT:
- nRotationAngle += xDelta;
- break;
- case CBCGPRotationObject::BCGP_ROTATION_RESET:
- nRotationAngle = 0;
- nDepth = 10;
- break;
- }
- pSeries->SetRotation(nRotationAngle);
- pSeries->SetDepthPercent(nDepth);
- }
- pChart->Redraw();
- }
复制代码 最后就是标签设置与图表类型切换按钮实现。
- void CMy123View::OnBnClickedButton1()
- {
- static bool bShowLabel=true;//是否显示标签;
- static int nLabelPos=0;//标签位置;
- static bool bCircleType=false;//圆形样式;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- ASSERT_VALID(pChart);
- pChart->ShowDataLabels(bShowLabel);//显示标签;
- bCircleType = pChart->GetChartCategory()==BCGPChartPyramid3D?true:false;
- if (bShowLabel)
- {
- BCGPChartDataLabelOptions dataLabelOptions = pChart->GetDataLabelOptions();
- switch (nLabelPos)
- {
- case 0:
- dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_DEFAULT_POS;
- break;
- case 1:
- dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_OUTSIDE_END;
- dataLabelOptions.m_dblAngle = -90;
- break;
- case 2:
- dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_OUTSIDE_END;
- dataLabelOptions.m_dblAngle = 90;
- break;
- case 3:
- dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_CENTER;
- break;
- }
- pChart->SetDataLabelsOptions(dataLabelOptions);
- }
- for (int i = 0; i < pChart->GetSeriesCount(); i++)
- {
- CBCGPChartPyramidSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPyramidSeries, pChart->GetSeries(i));
- ASSERT_VALID(pSeries);
- pSeries->SetCircularBase(bCircleType);//方形,圆形样式;
- pSeries->m_bDataLabelsInColumns = true;
- pSeries->SetGap(25);//金字塔每层间隔;
- }
- pChart->SetThemeOpacity(70);//设置透明度;
- pChart->Redraw();
- //
- nLabelPos++;
- nLabelPos = nLabelPos>3?0:nLabelPos;
- }
- void CMy123View::OnBnClickedButton2()
- {
- static bool b3D=false;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- ASSERT_VALID(pChart);
- BCGPChartCategory type = b3D?BCGPChartPyramid3D:BCGPChartPyramid;
- pChart->SetChartType(type);
- pChart->Redraw();
- b3D = !b3D;
- }
复制代码 例程有用到MFC扩展库,可以网站搜索下载。
例程源代码下载地址:
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
|