MFC扩展编程实例3D环面图表爆炸显示与设置标签位置
当前例程实现三维环面图表的创建,并可以让其呈现爆炸显示,及设置其标签显示位置。
效果如下图。
MFC扩展编程实例3D环面图表爆炸显示与设置标签位置
点击例程界面最左边按钮可以来回旋转图表,
点击对应的其他三个按钮,可以设置图表数据标签显示的位置,
可以让图表以爆炸形式显示,可以让图表填充整个显示区域。
下面是图表创建过程。
首先创建基于单文档的工程,class CMy123View : public CBCGPFormView。
在默认的对话框资源上添加两图片控件,修改ID为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);
- 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(BCGPChartTorus3D);//
- pChart->SetChartTitle(_T("3D环图表"));//设置图表标题;
- 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;
- }
复制代码 以及添加旋转图表控件的点击函数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;
- }
- void CMy123View::OnBnClickedButton2()
- {
- static bool bExplose=false;
- bExplose = !bExplose;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- for (int i = 0; i < pChart->GetSeriesCount(); i++)
- {
- CBCGPChartPieSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPieSeries, pChart->GetSeries(i));
- pSeries->SetPieExplosion(bExplose ? 10 : 0);//设置圆饼图每个数据爆炸像素大小;
- pSeries->SetDataPointPieExplosion(bExplose ? 20 : 0, 0);//设置第0个数据点的爆炸像素大小;
- }
- pChart->SetDirty(true,true);;//图表重绘设置生效;
- }
- void CMy123View::OnBnClickedButton3()
- {
- static bool bFit = false;
- bFit = !bFit;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- for (int i = 0; i < pChart->GetSeriesCount(); i++)
- {
- CBCGPChartPieSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPieSeries, pChart->GetSeries(i));
- pSeries->EnableFitDiagramArea(bFit);//设置圆饼图填充图表区域;
- }
- pChart->SetDirty(true,true);;//图表重绘设置生效;
- }
复制代码 例程是基于MFC扩展库编程的,可以网站搜索下载。
例程源代码下载地址:
|