MFC扩展编程实例圆饼创建与旋转操作及标签位置设置
当前例程创建一个圆饼图,再创建一个旋转控件来旋转圆饼图。
同时还实现对图表标签位置的设置,效果如图。
MFC扩展编程实例圆饼创建与旋转操作及标签位置设置
例程界面点击圆形按钮左右部分,可以对图表向左向右旋转操作。
多次点击设置标签 位置按钮,可以盆图表标签不同的位置。
具体可以在后面下载例程学习。
下面是例程创建过程与关键代码。
先创建基于class CMy123View : public CBCGPFormView单文档类。
添加视窗类两成员变量,
CBCGPRotationCtrl m_wndRotate;
CBCGPChartCtrl m_wndChart;
在视窗对话框上添加图片控件IDC_CHART与IDC_ROTATE,用于显示图表与操作图表。
准备好后就可以添加视窗类的虚函数,实现变量的初始化。
- 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()->EnableFlatIcons();
- CBCGPRotationObject* pRO = m_wndRotate.GetRotationObject();
- 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->EnablePart(CBCGPRotationObject::BCGP_ROTATION_UP, FALSE);
- pRO->EnablePart(CBCGPRotationObject::BCGP_ROTATION_DOWN, FALSE);
- //图表初始化;
- m_wndChart.SubclassDlgItem(IDC_CHART,this);
- CBCGPInfoTipOptions infoTipOptions;
- infoTipOptions.m_StemLocation = CBCGPPopupWindow::BCGPPopupWindowStemLocation_Left;
- m_wndChart.EnableInfoTip(TRUE, &infoTipOptions);//显示提示文本;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();//获取图表指针;
- pChart->SetChartType(BCGPChartPie);//创建为2D饼图;
- pChart->SetChartTitle(_T("2D圆饼图"));//设置图表标题;
- 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;
- }
复制代码 初始化时有将图表添加到布局管理器,所以在视窗类构造函数中开启布局管理器EnableLayout();
初始化变量后,就可以实现图表旋转功能,添加旋转控件的点击函数,
afx_msg void OnRotate();
ON_BN_CLICKED(IDC_ROTATE, OnRotate)
- void CMy123View::OnRotate()
- {
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- ASSERT_VALID(pChart);
- int xDelta = 10;
- double yDelta = 10.;
- for (int i = 0; i < pChart->GetSeriesCount(); i++)
- {
- CBCGPChartPieSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPieSeries, pChart->GetSeries(i));
- ASSERT_VALID(pSeries);
- int nRotationAngle = pSeries->GetPieRotation();
- double dblPieAngle = pSeries->GetPieAngle();
- switch (m_wndRotate.GetRotationObject()->GetClicked())
- {
- case CBCGPRotationObject::BCGP_ROTATION_UP:
- dblPieAngle -= yDelta;
- if (dblPieAngle < 1.)
- {
- dblPieAngle = 1.;
- }
- break;
- case CBCGPRotationObject::BCGP_ROTATION_DOWN:
- dblPieAngle += 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;
- dblPieAngle = 45.;
- break;
- }
- pSeries->SetPieRotation(nRotationAngle);
- pSeries->SetPieAngle(dblPieAngle);
- }
- pChart->SetDirty(true,true);
- }
复制代码
最后是实现标签位置的设置。
添加新按钮控件,关联点击函数,实现圆饼图表标签位置设置。
- void CMy123View::OnBnClickedButton1()
- {
- static int nIndex=0,nShow=0;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- pChart->ShowDataLabels(TRUE);
- BCGPChartDataLabelOptions dataLabelOptions = pChart->GetDataLabelOptions();
- dataLabelOptions.m_position = (BCGPChartDataLabelOptions::LabelPosition)(nIndex==5?nIndex-1:nIndex);
- dataLabelOptions.m_bUnderlineDataLabel = TRUE;
- dataLabelOptions.m_content = nShow>4 ? BCGPChartDataLabelOptions::LC_DP_INDEX : BCGPChartDataLabelOptions::LC_PIE_NAME_PERCENTAGE;
- dataLabelOptions.m_bDrawDataLabelBorder = dataLabelOptions.m_position != BCGPChartDataLabelOptions::LP_DEFAULT_POS && dataLabelOptions.m_position != BCGPChartDataLabelOptions::LP_OUTSIDE_END;
- pChart->SetDataLabelsOptions(dataLabelOptions);
- for (int i = 0; i < pChart->GetSeriesCount(); i++)
- {
- CBCGPChartPieSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPieSeries, pChart->GetSeries(i));
- pSeries->SetLegendLabelContent(nShow>4 ?
- (BCGPChartDataLabelOptions::LabelContent)(BCGPChartDataLabelOptions::LC_PIE_NAME_PERCENTAGE | BCGPChartDataLabelOptions::LC_DP_INDEX) :
- BCGPChartDataLabelOptions::LC_DEFAULT_CONTENT);
- }
- pChart->Redraw();
- //
- nIndex++;
- nIndex = nIndex>4?0:nIndex;
- nShow++;
- nShow = nShow>8?0:nShow;
- }
复制代码 例程用到的MFC扩展库可以搜索下载。
例程源代码下载地址:
|